You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
4.4 KiB
Racket
138 lines
4.4 KiB
Racket
8 years ago
|
#lang racket/base
|
||
|
(require (for-syntax racket/base "parser.rkt"))
|
||
|
(require br-parser-tools/lex
|
||
|
(prefix-in : br-parser-tools/lex-sre)
|
||
|
"parser.rkt"
|
||
|
"rule-structs.rkt"
|
||
6 years ago
|
(only-in brag/support from/to)
|
||
8 years ago
|
racket/string)
|
||
|
|
||
|
(provide lex/1 tokenize)
|
||
8 years ago
|
(module+ lex-abbrevs
|
||
|
(provide hide-char splice-char id-char letter digit NL id))
|
||
8 years ago
|
|
||
|
;; A newline can be any one of the following.
|
||
|
(define-lex-abbrev NL (:or "\r\n" "\r" "\n"))
|
||
|
|
||
7 years ago
|
;; reserved-chars = chars used for quantifiers & parse-tree filtering
|
||
7 years ago
|
(define-for-syntax quantifiers "+:*?{}") ; colon is reserved to separate rules and productions
|
||
8 years ago
|
(define-lex-trans reserved-chars
|
||
|
(λ(stx) #`(char-set #,(format "~a~a~a" quantifiers hide-char splice-char))))
|
||
|
|
||
|
(define-lex-trans hide-char-trans (λ(stx) #`(char-set #,(format "~a" hide-char))))
|
||
|
(define-lex-trans splice-char-trans (λ(stx) #`(char-set #,(format "~a" splice-char))))
|
||
|
|
||
|
(define-lex-abbrevs
|
||
|
[letter (:or (:/ "a" "z") (:/ #\A #\Z))]
|
||
|
[digit (:/ #\0 #\9)]
|
||
|
[id-char (:or letter digit (:& (char-set "+:*@!-.$%&/=?^_~<>") (char-complement (reserved-chars))))]
|
||
|
[hide-char (hide-char-trans)]
|
||
|
[splice-char (splice-char-trans)]
|
||
|
)
|
||
|
|
||
|
(define-lex-abbrev id (:& (complement (:+ digit)) (:+ id-char)))
|
||
7 years ago
|
(define-lex-abbrev id-separator (:or ":" "::="))
|
||
8 years ago
|
|
||
7 years ago
|
(define-lex-abbrev esc-chars (union "\\a" "\\b" "\\t" "\\n" "\\v" "\\f" "\\r" "\\e"))
|
||
7 years ago
|
|
||
7 years ago
|
(define (unescape-lexeme lexeme quote-char)
|
||
7 years ago
|
;; convert the literal string representation back into an escape char with lookup table
|
||
7 years ago
|
(define unescapes (hash "a" 7 "b" 8 "t" 9 "n" 10 "v" 11 "f" 12 "r" 13 "e" 27 "\"" 34 "'" 39 "\\" 92))
|
||
7 years ago
|
(define pat (regexp (format "(?<=^~a\\\\).(?=~a$)" quote-char quote-char)))
|
||
|
(cond
|
||
|
[(regexp-match pat lexeme)
|
||
7 years ago
|
=> (λ (m) (string quote-char (integer->char (hash-ref unescapes (car m))) quote-char))]
|
||
7 years ago
|
[else lexeme]))
|
||
|
|
||
|
|
||
8 years ago
|
(define lex/1
|
||
|
(lexer-src-pos
|
||
7 years ago
|
;; handle whitespace & escape chars within quotes as literal tokens: "\n" "\t" '\n' '\t'
|
||
|
;; match the escaped version, and then unescape them before they become token-LITs
|
||
8 years ago
|
[(:: "'"
|
||
7 years ago
|
(:or (:* (:or "\\'" esc-chars (:~ "'" "\\"))) "\\\\")
|
||
8 years ago
|
"'")
|
||
7 years ago
|
(token-LIT (unescape-lexeme lexeme #\'))]
|
||
8 years ago
|
[(:: "\""
|
||
7 years ago
|
(:or (:* (:or "\\\"" esc-chars (:~ "\"" "\\"))) "\\\\")
|
||
8 years ago
|
"\"")
|
||
7 years ago
|
(token-LIT (unescape-lexeme lexeme #\"))]
|
||
7 years ago
|
[(:or "()" "Ø" "∅") (token-EMPTY lexeme)]
|
||
8 years ago
|
["("
|
||
|
(token-LPAREN lexeme)]
|
||
|
["["
|
||
|
(token-LBRACKET lexeme)]
|
||
|
[")"
|
||
|
(token-RPAREN lexeme)]
|
||
|
["]"
|
||
|
(token-RBRACKET lexeme)]
|
||
|
[hide-char
|
||
|
(token-HIDE lexeme)]
|
||
|
[splice-char
|
||
|
(token-SPLICE lexeme)]
|
||
|
["|"
|
||
|
(token-PIPE lexeme)]
|
||
7 years ago
|
[(:or "+" "*" "?"
|
||
7 years ago
|
(:: "{" (:* digit) (:? (:: "," (:* digit))) "}"))
|
||
8 years ago
|
(token-REPEAT lexeme)]
|
||
6 years ago
|
;; Skip whitespace
|
||
8 years ago
|
[whitespace
|
||
|
(return-without-pos (lex/1 input-port))]
|
||
6 years ago
|
;; skip multiline comments
|
||
|
[(from/to "(*" "*)") (return-without-pos (lex/1 input-port))]
|
||
8 years ago
|
;; Skip comments up to end of line
|
||
8 years ago
|
[(:: (:or "#" ";")
|
||
8 years ago
|
(complement (:: (:* any-char) NL (:* any-char)))
|
||
|
(:or NL ""))
|
||
8 years ago
|
(return-without-pos (lex/1 input-port))]
|
||
6 years ago
|
;; skip commas (concatenation is implied)
|
||
|
["," (return-without-pos (lex/1 input-port))]
|
||
8 years ago
|
[(eof)
|
||
|
(token-EOF lexeme)]
|
||
7 years ago
|
[(:: id (:* whitespace) id-separator)
|
||
8 years ago
|
(token-RULE_HEAD lexeme)]
|
||
7 years ago
|
[(:: hide-char id (:* whitespace) id-separator)
|
||
8 years ago
|
(token-RULE_HEAD_HIDDEN lexeme)]
|
||
7 years ago
|
[(:: splice-char id (:* whitespace) id-separator)
|
||
8 years ago
|
(token-RULE_HEAD_SPLICED lexeme)]
|
||
|
[id
|
||
|
(token-ID lexeme)]
|
||
|
|
||
|
;; We call the error handler for everything else:
|
||
|
[(:: any-char)
|
||
|
(let-values ([(rest-of-text end-pos-2)
|
||
|
(lex-nonwhitespace input-port)])
|
||
|
((current-parser-error-handler)
|
||
|
#f
|
||
|
'error
|
||
|
(string-append lexeme rest-of-text)
|
||
|
(position->pos start-pos)
|
||
|
(position->pos end-pos-2)))]))
|
||
|
|
||
|
|
||
|
;; This is the helper for the error production.
|
||
|
(define lex-nonwhitespace
|
||
|
(lexer
|
||
|
[(:+ (char-complement whitespace))
|
||
|
(values lexeme end-pos)]
|
||
|
[any-char
|
||
|
(values lexeme end-pos)]
|
||
|
[(eof)
|
||
|
(values "" end-pos)]))
|
||
|
|
||
|
|
||
|
|
||
|
;; position->pos: position -> pos
|
||
7 years ago
|
;; Converts position structures from br-parser-tools/lex to our own pos structures.
|
||
8 years ago
|
(define (position->pos a-pos)
|
||
|
(pos (position-offset a-pos)
|
||
|
(position-line a-pos)
|
||
|
(position-col a-pos)))
|
||
|
|
||
|
|
||
|
|
||
|
;; tokenize: input-port -> (-> token)
|
||
7 years ago
|
(define (tokenize ip #:source [source (object-name ip)])
|
||
|
(λ () (parameterize ([file-path source])
|
||
|
(lex/1 ip))))
|