-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtokens.lkt
205 lines (191 loc) · 6.24 KB
/
tokens.lkt
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
lexer ada_lexer {
val bracket_char = p"(\\[\\\"[0-9a-fA-F]+\\\"\\])"
val p_string = p"\\\"(\\\"\\\"|{bracket_char}|[^\\n\\\"])*\\\""
val p_format_string_start =
p"f\\\"(\\\"\\\"|{bracket_char}|[^\\n\\\"\\{])*\\{"
val p_format_string_mid = p"\\}(\\\"\\\"|{bracket_char}|[^\\n\\\"\\{])*\\{"
val p_format_string_end =
p"\\}(\\\"\\\"|{bracket_char}|[^\\n\\\"\\{])*\\\""
# Simple format string literal without expressions to expand
val p_format_string_string =
p"f\\\"(\\\"\\\"|{bracket_char}|[^\\n\\\"\\{])*\\\""
val p_percent_string = p"%(%%|{bracket_char}|[^\\n%])*%"
val digit = p"[0-9]"
val extended_digit = p"[0-9a-zA-Z]"
val integer = p"({digit}(_?{digit})*)"
val exponent = p"([eE](\\+?|-){integer})"
val decimal_literal = p"{integer}(\\.?{integer})?{exponent}?"
val integer_literal = p"{integer}{exponent}?"
val base = p"{integer}"
val based_integer = p"{extended_digit}(_?{extended_digit})*"
val based_decimal_literal =
p"{base}[#:]{based_integer}(\\.{based_integer})?[#:]{exponent}?"
val based_integer_literal = p"{base}[#:]{based_integer}[#:]{exponent}?"
val ws = p"[ ]*"
# TODO: handle Unicode properties in Langkit and switch back to
# \p{ID_Start} and \p{ID_Continue}.
val identifier =
p"[\\$_]?"
& p"(\\p{Lu}|\\p{Ll}|\\p{Lt}|\\p{Lm}|\\p{Lo}|\\p{Nl}|{bracket_char})"
& p"(\\p{Lu}|\\p{Ll}|\\p{Lt}|\\p{Lm}|\\p{Lo}|\\p{Nl}|\\p{Nd}|\\p{Mn}"
& p"|\\p{Mc}|_|{bracket_char})*"
@unparsing_spacing(with=alphanumericals)
family alphanumericals {
Access
Delta
Digits
Mod
Range
}
# Blanks and trivia
@trivia()
Whitespace <- p"[ \\t\\r\\n\\f\\v]+"
@with_unparsing_newline
@trivia(comment=true)
Comment <- p"--(.?)+"
@with_unparsing_newline
@trivia()
PrepLine <- p"#(.?)+"
# Those keywords can also be attributes, which is why they have special
# handling, for example as in A'Access.
match no_case("access") {
if previous_token is Tick then send(Identifier, 6)
else send(Access, 6)
}
match no_case("range") {
if previous_token is Tick then send(Identifier, 5)
else send(Range, 5)
}
match no_case("digits") {
if previous_token is Tick then send(Identifier, 6)
else send(Digits, 6)
}
match no_case("delta") {
if previous_token is Tick then send(Identifier, 5)
else send(Delta, 5)
}
match no_case("mod") {
if previous_token is Tick then send(Identifier, 3)
else send(Mod, 3)
}
# Keywords
@unparsing_spacing(with=alphanumericals)
family alphanumericals {
Abort <- no_case("abort")
Else <- no_case("else")
New <- no_case("new")
Return <- no_case("return")
Abs <- no_case("abs")
Elsif <- no_case("elsif")
Not <- no_case("not")
Reverse <- no_case("reverse")
End <- no_case("end")
@symbol()
Null <- no_case("null")
Accept <- no_case("accept")
Entry <- no_case("entry")
Select <- no_case("select")
Exception <- no_case("exception")
Of <- no_case("of")
Separate <- no_case("separate")
Exit <- no_case("exit")
Or <- no_case("or")
All <- no_case("all")
Others <- no_case("others")
Subtype <- no_case("subtype")
And <- no_case("and")
For <- no_case("for")
Out <- no_case("out")
Array <- no_case("array")
Function <- no_case("function")
At <- no_case("at")
Generic <- no_case("generic")
Package <- no_case("package")
Task <- no_case("task")
Begin <- no_case("begin")
Goto <- no_case("goto")
Pragma <- no_case("pragma")
Terminate <- no_case("terminate")
Body <- no_case("body")
Private <- no_case("private")
Then <- no_case("then")
If <- no_case("if")
Procedure <- no_case("procedure")
Type <- no_case("type")
Case <- no_case("case")
In <- no_case("in")
Constant <- no_case("constant")
Is <- no_case("is")
Raise <- no_case("raise")
Use <- no_case("use")
Declare <- no_case("declare")
Delay <- no_case("delay")
Limited <- no_case("limited")
Record <- no_case("record")
When <- no_case("when")
Loop <- no_case("loop")
Rem <- no_case("rem")
While <- no_case("while")
Renames <- no_case("renames")
With <- no_case("with")
Do <- no_case("do")
Xor <- no_case("xor")
}
# Punctuation
ParOpen <- "("
ParClose <- ")"
BrackOpen <- "["
BrackClose <- "]"
Semicolon <- ";"
Colon <- ":"
Comma <- ","
Doubledot <- ".."
Assign <- ":="
Dot <- "."
Diamond <- "<>"
Lte <- "<="
Gte <- ">="
Arrow <- "=>"
Equal <- "="
Lt <- "<"
Gt <- ">"
Plus <- "+"
Minus <- "-"
Power <- "**"
Mult <- "*"
Amp <- "&"
Notequal <- "/="
Divide <- "/"
Tick <- "'"
Pipe <- or("|" | "!")
LabelStart <- "<<"
LabelEnd <- ">>"
Target <- "@"
# Literals
@unparsing_spacing(with=alphanumericals)
family alphanumericals {
Integer <- or(p"{integer_literal}" | p"{based_integer_literal}")
Decimal <- or(p"{decimal_literal}" | p"{based_decimal_literal}")
}
String <- or(p"{p_string}" | p"{p_percent_string}")
FormatStringString <- p"{p_format_string_string}"
FormatStringStart <- p"{p_format_string_start}"
FormatStringMid <- p"{p_format_string_mid}"
FormatStringEnd <- p"{p_format_string_end}"
# Identifiers
@unparsing_spacing(with=alphanumericals)
family alphanumericals {
@symbol()
Identifier <- p"{identifier}"
}
@symbol()
Char <- p"'{bracket_char}'"
# Attribute vs character literal quirk: A character literal is match via
# '.'. However, this sequence of characters can happen in other cases, like
# a qualified expression with a char as parameter: A'Class'('b'). In those
# cases, we need to send the tick token, rather than the char token.
match p"'.'" {
if previous_token is Identifier then send(Tick, 1)
else send(Char, 3)
}
}