*** empty log message ***

original commit: a17cdebeb65285162c71e3314daf8193ffa69de8
tokens
Scott Owens 23 years ago
parent a237798ba8
commit 8111e35427

@ -8,111 +8,133 @@
(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
`(letrec ((match (lambda (wrap)
(lambda (lb first-pos end-pos longest-match-length longest-match-action length) `(letrec ((match
(let ((match (lambda (lb first-pos longest-match-length longest-match-action length)
(push-back lb (- length longest-match-length)))) (let ((match
(if (not longest-match-action) (push-back lb (- length longest-match-length)))
(raise-read-error (end-pos (get-position lb)))
(format "lexer: No match found in input starting with: ~a" (if (not longest-match-action)
(list->string (lex-buffer-from lb))) (raise-read-error
#f (format "lexer: No match found in input starting with: ~a"
(position-line first-pos) (list->string (filter char? (lex-buffer-from lb))))
(position-col first-pos) #f
(position-offset first-pos) (position-line first-pos)
(- (position-offset end-pos) (position-offset first-pos)))) (position-col first-pos)
(longest-match-action (position-offset first-pos)
(lambda () (- (position-offset end-pos) (position-offset first-pos))))
first-pos) (,wrap
(lambda () (longest-match-action
end-pos) (lambda ()
(lambda () first-pos)
(if (char? (car match)) (lambda ()
(list->string (reverse match)) end-pos)
(list->string (reverse (cdr match))))) (lambda ()
lb))))) (if (char? (car match))
(lambda (lb) (list->string (reverse match))
(unless (lex-buffer? lb) (list->string (reverse (cdr match)))))
(raise-type-error lb))))))
'lexer (lambda (lb)
"lex-buf" (unless (lex-buffer? lb)
0 (raise-type-error
lb)) 'lexer
(let ((first-pos (get-position lb))) "lex-buf"
(let lexer-loop ( 0
;; current-state lb))
(state start-state) (let ((first-pos (get-position lb)))
;; the character to transition on (let lexer-loop (
(char (next-char lb)) ;; current-state
;; action for the longest match seen thus far (state start-state)
;; including a match at the current state ;; the character to transition on
(longest-match-action (char (next-char lb))
(vector-ref actions start-state)) ;; action for the longest match seen thus far
;; how many characters have been read ;; including a match at the current state
;; including the one just read (longest-match-action
(length 1) (vector-ref actions start-state))
;; how many characters are in the longest match ;; how many characters have been read
(longest-match-length 0) ;; including the one just read
(end-pos first-pos)) (length 1)
(let ((next-state ;; how many characters are in the longest match
(cond (longest-match-length 0))
((eof-object? char) (let ((next-state
(vector-ref eof-table state)) (cond
(else ((eof-object? char)
(vector-ref (vector-ref eof-table state))
trans-table (else
(bitwise-ior (char->integer char) (vector-ref
(arithmetic-shift state 8)))))) trans-table
(pos (get-position lb))) (bitwise-ior (char->integer char)
(cond (arithmetic-shift state 8)))))))
((not next-state) (match lb (cond
first-pos ((not next-state) (match lb
end-pos first-pos
longest-match-length longest-match-length
longest-match-action longest-match-action
length)) length))
(else (else
(let ((act (vector-ref actions next-state))) (let ((act (vector-ref actions next-state)))
(lexer-loop next-state (lexer-loop next-state
(next-char lb) (next-char lb)
(if act (if act
act act
longest-match-action) longest-match-action)
(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 ()
((_) ((_)
(raise-syntax-error #f "empty lexer is not allowed" stx)) (raise-syntax-error #f "empty lexer is not allowed" stx))
((_ re-act ...) ((_ re-act ...)
(begin (begin
(for-each (for-each
(lambda (x) (lambda (x)
(syntax-case x () (syntax-case x ()
((re act) (void)) ((re act) (void))
(_ (raise-syntax-error 'lexer (_ (raise-syntax-error 'lexer
"expects regular expression / action pairs" "expects regular expression / action pairs"
x)))) x))))
(syntax->list (syntax (re-act ...)))) (syntax->list (syntax (re-act ...))))
(let* ((table (generate-table (syntax (re-act ...)) stx)) (let* ((table (generate-table (syntax (re-act ...)) 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 `(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)

Loading…
Cancel
Save