diff options
Diffstat (limited to 'examples/typedlua/tllexer.lua')
-rw-r--r-- | examples/typedlua/tllexer.lua | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/examples/typedlua/tllexer.lua b/examples/typedlua/tllexer.lua new file mode 100644 index 0000000..bcfa802 --- /dev/null +++ b/examples/typedlua/tllexer.lua | |||
@@ -0,0 +1,80 @@ | |||
1 | local tllexer = {} | ||
2 | |||
3 | local lpeg = require "lpeglabel" | ||
4 | lpeg.locale(lpeg) | ||
5 | |||
6 | local function setffp (s, i, t) | ||
7 | if not t.ffp or i > t.ffp then | ||
8 | t.ffp = i | ||
9 | end | ||
10 | return false | ||
11 | end | ||
12 | |||
13 | local function updateffp () | ||
14 | return lpeg.Cmt(lpeg.Carg(1), setffp) | ||
15 | end | ||
16 | |||
17 | tllexer.Shebang = lpeg.P("#") * (lpeg.P(1) - lpeg.P("\n"))^0 * lpeg.P("\n") | ||
18 | |||
19 | local Space = lpeg.space^1 | ||
20 | |||
21 | local Equals = lpeg.P("=")^0 | ||
22 | local Open = "[" * lpeg.Cg(Equals, "init") * "[" * lpeg.P("\n")^-1 | ||
23 | local Close = "]" * lpeg.C(Equals) * "]" | ||
24 | local CloseEQ = lpeg.Cmt(Close * lpeg.Cb("init"), | ||
25 | function (s, i, a, b) return a == b end) | ||
26 | |||
27 | local LongString = Open * (lpeg.P(1) - CloseEQ)^0 * Close / | ||
28 | function (s, o) return s end | ||
29 | |||
30 | local Comment = lpeg.P("--") * LongString / | ||
31 | function () return end + | ||
32 | lpeg.P("--") * (lpeg.P(1) - lpeg.P("\n"))^0 | ||
33 | |||
34 | tllexer.Skip = (Space + Comment)^0 | ||
35 | |||
36 | local idStart = lpeg.alpha + lpeg.P("_") | ||
37 | local idRest = lpeg.alnum + lpeg.P("_") | ||
38 | |||
39 | local Keywords = lpeg.P("and") + "break" + "do" + "elseif" + "else" + "end" + | ||
40 | "false" + "for" + "function" + "goto" + "if" + "in" + | ||
41 | "local" + "nil" + "not" + "or" + "repeat" + "return" + | ||
42 | "then" + "true" + "until" + "while" | ||
43 | |||
44 | tllexer.Reserved = Keywords * -idRest | ||
45 | |||
46 | local Identifier = idStart * idRest^0 | ||
47 | |||
48 | tllexer.Name = -tllexer.Reserved * Identifier * -idRest | ||
49 | |||
50 | function tllexer.token (pat) | ||
51 | return pat * tllexer.Skip + updateffp() * lpeg.P(false) | ||
52 | end | ||
53 | |||
54 | function tllexer.symb (str) | ||
55 | return tllexer.token(lpeg.P(str)) | ||
56 | end | ||
57 | |||
58 | function tllexer.kw (str) | ||
59 | return tllexer.token(lpeg.P(str) * -idRest) | ||
60 | end | ||
61 | |||
62 | local Hex = (lpeg.P("0x") + lpeg.P("0X")) * lpeg.xdigit^1 | ||
63 | local Expo = lpeg.S("eE") * lpeg.S("+-")^-1 * lpeg.digit^1 | ||
64 | local Float = (((lpeg.digit^1 * lpeg.P(".") * lpeg.digit^0) + | ||
65 | (lpeg.P(".") * lpeg.digit^1)) * Expo^-1) + | ||
66 | (lpeg.digit^1 * Expo) | ||
67 | local Int = lpeg.digit^1 | ||
68 | |||
69 | tllexer.Number = Hex + Float + Int | ||
70 | |||
71 | local ShortString = lpeg.P('"') * | ||
72 | ((lpeg.P('\\') * lpeg.P(1)) + (lpeg.P(1) - lpeg.P('"')))^0 * | ||
73 | lpeg.P('"') + | ||
74 | lpeg.P("'") * | ||
75 | ((lpeg.P("\\") * lpeg.P(1)) + (lpeg.P(1) - lpeg.P("'")))^0 * | ||
76 | lpeg.P("'") | ||
77 | |||
78 | tllexer.String = LongString + ShortString | ||
79 | |||
80 | return tllexer | ||