*** empty log message ***

original commit: c9baa0dc23b3224bedcf90b21c027b830da94c55
tokens
Scott Owens 21 years ago
parent 18ecb857a2
commit 7ce12965f5

@ -60,39 +60,28 @@
"expects regular expression / action pairs" "expects regular expression / action pairs"
x)))) x))))
(syntax->list (syntax (re-act ...)))) (syntax->list (syntax (re-act ...))))
(let* ((re-act-lst (syntax->list (syntax (re-act ...)))) (let* ((spec/re-act-lst (syntax->list (syntax (re-act ...))))
(spec-act (let loop ((lst re-act-lst)) (spec-act (get-special-action spec/re-act-lst 'special #'here))
(cond (spec-comment-act (get-special-action spec/re-act-lst 'special-comment #'here))
((null? lst) (re-act-lst (filter-out-specials spec/re-act-lst '(special special-comment))))
#'(void))
(else
(syntax-case (car lst) ()
(((special) act)
(eq? (syntax-e (syntax special)) 'special)
(syntax act))
(_ (loop (cdr lst))))))))
(re-act-lst (let loop ((lst re-act-lst))
(cond
((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))) (let ((table (generate-table re-act-lst stx)))
(with-syntax ((start-state-stx (table-start table)) (with-syntax ((start-state-stx (table-start table))
(trans-table-stx (table-trans table)) (trans-table-stx (table-trans table))
(eof-table-stx (table-eof table)) (eof-table-stx (table-eof table))
(no-lookahead-stx (table-no-lookahead table)) (no-lookahead-stx (table-no-lookahead table))
(actions-stx `(vector ,@(vector->list (table-actions table)))) (actions-stx `(vector ,@(vector->list (table-actions table))))
(spec-act-stx spec-act) (spec-act-stx
(datum->syntax-object
spec-act
`(lambda (start-pos end-pos special return-without-pos input-port)
,spec-act)
spec-act))
(spec-comment-act-stx
(datum->syntax-object
spec-comment-act
`(lambda (start-pos end-pos ,(gensym) return-without-pos input-port)
,spec-comment-act)
spec-comment-act))
(wrap? wrap?)) (wrap? wrap?))
(syntax (syntax
(lexer-body start-state-stx (lexer-body start-state-stx
@ -101,6 +90,7 @@
actions-stx actions-stx
no-lookahead-stx no-lookahead-stx
spec-act-stx spec-act-stx
spec-comment-act-stx
wrap?)))))))))))) wrap?))))))))))))
(values (values
(build-lexer #f) (build-lexer #f)
@ -153,9 +143,10 @@
(let ((first-pos (get-position ip))) (let ((first-pos (get-position ip)))
(let-values (((longest-match-length length longest-match-action) (let-values (((longest-match-length length longest-match-action)
(lexer ip peek-string))) (lexer ip peek-string)))
(do-match ip first-pos longest-match-length length (vector-ref actions longest-match-action) wrap?))))) (check-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 special-action wrap?) (define (lexer-body start-state trans-table eof-table actions no-lookahead
special-action special-comment-action wrap?)
(lambda (ip) (lambda (ip)
(unless (input-port? ip) (unless (input-port? ip)
(raise-type-error (raise-type-error
@ -167,24 +158,14 @@
(first-char (peek-char-or-special ip 0))) (first-char (peek-char-or-special ip 0)))
(cond (cond
((eq? 'special first-char) ((eq? 'special first-char)
(let* ((val (read-char-or-special ip)) (let* ((comment? #f)
(end-pos (get-position ip))) (spec (with-handlers ((exn:special-comment?
(cond (lambda (x) (set! comment? #t) #f)))
(wrap? (read-char-or-special ip))))
(let/ec ret (do-match ip first-pos (if comment?
(list (special-action first-pos special-comment-action
end-pos special-action)
val spec wrap?)))
ret
ip)
first-pos
end-pos)))
(else
(special-action first-pos
end-pos
val
id
ip)))))
(else (else
(let lexer-loop ( (let lexer-loop (
;; current-state ;; current-state
@ -212,15 +193,15 @@
(+ (char->integer char) (* 256 state))))))) (+ (char->integer char) (* 256 state)))))))
(cond (cond
((not next-state) ((not next-state)
(do-match ip first-pos longest-match-length length longest-match-action wrap?)) (check-match ip first-pos longest-match-length length longest-match-action wrap?))
((vector-ref no-lookahead next-state) ((vector-ref no-lookahead next-state)
(let ((act (vector-ref actions next-state))) (let ((act (vector-ref actions next-state)))
(do-match ip (check-match ip
first-pos first-pos
(if act length longest-match-length) (if act length longest-match-length)
length length
(if act act longest-match-action) (if act act longest-match-action)
wrap?))) wrap?)))
(else (else
(let ((act (vector-ref actions next-state))) (let ((act (vector-ref actions next-state)))
(lexer-loop next-state (lexer-loop next-state
@ -235,7 +216,7 @@
(define id (lambda (x) x)) (define id (lambda (x) x))
(define (do-match lb first-pos longest-match-length length longest-match-action wrap?) (define (check-match lb first-pos longest-match-length length longest-match-action wrap?)
(unless longest-match-action (unless longest-match-action
(let* ((match (read-string length lb)) (let* ((match (read-string length lb))
(end-pos (get-position lb))) (end-pos (get-position lb)))
@ -246,27 +227,20 @@
(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)))))
(let* ((match (read-string longest-match-length lb)) (let ((match (read-string longest-match-length lb)))
(end-pos (get-position lb))) (do-match lb first-pos longest-match-action match wrap?)))
(cond
(wrap?
(let/ec ret
(list (longest-match-action
first-pos
end-pos
match
ret
lb)
first-pos
end-pos)))
(else
(longest-match-action
first-pos
end-pos
match
id
lb)))))
(define (do-match ip first-pos action value wrap?)
(let ((end-pos (get-position ip)))
(cond
(wrap?
(let/ec ret
(list (action first-pos end-pos value ret ip)
first-pos
end-pos)))
(else
(action first-pos end-pos value id ip)))))
(define-struct position (offset line col)) (define-struct position (offset line col))
(define (get-position ip) (define (get-position ip)

Loading…
Cancel
Save