forked from vsbenas/parser-gen
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathast.lua
More file actions
59 lines (49 loc) · 1.19 KB
/
ast.lua
File metadata and controls
59 lines (49 loc) · 1.19 KB
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
-- common abstract syntax tree (ast) functions
local function isfinal(t)
return t["t"] or t['tchar'] or t["nt"] or t["func"] or t["s"] or t["name"] or t["num"]
end
local function isaction(t)
return t["action"]
end
local function isrule(t)
return t and t["rulename"]
end
local function isgrammar(t)
if type(t) == "table" and not(t["action"]) then
return isrule(t[1])
end
return false
end
local function istoken (t)
return t["token"] == "1"
end
local function iscapture (action)
return action == "=>" or action == "gcap" or action == "scap" or action == "subcap" or action == "poscap"
end
local function finalNode (t)
if t["t"] then
return"t",t["t"] -- terminal
elseif t["tchar"] then
return "tchar", t["tchar"]
elseif t["nt"] then
return "nt", t["nt"], istoken(t) -- nonterminal
elseif t["func"] then
return "func", t["func"] -- function
elseif t["name"] then
return "name", t["name"]
elseif t["s"] then
return "s", t["s"]
elseif t["num"] then
return "num", t["num"]
end
return nil
end
return {
isfinal=isfinal,
isaction=isaction,
isrule=isrule,
isgrammar=isgrammar,
istoken=istoken,
iscapture=iscapture,
finalNode=finalNode
}