|
|
@ -1,6 +1,6 @@
|
|
|
|
#cs
|
|
|
|
#cs
|
|
|
|
(module yacc mzscheme
|
|
|
|
(module yacc mzscheme
|
|
|
|
|
|
|
|
|
|
|
|
(require-for-syntax "private-yacc/parser-builder.ss"
|
|
|
|
(require-for-syntax "private-yacc/parser-builder.ss"
|
|
|
|
"private-yacc/yacc-helper.ss")
|
|
|
|
"private-yacc/yacc-helper.ss")
|
|
|
|
(require "private-yacc/parser-actions.ss"
|
|
|
|
(require "private-yacc/parser-actions.ss"
|
|
|
@ -8,117 +8,120 @@
|
|
|
|
"private-lex/token.ss")
|
|
|
|
"private-lex/token.ss")
|
|
|
|
|
|
|
|
|
|
|
|
(provide parser)
|
|
|
|
(provide parser)
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax parser
|
|
|
|
(define-syntax (parser stx)
|
|
|
|
(lambda (stx)
|
|
|
|
(syntax-case stx ()
|
|
|
|
(syntax-case stx ()
|
|
|
|
((_ args ...)
|
|
|
|
((_ args ...)
|
|
|
|
(let ((arg-list (syntax->list (syntax (args ...))))
|
|
|
|
(let ((arg-list (syntax->list (syntax (args ...))))
|
|
|
|
(src-pos #f)
|
|
|
|
(debug #f)
|
|
|
|
(debug #f)
|
|
|
|
(error #f)
|
|
|
|
(error #f)
|
|
|
|
(tokens #f)
|
|
|
|
(tokens #f)
|
|
|
|
(start #f)
|
|
|
|
(start #f)
|
|
|
|
(end #f)
|
|
|
|
(end #f)
|
|
|
|
(precs #f)
|
|
|
|
(precs #f)
|
|
|
|
(suppress #f)
|
|
|
|
(suppress #f)
|
|
|
|
(grammar #f))
|
|
|
|
(grammar #f))
|
|
|
|
(for-each
|
|
|
|
(for-each
|
|
|
|
(lambda (arg)
|
|
|
|
(lambda (arg)
|
|
|
|
(syntax-case* arg (debug error tokens start end precs grammar suppress)
|
|
|
|
(syntax-case* arg (debug error tokens start end precs grammar suppress src-pos)
|
|
|
|
(lambda (a b)
|
|
|
|
(lambda (a b)
|
|
|
|
(eq? (syntax-object->datum a) (syntax-object->datum b)))
|
|
|
|
(eq? (syntax-object->datum a) (syntax-object->datum b)))
|
|
|
|
((debug filename)
|
|
|
|
((debug filename)
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((not (string? (syntax-object->datum (syntax filename))))
|
|
|
|
((not (string? (syntax-object->datum (syntax filename))))
|
|
|
|
(raise-syntax-error
|
|
|
|
(raise-syntax-error
|
|
|
|
'parser-debug
|
|
|
|
'parser-debug
|
|
|
|
"Debugging filename must be a string"
|
|
|
|
"Debugging filename must be a string"
|
|
|
|
(syntax filename)))
|
|
|
|
(syntax filename)))
|
|
|
|
(debug
|
|
|
|
(debug
|
|
|
|
(raise-syntax-error #f "Multiple debug declarations" stx))
|
|
|
|
(raise-syntax-error #f "Multiple debug declarations" stx))
|
|
|
|
(else
|
|
|
|
(else
|
|
|
|
(set! debug (syntax-object->datum (syntax filename))))))
|
|
|
|
(set! debug (syntax-object->datum (syntax filename))))))
|
|
|
|
((suppress)
|
|
|
|
((suppress)
|
|
|
|
(set! suppress #t))
|
|
|
|
(set! suppress #t))
|
|
|
|
((error expression)
|
|
|
|
((src-pos)
|
|
|
|
(if error
|
|
|
|
(set! src-pos #t))
|
|
|
|
(raise-syntax-error #f "Multiple error declarations" stx)
|
|
|
|
((error expression)
|
|
|
|
(set! error (syntax expression))))
|
|
|
|
(if error
|
|
|
|
((tokens def ...)
|
|
|
|
(raise-syntax-error #f "Multiple error declarations" stx)
|
|
|
|
(if tokens
|
|
|
|
(set! error (syntax expression))))
|
|
|
|
(raise-syntax-error #f "Multiple tokens declarations" stx)
|
|
|
|
((tokens def ...)
|
|
|
|
(set! tokens arg)))
|
|
|
|
(if tokens
|
|
|
|
((start symbol)
|
|
|
|
(raise-syntax-error #f "Multiple tokens declarations" stx)
|
|
|
|
(cond
|
|
|
|
(set! tokens arg)))
|
|
|
|
((not (identifier? (syntax symbol)))
|
|
|
|
((start symbol)
|
|
|
|
(raise-syntax-error
|
|
|
|
(cond
|
|
|
|
'parser-start
|
|
|
|
((not (identifier? (syntax symbol)))
|
|
|
|
"Start non-terminal must be a symbol"
|
|
|
|
(raise-syntax-error
|
|
|
|
(syntax symbol)))
|
|
|
|
'parser-start
|
|
|
|
(start
|
|
|
|
"Start non-terminal must be a symbol"
|
|
|
|
(raise-syntax-error #f "Multiple start declarations" stx))
|
|
|
|
(syntax symbol)))
|
|
|
|
(else
|
|
|
|
(start
|
|
|
|
(set! start (syntax symbol)))))
|
|
|
|
(raise-syntax-error #f "Multiple start declarations" stx))
|
|
|
|
((end symbols ...)
|
|
|
|
(else
|
|
|
|
(begin
|
|
|
|
(set! start (syntax symbol)))))
|
|
|
|
(for-each
|
|
|
|
((end symbols ...)
|
|
|
|
(lambda (sym)
|
|
|
|
(begin
|
|
|
|
(if (not (identifier? sym))
|
|
|
|
(for-each
|
|
|
|
(raise-syntax-error
|
|
|
|
(lambda (sym)
|
|
|
|
'parser-end
|
|
|
|
(if (not (identifier? sym))
|
|
|
|
"End token must be a symbol"
|
|
|
|
(raise-syntax-error
|
|
|
|
sym)))
|
|
|
|
'parser-end
|
|
|
|
(syntax->list (syntax (symbols ...))))
|
|
|
|
"End token must be a symbol"
|
|
|
|
(let ((d (duplicate-list? (syntax-object->datum
|
|
|
|
sym)))
|
|
|
|
(syntax (symbols ...))))))
|
|
|
|
(syntax->list (syntax (symbols ...))))
|
|
|
|
(if d
|
|
|
|
(let ((d (duplicate-list? (syntax-object->datum
|
|
|
|
(raise-syntax-error
|
|
|
|
(syntax (symbols ...))))))
|
|
|
|
'parser-end
|
|
|
|
(if d
|
|
|
|
(format "Duplicate end token definition for ~a" d)
|
|
|
|
|
|
|
|
arg)))
|
|
|
|
|
|
|
|
(if (= 0 (length (syntax->list (syntax (symbols ...)))))
|
|
|
|
|
|
|
|
(raise-syntax-error
|
|
|
|
(raise-syntax-error
|
|
|
|
'parser-end
|
|
|
|
'parser-end
|
|
|
|
"end declaration must contain at least 1 token"
|
|
|
|
(format "Duplicate end token definition for ~a" d)
|
|
|
|
arg))
|
|
|
|
arg)))
|
|
|
|
(if end
|
|
|
|
(if (= 0 (length (syntax->list (syntax (symbols ...)))))
|
|
|
|
(raise-syntax-error #f "Multiple end declarations" stx))
|
|
|
|
(raise-syntax-error
|
|
|
|
(set! end (syntax->list (syntax (symbols ...))))))
|
|
|
|
'parser-end
|
|
|
|
((precs decls ...)
|
|
|
|
"end declaration must contain at least 1 token"
|
|
|
|
(if precs
|
|
|
|
arg))
|
|
|
|
(raise-syntax-error #f "Multiple precs declarations" stx)
|
|
|
|
(if end
|
|
|
|
(set! precs arg)))
|
|
|
|
(raise-syntax-error #f "Multiple end declarations" stx))
|
|
|
|
((grammar prods ...)
|
|
|
|
(set! end (syntax->list (syntax (symbols ...))))))
|
|
|
|
(if grammar
|
|
|
|
((precs decls ...)
|
|
|
|
(raise-syntax-error #f "Multiple grammar declarations" stx)
|
|
|
|
(if precs
|
|
|
|
(set! grammar arg)))
|
|
|
|
(raise-syntax-error #f "Multiple precs declarations" stx)
|
|
|
|
(_ (raise-syntax-error 'parser-args "argument must match (debug filename), (error expression), (tokens def ...), (start non-term), (end tokens ...), (precs decls ...), or (grammar prods ...)" arg))))
|
|
|
|
(set! precs arg)))
|
|
|
|
(syntax->list (syntax (args ...))))
|
|
|
|
((grammar prods ...)
|
|
|
|
(if (not tokens)
|
|
|
|
(if grammar
|
|
|
|
(raise-syntax-error #f "missing tokens declaration" stx))
|
|
|
|
(raise-syntax-error #f "Multiple grammar declarations" stx)
|
|
|
|
(if (not error)
|
|
|
|
(set! grammar arg)))
|
|
|
|
(raise-syntax-error #f "missing error declaration" stx))
|
|
|
|
(_ (raise-syntax-error 'parser-args "argument must match (debug filename), (error expression), (tokens def ...), (start non-term), (end tokens ...), (precs decls ...), or (grammar prods ...)" arg))))
|
|
|
|
(if (not grammar)
|
|
|
|
(syntax->list (syntax (args ...))))
|
|
|
|
(raise-syntax-error #f "missing grammar declaration" stx))
|
|
|
|
(if (not tokens)
|
|
|
|
(if (not end)
|
|
|
|
(raise-syntax-error #f "missing tokens declaration" stx))
|
|
|
|
(raise-syntax-error #f "missing end declaration" stx))
|
|
|
|
(if (not error)
|
|
|
|
(if (not start)
|
|
|
|
(raise-syntax-error #f "missing error declaration" stx))
|
|
|
|
(raise-syntax-error #f "missing start declaration" stx))
|
|
|
|
(if (not grammar)
|
|
|
|
(build-parser (if debug debug "")
|
|
|
|
(raise-syntax-error #f "missing grammar declaration" stx))
|
|
|
|
suppress
|
|
|
|
(if (not end)
|
|
|
|
error
|
|
|
|
(raise-syntax-error #f "missing end declaration" stx))
|
|
|
|
tokens
|
|
|
|
(if (not start)
|
|
|
|
start
|
|
|
|
(raise-syntax-error #f "missing start declaration" stx))
|
|
|
|
end
|
|
|
|
(build-parser (if debug debug "")
|
|
|
|
precs
|
|
|
|
src-pos
|
|
|
|
grammar
|
|
|
|
suppress
|
|
|
|
#'here
|
|
|
|
error
|
|
|
|
stx)))
|
|
|
|
tokens
|
|
|
|
(_
|
|
|
|
start
|
|
|
|
(raise-syntax-error
|
|
|
|
end
|
|
|
|
#f
|
|
|
|
precs
|
|
|
|
"parser must have the form (parser args ...)"
|
|
|
|
grammar
|
|
|
|
stx)))))
|
|
|
|
#'here
|
|
|
|
|
|
|
|
stx)))
|
|
|
|
|
|
|
|
(_
|
|
|
|
|
|
|
|
(raise-syntax-error
|
|
|
|
|
|
|
|
#f
|
|
|
|
|
|
|
|
"parser must have the form (parser args ...)"
|
|
|
|
|
|
|
|
stx))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|