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-02-05 15:52:44 +00:00
|
|
|
#:export (parse-c99)
|
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-03-02 02:11:40 +00:00
|
|
|
;; @deffn {Procedure} parse-c99 [#:cpp-defs def-a-list] [#:inc-dirs dir-list] \
|
2017-01-08 00:06:09 +00:00
|
|
|
;; [#: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"
|
2017-02-05 15:52:44 +00:00
|
|
|
;; (parse-c #:cpp-defs '("ABC=123"))
|
2017-02-23 01:12:32 +00:00
|
|
|
;; #:inc-dirs '(("." "./incs" "/usr/include"))
|
|
|
|
;; #:inc-help (append '("myinc.h" "foo_t" "bar_t") c99-std-help)
|
2017-01-08 00:06:09 +00:00
|
|
|
;; #:mode 'file))
|
|
|
|
;; @end example
|
2017-03-02 02:11:40 +00:00
|
|
|
;; @end deffn
|
2016-12-17 21:34:43 +00:00
|
|
|
(define* (parse-c99 #:key
|
|
|
|
(cpp-defs '()) ; CPP defines
|
|
|
|
(inc-dirs '()) ; include dirs
|
2017-02-05 15:52:44 +00:00
|
|
|
(inc-help '()) ; include helpers
|
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 ()
|
2017-02-05 15:52:44 +00:00
|
|
|
(if (and (pair? cpp-defs) (pair? (car cpp-defs)))
|
|
|
|
(error "usage deprecated: use #:cpp-defs '(\"ABC=123\")"))
|
|
|
|
(let ((info (make-cpi debug cpp-defs (cons "." inc-dirs) inc-help)))
|
2016-12-17 21:34:43 +00:00
|
|
|
(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)))
|
|
|
|
|
|
|
|
;; --- last line ---
|