|
|
@ -20,7 +20,8 @@
|
|
|
|
(let ((match
|
|
|
|
(let ((match
|
|
|
|
(push-back lb (- length longest-match-length))))
|
|
|
|
(push-back lb (- length longest-match-length))))
|
|
|
|
(if (not longest-match-action)
|
|
|
|
(if (not longest-match-action)
|
|
|
|
(error 'lex "No match found in rest of input"))
|
|
|
|
(error 'lexer (format "No match found in input starting with: ~a"
|
|
|
|
|
|
|
|
(list->string (lex-buffer-from lb)))))
|
|
|
|
(longest-match-action
|
|
|
|
(longest-match-action
|
|
|
|
(lambda ()
|
|
|
|
(lambda ()
|
|
|
|
first-pos)
|
|
|
|
first-pos)
|
|
|
@ -33,9 +34,11 @@
|
|
|
|
lb)))))
|
|
|
|
lb)))))
|
|
|
|
(lambda (lb)
|
|
|
|
(lambda (lb)
|
|
|
|
(unless (lex-buffer? lb)
|
|
|
|
(unless (lex-buffer? lb)
|
|
|
|
(error 'lex
|
|
|
|
(raise-type-error
|
|
|
|
(format
|
|
|
|
'lexer
|
|
|
|
"Lexer expects argument of type lex-buf; given ~a" lb)))
|
|
|
|
"lex-buf"
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
lb))
|
|
|
|
(let ((first-pos (get-position lb)))
|
|
|
|
(let ((first-pos (get-position lb)))
|
|
|
|
(let lexer-loop (
|
|
|
|
(let lexer-loop (
|
|
|
|
;; current-state
|
|
|
|
;; current-state
|
|
|
@ -51,7 +54,6 @@
|
|
|
|
(length 1)
|
|
|
|
(length 1)
|
|
|
|
;; how many characters are in the longest match
|
|
|
|
;; how many characters are in the longest match
|
|
|
|
(longest-match-length 0)
|
|
|
|
(longest-match-length 0)
|
|
|
|
;;(end-pos first-pos))
|
|
|
|
|
|
|
|
(end-pos first-pos))
|
|
|
|
(end-pos first-pos))
|
|
|
|
(let ((next-state
|
|
|
|
(let ((next-state
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
@ -84,45 +86,44 @@
|
|
|
|
pos)))))))))))
|
|
|
|
pos)))))))))))
|
|
|
|
(lambda (stx)
|
|
|
|
(lambda (stx)
|
|
|
|
(syntax-case stx ()
|
|
|
|
(syntax-case stx ()
|
|
|
|
((_ (re act) ...)
|
|
|
|
((_ (re1 act1) (re act) ...)
|
|
|
|
(let* ((table (generate-table (syntax ((re act) ...))
|
|
|
|
(let* ((table (generate-table (syntax ((re1 act1) (re act) ...)) stx))
|
|
|
|
stx))
|
|
|
|
|
|
|
|
(code
|
|
|
|
(code
|
|
|
|
`(let ((start-state ,(table-start table))
|
|
|
|
`(let ((start-state ,(table-start table))
|
|
|
|
(trans-table ,(table-trans table))
|
|
|
|
(trans-table ,(table-trans table))
|
|
|
|
(eof-table ,(table-eof table))
|
|
|
|
(eof-table ,(table-eof table))
|
|
|
|
(actions (vector ,@(vector->list (table-actions table)))))
|
|
|
|
(actions (vector ,@(vector->list (table-actions table)))))
|
|
|
|
,code)))
|
|
|
|
,code)))
|
|
|
|
(datum->syntax-object #'here code #f)))))))
|
|
|
|
(datum->syntax-object #'here code #f)))
|
|
|
|
|
|
|
|
(_
|
|
|
|
|
|
|
|
(raise-syntax-error #f "Form should be (lexer (re act) ...) with at least 1 (re ast) pair" stx))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax define-lex-abbrev
|
|
|
|
(define-syntax (define-lex-abbrev stx)
|
|
|
|
(lambda (stx)
|
|
|
|
|
|
|
|
(syntax-case stx ()
|
|
|
|
(syntax-case stx ()
|
|
|
|
((_ name val)
|
|
|
|
((_ name re)
|
|
|
|
(syntax
|
|
|
|
(syntax
|
|
|
|
(define-syntax name
|
|
|
|
(define-syntax name
|
|
|
|
(make-lex-abbrev (quote-syntax val)))))
|
|
|
|
(make-lex-abbrev (quote-syntax re)))))
|
|
|
|
(_
|
|
|
|
(_
|
|
|
|
(raise-syntax-error
|
|
|
|
(raise-syntax-error
|
|
|
|
#f
|
|
|
|
#f
|
|
|
|
"Form should be (define-lex-abbrev name val)"
|
|
|
|
"Form should be (define-lex-abbrev name re)"
|
|
|
|
stx)))))
|
|
|
|
stx))))
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax define-lex-abbrevs
|
|
|
|
(define-syntax (define-lex-abbrevs stx)
|
|
|
|
(lambda (stx)
|
|
|
|
|
|
|
|
(syntax-case stx ()
|
|
|
|
(syntax-case stx ()
|
|
|
|
((_ x ...)
|
|
|
|
((_ x ...)
|
|
|
|
(let* ((abbrev (syntax->list (syntax (x ...))))
|
|
|
|
(let* ((abbrev (syntax->list (syntax (x ...))))
|
|
|
|
(r (map (lambda (a)
|
|
|
|
(r (map (lambda (a)
|
|
|
|
(syntax-case a ()
|
|
|
|
(syntax-case a ()
|
|
|
|
((name val)
|
|
|
|
((name re)
|
|
|
|
(identifier? (syntax name))
|
|
|
|
(identifier? (syntax name))
|
|
|
|
(syntax (define-lex-abbrev name val)))
|
|
|
|
(syntax (define-lex-abbrev name re)))
|
|
|
|
(_ (raise-syntax-error
|
|
|
|
(_ (raise-syntax-error
|
|
|
|
#f
|
|
|
|
'Lexer-abbreviation
|
|
|
|
"Lexer abbreviation must be (identifier value)"
|
|
|
|
"Form should be (identifier value)"
|
|
|
|
a))))
|
|
|
|
a))))
|
|
|
|
abbrev)))
|
|
|
|
abbrev)))
|
|
|
|
(datum->syntax-object
|
|
|
|
(datum->syntax-object
|
|
|
@ -132,8 +133,8 @@
|
|
|
|
(_
|
|
|
|
(_
|
|
|
|
(raise-syntax-error
|
|
|
|
(raise-syntax-error
|
|
|
|
#f
|
|
|
|
#f
|
|
|
|
"Form should be (define-lex-abbrevs (name val) ...)"
|
|
|
|
"Form should be (define-lex-abbrevs (name re) ...)"
|
|
|
|
stx)))))
|
|
|
|
stx))))
|
|
|
|
|
|
|
|
|
|
|
|
;; Lex buffer is NOT thread safe
|
|
|
|
;; Lex buffer is NOT thread safe
|
|
|
|
|
|
|
|
|
|
|
|