|
|
@ -8,27 +8,30 @@
|
|
|
|
(require (lib "list.ss")
|
|
|
|
(require (lib "list.ss")
|
|
|
|
(lib "readerr.ss" "syntax")
|
|
|
|
(lib "readerr.ss" "syntax")
|
|
|
|
"private-lex/token.ss")
|
|
|
|
"private-lex/token.ss")
|
|
|
|
(provide lexer define-lex-abbrev define-lex-abbrevs
|
|
|
|
(provide lexer lexer-src-loc define-lex-abbrev define-lex-abbrevs
|
|
|
|
make-lex-buf
|
|
|
|
make-lex-buf
|
|
|
|
get-position position-offset position-line position-col position?
|
|
|
|
get-position position-offset position-line position-col position?
|
|
|
|
define-tokens define-empty-tokens)
|
|
|
|
define-tokens define-empty-tokens)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax lexer
|
|
|
|
(define-syntaxes (lexer lexer-src-loc)
|
|
|
|
(let ((code
|
|
|
|
(let ((code
|
|
|
|
|
|
|
|
(lambda (wrap)
|
|
|
|
`(letrec ((match
|
|
|
|
`(letrec ((match
|
|
|
|
(lambda (lb first-pos end-pos longest-match-length longest-match-action length)
|
|
|
|
(lambda (lb first-pos longest-match-length longest-match-action length)
|
|
|
|
(let ((match
|
|
|
|
(let ((match
|
|
|
|
(push-back lb (- length longest-match-length))))
|
|
|
|
(push-back lb (- length longest-match-length)))
|
|
|
|
|
|
|
|
(end-pos (get-position lb)))
|
|
|
|
(if (not longest-match-action)
|
|
|
|
(if (not longest-match-action)
|
|
|
|
(raise-read-error
|
|
|
|
(raise-read-error
|
|
|
|
(format "lexer: No match found in input starting with: ~a"
|
|
|
|
(format "lexer: No match found in input starting with: ~a"
|
|
|
|
(list->string (lex-buffer-from lb)))
|
|
|
|
(list->string (filter char? (lex-buffer-from lb))))
|
|
|
|
#f
|
|
|
|
#f
|
|
|
|
(position-line first-pos)
|
|
|
|
(position-line first-pos)
|
|
|
|
(position-col first-pos)
|
|
|
|
(position-col first-pos)
|
|
|
|
(position-offset first-pos)
|
|
|
|
(position-offset first-pos)
|
|
|
|
(- (position-offset end-pos) (position-offset first-pos))))
|
|
|
|
(- (position-offset end-pos) (position-offset first-pos))))
|
|
|
|
|
|
|
|
(,wrap
|
|
|
|
(longest-match-action
|
|
|
|
(longest-match-action
|
|
|
|
(lambda ()
|
|
|
|
(lambda ()
|
|
|
|
first-pos)
|
|
|
|
first-pos)
|
|
|
@ -38,7 +41,7 @@
|
|
|
|
(if (char? (car match))
|
|
|
|
(if (char? (car match))
|
|
|
|
(list->string (reverse match))
|
|
|
|
(list->string (reverse match))
|
|
|
|
(list->string (reverse (cdr match)))))
|
|
|
|
(list->string (reverse (cdr match)))))
|
|
|
|
lb)))))
|
|
|
|
lb))))))
|
|
|
|
(lambda (lb)
|
|
|
|
(lambda (lb)
|
|
|
|
(unless (lex-buffer? lb)
|
|
|
|
(unless (lex-buffer? lb)
|
|
|
|
(raise-type-error
|
|
|
|
(raise-type-error
|
|
|
@ -60,8 +63,7 @@
|
|
|
|
;; including the one just read
|
|
|
|
;; including the one just read
|
|
|
|
(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))
|
|
|
|
|
|
|
|
(let ((next-state
|
|
|
|
(let ((next-state
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((eof-object? char)
|
|
|
|
((eof-object? char)
|
|
|
@ -70,12 +72,10 @@
|
|
|
|
(vector-ref
|
|
|
|
(vector-ref
|
|
|
|
trans-table
|
|
|
|
trans-table
|
|
|
|
(bitwise-ior (char->integer char)
|
|
|
|
(bitwise-ior (char->integer char)
|
|
|
|
(arithmetic-shift state 8))))))
|
|
|
|
(arithmetic-shift state 8)))))))
|
|
|
|
(pos (get-position lb)))
|
|
|
|
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((not next-state) (match lb
|
|
|
|
((not next-state) (match lb
|
|
|
|
first-pos
|
|
|
|
first-pos
|
|
|
|
end-pos
|
|
|
|
|
|
|
|
longest-match-length
|
|
|
|
longest-match-length
|
|
|
|
longest-match-action
|
|
|
|
longest-match-action
|
|
|
|
length))
|
|
|
|
length))
|
|
|
@ -89,8 +89,8 @@
|
|
|
|
(add1 length)
|
|
|
|
(add1 length)
|
|
|
|
(if act
|
|
|
|
(if act
|
|
|
|
length
|
|
|
|
length
|
|
|
|
longest-match-length)
|
|
|
|
longest-match-length)))))))))))))
|
|
|
|
pos)))))))))))
|
|
|
|
(values
|
|
|
|
(lambda (stx)
|
|
|
|
(lambda (stx)
|
|
|
|
(syntax-case stx ()
|
|
|
|
(syntax-case stx ()
|
|
|
|
((_)
|
|
|
|
((_)
|
|
|
@ -111,8 +111,30 @@
|
|
|
|
(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 `(lambda (x) x)))))
|
|
|
|
(datum->syntax-object #'here code #f))))))))
|
|
|
|
(datum->syntax-object #'here code #f))))))
|
|
|
|
|
|
|
|
(lambda (stx)
|
|
|
|
|
|
|
|
(syntax-case stx ()
|
|
|
|
|
|
|
|
((_)
|
|
|
|
|
|
|
|
(raise-syntax-error #f "empty lexer is not allowed" stx))
|
|
|
|
|
|
|
|
((_ re-act ...)
|
|
|
|
|
|
|
|
(begin
|
|
|
|
|
|
|
|
(for-each
|
|
|
|
|
|
|
|
(lambda (x)
|
|
|
|
|
|
|
|
(syntax-case x ()
|
|
|
|
|
|
|
|
((re act) (void))
|
|
|
|
|
|
|
|
(_ (raise-syntax-error 'lexer
|
|
|
|
|
|
|
|
"expects regular expression / action pairs"
|
|
|
|
|
|
|
|
x))))
|
|
|
|
|
|
|
|
(syntax->list (syntax (re-act ...))))
|
|
|
|
|
|
|
|
(let* ((table (generate-table (syntax (re-act ...)) stx))
|
|
|
|
|
|
|
|
(code
|
|
|
|
|
|
|
|
`(let ((start-state ,(table-start table))
|
|
|
|
|
|
|
|
(trans-table ,(table-trans table))
|
|
|
|
|
|
|
|
(eof-table ,(table-eof table))
|
|
|
|
|
|
|
|
(actions (vector ,@(vector->list (table-actions table)))))
|
|
|
|
|
|
|
|
,(code `(lambda (x) (list x first-pos end-pos))))))
|
|
|
|
|
|
|
|
(datum->syntax-object #'here code #f)))))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax (define-lex-abbrev stx)
|
|
|
|
(define-syntax (define-lex-abbrev stx)
|
|
|
|