allow blank eof rule
parent
73e59188a7
commit
5a316c669d
@ -1,412 +1,369 @@
|
|||||||
(module lex mzscheme
|
#lang racket/base
|
||||||
|
|
||||||
;; Provides the syntax used to create lexers and the functions needed to
|
;; Provides the syntax used to create lexers and the functions needed to
|
||||||
;; create and use the buffer that the lexer reads from. See docs.
|
;; create and use the buffer that the lexer reads from. See docs.
|
||||||
|
|
||||||
(require-for-syntax mzlib/list
|
(require (for-syntax mzlib/list
|
||||||
syntax/stx
|
syntax/stx
|
||||||
syntax/define
|
syntax/define
|
||||||
syntax/boundmap
|
syntax/boundmap
|
||||||
"private-lex/util.rkt"
|
"private-lex/util.rkt"
|
||||||
"private-lex/actions.rkt"
|
"private-lex/actions.rkt"
|
||||||
"private-lex/front.rkt"
|
"private-lex/front.rkt"
|
||||||
"private-lex/unicode-chars.rkt")
|
"private-lex/unicode-chars.rkt"
|
||||||
|
racket/base
|
||||||
|
racket/promise))
|
||||||
|
|
||||||
(require mzlib/stxparam
|
(require mzlib/stxparam
|
||||||
syntax/readerr
|
syntax/readerr
|
||||||
"private-lex/token.rkt")
|
"private-lex/token.rkt")
|
||||||
|
|
||||||
(provide lexer lexer-src-pos lexer-srcloc define-lex-abbrev define-lex-abbrevs define-lex-trans
|
(provide lexer lexer-src-pos lexer-srcloc define-lex-abbrev define-lex-abbrevs define-lex-trans
|
||||||
|
|
||||||
;; Dealing with tokens and related structures
|
;; Dealing with tokens and related structures
|
||||||
define-tokens define-empty-tokens token-name token-value token?
|
define-tokens define-empty-tokens token-name token-value token?
|
||||||
(struct position (offset line col))
|
(struct-out position)
|
||||||
(struct position-token (token start-pos end-pos))
|
(struct-out position-token)
|
||||||
(struct srcloc-token (token srcloc))
|
(struct-out srcloc-token)
|
||||||
|
|
||||||
;; File path for highlighting errors while lexing
|
;; File path for highlighting errors while lexing
|
||||||
file-path
|
file-path
|
||||||
lexer-file-path ;; alternate name
|
lexer-file-path ;; alternate name
|
||||||
|
|
||||||
;; Lex abbrevs for unicode char sets. See mzscheme manual section 3.4.
|
;; Lex abbrevs for unicode char sets. See mzscheme manual section 3.4.
|
||||||
any-char any-string nothing alphabetic lower-case upper-case title-case
|
any-char any-string nothing alphabetic lower-case upper-case title-case
|
||||||
numeric symbolic punctuation graphic whitespace blank iso-control
|
numeric symbolic punctuation graphic whitespace blank iso-control
|
||||||
|
|
||||||
;; A regular expression operator
|
;; A regular expression operator
|
||||||
char-set)
|
char-set)
|
||||||
|
|
||||||
;; wrap-action: syntax-object src-pos? -> syntax-object
|
;; wrap-action: syntax-object src-pos? -> syntax-object
|
||||||
(define-for-syntax (wrap-action action src-loc-style)
|
(define-for-syntax (wrap-action action src-loc-style)
|
||||||
(with-syntax ((action-stx
|
(with-syntax ([action-stx
|
||||||
(cond
|
(cond
|
||||||
[(eq? src-loc-style 'lexer-src-pos)
|
[(eq? src-loc-style 'lexer-src-pos)
|
||||||
#`(let/ec ret
|
#`(let/ec ret
|
||||||
(syntax-parameterize
|
(syntax-parameterize
|
||||||
([return-without-pos (make-rename-transformer #'ret)])
|
([return-without-pos (make-rename-transformer #'ret)])
|
||||||
(make-position-token #,action start-pos end-pos)))]
|
(make-position-token #,action start-pos end-pos)))]
|
||||||
[(eq? src-loc-style 'lexer-srcloc)
|
[(eq? src-loc-style 'lexer-srcloc)
|
||||||
#`(let/ec ret
|
#`(let/ec ret
|
||||||
(syntax-parameterize
|
(syntax-parameterize
|
||||||
([return-without-srcloc (make-rename-transformer #'ret)])
|
([return-without-srcloc (make-rename-transformer #'ret)])
|
||||||
(make-srcloc-token #,action lexeme-srcloc)))]
|
(make-srcloc-token #,action lexeme-srcloc)))]
|
||||||
[else action])))
|
[else action])])
|
||||||
(syntax/loc action
|
(syntax/loc action
|
||||||
(lambda (start-pos-p end-pos-p lexeme-p input-port-p)
|
(λ (start-pos-p end-pos-p lexeme-p input-port-p)
|
||||||
(define lexeme-srcloc-p (make-srcloc (object-name input-port-p)
|
(define lexeme-srcloc-p (make-srcloc (object-name input-port-p)
|
||||||
(position-line start-pos-p)
|
(position-line start-pos-p)
|
||||||
(position-col start-pos-p)
|
(position-col start-pos-p)
|
||||||
(position-offset start-pos-p)
|
(position-offset start-pos-p)
|
||||||
(and (number? (position-offset end-pos-p))
|
(and (number? (position-offset end-pos-p))
|
||||||
(number? (position-offset start-pos-p))
|
(number? (position-offset start-pos-p))
|
||||||
(- (position-offset end-pos-p)
|
(- (position-offset end-pos-p)
|
||||||
(position-offset start-pos-p)))))
|
(position-offset start-pos-p)))))
|
||||||
(syntax-parameterize
|
(syntax-parameterize
|
||||||
([start-pos (make-rename-transformer #'start-pos-p)]
|
([start-pos (make-rename-transformer #'start-pos-p)]
|
||||||
[end-pos (make-rename-transformer #'end-pos-p)]
|
[end-pos (make-rename-transformer #'end-pos-p)]
|
||||||
[lexeme (make-rename-transformer #'lexeme-p)]
|
[lexeme (make-rename-transformer #'lexeme-p)]
|
||||||
[input-port (make-rename-transformer #'input-port-p)]
|
[input-port (make-rename-transformer #'input-port-p)]
|
||||||
[lexeme-srcloc (make-rename-transformer #'lexeme-srcloc-p)])
|
[lexeme-srcloc (make-rename-transformer #'lexeme-srcloc-p)])
|
||||||
action-stx)))))
|
action-stx)))))
|
||||||
|
|
||||||
(define-for-syntax (make-lexer-trans src-loc-style)
|
(define-for-syntax (make-lexer-macro caller src-loc-style)
|
||||||
(lambda (stx)
|
(λ (stx)
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
((_ re-act ...)
|
[(_ . RE+ACTS)
|
||||||
(begin
|
(let ()
|
||||||
(for-each
|
(define spec/re-acts (syntax->list #'RE+ACTS))
|
||||||
(lambda (x)
|
(for/and ([x (in-list spec/re-acts)])
|
||||||
(syntax-case x ()
|
(syntax-case x ()
|
||||||
((re act) (void))
|
[(RE ACT) #t]
|
||||||
(_ (raise-syntax-error #f
|
[else (raise-syntax-error caller "not a regular expression / action pair" stx x)]))
|
||||||
"not a regular expression / action pair"
|
(define eof-act (get-special-action spec/re-acts #'eof #'eof))
|
||||||
stx
|
(define spec-act (get-special-action spec/re-acts #'special #'(void)))
|
||||||
x))))
|
(define spec-comment-act (get-special-action spec/re-acts #'special-comment #'#f))
|
||||||
(syntax->list (syntax (re-act ...))))
|
(define ids (list #'special #'special-comment #'eof))
|
||||||
(let* ((spec/re-act-lst
|
(define re-acts (filter (λ (spec/re-act)
|
||||||
(syntax->list (syntax (re-act ...))))
|
(syntax-case spec/re-act ()
|
||||||
(eof-act
|
[((special) act)
|
||||||
(get-special-action spec/re-act-lst #'eof #''eof))
|
(not (ormap
|
||||||
(spec-act
|
(λ (x)
|
||||||
(get-special-action spec/re-act-lst #'special #'(void)))
|
(and (identifier? #'special)
|
||||||
(spec-comment-act
|
(module-or-top-identifier=? #'special x)))
|
||||||
(get-special-action spec/re-act-lst #'special-comment #'#f))
|
ids))]
|
||||||
(ids (list #'special #'special-comment #'eof))
|
[_ #t])) spec/re-acts))
|
||||||
(re-act-lst
|
(define names (map (λ (x) (datum->syntax #f (gensym))) re-acts))
|
||||||
(filter
|
(define acts (map (λ (x) (stx-car (stx-cdr x))) re-acts))
|
||||||
(lambda (spec/re-act)
|
(define re-actnames (map (λ (re-act name) (list (stx-car re-act) name)) re-acts names))
|
||||||
(syntax-case spec/re-act ()
|
(when (null? spec/re-acts)
|
||||||
(((special) act)
|
(raise-syntax-error caller "expected at least one action" stx))
|
||||||
(not (ormap
|
(define-values (trans start action-names no-look disappeared-uses) (build-lexer re-actnames))
|
||||||
(lambda (x)
|
(when (vector-ref action-names start) ;; Start state is final
|
||||||
(and (identifier? #'special)
|
(unless (and
|
||||||
(module-or-top-identifier=? (syntax special) x)))
|
;; All the successor states are final
|
||||||
ids)))
|
(vector? (vector-ref trans start))
|
||||||
(_ #t)))
|
(andmap (λ (x) (vector-ref action-names (vector-ref x 2)))
|
||||||
spec/re-act-lst))
|
(vector->list (vector-ref trans start)))
|
||||||
(name-lst (map (lambda (x) (datum->syntax-object #f (gensym))) re-act-lst))
|
;; Each character has a successor state
|
||||||
(act-lst (map (lambda (x) (stx-car (stx-cdr x))) re-act-lst))
|
(let loop ([check 0]
|
||||||
(re-actname-lst (map (lambda (re-act name)
|
[nexts (vector->list (vector-ref trans start))])
|
||||||
(list (stx-car re-act)
|
(cond
|
||||||
name))
|
[(null? nexts) #f]
|
||||||
re-act-lst
|
[else
|
||||||
name-lst)))
|
(let ([next (car nexts)])
|
||||||
(when (null? spec/re-act-lst)
|
(and (= (vector-ref next 0) check)
|
||||||
(raise-syntax-error (or src-loc-style 'lexer) "expected at least one action" stx))
|
(let ([next-check (vector-ref next 1)])
|
||||||
(let-values (((trans start action-names no-look disappeared-uses)
|
(or (>= next-check max-char-num)
|
||||||
(build-lexer re-actname-lst)))
|
(loop (add1 next-check) (cdr nexts))))))])))
|
||||||
(when (vector-ref action-names start) ;; Start state is final
|
(eprintf "warning: lexer at ~a can accept the empty string\n" stx)))
|
||||||
(unless (and
|
(with-syntax ([START-STATE-STX start]
|
||||||
;; All the successor states are final
|
[TRANS-TABLE-STX trans]
|
||||||
(andmap (lambda (x) (vector-ref action-names (vector-ref x 2)))
|
[NO-LOOKAHEAD-STX no-look]
|
||||||
(vector->list (vector-ref trans start)))
|
[(NAME ...) names]
|
||||||
;; Each character has a successor state
|
[(ACT ...) (map (λ (a) (wrap-action a src-loc-style)) acts)]
|
||||||
(let loop ((check 0)
|
[(ACT-NAME ...) (vector->list action-names)]
|
||||||
(nexts (vector->list (vector-ref trans start))))
|
[SPEC-ACT-STX (wrap-action spec-act src-loc-style)]
|
||||||
(cond
|
[HAS-COMMENT-ACT?-STX (if (syntax-e spec-comment-act) #t #f)]
|
||||||
((null? nexts) #f)
|
[SPEC-COMMENT-ACT-STX (wrap-action spec-comment-act src-loc-style)]
|
||||||
(else
|
[EOF-ACT-STX (wrap-action eof-act src-loc-style)])
|
||||||
(let ((next (car nexts)))
|
(syntax-property
|
||||||
(and (= (vector-ref next 0) check)
|
(syntax/loc stx (let ([NAME ACT] ...)
|
||||||
(let ((next-check (vector-ref next 1)))
|
(let ([proc (lexer-body START-STATE-STX
|
||||||
(or (>= next-check max-char-num)
|
TRANS-TABLE-STX
|
||||||
(loop (add1 next-check) (cdr nexts))))))))))
|
(vector ACT-NAME ...)
|
||||||
(eprintf "Warning: lexer at ~a can accept the empty string.\n" stx)))
|
NO-LOOKAHEAD-STX
|
||||||
(with-syntax ((start-state-stx start)
|
SPEC-ACT-STX
|
||||||
(trans-table-stx trans)
|
HAS-COMMENT-ACT?-STX
|
||||||
(no-lookahead-stx no-look)
|
SPEC-COMMENT-ACT-STX
|
||||||
((name ...) name-lst)
|
EOF-ACT-STX)])
|
||||||
((act ...) (map (lambda (a)
|
;; reverse eta to get named procedures:
|
||||||
(wrap-action a src-loc-style))
|
(λ (port) (proc port)))))
|
||||||
act-lst))
|
'disappeared-use disappeared-uses)))])))
|
||||||
((act-name ...) (vector->list action-names))
|
|
||||||
(spec-act-stx
|
|
||||||
(wrap-action spec-act src-loc-style))
|
|
||||||
(has-comment-act?-stx
|
|
||||||
(if (syntax-e spec-comment-act) #t #f))
|
|
||||||
(spec-comment-act-stx
|
|
||||||
(wrap-action spec-comment-act src-loc-style))
|
|
||||||
(eof-act-stx (wrap-action eof-act src-loc-style)))
|
|
||||||
(syntax-property
|
|
||||||
(syntax/loc stx
|
|
||||||
(let ([name act] ...)
|
|
||||||
(let ([proc
|
|
||||||
(lexer-body start-state-stx
|
|
||||||
trans-table-stx
|
|
||||||
(vector act-name ...)
|
|
||||||
no-lookahead-stx
|
|
||||||
spec-act-stx
|
|
||||||
has-comment-act?-stx
|
|
||||||
spec-comment-act-stx
|
|
||||||
eof-act-stx)])
|
|
||||||
;; reverse eta to get named procedures:
|
|
||||||
(lambda (port) (proc port)))))
|
|
||||||
'disappeared-use
|
|
||||||
disappeared-uses)))))))))
|
|
||||||
|
|
||||||
(define-syntax lexer (make-lexer-trans #f))
|
(define-syntax lexer (make-lexer-macro 'lexer #f))
|
||||||
(define-syntax lexer-src-pos (make-lexer-trans 'lexer-src-pos))
|
(define-syntax lexer-src-pos (make-lexer-macro 'lexer-src-pos 'lexer-src-pos))
|
||||||
(define-syntax lexer-srcloc (make-lexer-trans 'lexer-srcloc))
|
(define-syntax lexer-srcloc (make-lexer-macro 'lexer-srcloc 'lexer-srcloc))
|
||||||
|
|
||||||
(define-syntax (define-lex-abbrev stx)
|
(define-syntax (define-lex-abbrev stx)
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
((_ name re)
|
[(_ NAME RE) (identifier? #'NAME)
|
||||||
(identifier? (syntax name))
|
(syntax/loc stx
|
||||||
(syntax/loc stx
|
(define-syntax NAME
|
||||||
(define-syntax name
|
(make-lex-abbrev (λ () (quote-syntax RE)))))]
|
||||||
(make-lex-abbrev (lambda () (quote-syntax re))))))
|
[_ (raise-syntax-error 'define-lex-abbrev "form should be (define-lex-abbrev name re)" stx)]))
|
||||||
(_
|
|
||||||
(raise-syntax-error
|
|
||||||
#f
|
|
||||||
"form should be (define-lex-abbrev name re)"
|
|
||||||
stx))))
|
|
||||||
|
|
||||||
(define-syntax (define-lex-abbrevs stx)
|
(define-syntax (define-lex-abbrevs stx)
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
((_ x ...)
|
[(_ . XS)
|
||||||
(with-syntax (((abbrev ...)
|
(with-syntax ([(ABBREV ...) (map
|
||||||
(map
|
(λ (a)
|
||||||
(lambda (a)
|
(syntax-case a ()
|
||||||
(syntax-case a ()
|
[(NAME RE) (identifier? #'NAME)
|
||||||
((name re)
|
(syntax/loc a (define-lex-abbrev NAME RE))]
|
||||||
(identifier? (syntax name))
|
[_ (raise-syntax-error
|
||||||
(syntax/loc a (define-lex-abbrev name re)))
|
#f
|
||||||
(_ (raise-syntax-error
|
"form should be (define-lex-abbrevs (name re) ...)"
|
||||||
#f
|
stx
|
||||||
"form should be (define-lex-abbrevs (name re) ...)"
|
a)]))
|
||||||
stx
|
(syntax->list #'XS))])
|
||||||
a))))
|
(syntax/loc stx (begin ABBREV ...)))]
|
||||||
(syntax->list (syntax (x ...))))))
|
[_ (raise-syntax-error #f "form should be (define-lex-abbrevs (name re) ...)" stx)]))
|
||||||
(syntax/loc stx (begin abbrev ...))))
|
|
||||||
(_
|
|
||||||
(raise-syntax-error
|
|
||||||
#f
|
|
||||||
"form should be (define-lex-abbrevs (name re) ...)"
|
|
||||||
stx))))
|
|
||||||
|
|
||||||
(define-syntax (define-lex-trans stx)
|
(define-syntax (define-lex-trans stx)
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
((_ name-form body-form)
|
[(_ name-form body-form)
|
||||||
(let-values (((name body)
|
(let-values (((name body)
|
||||||
(normalize-definition (syntax (define-syntax name-form body-form)) #'lambda)))
|
(normalize-definition #'(define-syntax name-form body-form) #'λ)))
|
||||||
|
|
||||||
#`(define-syntax #,name
|
#`(define-syntax #,name
|
||||||
(let ((func #,body))
|
(let ((func #,body))
|
||||||
(unless (procedure? func)
|
(unless (procedure? func)
|
||||||
(raise-syntax-error 'define-lex-trans "expected a procedure as the transformer, got ~e" func))
|
(raise-syntax-error 'define-lex-trans "expected a procedure as the transformer, got ~e" func))
|
||||||
(unless (procedure-arity-includes? func 1)
|
(unless (procedure-arity-includes? func 1)
|
||||||
(raise-syntax-error 'define-lex-trans "expected a procedure that accepts 1 argument as the transformer, got ~e" func))
|
(raise-syntax-error 'define-lex-trans "expected a procedure that accepts 1 argument as the transformer, got ~e" func))
|
||||||
(make-lex-trans func)))))
|
(make-lex-trans func))))]
|
||||||
(_
|
[_
|
||||||
(raise-syntax-error
|
(raise-syntax-error
|
||||||
#f
|
#f
|
||||||
"form should be (define-lex-trans name transformer)"
|
"form should be (define-lex-trans name transformer)"
|
||||||
stx))))
|
stx)]))
|
||||||
|
|
||||||
|
|
||||||
(define (get-next-state-helper char min max table)
|
(define (get-next-state-helper char min max table)
|
||||||
(if (>= min max)
|
(cond
|
||||||
#f
|
[(>= min max) #f]
|
||||||
(let* ((try (quotient (+ min max) 2))
|
[else
|
||||||
(el (vector-ref table try))
|
(define try (quotient (+ min max) 2))
|
||||||
(r1 (vector-ref el 0))
|
(define el (vector-ref table try))
|
||||||
(r2 (vector-ref el 1)))
|
(define r1 (vector-ref el 0))
|
||||||
(cond
|
(define r2 (vector-ref el 1))
|
||||||
((and (>= char r1) (<= char r2)) (vector-ref el 2))
|
(cond
|
||||||
((< char r1) (get-next-state-helper char min try table))
|
[(and (>= char r1) (<= char r2)) (vector-ref el 2)]
|
||||||
(else (get-next-state-helper char (add1 try) max table))))))
|
[(< char r1) (get-next-state-helper char min try table)]
|
||||||
|
[else (get-next-state-helper char (add1 try) max table)])]))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(define (get-next-state char table)
|
(define (get-next-state char table)
|
||||||
(if table
|
(and table (get-next-state-helper char 0 (vector-length table) table)))
|
||||||
(get-next-state-helper char 0 (vector-length table) table)
|
|
||||||
#f))
|
|
||||||
|
|
||||||
(define (lexer-body start-state trans-table actions no-lookahead special-action
|
(define (lexer-body start-state trans-table actions no-lookahead special-action
|
||||||
has-special-comment-action? special-comment-action eof-action)
|
has-special-comment-action? special-comment-action eof-action)
|
||||||
(letrec ((lexer
|
(letrec ([lexer
|
||||||
(lambda (ip)
|
(λ (ip)
|
||||||
(let ((first-pos (get-position ip))
|
(let ((first-pos (get-position ip))
|
||||||
(first-char (peek-char-or-special ip 0)))
|
(first-char (peek-char-or-special ip 0)))
|
||||||
;(printf "(peek-char-or-special port 0) = ~e\n" first-char)
|
;(printf "(peek-char-or-special port 0) = ~e\n" first-char)
|
||||||
(cond
|
(cond
|
||||||
((eof-object? first-char)
|
[(eof-object? first-char)
|
||||||
(do-match ip first-pos eof-action (read-char-or-special ip)))
|
(do-match ip first-pos eof-action (read-char-or-special ip))]
|
||||||
((special-comment? first-char)
|
[(special-comment? first-char)
|
||||||
(read-char-or-special ip)
|
(read-char-or-special ip)
|
||||||
(cond
|
(cond
|
||||||
(has-special-comment-action?
|
(has-special-comment-action?
|
||||||
(do-match ip first-pos special-comment-action #f))
|
(do-match ip first-pos special-comment-action #f))
|
||||||
(else (lexer ip))))
|
(else (lexer ip)))]
|
||||||
((not (char? first-char))
|
[(not (char? first-char))
|
||||||
(do-match ip first-pos special-action (read-char-or-special ip)))
|
(do-match ip first-pos special-action (read-char-or-special ip))]
|
||||||
(else
|
[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 first-char)
|
(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
|
||||||
(vector-ref actions start-state))
|
(vector-ref actions start-state))
|
||||||
;; how many bytes precede char
|
;; how many bytes precede char
|
||||||
(length-bytes 0)
|
(length-bytes 0)
|
||||||
;; how many characters have been read
|
;; how many characters have been read
|
||||||
;; including the one just read
|
;; including the one just read
|
||||||
(length-chars 1)
|
(length-chars 1)
|
||||||
;; how many characters are in the longest match
|
;; how many characters are in the longest match
|
||||||
(longest-match-length 0))
|
(longest-match-length 0))
|
||||||
(let ((next-state
|
(let ([next-state
|
||||||
(cond
|
(cond
|
||||||
((not (char? char)) #f)
|
[(not (char? char)) #f]
|
||||||
(else (get-next-state (char->integer char)
|
[else (get-next-state (char->integer char)
|
||||||
(vector-ref trans-table state))))))
|
(vector-ref trans-table state))])])
|
||||||
(cond
|
(cond
|
||||||
((not next-state)
|
[(not next-state)
|
||||||
(check-match ip first-pos longest-match-length
|
(check-match ip first-pos longest-match-length
|
||||||
length-chars longest-match-action))
|
length-chars longest-match-action)]
|
||||||
((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)))
|
||||||
(check-match ip
|
(check-match ip
|
||||||
first-pos
|
first-pos
|
||||||
(if act length-chars longest-match-length)
|
(if act length-chars longest-match-length)
|
||||||
length-chars
|
length-chars
|
||||||
(if act act longest-match-action))))
|
(if act act longest-match-action)))]
|
||||||
(else
|
[else
|
||||||
(let* ((act (vector-ref actions next-state))
|
(let* ([act (vector-ref actions next-state)]
|
||||||
(next-length-bytes (+ (char-utf-8-length char) length-bytes))
|
[next-length-bytes (+ (char-utf-8-length char) length-bytes)]
|
||||||
(next-char (peek-char-or-special ip next-length-bytes)))
|
[next-char (peek-char-or-special ip next-length-bytes)])
|
||||||
#;(printf "(peek-char-or-special port ~e) = ~e\n"
|
#;(printf "(peek-char-or-special port ~e) = ~e\n"
|
||||||
next-length-bytes next-char)
|
next-length-bytes next-char)
|
||||||
(lexer-loop next-state
|
(lexer-loop next-state
|
||||||
next-char
|
next-char
|
||||||
(if act
|
(if act
|
||||||
act
|
act
|
||||||
longest-match-action)
|
longest-match-action)
|
||||||
next-length-bytes
|
next-length-bytes
|
||||||
(add1 length-chars)
|
(add1 length-chars)
|
||||||
(if act
|
(if act
|
||||||
length-chars
|
length-chars
|
||||||
longest-match-length)))))))))))))
|
longest-match-length)))])))])))])
|
||||||
(lambda (ip)
|
(λ (ip)
|
||||||
(unless (input-port? ip)
|
(unless (input-port? ip)
|
||||||
(raise-argument-error
|
(raise-argument-error 'lexer "input-port?" 0 ip))
|
||||||
'lexer
|
(lexer ip))))
|
||||||
"input-port?"
|
|
||||||
0
|
|
||||||
ip))
|
|
||||||
(lexer ip))))
|
|
||||||
|
|
||||||
(define (check-match lb first-pos longest-match-length length longest-match-action)
|
(define (check-match lb first-pos longest-match-length length longest-match-action)
|
||||||
(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)])
|
||||||
(raise-read-error
|
(raise-read-error
|
||||||
(format "lexer: No match found in input starting with: ~a" match)
|
(format "lexer: No match found in input starting with: ~a" match)
|
||||||
(file-path)
|
(file-path)
|
||||||
(position-line first-pos)
|
(position-line first-pos)
|
||||||
(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)])
|
||||||
;(printf "(read-string ~e port) = ~e\n" longest-match-length match)
|
;(printf "(read-string ~e port) = ~e\n" longest-match-length match)
|
||||||
(do-match lb first-pos longest-match-action match)))
|
(do-match lb first-pos longest-match-action match)))
|
||||||
|
|
||||||
(define file-path (make-parameter #f))
|
(define file-path (make-parameter #f))
|
||||||
(define lexer-file-path file-path)
|
(define lexer-file-path file-path)
|
||||||
|
|
||||||
(define (do-match ip first-pos action value)
|
(define (do-match ip first-pos action value)
|
||||||
#;(printf "(action ~a ~a ~a ~a)\n"
|
#;(printf "(action ~a ~a ~a ~a)\n"
|
||||||
(position-offset first-pos) (position-offset (get-position ip)) value ip)
|
(position-offset first-pos) (position-offset (get-position ip)) value ip)
|
||||||
(action first-pos (get-position ip) value ip))
|
(action first-pos (get-position ip) value ip))
|
||||||
|
|
||||||
(define (get-position ip)
|
(define (get-position ip)
|
||||||
(let-values (((line col off) (port-next-location ip)))
|
(define-values (line col off) (port-next-location ip))
|
||||||
(make-position off line col)))
|
(make-position off line col))
|
||||||
|
|
||||||
(define-syntax (create-unicode-abbrevs stx)
|
(define-syntax (create-unicode-abbrevs stx)
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
((_ ctxt)
|
[(_ CTXT)
|
||||||
(with-syntax (((ranges ...) (map (lambda (range)
|
(with-syntax ([(RANGES ...) (for/list ([range (in-list (list (force alphabetic-ranges)
|
||||||
`(union ,@(map (lambda (x)
|
(force lower-case-ranges)
|
||||||
`(char-range ,(integer->char (car x))
|
(force upper-case-ranges)
|
||||||
,(integer->char (cdr x))))
|
(force title-case-ranges)
|
||||||
range)))
|
(force numeric-ranges)
|
||||||
(list (force alphabetic-ranges)
|
(force symbolic-ranges)
|
||||||
(force lower-case-ranges)
|
(force punctuation-ranges)
|
||||||
(force upper-case-ranges)
|
(force graphic-ranges)
|
||||||
(force title-case-ranges)
|
(force whitespace-ranges)
|
||||||
(force numeric-ranges)
|
(force blank-ranges)
|
||||||
(force symbolic-ranges)
|
(force iso-control-ranges)))])
|
||||||
(force punctuation-ranges)
|
`(union ,@(map (λ (x)
|
||||||
(force graphic-ranges)
|
`(char-range ,(integer->char (car x))
|
||||||
(force whitespace-ranges)
|
,(integer->char (cdr x))))
|
||||||
(force blank-ranges)
|
range)))]
|
||||||
(force iso-control-ranges))))
|
[(NAMES ...) (for/list ([sym (in-list '(alphabetic
|
||||||
((names ...) (map (lambda (sym)
|
lower-case
|
||||||
(datum->syntax-object (syntax ctxt) sym #f))
|
upper-case
|
||||||
'(alphabetic
|
title-case
|
||||||
lower-case
|
numeric
|
||||||
upper-case
|
symbolic
|
||||||
title-case
|
punctuation
|
||||||
numeric
|
graphic
|
||||||
symbolic
|
whitespace
|
||||||
punctuation
|
blank
|
||||||
graphic
|
iso-control))])
|
||||||
whitespace
|
(datum->syntax #'CTXT sym #f))])
|
||||||
blank
|
#'(define-lex-abbrevs (NAMES RANGES) ...))]))
|
||||||
iso-control))))
|
|
||||||
(syntax (define-lex-abbrevs (names ranges) ...))))))
|
|
||||||
|
|
||||||
(define-lex-abbrev any-char (char-complement (union)))
|
(define-lex-abbrev any-char (char-complement (union)))
|
||||||
(define-lex-abbrev any-string (intersection))
|
(define-lex-abbrev any-string (intersection))
|
||||||
(define-lex-abbrev nothing (union))
|
(define-lex-abbrev nothing (union))
|
||||||
(create-unicode-abbrevs #'here)
|
(create-unicode-abbrevs #'here)
|
||||||
|
|
||||||
(define-lex-trans (char-set stx)
|
(define-lex-trans (char-set stx)
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
((_ str)
|
[(_ STR)
|
||||||
(string? (syntax-e (syntax str)))
|
(string? (syntax-e #'STR))
|
||||||
(with-syntax (((char ...) (string->list (syntax-e (syntax str)))))
|
(with-syntax ([(CHAR ...) (string->list (syntax-e #'STR))])
|
||||||
(syntax (union char ...))))))
|
#'(union CHAR ...))]))
|
||||||
|
|
||||||
(define-syntax provide-lex-keyword
|
(define-syntax provide-lex-keyword
|
||||||
(syntax-rules ()
|
(syntax-rules ()
|
||||||
[(_ id ...)
|
[(_ ID ...)
|
||||||
(begin
|
(begin
|
||||||
(define-syntax-parameter id
|
(define-syntax-parameter ID
|
||||||
(make-set!-transformer
|
(make-set!-transformer
|
||||||
(lambda (stx)
|
(λ (stx)
|
||||||
(raise-syntax-error
|
(raise-syntax-error
|
||||||
#f
|
'provide-lex-keyword
|
||||||
(format "use of a lexer keyword (~a) is not in an appropriate lexer action"
|
(format "use of a lexer keyword (~a) is not in an appropriate lexer action" 'ID)
|
||||||
'id)
|
stx))))
|
||||||
stx))))
|
...
|
||||||
...
|
(provide ID ...))]))
|
||||||
(provide id ...))]))
|
|
||||||
|
|
||||||
(provide-lex-keyword start-pos end-pos lexeme lexeme-srcloc input-port return-without-pos return-without-srcloc)
|
(provide-lex-keyword start-pos end-pos lexeme lexeme-srcloc input-port return-without-pos return-without-srcloc)
|
||||||
|
|
||||||
)
|
|
||||||
|
Loading…
Reference in New Issue