-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.lisp
130 lines (124 loc) · 4.03 KB
/
lexer.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
;;; take int account:
;;; how to support all numerical identifier?
;;; leading zeros in integer is not allowed
;;;
;;; sanitize integer? should accept leading spaces
(in-package #:sawyer)
;;; The main lexer
(define-lexer toml-lexer (s)
;; Spaces and comments
("%s+" :next-token)
("%n+" (values :newline))
("#[^%n]*" :next-token)
;; bare keyword
("[%d%a_-]+" (values :bare-keyword $$))
;; basic strings
("\"" (push-lexer s #'string-lexer :string))
("'([^'%n]*)'" (values :literal-string $1))
;; equal
("=" (push-lexer s #'toml-value-lexer :equal))
;; arrays of tables
("%[%[" :double-bracket-open)
("%]%]" :double-bracket-close)
;; table headers:
("%[" :bracket-open)
("%]" :bracket-close)
("%." :dot)
;; comment at the end of the file
("#.*" :next-token)
)
(define-lexer toml-value-lexer (s)
;; space
("%s+" :next-token)
;; Dates
;; offset date-time
("(%d%d%d%d)%-(%d%d)%-(%d%d)((T|t| )(%d%d):(%d%d):(%d%d)(%.%d+)?)(Z|((%+|%-)(%d%d):(%d%d)))"
(pop-lexer s :offset-date-time $$))
;; local date-time
("(%d%d%d%d)%-(%d%d)%-(%d%d)(T|t| )(%d%d):(%d%d):(%d%d)(%.%d+)?"
(pop-lexer s :local-date-time $$))
;; local date
("(%d%d%d%d)%-(%d%d)%-(%d%d)"
(pop-lexer s :local-date $$))
;; local time
("(%d%d):(%d%d):(%d%d)(%.%d+)?"
(pop-lexer s :local-time $$))
;; Numbers
("([+-])?(([1-9](_%d|%d)*)|0)(%.%d(_%d|%d)*)?([Ee]([+-])?%d(_%d|%d)*)?" (pop-lexer s :number (let ((*read-default-float-format* 'double-float))(parse-number (remove #\_ $$)))))
;; Strings
("'''%n?"
(progn
(pop-lexer s :wathever)
(push-lexer s #'multi-line-literal-string-lexer :multi-line-literal-string)))
("'([^'%n]*)'" (pop-lexer s :literal-string $1))
("\"\"\"%n?"
(progn (pop-lexer s :whatever)
(push-lexer s #'multi-line-string-lexer :multi-line-string)))
("\"" (progn (pop-lexer s :whatever) (push-lexer s #'string-lexer :string)))
;; Booleans
("true" (pop-lexer s :true t))
("false" (pop-lexer s :false nil))
("%["
(progn (pop-lexer s :whatever)
(push-lexer s #'array-lexer :bracket-open)))
("%{"
(progn (pop-lexer s :whatever)
(push-lexer s #'inline-table-lexer :curly-brace-open)))
)
(define-lexer array-lexer (s)
;; finish the array
("%]" (pop-lexer s :bracket-close))
;; Spaces and comments
("%s+|%n+" :next-token)
("#.-%n" :next-token)
;; comma separates values inside an array
("," :comma)
;; Dates
;; offset date-time
("(%d%d%d%d)%-(%d%d)%-(%d%d)((T|t| )(%d%d):(%d%d):(%d%d)(%.%d+)?)(Z|((%+|%-)(%d%d):(%d%d)))"
(values :offset-date-time $$))
;; local date-time
("(%d%d%d%d)%-(%d%d)%-(%d%d)(T|t| )(%d%d):(%d%d):(%d%d)(%.%d+)?"
(values :local-date-time $$))
;; local date
("(%d%d%d%d)%-(%d%d)%-(%d%d)"
(values :local-date $$))
;; local time
("(%d%d):(%d%d):(%d%d)(%.%d+)?"
(values :local-time $$))
;; Numbers
("([+-])?(([1-9](_%d|%d)*)|0)(%.%d((_%d)|%d)*)?([Ee]([+-])?%d((_%d)|%d)*)?"
(values :number (let ((*read-default-float-format* 'double-float))(parse-number (remove #\_ $$)))))
;; Strings
("'''%n?"
(push-lexer s #'multi-line-literal-string-lexer :multi-line-literal-string))
("'([^'%n]*)'" (values :literal-string $1))
("\"\"\"%n?"
(push-lexer s #'multi-line-string-lexer :multi-line-string))
("\"" (push-lexer s #'string-lexer :string))
;; Booleans
("true" :true)
("false" :false)
("%[" (push-lexer s #'array-lexer :bracket-open))
("%{" (push-lexer s #'inline-table-lexer :curly-brace-open))
;; end of the value
("$" (error "Unterminated array"))
)
(define-lexer inline-table-lexer (s)
;; finish the inline table
("%}" (pop-lexer s :curly-brace-close))
;; Spaces
("%s+" :next-token)
;; comma separates key/value pairs inside an inline table
("," :comma)
;; bare keyword
("[%d%a_-]+" (values :bare-keyword $$))
;; basic strings
("\"" (push-lexer s #'string-lexer :string))
;; equal
("=" (push-lexer s #'toml-value-lexer :equal))
;; end of the value
("$" (error "Unterminated inline table"))
)
;;
;(tokenize #'toml-lexer (slurp "input"))