|
|
@ -11,11 +11,10 @@
|
|
|
|
(lib "cffi.ss" "compiler"))
|
|
|
|
(lib "cffi.ss" "compiler"))
|
|
|
|
|
|
|
|
|
|
|
|
(provide lexer lexer-src-pos define-lex-abbrev define-lex-abbrevs
|
|
|
|
(provide lexer lexer-src-pos define-lex-abbrev define-lex-abbrevs
|
|
|
|
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-syntaxes (lexer lexer-src-pos)
|
|
|
|
(define-syntaxes (lexer-experiment lexer-src-pos-experiment)
|
|
|
|
(let ((build-lexer
|
|
|
|
(let ((build-lexer
|
|
|
|
(lambda (wrap?)
|
|
|
|
(lambda (wrap?)
|
|
|
|
(lambda (stx)
|
|
|
|
(lambda (stx)
|
|
|
@ -42,7 +41,7 @@
|
|
|
|
(build-lexer #f)
|
|
|
|
(build-lexer #f)
|
|
|
|
(build-lexer #t))))
|
|
|
|
(build-lexer #t))))
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntaxes (lexer-old lexer-src-pos-old)
|
|
|
|
(define-syntaxes (lexer lexer-src-pos)
|
|
|
|
(let ((build-lexer
|
|
|
|
(let ((build-lexer
|
|
|
|
(lambda (wrap?)
|
|
|
|
(lambda (wrap?)
|
|
|
|
(lambda (stx)
|
|
|
|
(lambda (stx)
|
|
|
@ -114,32 +113,32 @@
|
|
|
|
stx))))
|
|
|
|
stx))))
|
|
|
|
|
|
|
|
|
|
|
|
(define (compiled-lexer-body lexer actions wrap?)
|
|
|
|
(define (compiled-lexer-body lexer actions wrap?)
|
|
|
|
(lambda (lb)
|
|
|
|
(lambda (ip)
|
|
|
|
(unless (lex-buffer? lb)
|
|
|
|
(unless (input-port? ip)
|
|
|
|
(raise-type-error
|
|
|
|
(raise-type-error
|
|
|
|
'lexer
|
|
|
|
'lexer
|
|
|
|
"lex-buf"
|
|
|
|
"input-port"
|
|
|
|
0
|
|
|
|
0
|
|
|
|
lb))
|
|
|
|
ip))
|
|
|
|
(let ((first-pos (get-position lb)))
|
|
|
|
(let ((first-pos (get-position ip)))
|
|
|
|
(let-values (((longest-match-length longest-match-action)
|
|
|
|
(let-values (((longest-match-length longest-match-action)
|
|
|
|
(lexer lb next-char)))
|
|
|
|
(lexer ip peek-string)))
|
|
|
|
(do-match lb first-pos longest-match-length (vector-ref actions longest-match-action) wrap?)))))
|
|
|
|
(do-match ip first-pos longest-match-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 wrap?)
|
|
|
|
(lambda (lb)
|
|
|
|
(lambda (ip)
|
|
|
|
(unless (lex-buffer? lb)
|
|
|
|
(unless (input-port? ip)
|
|
|
|
(raise-type-error
|
|
|
|
(raise-type-error
|
|
|
|
'lexer
|
|
|
|
'lexer
|
|
|
|
"lex-buf"
|
|
|
|
"input-port"
|
|
|
|
0
|
|
|
|
0
|
|
|
|
lb))
|
|
|
|
ip))
|
|
|
|
(let ((first-pos (get-position lb)))
|
|
|
|
(let ((first-pos (get-position ip)))
|
|
|
|
(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 (next-char lb))
|
|
|
|
(char (peek-string 1 0 ip))
|
|
|
|
;; 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
|
|
|
@ -159,10 +158,10 @@
|
|
|
|
(+ (char->integer (string-ref char 0)) (* 256 state)))))))
|
|
|
|
(+ (char->integer (string-ref char 0)) (* 256 state)))))))
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((not next-state)
|
|
|
|
((not next-state)
|
|
|
|
(do-match lb first-pos longest-match-length longest-match-action wrap?))
|
|
|
|
(do-match ip first-pos longest-match-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 lb
|
|
|
|
(do-match ip
|
|
|
|
first-pos
|
|
|
|
first-pos
|
|
|
|
(if act length longest-match-length)
|
|
|
|
(if act length longest-match-length)
|
|
|
|
(if act act longest-match-action)
|
|
|
|
(if act act longest-match-action)
|
|
|
@ -170,7 +169,7 @@
|
|
|
|
(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)
|
|
|
|
(peek-string 1 length ip)
|
|
|
|
(if act
|
|
|
|
(if act
|
|
|
|
act
|
|
|
|
act
|
|
|
|
longest-match-action)
|
|
|
|
longest-match-action)
|
|
|
@ -180,7 +179,7 @@
|
|
|
|
longest-match-length))))))))))
|
|
|
|
longest-match-length))))))))))
|
|
|
|
|
|
|
|
|
|
|
|
(define (do-match lb first-pos longest-match-length longest-match-action wrap?)
|
|
|
|
(define (do-match lb first-pos longest-match-length longest-match-action wrap?)
|
|
|
|
(let* ((match (get-match lb longest-match-length))
|
|
|
|
(let* ((match (read-string longest-match-length lb))
|
|
|
|
(end-pos (get-position lb)))
|
|
|
|
(end-pos (get-position lb)))
|
|
|
|
(if (not longest-match-action)
|
|
|
|
(if (not longest-match-action)
|
|
|
|
(raise-read-error
|
|
|
|
(raise-read-error
|
|
|
@ -210,57 +209,9 @@
|
|
|
|
lb)))))
|
|
|
|
lb)))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Lex buffer is NOT thread safe
|
|
|
|
|
|
|
|
;; c = char | eof
|
|
|
|
|
|
|
|
;; lex-buf =
|
|
|
|
|
|
|
|
;; (make-lex-buffer input-port int int int int)
|
|
|
|
|
|
|
|
(define-struct lex-buffer (ip peek-amt line-start col-start offset-start))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; make-lex-buf: input-port -> lex-buf
|
|
|
|
|
|
|
|
(define make-lex-buf
|
|
|
|
|
|
|
|
(case-lambda
|
|
|
|
|
|
|
|
((ip)
|
|
|
|
|
|
|
|
(cond
|
|
|
|
|
|
|
|
((not (input-port? ip))
|
|
|
|
|
|
|
|
(raise-type-error 'make-lex-buf "input-port" 0 ip))
|
|
|
|
|
|
|
|
(else
|
|
|
|
|
|
|
|
(port-count-lines! ip)
|
|
|
|
|
|
|
|
(make-lex-buffer ip 0 0 0 0))))
|
|
|
|
|
|
|
|
((ip offsets)
|
|
|
|
|
|
|
|
(cond
|
|
|
|
|
|
|
|
((not (input-port? ip))
|
|
|
|
|
|
|
|
(raise-type-error 'make-lex-buf "input-port" 0 ip offsets))
|
|
|
|
|
|
|
|
((or (not (= 3 (length offsets)))
|
|
|
|
|
|
|
|
(not (andmap integer? offsets))
|
|
|
|
|
|
|
|
(not (andmap exact? offsets))
|
|
|
|
|
|
|
|
(not (andmap (lambda (x) (>= x 0)) offsets)))
|
|
|
|
|
|
|
|
(raise-type-error 'make-lex-buf "list of 3 non-negative exact integers" 1 ip offsets))
|
|
|
|
|
|
|
|
(else
|
|
|
|
|
|
|
|
(port-count-lines! ip)
|
|
|
|
|
|
|
|
(make-lex-buffer ip 0 (car offsets) (cadr offsets) (caddr offsets)))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; next-char: lex-buffer -> (string or eof)
|
|
|
|
|
|
|
|
(define (next-char lb)
|
|
|
|
|
|
|
|
(let* ((peek-amt (lex-buffer-peek-amt lb))
|
|
|
|
|
|
|
|
(str (peek-string 1 peek-amt (lex-buffer-ip lb))))
|
|
|
|
|
|
|
|
(set-lex-buffer-peek-amt! lb (add1 peek-amt))
|
|
|
|
|
|
|
|
str))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; get-match: lex-buf * int -> string
|
|
|
|
|
|
|
|
;; reads the next i characters.
|
|
|
|
|
|
|
|
(define (get-match lb i)
|
|
|
|
|
|
|
|
(set-lex-buffer-peek-amt! lb 0)
|
|
|
|
|
|
|
|
(read-string i (lex-buffer-ip lb)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-struct position (offset line col))
|
|
|
|
(define-struct position (offset line col))
|
|
|
|
(define (get-position lb)
|
|
|
|
(define (get-position ip)
|
|
|
|
(let-values (((line col off) (port-next-location (lex-buffer-ip lb))))
|
|
|
|
(let-values (((line col off) (port-next-location ip)))
|
|
|
|
(if (and line col)
|
|
|
|
(make-position off line col)))
|
|
|
|
(make-position (+ (lex-buffer-offset-start lb) off)
|
|
|
|
|
|
|
|
(+ (lex-buffer-line-start lb) line)
|
|
|
|
|
|
|
|
(if (= line 1)
|
|
|
|
|
|
|
|
(+ (lex-buffer-col-start lb) col)
|
|
|
|
|
|
|
|
col))
|
|
|
|
|
|
|
|
(make-position (+ (lex-buffer-offset-start lb) off) #f #f))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|