nyacc: working on cpp-include fix

This commit is contained in:
Matt Wette 2017-01-01 07:54:21 -08:00 committed by Jan Nieuwenhuizen
parent 6309b99aaa
commit f5d1522166
10 changed files with 1347 additions and 1329 deletions

View file

@ -24,13 +24,9 @@
(defines cpi-defs set-cpi-defs!) ; #defines
(incdirs cpi-incs set-cpi-incs!) ; #includes
(tn-dict cpi-tynd set-cpi-tynd!) ; typename dict (("<x>" foo_t ..
;;
;;(typnams cpi-tyns set-cpi-tyns!) ; typedef names
;;
(ptl cpi-ptl set-cpi-ptl!) ; parent typename list
(ctl cpi-ctl set-cpi-ctl!) ; current typename list
;;
;;(typdcls cpi-tdls set-cpi-tdls!) ; typedef decls
(top cpi-top set-cpi-top!) ; top level?
)
(define std-dict
@ -75,6 +71,7 @@
(set-cpi-tynd! cpi (append tn-dict std-dict))
(set-cpi-ptl! cpi '()) ; list of lists of strings
(set-cpi-ctl! cpi '()) ; list of strings ?
(set-cpi-top! cpi #f) ; at top level
cpi))
;; Need to have a "CPI" stack to deal with types (re)defined in multiple
@ -131,6 +128,22 @@
(use-modules (ice-9 pretty-print))
;; The following three routines are used in an attempt to track the state
;; of the parse with respect to top-level declarations, in order to know
;; when includes can be parsed recursively. See how include is handled in
;; the lexer.
(define (at-top!) ;; declare parse at top-level; called by the parser
(let ((info (fluid-ref *info*)))
(set-cpi-top! info #t)))
(define (at-top?) ;; predicate to determine if at top level; called by lexer
(cpi-top (fluid-ref *info*)))
(define (not-top!) ;; declare parser not at top-level; called by the lexer
(let ((info (fluid-ref *info*)))
(set-cpi-top! info #f)))
;; @deffn find-new-typenames decl
;; Helper for @code{save-typenames}.
;; Given declaration return a list of new typenames (via @code{typedef}).
@ -274,8 +287,8 @@
(skip (list 'keep)) ; CPP skip-input stack
(info (fluid-ref *info*)) ; assume make and run in same thread
(pstk '()) ; port stack
(x-def? (or xdef? (lambda (name mode) (eqv? mode 'code)))))
;; Return the first (tval lval) pair not excluded by the CPP.
(x-def? (or xdef? def-xdef?)))
;; Return the first (tval . lval) pair not excluded by the CPP.
(lambda ()
(define (eval-flow?)
@ -309,6 +322,7 @@
(or (with-input-from-file pth run-parse)
(throw 'parse-error "~A" pth))
(perr file))))
(simple-format #t "INCLUDE top?=~S\n" (at-top?))
(for-each add-define (xp1 tree)) ; add def's
;; Attach tree onto "include" statement.
(if (pair? tree) (set! stmt (append stmt (list tree))))

View file

@ -1,10 +1,10 @@
;; ./mach.d/c99act.scm
;; Copyright (C) 2015,2016 Matthew R. Wette
;; Copyright 2016,2017 Matthew R. Wette
;;
;; This software is covered by the GNU GENERAL PUBLIC LICENCE, Version 3,
;; or any later version published by the Free Software Foundation. See the
;; file COPYING included with the this distribution.
;; or any later version published by the Free Software Foundation. See
;; the file COPYING included with the this distribution.
(define act-v
(vector
@ -399,10 +399,10 @@
;; struct-declarator => ":" constant-expression
(lambda ($2 $1 . $rest)
`(comp-declr (bit-field ,$2)))
;; enum-specifier => "enum" identifier "{" enumerator-list "}"
;; enum-specifier => "enum" ident-like "{" enumerator-list "}"
(lambda ($5 $4 $3 $2 $1 . $rest)
`(enum-def ,$2 ,(tl->list $4)))
;; enum-specifier => "enum" identifier "{" enumerator-list "," "}"
;; enum-specifier => "enum" ident-like "{" enumerator-list "," "}"
(lambda ($6 $5 $4 $3 $2 $1 . $rest)
`(enum-def ,$2 ,(tl->list $4)))
;; enum-specifier => "enum" "{" enumerator-list "}"
@ -411,7 +411,7 @@
;; enum-specifier => "enum" "{" enumerator-list "," "}"
(lambda ($5 $4 $3 $2 $1 . $rest)
`(enum-def ,(tl->list $3)))
;; enum-specifier => "enum" identifier
;; enum-specifier => "enum" ident-like
(lambda ($2 $1 . $rest) `(enum-ref ,$2))
;; enumerator-list => enumerator
(lambda ($1 . $rest) (make-tl 'enum-def-list $1))
@ -699,9 +699,9 @@
(lambda ($3 $2 $1 . $rest) `(return ,$2))
;; jump-statement => "return" ";"
(lambda ($2 $1 . $rest) `(return (expr)))
;; translation-unit => external-declaration
;; translation-unit => external-declaration-proxy
(lambda ($1 . $rest) (make-tl 'trans-unit $1))
;; translation-unit => translation-unit external-declaration
;; translation-unit => translation-unit external-declaration-proxy
(lambda ($2 $1 . $rest)
(cond ((eqv? 'trans-unit (car $2))
(let* ((t1 (tl-append $1 '(extern-C-begin)))
@ -709,6 +709,10 @@
(t3 (tl-append t2 '(extern-C-end))))
t3))
(else (tl-append $1 $2))))
;; external-declaration-proxy => $P2 external-declaration
(lambda ($2 $1 . $rest) $2)
;; $P2 =>
(lambda $rest (at-top!))
;; external-declaration => function-definition
(lambda ($1 . $rest) $1)
;; external-declaration => declaration

File diff suppressed because it is too large Load diff

View file

@ -1,10 +1,10 @@
;; ./mach.d/c99xact.scm
;; Copyright (C) 2015,2016 Matthew R. Wette
;; Copyright 2016,2017 Matthew R. Wette
;;
;; This software is covered by the GNU GENERAL PUBLIC LICENCE, Version 3,
;; or any later version published by the Free Software Foundation. See the
;; file COPYING included with the this distribution.
;; or any later version published by the Free Software Foundation. See
;; the file COPYING included with the this distribution.
(define act-v
(vector
@ -399,10 +399,10 @@
;; struct-declarator => ":" constant-expression
(lambda ($2 $1 . $rest)
`(comp-declr (bit-field ,$2)))
;; enum-specifier => "enum" identifier "{" enumerator-list "}"
;; enum-specifier => "enum" ident-like "{" enumerator-list "}"
(lambda ($5 $4 $3 $2 $1 . $rest)
`(enum-def ,$2 ,(tl->list $4)))
;; enum-specifier => "enum" identifier "{" enumerator-list "," "}"
;; enum-specifier => "enum" ident-like "{" enumerator-list "," "}"
(lambda ($6 $5 $4 $3 $2 $1 . $rest)
`(enum-def ,$2 ,(tl->list $4)))
;; enum-specifier => "enum" "{" enumerator-list "}"
@ -411,7 +411,7 @@
;; enum-specifier => "enum" "{" enumerator-list "," "}"
(lambda ($5 $4 $3 $2 $1 . $rest)
`(enum-def ,(tl->list $3)))
;; enum-specifier => "enum" identifier
;; enum-specifier => "enum" ident-like
(lambda ($2 $1 . $rest) `(enum-ref ,$2))
;; enumerator-list => enumerator
(lambda ($1 . $rest) (make-tl 'enum-def-list $1))
@ -699,9 +699,9 @@
(lambda ($3 $2 $1 . $rest) `(return ,$2))
;; jump-statement => "return" ";"
(lambda ($2 $1 . $rest) `(return (expr)))
;; translation-unit => external-declaration
;; translation-unit => external-declaration-proxy
(lambda ($1 . $rest) (make-tl 'trans-unit $1))
;; translation-unit => translation-unit external-declaration
;; translation-unit => translation-unit external-declaration-proxy
(lambda ($2 $1 . $rest)
(cond ((eqv? 'trans-unit (car $2))
(let* ((t1 (tl-append $1 '(extern-C-begin)))
@ -709,6 +709,10 @@
(t3 (tl-append t2 '(extern-C-end))))
t3))
(else (tl-append $1 $2))))
;; external-declaration-proxy => $P2 external-declaration
(lambda ($2 $1 . $rest) $2)
;; $P2 =>
(lambda $rest (at-top!))
;; external-declaration => function-definition
(lambda ($1 . $rest) $1)
;; external-declaration => declaration

File diff suppressed because it is too large Load diff

View file

@ -1,10 +1,10 @@
;; ./mach.d/cppact.scm
;; Copyright (C) 2015,2016 Matthew R. Wette
;;
;;
;; This software is covered by the GNU GENERAL PUBLIC LICENCE, Version 3,
;; or any later version published by the Free Software Foundation. See the
;; file COPYING included with the this distribution.
;; or any later version published by the Free Software Foundation. See
;; the file COPYING included with the this distribution.
(define act-v
(vector

View file

@ -1,10 +1,10 @@
;; ./mach.d/cpptab.scm
;; Copyright (C) 2015,2016 Matthew R. Wette
;;
;;
;; This software is covered by the GNU GENERAL PUBLIC LICENCE, Version 3,
;; or any later version published by the Free Software Foundation. See the
;; file COPYING included with the this distribution.
;; or any later version published by the Free Software Foundation. See
;; the file COPYING included with the this distribution.
(define len-v
#(1 1 5 1 3 1 3 1 3 1 3 1 3 1 3 3 1 3 3 3 3 1 3 3 1 3 3 1 3 3 3 1 2 2 2 2

View file

@ -38,7 +38,7 @@
;; The output of the end parser will be a SXML tree (w/o the @code{*TOP*} node.
(define c99-spec
(lalr-spec
(notice lang-crn-lic)
(notice (string-append "Copyright 2016,2017 Matthew R. Wette" lang-crn-lic))
(prec< 'then "else") ; "then/else" SR-conflict resolution
(prec< 'imp ; "implied type" SR-conflict resolution
"char" "short" "int" "long"
@ -345,13 +345,13 @@
)
(enum-specifier ; S 6.7.2.2
("enum" identifier "{" enumerator-list "}"
("enum" ident-like "{" enumerator-list "}"
($$ `(enum-def ,$2 ,(tl->list $4))))
("enum" identifier "{" enumerator-list "," "}"
("enum" ident-like "{" enumerator-list "," "}"
($$ `(enum-def ,$2 ,(tl->list $4))))
("enum" "{" enumerator-list "}" ($$ `(enum-def ,(tl->list $3))))
("enum" "{" enumerator-list "," "}" ($$ `(enum-def ,(tl->list $3))))
("enum" identifier ($$ `(enum-ref ,$2)))
("enum" ident-like ($$ `(enum-ref ,$2)))
)
;; keeping old enum-def-list in parse tree
@ -609,9 +609,9 @@
;; external definitions
(translation-unit
(external-declaration ($$ (make-tl 'trans-unit $1)))
(external-declaration-proxy ($$ (make-tl 'trans-unit $1)))
(translation-unit
external-declaration
external-declaration-proxy
($$ (cond ((eqv? 'trans-unit (car $2))
(let* ((t1 (tl-append $1 '(extern-C-begin)))
(t2 (tl-extend t1 (cdr $2)))
@ -620,12 +620,14 @@
(else (tl-append $1 $2)))))
)
(external-declaration-proxy (($$ (at-top!)) external-declaration ($$ $2)))
(external-declaration
(function-definition)
(declaration)
(lone-comment)
(cpp-statement)
;; The following is a kludge to deal with @code{extern "C" @{}.
;; The following is a kludge to deal with @code{extern "C" @{ ...}.
("extern" $string "{" translation-unit "}" ($$ (tl->list $4)))
)
@ -644,8 +646,6 @@
)
(opt-code-comment () (code-comment))
;;(opt-lone-comment () (lone-comment))
;;(opt-comment () (code-comment) (lone-comment))
;; non-terminal leaves
(identifier

View file

@ -48,9 +48,17 @@
(cons 'mtab mtab)
(cons 'act-v act-v))))
(define* (my-c-lexer #:key (mode 'file) (xdef? #f))
(let ((def-lxr (gen-c-lexer #:mode mode #:xdef? xdef?)))
(lambda ()
(let ((tok (def-lxr)))
;;(simple-format #t "~S\n" tok)
tok))))
(define (run-parse)
(let ((info (fluid-ref *info*)))
(raw-parser (gen-c-lexer) #:debug (cpi-debug info))))
;;(raw-parser (gen-c-lexer) #:debug (cpi-debug info))))
(raw-parser (my-c-lexer) #:debug (cpi-debug info))))
;; @item parse-c [#:cpp-defs def-a-list] [#:inc-dirs dir-list] [#:debug bool] \
;; [#:mode ('code|'file)]
@ -70,7 +78,7 @@
(with-fluid*
*info* info
(lambda ()
(raw-parser (gen-c-lexer #:mode mode #:xdef? xdef?)
(raw-parser (my-c-lexer #:mode mode #:xdef? xdef?)
#:debug debug)))))
(lambda (key fmt . rest)
(apply simple-format (current-error-port) (string-append fmt "\n") rest)

View file

@ -1,10 +1,10 @@
;;; module/nyacc/util.scm
;;;
;;; Copyright (C) 2015 Matthew R. Wette
;;; Copyright (C) 2015-2017 Matthew R. Wette
;;;
;;; This software is covered by the GNU GENERAL PUBLIC LICENCE, Version 3,
;;; or any later version published by the Free Software Foundation. See the
;;; file COPYING included with the nyacc distribution.
;;; or any later version published by the Free Software Foundation. See
;;; the file COPYING included with the nyacc distribution.
;; runtime utilities for the parsers -- needs work
@ -33,11 +33,11 @@
;; This is a generic copyright/licence that will be printed in the output
;; of the examples/nyacc/lang/*/ actions.scm and tables.scm files.
(define lang-crn-lic "Copyright (C) 2015,2016 Matthew R. Wette
(define lang-crn-lic "
This software is covered by the GNU GENERAL PUBLIC LICENCE, Version 3,
or any later version published by the Free Software Foundation. See the
file COPYING included with the this distribution.")
or any later version published by the Free Software Foundation. See
the file COPYING included with the this distribution.")
(define (fmterr fmt . args)
(apply simple-format (current-error-port) fmt args))
@ -47,21 +47,21 @@ file COPYING included with the this distribution.")
(define *input-stack* (make-fluid '()))
(define (reset-input-stack)
(fluid-set! *input-stack* '()))
(fluid-set! *input-stack* '()))
(define (push-input port)
(let ((curr (current-input-port))
(ipstk (fluid-ref *input-stack*)))
(fluid-set! *input-stack* (cons curr ipstk))
(set-current-input-port port)))
(let ((curr (current-input-port))
(ipstk (fluid-ref *input-stack*)))
(fluid-set! *input-stack* (cons curr ipstk))
(set-current-input-port port)))
;; Return #f if empty
(define (pop-input)
(let ((ipstk (fluid-ref *input-stack*)))
(if (null? ipstk) #f
(begin
(set-current-input-port (car ipstk))
(fluid-set! *input-stack* (cdr ipstk))))))
(let ((ipstk (fluid-ref *input-stack*)))
(if (null? ipstk) #f
(begin
(set-current-input-port (car ipstk))
(fluid-set! *input-stack* (cdr ipstk))))))
;; It may be possible to reimplement with closures, using soft-ports.
;; (push-string-input ...