2016-12-17 21:34:43 +00:00
|
|
|
;;; nyacc/lang/c99/parser.scm
|
|
|
|
;;;
|
2017-01-08 00:06:09 +00:00
|
|
|
;;; Copyright (C) 2015-2017 Matthew R. Wette
|
2016-12-17 21:34:43 +00:00
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;;; (at your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful,
|
|
|
|
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;; C parser
|
|
|
|
|
|
|
|
(define-module (nyacc lang c99 parser)
|
2017-01-08 00:06:09 +00:00
|
|
|
#:export (parse-c99
|
|
|
|
def-xdef? c99-std-dict
|
|
|
|
gen-c-lexer
|
|
|
|
gen-gcc-defs
|
|
|
|
)
|
2016-12-17 21:34:43 +00:00
|
|
|
#:use-module (nyacc lex)
|
|
|
|
#:use-module (nyacc parse)
|
|
|
|
#:use-module (nyacc lang util)
|
|
|
|
#:use-module (nyacc lang c99 cpp)
|
|
|
|
)
|
|
|
|
|
2017-03-26 20:27:20 +00:00
|
|
|
(cond-expand
|
|
|
|
(guile-2)
|
|
|
|
(guile
|
|
|
|
(use-modules (ice-9 syncase))
|
|
|
|
(use-modules (ice-9 optargs)))
|
|
|
|
(mes))
|
|
|
|
|
2016-12-17 21:34:43 +00:00
|
|
|
(include-from-path "nyacc/lang/c99/mach.d/c99tab.scm")
|
|
|
|
(include-from-path "nyacc/lang/c99/body.scm")
|
|
|
|
(include-from-path "nyacc/lang/c99/mach.d/c99act.scm")
|
|
|
|
|
|
|
|
;; Parse given a token generator. Uses fluid @code{*info*}.
|
2017-01-08 00:06:09 +00:00
|
|
|
;; A little ugly wrt re-throw but
|
2016-12-17 21:34:43 +00:00
|
|
|
(define raw-parser
|
2017-01-08 00:06:09 +00:00
|
|
|
(let ((c99-parser (make-lalr-parser
|
|
|
|
(list (cons 'len-v len-v) (cons 'pat-v pat-v)
|
|
|
|
(cons 'rto-v rto-v) (cons 'mtab mtab)
|
|
|
|
(cons 'act-v act-v)))))
|
|
|
|
(lambda* (lexer #:key (debug #f))
|
2017-01-11 16:14:45 +00:00
|
|
|
(catch
|
2017-01-08 00:06:09 +00:00
|
|
|
'nyacc-error
|
|
|
|
(lambda () (c99-parser lexer #:debug debug))
|
2017-01-11 16:14:45 +00:00
|
|
|
(lambda (key fmt . args)
|
|
|
|
(report-error fmt args)
|
|
|
|
(pop-input) ; not sure this is the right way
|
|
|
|
(throw 'c99-error "C99 parse error")))
|
2017-01-08 20:16:28 +00:00
|
|
|
)))
|
2017-01-01 15:54:21 +00:00
|
|
|
|
2017-01-08 00:06:09 +00:00
|
|
|
;; This is used to parse included files at top level.
|
2016-12-17 21:34:43 +00:00
|
|
|
(define (run-parse)
|
|
|
|
(let ((info (fluid-ref *info*)))
|
2017-01-06 16:03:41 +00:00
|
|
|
(raw-parser (gen-c-lexer) #:debug (cpi-debug info))))
|
2016-12-17 21:34:43 +00:00
|
|
|
|
2017-01-08 00:06:09 +00:00
|
|
|
;; @deffn parse-c99 [#:cpp-defs def-a-list] [#:inc-dirs dir-list] \
|
|
|
|
;; [#:mode ('code|'file)] [#:debug bool]
|
2016-12-17 21:34:43 +00:00
|
|
|
;; This needs to be explained in some detail.
|
|
|
|
;; tdd = typedef dict: (("<time>" time_t) ... ("<unistd.h>" ...))
|
2017-01-08 00:06:09 +00:00
|
|
|
;; Default mode is @code{'code}.
|
|
|
|
;; @example
|
|
|
|
;; (with-input-from-file "abc.c"
|
|
|
|
;; (parse-c #:cpp-defs '(("ABC" . "123"))
|
|
|
|
;; #:inc-dirs (append '("." "./incs" "/usr/include") c99-std-dict)
|
|
|
|
;; #:td-dict '(("myinc.h" "foo_t" "bar_t"))
|
|
|
|
;; #:mode 'file))
|
|
|
|
;; @end example
|
2016-12-17 21:34:43 +00:00
|
|
|
(define* (parse-c99 #:key
|
|
|
|
(cpp-defs '()) ; CPP defines
|
|
|
|
(inc-dirs '()) ; include dirs
|
|
|
|
(td-dict '()) ; typedef dictionary
|
2017-01-08 00:06:09 +00:00
|
|
|
(mode 'code) ; mode: 'file or 'code
|
2016-12-17 21:34:43 +00:00
|
|
|
(xdef? #f) ; pred to determine expand
|
|
|
|
(debug #f)) ; debug
|
|
|
|
(catch
|
2017-01-08 00:06:09 +00:00
|
|
|
'c99-error
|
2016-12-17 21:34:43 +00:00
|
|
|
(lambda ()
|
|
|
|
(let ((info (make-cpi debug cpp-defs (cons "." inc-dirs) td-dict)))
|
|
|
|
(with-fluid*
|
|
|
|
*info* info
|
|
|
|
(lambda ()
|
2017-01-08 00:06:09 +00:00
|
|
|
(raw-parser (gen-c-lexer #:mode mode #:xdef? xdef?)
|
2016-12-17 21:34:43 +00:00
|
|
|
#:debug debug)))))
|
|
|
|
(lambda (key fmt . rest)
|
2017-01-08 00:06:09 +00:00
|
|
|
(report-error fmt rest)
|
2016-12-17 21:34:43 +00:00
|
|
|
#f)))
|
|
|
|
|
|
|
|
(define parse-c parse-c99)
|
|
|
|
|
2017-01-08 00:06:09 +00:00
|
|
|
(use-modules (ice-9 rdelim))
|
|
|
|
(use-modules (ice-9 popen))
|
|
|
|
(use-modules (ice-9 regex))
|
|
|
|
|
|
|
|
;; @deffn gen-gcc-defs args => '(("ABC" . "123") ...)
|
|
|
|
;; Generate a list of default defines produced by gcc.
|
|
|
|
(define gen-gcc-defs
|
|
|
|
;; @code{"gcc -dM -E"} will generate lines like @code{"#define ABC 123"}.
|
|
|
|
;; We generate and return a list like @code{'(("ABC" . "123") ...)}.
|
|
|
|
(let ((rx (make-regexp "#define\\s+(\\S+)\\s+(.*)")))
|
|
|
|
(lambda (args)
|
|
|
|
(map
|
|
|
|
(lambda (l)
|
|
|
|
(let ((m (regexp-exec rx l)))
|
|
|
|
(cons (match:substring m 1) (match:substring m 2))))
|
|
|
|
(let ((ip (open-input-pipe "gcc -dM -E - </dev/null")))
|
|
|
|
(let iter ((lines '()) (line (read-line ip 'trim)))
|
|
|
|
(if (eof-object? line) lines
|
|
|
|
(iter (cons line lines) (read-line ip 'trim)))))))))
|
|
|
|
|
2016-12-17 21:34:43 +00:00
|
|
|
;; --- last line ---
|