You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
397 lines
18 KiB
Racket
397 lines
18 KiB
Racket
16 years ago
|
#lang scheme/base
|
||
|
|
||
|
(require (for-syntax scheme/base
|
||
14 years ago
|
"private-yacc/parser-builder.rkt"
|
||
|
"private-yacc/grammar.rkt"
|
||
|
"private-yacc/yacc-helper.rkt"
|
||
|
"private-yacc/parser-actions.rkt"))
|
||
|
(require "private-lex/token.rkt"
|
||
|
"private-yacc/parser-actions.rkt"
|
||
|
mzlib/etc
|
||
|
mzlib/pretty
|
||
|
syntax/readerr)
|
||
|
|
||
|
(provide parser)
|
||
|
|
||
|
|
||
20 years ago
|
;; convert-parse-table : (vectorof (listof (cons/c gram-sym? action?))) ->
|
||
|
;; (vectorof (symbol runtime-action hashtable))
|
||
|
(define-for-syntax (convert-parse-table table)
|
||
|
(list->vector
|
||
|
(map
|
||
|
(lambda (state-entry)
|
||
16 years ago
|
(let ((ht (make-hasheq)))
|
||
20 years ago
|
(for-each
|
||
|
(lambda (gs/action)
|
||
16 years ago
|
(hash-set! ht
|
||
|
(gram-sym-symbol (car gs/action))
|
||
|
(action->runtime-action (cdr gs/action))))
|
||
20 years ago
|
state-entry)
|
||
|
ht))
|
||
|
(vector->list table))))
|
||
|
|
||
23 years ago
|
(define-syntax (parser stx)
|
||
|
(syntax-case stx ()
|
||
|
((_ args ...)
|
||
|
(let ((arg-list (syntax->list (syntax (args ...))))
|
||
|
(src-pos #f)
|
||
|
(debug #f)
|
||
|
(error #f)
|
||
|
(tokens #f)
|
||
|
(start #f)
|
||
|
(end #f)
|
||
|
(precs #f)
|
||
|
(suppress #f)
|
||
23 years ago
|
(grammar #f)
|
||
|
(yacc-output #f))
|
||
23 years ago
|
(for-each
|
||
|
(lambda (arg)
|
||
20 years ago
|
(syntax-case* arg (debug error tokens start end precs grammar
|
||
|
suppress src-pos yacc-output)
|
||
23 years ago
|
(lambda (a b)
|
||
20 years ago
|
(eq? (syntax-e a) (syntax-e b)))
|
||
23 years ago
|
((debug filename)
|
||
|
(cond
|
||
20 years ago
|
((not (string? (syntax-e (syntax filename))))
|
||
23 years ago
|
(raise-syntax-error
|
||
20 years ago
|
#f
|
||
23 years ago
|
"Debugging filename must be a string"
|
||
20 years ago
|
stx
|
||
23 years ago
|
(syntax filename)))
|
||
|
(debug
|
||
|
(raise-syntax-error #f "Multiple debug declarations" stx))
|
||
|
(else
|
||
20 years ago
|
(set! debug (syntax-e (syntax filename))))))
|
||
23 years ago
|
((suppress)
|
||
|
(set! suppress #t))
|
||
|
((src-pos)
|
||
|
(set! src-pos #t))
|
||
|
((error expression)
|
||
|
(if error
|
||
|
(raise-syntax-error #f "Multiple error declarations" stx)
|
||
|
(set! error (syntax expression))))
|
||
|
((tokens def ...)
|
||
20 years ago
|
(begin
|
||
|
(when tokens
|
||
|
(raise-syntax-error #f "Multiple tokens declarations" stx))
|
||
|
(let ((defs (syntax->list (syntax (def ...)))))
|
||
|
(for-each
|
||
|
(lambda (d)
|
||
|
(unless (identifier? d)
|
||
|
(raise-syntax-error
|
||
|
#f
|
||
|
"Token-group name must be an identifier"
|
||
|
stx
|
||
|
d)))
|
||
|
defs)
|
||
|
(set! tokens defs))))
|
||
22 years ago
|
((start symbol ...)
|
||
|
(let ((symbols (syntax->list (syntax (symbol ...)))))
|
||
|
(for-each
|
||
|
(lambda (sym)
|
||
|
(unless (identifier? sym)
|
||
20 years ago
|
(raise-syntax-error #f
|
||
22 years ago
|
"Start symbol must be a symbol"
|
||
20 years ago
|
stx
|
||
22 years ago
|
sym)))
|
||
|
symbols)
|
||
|
(when start
|
||
|
(raise-syntax-error #f "Multiple start declarations" stx))
|
||
|
(when (null? symbols)
|
||
20 years ago
|
(raise-syntax-error #f
|
||
22 years ago
|
"Missing start symbol"
|
||
20 years ago
|
stx
|
||
|
arg))
|
||
22 years ago
|
(set! start symbols)))
|
||
23 years ago
|
((end symbols ...)
|
||
22 years ago
|
(let ((symbols (syntax->list (syntax (symbols ...)))))
|
||
23 years ago
|
(for-each
|
||
|
(lambda (sym)
|
||
22 years ago
|
(unless (identifier? sym)
|
||
20 years ago
|
(raise-syntax-error #f
|
||
22 years ago
|
"End token must be a symbol"
|
||
20 years ago
|
stx
|
||
22 years ago
|
sym)))
|
||
|
symbols)
|
||
20 years ago
|
(let ((d (duplicate-list? (map syntax-e symbols))))
|
||
22 years ago
|
(when d
|
||
20 years ago
|
(raise-syntax-error
|
||
|
#f
|
||
|
(format "Duplicate end token definition for ~a" d)
|
||
|
stx
|
||
|
arg))
|
||
22 years ago
|
(when (null? symbols)
|
||
20 years ago
|
(raise-syntax-error
|
||
|
#f
|
||
|
"end declaration must contain at least 1 token"
|
||
|
stx
|
||
|
arg))
|
||
22 years ago
|
(when end
|
||
23 years ago
|
(raise-syntax-error #f "Multiple end declarations" stx))
|
||
22 years ago
|
(set! end symbols))))
|
||
23 years ago
|
((precs decls ...)
|
||
|
(if precs
|
||
|
(raise-syntax-error #f "Multiple precs declarations" stx)
|
||
20 years ago
|
(set! precs (syntax/loc arg (decls ...)))))
|
||
23 years ago
|
((grammar prods ...)
|
||
|
(if grammar
|
||
|
(raise-syntax-error #f "Multiple grammar declarations" stx)
|
||
20 years ago
|
(set! grammar (syntax/loc arg (prods ...)))))
|
||
23 years ago
|
((yacc-output filename)
|
||
|
(cond
|
||
20 years ago
|
((not (string? (syntax-e (syntax filename))))
|
||
|
(raise-syntax-error #f
|
||
22 years ago
|
"Yacc-output filename must be a string"
|
||
20 years ago
|
stx
|
||
22 years ago
|
(syntax filename)))
|
||
23 years ago
|
(yacc-output
|
||
|
(raise-syntax-error #f "Multiple yacc-output declarations" stx))
|
||
|
(else
|
||
20 years ago
|
(set! yacc-output (syntax-e (syntax filename))))))
|
||
|
(_ (raise-syntax-error #f "argument must match (debug filename), (error expression), (tokens def ...), (start non-term), (end tokens ...), (precs decls ...), or (grammar prods ...)" stx arg))))
|
||
23 years ago
|
(syntax->list (syntax (args ...))))
|
||
22 years ago
|
(unless tokens
|
||
|
(raise-syntax-error #f "missing tokens declaration" stx))
|
||
|
(unless error
|
||
|
(raise-syntax-error #f "missing error declaration" stx))
|
||
|
(unless grammar
|
||
|
(raise-syntax-error #f "missing grammar declaration" stx))
|
||
|
(unless end
|
||
|
(raise-syntax-error #f "missing end declaration" stx))
|
||
|
(unless start
|
||
|
(raise-syntax-error #f "missing start declaration" stx))
|
||
20 years ago
|
(let-values (((table all-term-syms actions check-syntax-fix)
|
||
23 years ago
|
(build-parser (if debug debug "")
|
||
|
src-pos
|
||
|
suppress
|
||
|
tokens
|
||
|
start
|
||
|
end
|
||
|
precs
|
||
21 years ago
|
grammar)))
|
||
22 years ago
|
(when (and yacc-output (not (string=? yacc-output "")))
|
||
21 years ago
|
(with-handlers [(exn:fail:filesystem?
|
||
22 years ago
|
(lambda (e)
|
||
13 years ago
|
(eprintf
|
||
14 years ago
|
"Cannot write yacc-output to file \"~a\"\n"
|
||
21 years ago
|
yacc-output)))]
|
||
22 years ago
|
(call-with-output-file yacc-output
|
||
|
(lambda (port)
|
||
16 years ago
|
(display-yacc (syntax->datum grammar)
|
||
22 years ago
|
tokens
|
||
16 years ago
|
(map syntax->datum start)
|
||
22 years ago
|
(if precs
|
||
16 years ago
|
(syntax->datum precs)
|
||
22 years ago
|
#f)
|
||
16 years ago
|
port))
|
||
|
#:exists 'truncate)))
|
||
23 years ago
|
(with-syntax ((check-syntax-fix check-syntax-fix)
|
||
|
(err error)
|
||
|
(ends end)
|
||
22 years ago
|
(starts start)
|
||
22 years ago
|
(debug debug)
|
||
20 years ago
|
(table (convert-parse-table table))
|
||
|
(all-term-syms all-term-syms)
|
||
23 years ago
|
(actions actions)
|
||
|
(src-pos src-pos))
|
||
|
(syntax
|
||
|
(begin
|
||
|
check-syntax-fix
|
||
20 years ago
|
(parser-body debug err (quote starts) (quote ends) table all-term-syms actions src-pos)))))))
|
||
23 years ago
|
(_
|
||
22 years ago
|
(raise-syntax-error #f
|
||
|
"parser must have the form (parser args ...)"
|
||
|
stx))))
|
||
21 years ago
|
|
||
23 years ago
|
(define (reduce-stack stack num ret-vals src-pos)
|
||
|
(cond
|
||
|
((> num 0)
|
||
|
(let* ((top-frame (car stack))
|
||
|
(ret-vals
|
||
|
(if src-pos
|
||
|
(cons (stack-frame-value top-frame)
|
||
|
(cons (stack-frame-start-pos top-frame)
|
||
|
(cons (stack-frame-end-pos top-frame)
|
||
|
ret-vals)))
|
||
|
(cons (stack-frame-value top-frame) ret-vals))))
|
||
|
(reduce-stack (cdr stack) (sub1 num) ret-vals src-pos)))
|
||
|
(else (values stack ret-vals))))
|
||
21 years ago
|
|
||
|
;; extract-helper : (symbol or make-token) any any -> symbol any any any
|
||
|
(define (extract-helper tok v1 v2)
|
||
|
(cond
|
||
|
((symbol? tok)
|
||
|
(values tok #f v1 v2))
|
||
|
((token? tok)
|
||
|
(values (real-token-name tok) (real-token-value tok) v1 v2))
|
||
13 years ago
|
(else (raise-argument-error 'parser
|
||
|
"(or/c symbol? token?)"
|
||
|
0
|
||
|
tok))))
|
||
12 years ago
|
|
||
|
;; well-formed-position-token?: any -> boolean
|
||
|
;; Returns true if pt is a position token whose position-token-token
|
||
|
;; is itself a token or a symbol.
|
||
|
;; This is meant to help raise more precise error messages when
|
||
|
;; a tokenizer produces an erroneous position-token wrapped twice.
|
||
|
;; (as often happens when omitting return-without-pos).
|
||
|
(define (well-formed-position-token? pt)
|
||
|
(and (position-token? pt)
|
||
|
(let ([t (position-token-token pt)])
|
||
|
(or (symbol? t)
|
||
|
(token? t)))))
|
||
21 years ago
|
|
||
|
;; extract-src-pos : position-token -> symbol any any any
|
||
|
(define (extract-src-pos ip)
|
||
|
(cond
|
||
12 years ago
|
((well-formed-position-token? ip)
|
||
21 years ago
|
(extract-helper (position-token-token ip)
|
||
|
(position-token-start-pos ip)
|
||
|
(position-token-end-pos ip)))
|
||
|
(else
|
||
13 years ago
|
(raise-argument-error 'parser
|
||
12 years ago
|
"well-formed-position-token?"
|
||
13 years ago
|
0
|
||
|
ip))))
|
||
21 years ago
|
|
||
|
;; extract-no-src-pos : (symbol or make-token) -> symbol any any any
|
||
|
(define (extract-no-src-pos ip)
|
||
|
(extract-helper ip #f #f))
|
||
|
|
||
16 years ago
|
(define-struct stack-frame (state value start-pos end-pos) #:inspector (make-inspector))
|
||
21 years ago
|
|
||
22 years ago
|
(define (make-empty-stack i) (list (make-stack-frame i #f #f #f)))
|
||
21 years ago
|
|
||
20 years ago
|
|
||
|
;; The table is a vector that maps each state to a hash-table that maps a
|
||
|
;; terminal symbol to either an accept, shift, reduce, or goto structure.
|
||
|
; We encode the structures according to the runtime-action data definition in
|
||
14 years ago
|
;; parser-actions.rkt
|
||
20 years ago
|
(define (parser-body debug? err starts ends table all-term-syms actions src-pos)
|
||
21 years ago
|
(local ((define extract
|
||
23 years ago
|
(if src-pos
|
||
21 years ago
|
extract-src-pos
|
||
|
extract-no-src-pos))
|
||
|
|
||
|
(define (fix-error stack tok val start-pos end-pos get-token)
|
||
|
(when debug? (pretty-print stack))
|
||
|
(local ((define (remove-input tok val start-pos end-pos)
|
||
|
(if (memq tok ends)
|
||
|
(raise-read-error "parser: Cannot continue after error"
|
||
|
#f #f #f #f #f)
|
||
|
(let ((a (find-action stack tok val start-pos end-pos)))
|
||
23 years ago
|
(cond
|
||
20 years ago
|
((runtime-shift? a)
|
||
14 years ago
|
;; (printf "shift:~a\n" (runtime-shift-state a))
|
||
20 years ago
|
(cons (make-stack-frame (runtime-shift-state a)
|
||
21 years ago
|
val
|
||
|
start-pos
|
||
|
end-pos)
|
||
|
stack))
|
||
23 years ago
|
(else
|
||
14 years ago
|
;; (printf "discard input:~a\n" tok)
|
||
21 years ago
|
(let-values (((tok val start-pos end-pos)
|
||
|
(extract (get-token))))
|
||
|
(remove-input tok val start-pos end-pos))))))))
|
||
|
(let remove-states ()
|
||
|
(let ((a (find-action stack 'error #f start-pos end-pos)))
|
||
|
(cond
|
||
20 years ago
|
((runtime-shift? a)
|
||
14 years ago
|
;; (printf "shift:~a\n" (runtime-shift-state a))
|
||
21 years ago
|
(set! stack
|
||
|
(cons
|
||
20 years ago
|
(make-stack-frame (runtime-shift-state a)
|
||
21 years ago
|
#f
|
||
|
start-pos
|
||
|
end-pos)
|
||
|
stack))
|
||
21 years ago
|
(remove-input tok val start-pos end-pos))
|
||
21 years ago
|
(else
|
||
14 years ago
|
;; (printf "discard state:~a\n" (car stack))
|
||
21 years ago
|
(cond
|
||
|
((< (length stack) 2)
|
||
|
(raise-read-error "parser: Cannot continue after error"
|
||
|
#f #f #f #f #f))
|
||
|
(else
|
||
|
(set! stack (cdr stack))
|
||
|
(remove-states)))))))))
|
||
|
|
||
|
(define (find-action stack tok val start-pos end-pos)
|
||
16 years ago
|
(unless (hash-ref all-term-syms
|
||
|
tok
|
||
|
#f)
|
||
20 years ago
|
(if src-pos
|
||
|
(err #f tok val start-pos end-pos)
|
||
|
(err #f tok val))
|
||
|
(raise-read-error (format "parser: got token of unknown type ~a" tok)
|
||
|
#f #f #f #f #f))
|
||
16 years ago
|
(hash-ref (vector-ref table (stack-frame-state (car stack)))
|
||
|
tok
|
||
|
#f))
|
||
20 years ago
|
|
||
21 years ago
|
(define (make-parser start-number)
|
||
|
(lambda (get-token)
|
||
15 years ago
|
(unless (and (procedure? get-token)
|
||
|
(procedure-arity-includes? get-token 0))
|
||
|
(error 'get-token "expected a nullary procedure, got ~e" get-token))
|
||
21 years ago
|
(let parsing-loop ((stack (make-empty-stack start-number))
|
||
|
(ip (get-token)))
|
||
|
(let-values (((tok val start-pos end-pos)
|
||
|
(extract ip)))
|
||
|
(let ((action (find-action stack tok val start-pos end-pos)))
|
||
22 years ago
|
(cond
|
||
20 years ago
|
((runtime-shift? action)
|
||
14 years ago
|
;; (printf "shift:~a\n" (runtime-shift-state action))
|
||
20 years ago
|
(parsing-loop (cons (make-stack-frame (runtime-shift-state action)
|
||
21 years ago
|
val
|
||
|
start-pos
|
||
|
end-pos)
|
||
|
stack)
|
||
|
(get-token)))
|
||
20 years ago
|
((runtime-reduce? action)
|
||
14 years ago
|
;; (printf "reduce:~a\n" (runtime-reduce-prod-num action))
|
||
22 years ago
|
(let-values (((new-stack args)
|
||
|
(reduce-stack stack
|
||
20 years ago
|
(runtime-reduce-rhs-length action)
|
||
22 years ago
|
null
|
||
|
src-pos)))
|
||
20 years ago
|
(let ((goto
|
||
|
(runtime-goto-state
|
||
16 years ago
|
(hash-ref
|
||
20 years ago
|
(vector-ref table (stack-frame-state (car new-stack)))
|
||
|
(runtime-reduce-lhs action)))))
|
||
22 years ago
|
(parsing-loop
|
||
|
(cons
|
||
|
(if src-pos
|
||
|
(make-stack-frame
|
||
|
goto
|
||
20 years ago
|
(apply (vector-ref actions (runtime-reduce-prod-num action)) args)
|
||
21 years ago
|
(if (null? args) start-pos (cadr args))
|
||
22 years ago
|
(if (null? args)
|
||
21 years ago
|
end-pos
|
||
20 years ago
|
(list-ref args (- (* (runtime-reduce-rhs-length action) 3) 1))))
|
||
22 years ago
|
(make-stack-frame
|
||
|
goto
|
||
20 years ago
|
(apply (vector-ref actions (runtime-reduce-prod-num action)) args)
|
||
22 years ago
|
#f
|
||
|
#f))
|
||
|
new-stack)
|
||
|
ip))))
|
||
20 years ago
|
((runtime-accept? action)
|
||
14 years ago
|
;; (printf "accept\n")
|
||
22 years ago
|
(stack-frame-value (car stack)))
|
||
|
(else
|
||
|
(if src-pos
|
||
21 years ago
|
(err #t tok val start-pos end-pos)
|
||
|
(err #t tok val))
|
||
|
(parsing-loop (fix-error stack tok val start-pos end-pos get-token)
|
||
|
(get-token))))))))))
|
||
22 years ago
|
(cond
|
||
|
((null? (cdr starts)) (make-parser 0))
|
||
|
(else
|
||
|
(let loop ((l starts)
|
||
|
(i 0))
|
||
|
(cond
|
||
|
((null? l) null)
|
||
|
(else (cons (make-parser i) (loop (cdr l) (add1 i))))))))))
|