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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
local len, match = string.len, string.match
local mini_lexer = {}
local mini_lexer_mt = {
__index = mini_lexer,
}
local literal_chars = {['('] = 1, [')'] = 1, ['~'] = 1, [','] = 1}
local oper_table = {['+'] = 0, ['-'] = 0, ['*'] = 1, ['/'] = 1, ['^'] = 2}
local function new_lexer(src)
local lexer = {n = 1, src= src}
setmetatable(lexer, mini_lexer_mt)
lexer:next()
return lexer
end
function mini_lexer.char(lexer)
local n = lexer.n
return lexer.src:sub(n, n)
end
function mini_lexer.consume(lexer, char)
local c = lexer:char()
assert(c == char, "expecting character " .. char)
lexer.n = lexer.n + 1
end
function mini_lexer.incr(lexer, n)
lexer.n = lexer.n + (n or 1)
end
function mini_lexer.match(lexer, pattern)
return match(lexer.src, '^' .. pattern, lexer.n)
end
function mini_lexer.consume(lexer, pattern)
local m = match(lexer.src, '^' .. pattern, lexer.n)
if m then
lexer.n = lexer.n + len(m)
return m
end
end
function mini_lexer.next_token(lexer)
lexer:consume('%s*')
if lexer.n > len(lexer.src) then return {type= 'EOF'} end
local c = lexer:char()
if c == '[' then
local str = lexer:consume('%b[]')
return {type= 'ident', value= str:sub(2,-2)}
end
if oper_table[c] then
local prio = oper_table[c]
lexer:incr()
return {type= 'operator', symbol= c, priority = prio}
end
if literal_chars[c] then
lexer:incr()
return {type= c}
end
if lexer:match('[%l%u_]') then
local str = lexer:consume('[%l%u_]%w*')
return {type= 'ident', value= str}
end
if lexer:match('[1-9]') then
local str = lexer:consume('[1-9]%d*%.%d*')
if not str then
str = lexer:consume('[1-9]%d*')
end
return {type= 'number', value= tonumber(str)}
end
msg = { "syntax error in expression:",
string.format(' %s', lexer.src),
string.format(' %s^', string.rep(' ', lexer.n - 1)) }
error(table.concat(msg, '\n'))
end
function mini_lexer.next(lexer)
lexer.token = lexer:next_token()
end
local function accept(lexer, token_type)
if lexer.token.type == token_type then
lexer:next()
return 1
end
return 0
end
local function expect(lexer, token_type)
if accept(lexer, token_type) == 0 then
error("expecting " .. token_type)
end
end
local expr
local function factor(lexer, actions)
local token = lexer.token
if token.type == 'ident' then
local id = token.value
lexer:next()
return actions.ident(id)
elseif token.type == 'number' then
local x = token.value
lexer:next()
return actions.number(x)
elseif token.type == '(' then
lexer:next()
local a = expr(lexer, actions, 0)
expect(lexer, ')')
return a
end
error('expecting variable or number')
end
expr = function(lexer, actions, prio)
if prio > 2 then
return factor(lexer, actions)
end
local a
local token = lexer.token
if prio == 0 and token.type == 'operator' and token.symbol == '-' then
local symbol = token.symbol
lexer:next()
local b = expr(lexer, actions, prio + 1)
a = actions.prefix(symbol, b)
else
a = expr(lexer, actions, prio + 1)
end
token = lexer.token
while token.type == 'operator' and token.priority >= prio do
local symbol = token.symbol
accept(lexer, 'operator')
local b = expr(lexer, actions, prio + 1)
a, token = actions.infix(symbol, a, b), lexer.token
end
return a
end
local function expr_list(lexer, actions)
local a = expr(lexer, actions, 0)
local els = actions.exprlist(a)
while accept(lexer, ',') > 0 do
local b = expr(lexer, actions, 0)
els = actions.exprlist(b, els)
end
return els
end
local function schema(lexer, actions)
local y = expr_list(lexer, actions)
expect(lexer, '~')
local x = expr_list(lexer, actions)
return actions.schema(x, y)
end
local function parse_expr(lexer, actions)
return expr(lexer, actions, 0)
end
local AST_create = {
infix = function(sym, a, b) return {operator= sym, a, b} end,
ident = function(id) return {ident= id} end,
prefix = function(sym, a) return {operator= sym, a} end,
number = function(x) return {number= x} end,
exprlist = function(a, ls) if ls then ls[#ls+1] = a else ls = {list= true, a} end; return ls end,
schema = function(x, y) return {schema= true, x= x, y= y} end,
}
local format, concat = string.format, table.concat
local AST_print
local function is_ident_simple(s)
return s:match('^[_%l%u]%w*$')
end
local function AST_print_op(e, prio)
if #e == 1 then
local c, c_prio = AST_print(e[1])
if c_prio < prio then c = format('(%s)', c) end
return format("%s%s", e.operator, c)
else
local a, a_prio = AST_print(e[1])
local b, b_prio = AST_print(e[2])
if a_prio < prio then a = format('(%s)', a) end
if b_prio < prio then b = format('(%s)', b) end
local temp = (prio < 2 and "%s %s %s" or "%s%s%s")
return format(temp, a, e.operator, b)
end
end
local function AST_print_exprlist(e)
local t = {}
for k = 1, #e do t[k] = AST_print(e[k]) end
return concat(t, ', ')
end
AST_print = function(e)
if e.schema then
local ys = AST_print_exprlist(e.y)
local xs = AST_print_exprlist(e.x)
return format("%s ~ %s", ys, xs)
elseif e.list then
return AST_print_exprlist(e)
elseif e.ident then
local s = e.ident
if not is_ident_simple(s) then s = format('[%s]', s) end
return s, 3
elseif e.number then
return e.number, 3
else
local prio = oper_table[e.operator]
local s = AST_print_op(e, prio)
return s, prio
end
end
return {lexer = new_lexer, schema= schema, parse = parse_expr, AST= AST_create, print = AST_print}
|