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.
375 lines
16 KiB
Racket
375 lines
16 KiB
Racket
23 years ago
|
(module input-file-parser mzscheme
|
||
|
|
||
|
;; routines for parsing the input to the parser generator and producing a
|
||
|
;; grammar (See grammar.ss)
|
||
|
|
||
23 years ago
|
(require "yacc-helper.ss"
|
||
|
"../private-lex/token-syntax.ss"
|
||
|
"grammar.ss"
|
||
17 years ago
|
mzlib/class
|
||
|
mzlib/contract)
|
||
21 years ago
|
(require-for-template mzscheme)
|
||
22 years ago
|
|
||
23 years ago
|
|
||
22 years ago
|
(provide/contract
|
||
21 years ago
|
(parse-input ((listof identifier?) (listof identifier?) (listof identifier?)
|
||
20 years ago
|
(union false/c syntax?) syntax? any/c . -> . (is-a?/c grammar%)))
|
||
21 years ago
|
(get-term-list ((listof identifier?) . -> . (listof identifier?))))
|
||
23 years ago
|
|
||
23 years ago
|
(define stx-for-original-property (read-syntax #f (open-input-string "original")))
|
||
|
|
||
16 years ago
|
;; get-args: ??? -> (values (listof syntax) (or/c #f (cons integer? stx)))
|
||
21 years ago
|
(define (get-args i rhs src-pos term-defs)
|
||
16 years ago
|
(let ((empty-table (make-hash-table))
|
||
|
(biggest-pos #f))
|
||
23 years ago
|
(hash-table-put! empty-table 'error #t)
|
||
23 years ago
|
(for-each (lambda (td)
|
||
|
(let ((v (syntax-local-value td)))
|
||
|
(if (e-terminals-def? v)
|
||
|
(for-each (lambda (s)
|
||
|
(hash-table-put! empty-table (syntax-object->datum s) #t))
|
||
|
(syntax->list (e-terminals-def-t v))))))
|
||
21 years ago
|
term-defs)
|
||
16 years ago
|
(let ([args
|
||
|
(let get-args ((i i)
|
||
|
(rhs rhs))
|
||
|
(cond
|
||
|
((null? rhs) null)
|
||
|
(else
|
||
|
(let ((b (car rhs))
|
||
|
(name (if (hash-table-get empty-table (syntax-object->datum (car rhs)) (lambda () #f))
|
||
|
(gensym)
|
||
|
(string->symbol (format "$~a" i)))))
|
||
|
(cond
|
||
|
(src-pos
|
||
|
(let ([start-pos-id
|
||
|
(datum->syntax-object b (string->symbol (format "$~a-start-pos" i)) b stx-for-original-property)]
|
||
|
[end-pos-id
|
||
|
(datum->syntax-object b (string->symbol (format "$~a-end-pos" i)) b stx-for-original-property)])
|
||
|
(set! biggest-pos (cons start-pos-id end-pos-id))
|
||
|
`(,(datum->syntax-object b name b stx-for-original-property)
|
||
|
,start-pos-id
|
||
|
,end-pos-id
|
||
|
,@(get-args (add1 i) (cdr rhs)))))
|
||
|
(else
|
||
|
`(,(datum->syntax-object b name b stx-for-original-property)
|
||
|
,@(get-args (add1 i) (cdr rhs)))))))))])
|
||
|
(values args biggest-pos))))
|
||
23 years ago
|
|
||
23 years ago
|
;; Given the list of terminal symbols and the precedence/associativity definitions,
|
||
|
;; builds terminal structures (See grammar.ss)
|
||
|
;; build-terms: symbol list * symbol list list -> term list
|
||
|
(define (build-terms term-list precs)
|
||
|
(let ((counter 0)
|
||
|
|
||
|
;;(term-list (cons (gensym) term-list))
|
||
|
|
||
|
;; Will map a terminal symbol to its precedence/associativity
|
||
|
(prec-table (make-hash-table)))
|
||
|
|
||
|
;; Fill the prec table
|
||
|
(for-each
|
||
|
(lambda (p-decl)
|
||
|
(begin0
|
||
|
(let ((assoc (car p-decl)))
|
||
|
(for-each
|
||
|
(lambda (term-sym)
|
||
|
(hash-table-put! prec-table term-sym (make-prec counter assoc)))
|
||
|
(cdr p-decl)))
|
||
|
(set! counter (add1 counter))))
|
||
|
precs)
|
||
|
|
||
|
;; Build the terminal structures
|
||
|
(map
|
||
|
(lambda (term-sym)
|
||
22 years ago
|
(make-term term-sym
|
||
|
#f
|
||
|
(hash-table-get prec-table term-sym (lambda () #f))))
|
||
23 years ago
|
term-list)))
|
||
|
|
||
23 years ago
|
;; Retrieves the terminal symbols from a terminals-def (See terminal-syntax.ss)
|
||
21 years ago
|
;; get-terms-from-def: identifier? -> (listof identifier?)
|
||
23 years ago
|
(define (get-terms-from-def term-syn)
|
||
|
(let ((t (syntax-local-value term-syn (lambda () #f))))
|
||
|
(cond
|
||
23 years ago
|
((terminals-def? t) (syntax->list (terminals-def-t t)))
|
||
23 years ago
|
((e-terminals-def? t) (syntax->list (e-terminals-def-t t)))
|
||
23 years ago
|
(else
|
||
|
(raise-syntax-error
|
||
|
'parser-tokens
|
||
|
"undefined token group"
|
||
|
term-syn)))))
|
||
23 years ago
|
|
||
21 years ago
|
(define (get-term-list term-group-names)
|
||
|
(remove-duplicates
|
||
|
(cons (datum->syntax-object #f 'error)
|
||
|
(apply append
|
||
|
(map get-terms-from-def term-group-names)))))
|
||
23 years ago
|
|
||
21 years ago
|
(define (parse-input term-defs start ends prec-decls prods src-pos)
|
||
|
(let* ((start-syms (map syntax-e start))
|
||
23 years ago
|
|
||
21 years ago
|
(list-of-terms (map syntax-e (get-term-list term-defs)))
|
||
23 years ago
|
|
||
|
(end-terms
|
||
|
(map
|
||
|
(lambda (end)
|
||
21 years ago
|
(unless (memq (syntax-e end) list-of-terms)
|
||
|
(raise-syntax-error
|
||
|
'parser-end-tokens
|
||
|
(format "End token ~a not defined as a token"
|
||
|
(syntax-e end))
|
||
|
end))
|
||
|
(syntax-e end))
|
||
23 years ago
|
ends))
|
||
22 years ago
|
|
||
23 years ago
|
;; Get the list of terminals out of input-terms
|
||
23 years ago
|
|
||
|
(list-of-non-terms
|
||
21 years ago
|
(syntax-case prods ()
|
||
|
(((non-term production ...) ...)
|
||
23 years ago
|
(begin
|
||
|
(for-each
|
||
|
(lambda (nts)
|
||
|
(if (memq (syntax-object->datum nts) list-of-terms)
|
||
|
(raise-syntax-error
|
||
|
'parser-non-terminals
|
||
|
(format "~a used as both token and non-terminal"
|
||
|
(syntax-object->datum nts))
|
||
|
nts)))
|
||
|
(syntax->list (syntax (non-term ...))))
|
||
|
|
||
|
(let ((dup (duplicate-list? (syntax-object->datum
|
||
|
(syntax (non-term ...))))))
|
||
|
(if dup
|
||
|
(raise-syntax-error
|
||
|
'parser-non-terminals
|
||
|
(format "non-terminal ~a defined multiple times"
|
||
|
dup)
|
||
|
prods)))
|
||
|
|
||
|
(syntax-object->datum (syntax (non-term ...)))))
|
||
|
(_
|
||
|
(raise-syntax-error
|
||
23 years ago
|
'parser-grammar
|
||
|
"Grammar must be of the form (grammar (non-terminal productions ...) ...)"
|
||
23 years ago
|
prods))))
|
||
|
|
||
|
;; Check the precedence declarations for errors and turn them into data
|
||
|
(precs
|
||
21 years ago
|
(syntax-case prec-decls ()
|
||
|
(((type term ...) ...)
|
||
23 years ago
|
(let ((p-terms
|
||
21 years ago
|
(syntax-object->datum (syntax (term ... ...)))))
|
||
23 years ago
|
(cond
|
||
|
((duplicate-list? p-terms) =>
|
||
|
(lambda (d)
|
||
|
(raise-syntax-error
|
||
|
'parser-precedences
|
||
|
(format "duplicate precedence declaration for token ~a"
|
||
|
d)
|
||
|
prec-decls)))
|
||
|
(else
|
||
|
(for-each
|
||
|
(lambda (a)
|
||
|
(for-each
|
||
|
(lambda (t)
|
||
|
(if (not (memq (syntax-object->datum t)
|
||
|
list-of-terms))
|
||
|
(raise-syntax-error
|
||
|
'parser-precedences
|
||
|
(format
|
||
|
"Precedence declared for non-token ~a"
|
||
|
(syntax-object->datum t))
|
||
|
t)))
|
||
|
(syntax->list a)))
|
||
|
(syntax->list (syntax ((term ...) ...))))
|
||
|
(for-each
|
||
|
(lambda (type)
|
||
|
(if (not (memq (syntax-object->datum type)
|
||
|
`(left right nonassoc)))
|
||
|
(raise-syntax-error
|
||
|
'parser-precedences
|
||
|
"Associativity must be left, right or nonassoc"
|
||
|
type)))
|
||
|
(syntax->list (syntax (type ...))))
|
||
21 years ago
|
(syntax-object->datum prec-decls)))))
|
||
23 years ago
|
(#f null)
|
||
23 years ago
|
(_
|
||
|
(raise-syntax-error
|
||
|
'parser-precedences
|
||
23 years ago
|
"Precedence declaration must be of the form (precs (assoc term ...) ...) where assoc is left, right or nonassoc"
|
||
23 years ago
|
prec-decls))))
|
||
|
|
||
|
(terms (build-terms list-of-terms precs))
|
||
|
|
||
22 years ago
|
(non-terms (map (lambda (non-term) (make-non-term non-term #f))
|
||
|
list-of-non-terms))
|
||
23 years ago
|
(term-table (make-hash-table))
|
||
|
(non-term-table (make-hash-table)))
|
||
|
|
||
|
(for-each (lambda (t)
|
||
|
(hash-table-put! term-table (gram-sym-symbol t) t))
|
||
|
terms)
|
||
|
|
||
|
(for-each (lambda (nt)
|
||
|
(hash-table-put! non-term-table (gram-sym-symbol nt) nt))
|
||
|
non-terms)
|
||
|
|
||
|
(let* (
|
||
|
;; parse-prod: syntax-object -> gram-sym vector
|
||
|
(parse-prod
|
||
|
(lambda (prod-so)
|
||
|
(syntax-case prod-so ()
|
||
|
((prod-rhs-sym ...)
|
||
|
(andmap identifier? (syntax->list prod-so))
|
||
23 years ago
|
(begin
|
||
|
(for-each (lambda (t)
|
||
|
(if (memq (syntax-object->datum t) end-terms)
|
||
|
(raise-syntax-error
|
||
|
'parser-production-rhs
|
||
|
(format "~a is an end token and cannot be used in a production"
|
||
|
(syntax-object->datum t))
|
||
|
t)))
|
||
|
(syntax->list prod-so))
|
||
|
(list->vector
|
||
|
(map (lambda (s)
|
||
|
(hash-table-get
|
||
|
term-table
|
||
|
(syntax-object->datum s)
|
||
|
(lambda ()
|
||
|
(hash-table-get
|
||
|
non-term-table
|
||
|
(syntax-object->datum s)
|
||
|
(lambda ()
|
||
|
(raise-syntax-error
|
||
|
'parser-production-rhs
|
||
|
(format
|
||
|
"~a is not declared as a terminal or non-terminal"
|
||
|
(syntax-object->datum s))
|
||
|
s))))))
|
||
|
(syntax->list prod-so)))))
|
||
23 years ago
|
(_
|
||
|
(raise-syntax-error
|
||
|
'parser-production-rhs
|
||
|
"production right-hand-side must have form (symbol ...)"
|
||
|
prod-so)))))
|
||
|
|
||
23 years ago
|
;; parse-action: syntax-object * syntax-object -> syntax-object
|
||
23 years ago
|
(parse-action
|
||
23 years ago
|
(lambda (rhs act)
|
||
16 years ago
|
(let-values ([(args biggest) (get-args 1 (syntax->list rhs) src-pos term-defs)])
|
||
|
(let ([act
|
||
|
(if biggest
|
||
|
(with-syntax ([$n-start-pos (datum->syntax-object (car biggest) '$n-start-pos)]
|
||
|
[$n-end-pos (datum->syntax-object (cdr biggest) '$n-end-pos)])
|
||
|
#`(let ([$n-start-pos #,(car biggest)]
|
||
|
[$n-end-pos #,(cdr biggest)])
|
||
|
#,act))
|
||
|
act)])
|
||
|
(quasisyntax/loc act
|
||
|
(lambda #,args
|
||
|
#,act))))))
|
||
23 years ago
|
|
||
23 years ago
|
;; parse-prod+action: non-term * syntax-object -> production
|
||
|
(parse-prod+action
|
||
23 years ago
|
(lambda (nt prod-so)
|
||
21 years ago
|
(syntax-case prod-so ()
|
||
23 years ago
|
((prod-rhs action)
|
||
|
(let ((p (parse-prod (syntax prod-rhs))))
|
||
23 years ago
|
(make-prod
|
||
|
nt
|
||
|
p
|
||
22 years ago
|
#f
|
||
23 years ago
|
(let loop ((i (sub1 (vector-length p))))
|
||
|
(if (>= i 0)
|
||
|
(let ((gs (vector-ref p i)))
|
||
|
(if (term? gs)
|
||
|
(term-prec gs)
|
||
|
(loop (sub1 i))))
|
||
|
#f))
|
||
23 years ago
|
(parse-action (syntax prod-rhs) (syntax action)))))
|
||
23 years ago
|
((prod-rhs (prec term) action)
|
||
|
(identifier? (syntax term))
|
||
23 years ago
|
(let ((p (parse-prod (syntax prod-rhs))))
|
||
23 years ago
|
(make-prod
|
||
|
nt
|
||
|
p
|
||
22 years ago
|
#f
|
||
23 years ago
|
(term-prec
|
||
|
(hash-table-get
|
||
|
term-table
|
||
|
(syntax-object->datum (syntax term))
|
||
|
(lambda ()
|
||
|
(raise-syntax-error
|
||
|
'parser-production-rhs
|
||
|
(format
|
||
|
"unrecognized terminal ~a in precedence declaration"
|
||
|
(syntax-object->datum (syntax term)))
|
||
|
(syntax term)))))
|
||
23 years ago
|
(parse-action (syntax prod-rhs) (syntax action)))))
|
||
23 years ago
|
(_
|
||
|
(raise-syntax-error
|
||
|
'parser-production-rhs
|
||
|
"production must have form [(symbol ...) expression] or [(symbol ...) (prec symbol) expression]"
|
||
|
prod-so)))))
|
||
|
|
||
|
;; parse-prod-for-nt: syntax-object -> production list
|
||
|
(parse-prods-for-nt
|
||
|
(lambda (prods-so)
|
||
|
(syntax-case prods-so ()
|
||
|
((nt productions ...)
|
||
|
(> (length (syntax->list (syntax (productions ...)))) 0)
|
||
22 years ago
|
(let ((nt (hash-table-get non-term-table
|
||
|
(syntax-object->datum (syntax nt)))))
|
||
23 years ago
|
(map (lambda (p) (parse-prod+action nt p))
|
||
23 years ago
|
(syntax->list (syntax (productions ...))))))
|
||
|
(_
|
||
23 years ago
|
(raise-syntax-error
|
||
|
'parser-productions
|
||
|
"A production for a non-terminal must be (non-term right-hand-side ...) with at least 1 right hand side"
|
||
|
prods-so))))))
|
||
23 years ago
|
|
||
22 years ago
|
(for-each
|
||
|
(lambda (sstx ssym)
|
||
|
(unless (memq ssym list-of-non-terms)
|
||
|
(raise-syntax-error
|
||
|
'parser-start
|
||
|
(format "Start symbol ~a not defined as a non-terminal" ssym)
|
||
|
sstx)))
|
||
|
start start-syms)
|
||
23 years ago
|
|
||
22 years ago
|
(let* ((starts (map (lambda (x) (make-non-term (gensym) #f)) start-syms))
|
||
|
(end-non-terms (map (lambda (x) (make-non-term (gensym) #f)) start-syms))
|
||
21 years ago
|
(parsed-prods (map parse-prods-for-nt (syntax->list prods)))
|
||
22 years ago
|
(start-prods
|
||
|
(map (lambda (start end-non-term)
|
||
|
(list (make-prod start (vector end-non-term) #f #f
|
||
21 years ago
|
(syntax (lambda (x) x)))))
|
||
22 years ago
|
starts end-non-terms))
|
||
23 years ago
|
(prods
|
||
22 years ago
|
`(,@start-prods
|
||
22 years ago
|
,@(map
|
||
|
(lambda (end-nt start-sym)
|
||
|
(map
|
||
|
(lambda (end)
|
||
|
(make-prod end-nt
|
||
|
(vector
|
||
|
(hash-table-get non-term-table start-sym)
|
||
|
(hash-table-get term-table end))
|
||
|
#f
|
||
|
#f
|
||
21 years ago
|
(syntax (lambda (x) x))))
|
||
22 years ago
|
end-terms))
|
||
|
end-non-terms start-syms)
|
||
|
,@parsed-prods)))
|
||
21 years ago
|
|
||
23 years ago
|
(make-object grammar%
|
||
|
prods
|
||
22 years ago
|
(map car start-prods)
|
||
23 years ago
|
terms
|
||
22 years ago
|
(append starts (append end-non-terms non-terms))
|
||
23 years ago
|
(map (lambda (term-name)
|
||
|
(hash-table-get term-table term-name))
|
||
|
end-terms)))))))
|