- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiny.lua
More file actions
Latest commit
150 lines (114 loc) · 4.74 KB
/
Copy pathtiny.lua
File metadata and controls
150 lines (114 loc) · 4.74 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
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
locallpeg=require"lpeglabel"
localrelabel=require"relabel"
--local lpeglabel = require "lpeglabel"
lpeg.locale(lpeg)
localP, V, C, Ct, R, S, B, Cmt=lpeg.P, lpeg.V, lpeg.C, lpeg.Ct, lpeg.R, lpeg.S, lpeg.B, lpeg.Cmt-- for lpeg
localT=lpeg.T-- lpeglabel
localspace=lpeg.space
localalpha=lpeg.alpha
localterror= {}
localvars= {}
localfunctionnewError(s)
table.insert(terror, s)
return#terror
end
-- error definitions
localerrSemicolon=newError("Unexpected semicolon")
localerrMissingSemicolon=newError("Missing semicolon")
localerrInvalidStatement=newError("Invalid statement")
localerrIfMissingThen=newError("Missing keyword 'then' in If statement")
localerrIfMissingEnd=newError("Missing keyword 'end' in If statement")
localerrRepeatMissingUntil=newError("Missing keyword 'until' in Repeat statement")
localerrAssMissingExp=newError("Missing expression on RHS of assignment")
localerrReadMissingId=newError("Missing Identifier for read operation")
localerrWriteMissingExp=newError("Missing Expression for write operation")
localerrCompMissingSExp=newError("Missing Simple Expression for comparison operation")
localerrAddopMissingTerm=newError("Missing Term for add operation")
localerrMulopMissingFactor=newError("Missing Factor for mul operation")
localerrMissingClosingBracket=newError("Missing closing bracket")
localerrExtra=newError("Extra")
functiontoken (patt)
returnpatt*V"Skip"
end
functionsym (str)
returntoken(P(str))
end
functionkw (str)
returntoken(P(str))
end
functiontry (patt, err)
returnpatt+T(err)
end
functionthrows(patt,err) -- if pattern is matched throw error
returnpatt*T(err)
end
--[[ todo
function expect (rule)
return V(rule) + T(getLabel(rule))
end
-]]--
localgram=P {
"program",
program=V"Skip" *V"stmtsequence" *-1+T(errExtra),
stmtsequence=V"statement" * (sym(";") * (V"eossemicolon" +V"statement") +throws(#V"firstTokens",errMissingSemicolon))^0,
eossemicolon=throws(-1,errSemicolon), -- semicolon at the end of the input
firstTokens=V"keywordsStart" +V"Identifier", -- for the missing semicolon test
keywords=V"keywordsStart" +V"keywordsRest",
keywordsStart=P"if" +P"repeat" +P"read" +P"write", -- keywords that appear in the beginning of a statement, necessary to check for missing semicolons
keywordsRest=P"then" +P"else" +P"end" +P"until",
statement=try(V"assignstmt" +V"ifstmt" +V"repeatstmt" +V"readstmt" +V"writestmt",errInvalidStatement), -- assignstmt is moved first to match "ifa:=4" instead of "if a (expect:then).."
ifstmt=kw("if") *V"exp" *try(kw("then"),errIfMissingThen) *V"stmtsequence" * (kw("else") *V"stmtsequence")^-1*try(kw("end"),errIfMissingEnd),
repeatstmt=kw("repeat") *V"stmtsequence" *try(kw("until"),errRepeatMissingUntil) *V"exp",
assignstmt=V"Identifier" *sym(":=") *try(V"exp",errAssMissingExp),
readstmt=kw("read") *try(V"Identifier",errReadMissingId),
writestmt=kw("write") *try(V"exp",errWriteMissingExp),
exp=V"simpleexp" * (V"comparisonop" *try(V"simpleexp",errCompMissingSExp))^0,
comparisonop=sym("<") +sym("="),
simpleexp=V"term" * (V"addop" *try(V"term",errAddopMissingTerm))^0,
addop=sym("+") +sym("-"),
term=V"factor" * (V"mulop" *try(V"factor",errMulopMissingFactor))^0,
mulop=sym("*") +sym("/"),
factor=sym("(") *V"exp" *try(sym(")"),errMissingClosingBracket) +V"Number" +V"Identifier",
Number=token(P"-"^-1*R("09")^1),
Identifier=token(alpha^1-#V"Reserved"),
Reserved=V"keywords" *-alpha, -- ifabc is a valid identifier; if, if3 if. are not
Skip= (space)^0,
}
functionmymatch(s,g)
localr, e, sfail=g:match(s)
ifnotrthen
localline, col=relabel.calcline(s, #s-#sfail)
localmsg="Error at line " ..line.." (col " ..col.."): "
ife==0then
returnr, msg.."Syntax error before '" ..sfail.."'"
else
returnr, msg..terror[e] .." before '" ..sfail.."'"
end
end
returnr
end
functiontiny(str) -- to use from test file
localr, e, sfail=gram:match(str)
returnr,e
end
ifarg[1] then
-- argument must be in quotes if it contains spaces
print(mymatch(arg[1],gram));
end
localre= {
tiny=tiny,
errSemicolon=errSemicolon,
errMissingSemicolon=errMissingSemicolon,
errInvalidStatement=errInvalidStatement,
errIfMissingThen=errIfMissingThen,
errIfMissingEnd=errIfMissingEnd,
errRepeatMissingUntil=errRepeatMissingUntil,
errAssMissingExp=errAssMissingExp,
errReadMissingId=errReadMissingId,
errWriteMissingExp=errWriteMissingExp,
errCompMissingSExp=errCompMissingSExp,
errAddopMissingTerm=errAddopMissingTerm,
errMulopMissingFactor=errMulopMissingFactor,
errMissingClosingBracket=errMissingClosingBracket
}
returnre