-
Notifications
You must be signed in to change notification settings - Fork 14
/
ast.lua
executable file
·192 lines (138 loc) · 4.36 KB
/
ast.lua
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
-- Abstract Syntax Tree module for vstruct
-- This module implements the parser for vstruct format definitions. It is a
-- fairly simple recursive-descent parser that constructs an AST using Lua
-- tables, and then generates lua source from it.
-- See ast/*.lua for the implementations of various node types in the AST,
-- and see lexer.lua for the implementation of the lexer.
-- Copyright (c) 2011 Ben "ToxicFrog" Kelly
local struct = require "vstruct"
local lexer = require "vstruct.lexer"
local ast = {}
local cache = {}
-- load the implementations of the various AST node types
for _,node in ipairs { "IO", "List", "Name", "Table", "Repeat", "Generator", "Root", "Bitpack" } do
ast[node] = require ((...).."."..node)
end
-- given a source string, compile it
-- returns a table containing pack and unpack functions and the original
-- source - see README#vstruct.compile for a description.
--
-- if (struct.cache) is non-nil, will return the cached version, if present
-- if (struct.cache) is true, will create a new cache entry, if needed
function ast.parse(source)
if struct.cache ~= nil and cache[source] then
return cache[source]
end
local lex = lexer(source)
local root = ast.Root(ast.List())
for node in (function() return ast.next(lex) end) do
root:append(node)
end
root = root:gen(ast.Generator())
if struct.cache == true then
cache[source] = root
end
return root
end
-- used by the rest of the parser to report syntax errors
function ast.error(lex, expected)
error("vstruct: parsing format string at "..lex.where()..": expected "..expected..", got "..lex.peek().type)
end
-- Everything below this line is internal to the recursive descent parser
function ast.name(lex)
local name = lex.next().text
local next = lex.peek()
if next and next.type == "number" and not lex.whitespace() then
return ast.IO(name, lex.next().text)
else
return ast.IO(name, nil)
end
end
function ast.key(lex)
local name = lex.next().text
local next = lex.peek()
next = ast.next(lex)
if next.tag == "io" or next.tag == "table" then
return ast.Name(name, next)
else
ast.error(lex, "value (field or table)")
end
end
function ast.next(lex)
local tok = lex.peek()
if not tok then
return nil
end
if tok.type == '(' then
return ast.group(lex)
elseif tok.type == '{' then
return ast.table(lex)
elseif tok.type == '[' then
return ast.bitpack(lex)
elseif tok.type == "name" then
return ast.name(lex)
elseif tok.type == "key" then
return ast.key(lex)
elseif tok.type == "number" then
return ast.repetition(lex)
elseif tok.type == "control" then
return ast.control(lex)
else
ast.error(lex, "'(', '{', '[', name, number, control, or io specifier")
end
end
function ast.repetition(lex)
local count = tonumber(lex.next().text)
ast.require(lex, "*");
return ast.Repeat(count, ast.next(lex))
end
function ast.group(lex)
ast.require(lex, '(')
local group = ast.List()
group.tag = "group"
while lex.peek().type ~= ')' do
group:append(ast.next(lex))
end
ast.require(lex, ')')
return group
end
function ast.table(lex)
ast.require(lex, '{')
local group = ast.Table()
while lex.peek().type ~= '}' do
group:append(ast.next(lex))
end
ast.require(lex, '}')
return group
end
function ast.bitpack(lex)
ast.require(lex, "[")
local bitpack = ast.Bitpack(tonumber(ast.require(lex, "number").text))
ast.require(lex, "|")
while lex.peek().type ~= "]" do
bitpack:append(ast.next(lex))
end
ast.require(lex, "]")
bitpack:finalize()
return bitpack
end
function ast.require(lex, type)
local t = lex.next()
if t.type ~= type then
ast.error(lex, type)
end
return t
end
return ast
--[[
format -> commands
command -> repeat | bitpack | group | named | value | control
repeat -> NUMBER '*' command | command '*' NUMBER
bitpack -> NUMBER '|' commands '|'
group -> '(' commands ')'
named -> NAME ':' value
value -> table | primitive
table -> '{' commands '}'
primitive -> ATOM NUMBERS
control -> SEEK NUMBER | ENDIANNESS
--]]