*** empty log message ***

original commit: 3f7364ab57ca2788d6a87f503d824e3719fd0676
tokens
Scott Owens 21 years ago
parent 1bf8516b3b
commit 18ecb857a2

@ -16,7 +16,7 @@
(define file-path (make-parameter #f)) (define file-path (make-parameter #f))
(define-syntaxes (lexer-exp lexer-src-pos-exp) #;(define-syntaxes (lexer-exp lexer-src-pos-exp)
(let ((build-lexer (let ((build-lexer
(lambda (wrap?) (lambda (wrap?)
(lambda (stx) (lambda (stx)
@ -60,20 +60,48 @@
"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* ((re-act-lst (syntax->list (syntax (re-act ...))))
(with-syntax ((start-state-stx (table-start table)) (spec-act (let loop ((lst re-act-lst))
(trans-table-stx (table-trans table)) (cond
(eof-table-stx (table-eof table)) ((null? lst)
(no-lookahead-stx (table-no-lookahead table)) #'(void))
(actions-stx `(vector ,@(vector->list (table-actions table)))) (else
(wrap? wrap?)) (syntax-case (car lst) ()
(syntax (((special) act)
(lexer-body start-state-stx (eq? (syntax-e (syntax special)) 'special)
trans-table-stx (syntax act))
eof-table-stx (_ (loop (cdr lst))))))))
actions-stx (re-act-lst (let loop ((lst re-act-lst))
no-lookahead-stx (cond
wrap?))))))))))) ((null? lst)
null)
(else
(syntax-case (car lst) ()
(((special) act)
(eq? (syntax-e (syntax special)) 'special)
(loop (cdr lst)))
(_ (cons (car lst) (loop (cdr lst)))))))))
(spec-act (datum->syntax-object
spec-act
`(lambda (start-pos end-pos lexeme return-without-pos input-port)
,spec-act)
spec-act)))
(let ((table (generate-table re-act-lst stx)))
(with-syntax ((start-state-stx (table-start table))
(trans-table-stx (table-trans table))
(eof-table-stx (table-eof table))
(no-lookahead-stx (table-no-lookahead table))
(actions-stx `(vector ,@(vector->list (table-actions table))))
(spec-act-stx spec-act)
(wrap? wrap?))
(syntax
(lexer-body start-state-stx
trans-table-stx
eof-table-stx
actions-stx
no-lookahead-stx
spec-act-stx
wrap?))))))))))))
(values (values
(build-lexer #f) (build-lexer #f)
(build-lexer #t)))) (build-lexer #t))))
@ -114,7 +142,7 @@
"Form should be (define-lex-abbrevs (name re) ...)" "Form should be (define-lex-abbrevs (name re) ...)"
stx)))) stx))))
(define (compiled-lexer-body lexer actions wrap?) #;(define (compiled-lexer-body lexer actions wrap?)
(lambda (ip) (lambda (ip)
(unless (input-port? ip) (unless (input-port? ip)
(raise-type-error (raise-type-error
@ -127,7 +155,7 @@
(lexer ip peek-string))) (lexer ip peek-string)))
(do-match ip first-pos longest-match-length length (vector-ref actions longest-match-action) wrap?))))) (do-match ip first-pos longest-match-length length (vector-ref actions longest-match-action) wrap?)))))
(define (lexer-body start-state trans-table eof-table actions no-lookahead wrap?) (define (lexer-body start-state trans-table eof-table actions no-lookahead special-action wrap?)
(lambda (ip) (lambda (ip)
(unless (input-port? ip) (unless (input-port? ip)
(raise-type-error (raise-type-error
@ -135,51 +163,75 @@
"input-port" "input-port"
0 0
ip)) ip))
(let ((first-pos (get-position ip))) (let ((first-pos (get-position ip))
(let lexer-loop ( (first-char (peek-char-or-special ip 0)))
;; current-state (cond
(state start-state) ((eq? 'special first-char)
;; the character to transition on (let* ((val (read-char-or-special ip))
(char (peek-string 1 0 ip)) (end-pos (get-position ip)))
;; action for the longest match seen thus far (cond
;; including a match at the current state (wrap?
(longest-match-action (let/ec ret
(vector-ref actions start-state)) (list (special-action first-pos
;; how many characters have been read end-pos
;; including the one just read val
(length 1) ret
;; how many characters are in the longest match ip)
(longest-match-length 1)) first-pos
(let ((next-state end-pos)))
(cond (else
((eof-object? char) (special-action first-pos
(vector-ref eof-table state)) end-pos
(else val
(vector-ref id
trans-table ip)))))
(+ (char->integer (string-ref char 0)) (* 256 state))))))) (else
(cond (let lexer-loop (
((not next-state) ;; current-state
(do-match ip first-pos longest-match-length length longest-match-action wrap?)) (state start-state)
((vector-ref no-lookahead next-state) ;; the character to transition on
(let ((act (vector-ref actions next-state))) (char first-char)
(do-match ip ;; action for the longest match seen thus far
first-pos ;; including a match at the current state
(if act length longest-match-length) (longest-match-action
length (vector-ref actions start-state))
(if act act longest-match-action) ;; how many characters have been read
wrap?))) ;; including the one just read
(else (length 1)
(let ((act (vector-ref actions next-state))) ;; how many characters are in the longest match
(lexer-loop next-state (longest-match-length 1))
(peek-string 1 length ip) (let ((next-state
(if act (cond
act ((eof-object? char)
longest-match-action) (vector-ref eof-table state))
(add1 length) ((eq? char 'special)
(if act #f)
length (else
longest-match-length)))))))))) (vector-ref
trans-table
(+ (char->integer char) (* 256 state)))))))
(cond
((not next-state)
(do-match ip first-pos longest-match-length length longest-match-action wrap?))
((vector-ref no-lookahead next-state)
(let ((act (vector-ref actions next-state)))
(do-match ip
first-pos
(if act length longest-match-length)
length
(if act act longest-match-action)
wrap?)))
(else
(let ((act (vector-ref actions next-state)))
(lexer-loop next-state
(peek-char-or-special ip length)
(if act
act
longest-match-action)
(add1 length)
(if act
length
longest-match-length))))))))))))
(define id (lambda (x) x)) (define id (lambda (x) x))

Loading…
Cancel
Save