1 ;;; nyacc/lang/c99/parser.scm
3 ;;; Copyright (C) 2015-2017 Matthew R. Wette
5 ;;; This program is free software: you can redistribute it and/or modify
6 ;;; it under the terms of the GNU General Public License as published by
7 ;;; the Free Software Foundation, either version 3 of the License, or
8 ;;; (at your option) any later version.
10 ;;; This program is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;; GNU General Public License for more details.
15 ;;; You should have received a copy of the GNU General Public License
16 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (nyacc lang c99 parser)
22 #:use-module (nyacc lex)
23 #:use-module (nyacc parse)
24 #:use-module (nyacc lang util)
25 #:use-module (nyacc lang c99 cpp)
31 (use-modules (ice-9 syncase))
32 (use-modules (ice-9 optargs)))
35 (include-from-path "nyacc/lang/c99/mach.d/c99tab.scm")
36 (include-from-path "nyacc/lang/c99/body.scm")
37 (include-from-path "nyacc/lang/c99/mach.d/c99act.scm")
39 ;; Parse given a token generator. Uses fluid @code{*info*}.
40 ;; A little ugly wrt re-throw but
42 (let ((parser (make-lalr-parser
43 (list (cons 'len-v len-v) (cons 'pat-v pat-v)
44 (cons 'rto-v rto-v) (cons 'mtab mtab)
45 (cons 'act-v act-v)))))
46 (lambda* (lexer #:key (debug #f))
49 (lambda () (parser lexer #:debug debug))
50 (lambda (key fmt . args)
51 (report-error fmt args)
52 (pop-input) ; not sure this is the right way
53 (throw 'c99-error "C99 parse error"))))))
55 ;; This is used to parse included files at top level.
57 (let ((info (fluid-ref *info*)))
58 (raw-parser (gen-c-lexer) #:debug (cpi-debug info))))
60 ;; @deffn {Procedure} parse-c99 [#:cpp-defs def-a-list] [#:inc-dirs dir-list] \
61 ;; [#:mode ('code|'file)] [#:debug bool]
62 ;; This needs to be explained in some detail.
63 ;; tdd = typedef dict: (("<time>" time_t) ... ("<unistd.h>" ...))
64 ;; Default mode is @code{'code}.
66 ;; (with-input-from-file "abc.c"
67 ;; (parse-c #:cpp-defs '("ABC=123"))
68 ;; #:inc-dirs '(("." "./incs" "/usr/include"))
69 ;; #:inc-help (append '("myinc.h" "foo_t" "bar_t") c99-std-help)
73 (define* (parse-c99 #:key
74 (cpp-defs '()) ; CPP defines
75 (inc-dirs '()) ; include dirs
76 (inc-help '()) ; include helpers
77 (mode 'code) ; mode: 'file or 'code
78 (xdef? #f) ; pred to determine expand
83 (if (and (pair? cpp-defs) (pair? (car cpp-defs)))
84 (error "usage deprecated: use #:cpp-defs '(\"ABC=123\")"))
85 (let ((info (make-cpi debug cpp-defs (cons "." inc-dirs) inc-help)))
89 (raw-parser (gen-c-lexer #:mode mode #:xdef? xdef?)
91 (lambda (key fmt . rest)
92 (report-error fmt rest)