|
|
@ -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,12 +60,39 @@
|
|
|
|
"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 ...))))
|
|
|
|
|
|
|
|
(spec-act (let loop ((lst re-act-lst))
|
|
|
|
|
|
|
|
(cond
|
|
|
|
|
|
|
|
((null? lst)
|
|
|
|
|
|
|
|
#'(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)))
|
|
|
|
(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)
|
|
|
|
(wrap? wrap?))
|
|
|
|
(wrap? wrap?))
|
|
|
|
(syntax
|
|
|
|
(syntax
|
|
|
|
(lexer-body start-state-stx
|
|
|
|
(lexer-body start-state-stx
|
|
|
@ -73,7 +100,8 @@
|
|
|
|
eof-table-stx
|
|
|
|
eof-table-stx
|
|
|
|
actions-stx
|
|
|
|
actions-stx
|
|
|
|
no-lookahead-stx
|
|
|
|
no-lookahead-stx
|
|
|
|
wrap?)))))))))))
|
|
|
|
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,12 +163,34 @@
|
|
|
|
"input-port"
|
|
|
|
"input-port"
|
|
|
|
0
|
|
|
|
0
|
|
|
|
ip))
|
|
|
|
ip))
|
|
|
|
(let ((first-pos (get-position ip)))
|
|
|
|
(let ((first-pos (get-position ip))
|
|
|
|
|
|
|
|
(first-char (peek-char-or-special ip 0)))
|
|
|
|
|
|
|
|
(cond
|
|
|
|
|
|
|
|
((eq? 'special first-char)
|
|
|
|
|
|
|
|
(let* ((val (read-char-or-special ip))
|
|
|
|
|
|
|
|
(end-pos (get-position ip)))
|
|
|
|
|
|
|
|
(cond
|
|
|
|
|
|
|
|
(wrap?
|
|
|
|
|
|
|
|
(let/ec ret
|
|
|
|
|
|
|
|
(list (special-action first-pos
|
|
|
|
|
|
|
|
end-pos
|
|
|
|
|
|
|
|
val
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
ip)
|
|
|
|
|
|
|
|
first-pos
|
|
|
|
|
|
|
|
end-pos)))
|
|
|
|
|
|
|
|
(else
|
|
|
|
|
|
|
|
(special-action first-pos
|
|
|
|
|
|
|
|
end-pos
|
|
|
|
|
|
|
|
val
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
ip)))))
|
|
|
|
|
|
|
|
(else
|
|
|
|
(let lexer-loop (
|
|
|
|
(let lexer-loop (
|
|
|
|
;; current-state
|
|
|
|
;; current-state
|
|
|
|
(state start-state)
|
|
|
|
(state start-state)
|
|
|
|
;; the character to transition on
|
|
|
|
;; the character to transition on
|
|
|
|
(char (peek-string 1 0 ip))
|
|
|
|
(char first-char)
|
|
|
|
;; action for the longest match seen thus far
|
|
|
|
;; action for the longest match seen thus far
|
|
|
|
;; including a match at the current state
|
|
|
|
;; including a match at the current state
|
|
|
|
(longest-match-action
|
|
|
|
(longest-match-action
|
|
|
@ -154,10 +204,12 @@
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((eof-object? char)
|
|
|
|
((eof-object? char)
|
|
|
|
(vector-ref eof-table state))
|
|
|
|
(vector-ref eof-table state))
|
|
|
|
|
|
|
|
((eq? char 'special)
|
|
|
|
|
|
|
|
#f)
|
|
|
|
(else
|
|
|
|
(else
|
|
|
|
(vector-ref
|
|
|
|
(vector-ref
|
|
|
|
trans-table
|
|
|
|
trans-table
|
|
|
|
(+ (char->integer (string-ref char 0)) (* 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?))
|
|
|
|
(do-match ip first-pos longest-match-length length longest-match-action wrap?))
|
|
|
@ -172,14 +224,14 @@
|
|
|
|
(else
|
|
|
|
(else
|
|
|
|
(let ((act (vector-ref actions next-state)))
|
|
|
|
(let ((act (vector-ref actions next-state)))
|
|
|
|
(lexer-loop next-state
|
|
|
|
(lexer-loop next-state
|
|
|
|
(peek-string 1 length ip)
|
|
|
|
(peek-char-or-special ip length)
|
|
|
|
(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))))))))))))
|
|
|
|
|
|
|
|
|
|
|
|
(define id (lambda (x) x))
|
|
|
|
(define id (lambda (x) x))
|
|
|
|
|
|
|
|
|
|
|
|