diff options
author | David Walter Seikel | 2014-04-21 20:59:39 +1000 |
---|---|---|
committer | David Walter Seikel | 2014-04-21 20:59:39 +1000 |
commit | 9621add2918cc4943e6693b74ae85d51dd264fcf (patch) | |
tree | fff1edf2c69d7a08a0e12885eecc9b96ed847a6a /LuaSL/testLua/yueliang-0.4.1/nat-5.0.3/llex_mk3.lua | |
parent | LuaSL_test's window doesn't need to be so huge. (diff) | |
download | SledjHamr-9621add2918cc4943e6693b74ae85d51dd264fcf.zip SledjHamr-9621add2918cc4943e6693b74ae85d51dd264fcf.tar.gz SledjHamr-9621add2918cc4943e6693b74ae85d51dd264fcf.tar.bz2 SledjHamr-9621add2918cc4943e6693b74ae85d51dd264fcf.tar.xz |
We don't need the testlua directory any more.
Diffstat (limited to '')
-rw-r--r-- | LuaSL/testLua/yueliang-0.4.1/nat-5.0.3/llex_mk3.lua | 266 |
1 files changed, 0 insertions, 266 deletions
diff --git a/LuaSL/testLua/yueliang-0.4.1/nat-5.0.3/llex_mk3.lua b/LuaSL/testLua/yueliang-0.4.1/nat-5.0.3/llex_mk3.lua deleted file mode 100644 index 1cd22b5..0000000 --- a/LuaSL/testLua/yueliang-0.4.1/nat-5.0.3/llex_mk3.lua +++ /dev/null | |||
@@ -1,266 +0,0 @@ | |||
1 | --[[-------------------------------------------------------------------- | ||
2 | |||
3 | llex.lua | ||
4 | Lua 5 lexical analyzer in Lua | ||
5 | This file is part of Yueliang. | ||
6 | |||
7 | Copyright (c) 2006 Kein-Hong Man <khman@users.sf.net> | ||
8 | The COPYRIGHT file describes the conditions | ||
9 | under which this software may be distributed. | ||
10 | |||
11 | See the ChangeLog for more information. | ||
12 | |||
13 | ----------------------------------------------------------------------]] | ||
14 | |||
15 | --[[-------------------------------------------------------------------- | ||
16 | -- Notes: | ||
17 | -- * takes in the entire source at once | ||
18 | -- * code is heavily optimized for size | ||
19 | -- | ||
20 | -- local lex_init = require("llex.lua") | ||
21 | -- local llex = lex_init(z, source) | ||
22 | -- llex:chunkid() | ||
23 | -- * returns formatted name of chunk id | ||
24 | -- llex:errorline(s, line) | ||
25 | -- * throws an error with a formatted message | ||
26 | -- llex:lex() | ||
27 | -- * returns next lexical element (token, seminfo) | ||
28 | -- llex.ln | ||
29 | -- * line number | ||
30 | ----------------------------------------------------------------------]] | ||
31 | |||
32 | return | ||
33 | function(z, source) | ||
34 | -------------------------------------------------------------------- | ||
35 | -- initialize variables | ||
36 | -- * I is the upvalue, i is the local version for space/efficiency | ||
37 | -------------------------------------------------------------------- | ||
38 | local string = string | ||
39 | local find, sub = string.find, string.sub | ||
40 | local EOF = "<eof>" | ||
41 | local luaX = { ln = 1 } | ||
42 | local I = 1 | ||
43 | -------------------------------------------------------------------- | ||
44 | -- initialize keyword list | ||
45 | -------------------------------------------------------------------- | ||
46 | local kw = {} | ||
47 | for v in string.gfind([[ | ||
48 | and break do else elseif end false for function if in | ||
49 | local nil not or repeat return then true until while]], "%S+") do | ||
50 | kw[v] = true | ||
51 | end | ||
52 | -------------------------------------------------------------------- | ||
53 | -- returns a chunk name or id | ||
54 | -------------------------------------------------------------------- | ||
55 | function luaX:chunkid() | ||
56 | if find(source, "^[=@]") then | ||
57 | return sub(source, 2) -- remove first char | ||
58 | end | ||
59 | return "[string]" | ||
60 | end | ||
61 | -------------------------------------------------------------------- | ||
62 | -- formats error message and throws error | ||
63 | -- * a simplified version, does not report what token was responsible | ||
64 | -------------------------------------------------------------------- | ||
65 | function luaX:errorline(s, line) | ||
66 | error(string.format("%s:%d: %s", self:chunkid(), line or self.ln, s)) | ||
67 | end | ||
68 | ---------------------------------------------------------------------- | ||
69 | -- reads a long string or long comment | ||
70 | ---------------------------------------------------------------------- | ||
71 | local function read_long(i, is_str) | ||
72 | local luaX = luaX | ||
73 | local string = string | ||
74 | local cont = 1 | ||
75 | if sub(z, i, i) == "\n" then | ||
76 | i = i + 1 | ||
77 | luaX.ln = luaX.ln + 1 | ||
78 | end | ||
79 | local j = i | ||
80 | while true do | ||
81 | local p, q, r = find(z, "([\n%[%]])", i) -- (long range) | ||
82 | if not p then | ||
83 | luaX:errorline(is_str and "unfinished long string" or | ||
84 | "unfinished long comment") | ||
85 | end | ||
86 | i = p + 1 | ||
87 | if r == "\n" then | ||
88 | luaX.ln = luaX.ln + 1 | ||
89 | elseif sub(z, i, i) == r then -- only [[ or ]] | ||
90 | i = i + 1 | ||
91 | if r == "[" then | ||
92 | cont = cont + 1 | ||
93 | else-- r == "]" then | ||
94 | if cont == 1 then break end -- last ]] found | ||
95 | cont = cont - 1 | ||
96 | end | ||
97 | end | ||
98 | end--while | ||
99 | I = i | ||
100 | return sub(z, j, i - 3) | ||
101 | end | ||
102 | ---------------------------------------------------------------------- | ||
103 | -- reads a string | ||
104 | ---------------------------------------------------------------------- | ||
105 | local function read_string(i, del) | ||
106 | local luaX = luaX | ||
107 | local string = string | ||
108 | local buff = "" | ||
109 | while true do | ||
110 | local p, q, r = find(z, "([\n\\\"\'])", i) -- (long range) | ||
111 | if p then | ||
112 | if r == "\n" then | ||
113 | luaX:errorline("unfinished string") | ||
114 | end | ||
115 | buff = buff..sub(z, i, p - 1) -- normal portions | ||
116 | i = p | ||
117 | if r == "\\" then -- handle escapes | ||
118 | i = i + 1 | ||
119 | r = sub(z, i, i) | ||
120 | if r == "" then break end -- (error) | ||
121 | p = find("\nabfnrtv", r, 1, 1) | ||
122 | ------------------------------------------------------ | ||
123 | if p then -- special escapes | ||
124 | r = sub("\n\a\b\f\n\r\t\v", p, p) | ||
125 | if p == 1 then luaX.ln = luaX.ln + 1 end | ||
126 | i = i + 1 | ||
127 | ------------------------------------------------------ | ||
128 | elseif find(r, "%D") then -- other non-digits | ||
129 | i = i + 1 | ||
130 | ------------------------------------------------------ | ||
131 | else -- \xxx sequence | ||
132 | local p, q, s = find(z, "^(%d%d?%d?)", i) | ||
133 | i = q + 1 | ||
134 | if s + 1 > 256 then -- UCHAR_MAX | ||
135 | luaX:errorline("escape sequence too large") | ||
136 | end | ||
137 | r = string.char(s) | ||
138 | ------------------------------------------------------ | ||
139 | end--if p | ||
140 | else | ||
141 | i = i + 1 | ||
142 | if r == del then | ||
143 | I = i | ||
144 | return buff -- ending delimiter | ||
145 | end | ||
146 | end--if r | ||
147 | buff = buff..r | ||
148 | else | ||
149 | break -- (error) | ||
150 | end--if p | ||
151 | end--while | ||
152 | luaX:errorline("unfinished string") | ||
153 | end | ||
154 | ---------------------------------------------------------------------- | ||
155 | -- main lexer function | ||
156 | ---------------------------------------------------------------------- | ||
157 | function luaX:lex() | ||
158 | local string = string | ||
159 | local find, len = find, string.len | ||
160 | while true do--outer | ||
161 | local i = I | ||
162 | -- inner loop allows break to be used to nicely section tests | ||
163 | while true do--inner | ||
164 | ---------------------------------------------------------------- | ||
165 | local p, _, r = find(z, "^([_%a][_%w]*)", i) | ||
166 | if p then | ||
167 | I = i + len(r) | ||
168 | if kw[r] then return r end -- keyword | ||
169 | return "<name>", r -- identifier | ||
170 | end | ||
171 | ---------------------------------------------------------------- | ||
172 | local p, q, r = find(z, "^(%.?)%d", i) | ||
173 | if p then -- numeral | ||
174 | if r == "." then i = i + 1 end | ||
175 | local _, n, r, s = find(z, "^%d*(%.?%.?)%d*([eE]?)", i) | ||
176 | q = n | ||
177 | i = q + 1 | ||
178 | if len(r) == 2 then | ||
179 | self:errorline("ambiguous syntax (dots follows digits)") | ||
180 | end | ||
181 | if len(s) == 1 then -- optional exponent | ||
182 | local _, n = find(z, "^[%+%-]?%d*", i) -- optional sign | ||
183 | q = n | ||
184 | i = q + 1 | ||
185 | end | ||
186 | r = tonumber(sub(z, p, q)) | ||
187 | I = i | ||
188 | if not r then self:errorline("malformed number") end | ||
189 | return "<number>", r | ||
190 | end | ||
191 | ---------------------------------------------------------------- | ||
192 | local p, q, r = find(z, "^(%s)[ \t]*", i) | ||
193 | if p then | ||
194 | if r == "\n" then -- newline | ||
195 | self.ln = self.ln + 1 | ||
196 | I = i + 1 | ||
197 | else | ||
198 | I = q + 1 -- whitespace | ||
199 | end | ||
200 | break -- (continue) | ||
201 | end | ||
202 | ---------------------------------------------------------------- | ||
203 | local p, _, r = find(z, "^(%p)", i) -- symbols/punctuation | ||
204 | if p then | ||
205 | local q = find("-[\"\'.=<>~", r, 1, 1) | ||
206 | if q then -- further processing for more complex symbols | ||
207 | ---------------------------------------------------- | ||
208 | if q <= 2 then | ||
209 | if q == 1 then -- minus | ||
210 | if find(z, "^%-%-", i) then | ||
211 | i = i + 2 | ||
212 | if find(z, "^%[%[", i) then -- long comment | ||
213 | read_long(i + 2) | ||
214 | else -- short comment | ||
215 | I = find(z, "\n", i) or (len(z) + 1) | ||
216 | end | ||
217 | break -- (continue) | ||
218 | end | ||
219 | -- (fall through for "-") | ||
220 | elseif q == 2 then -- [ or long string | ||
221 | if find(z, "^%[%[", i) then | ||
222 | return "<string>", read_long(i + 2, true) | ||
223 | end | ||
224 | -- (fall through for "[") | ||
225 | end | ||
226 | ---------------------------------------------------- | ||
227 | elseif q <= 5 then | ||
228 | if q < 5 then -- strings | ||
229 | return "<string>", read_string(i + 1, r) | ||
230 | end | ||
231 | local _, _, s = find(z, "^(%.%.?%.?)", i) -- dots | ||
232 | r = s | ||
233 | -- (fall through) | ||
234 | ---------------------------------------------------- | ||
235 | else -- relational/logic | ||
236 | local _, _, s = find(z, "^(%p=?)", i) | ||
237 | r = s | ||
238 | -- (fall through) | ||
239 | end | ||
240 | end | ||
241 | I = i + len(r); return r -- for other symbols, fall through | ||
242 | end | ||
243 | ---------------------------------------------------------------- | ||
244 | local r = sub(z, i, i) | ||
245 | if r ~= "" then | ||
246 | if find(r, "%c") then -- invalid control char | ||
247 | self:errorline("invalid control char("..string.byte(r)..")") | ||
248 | end | ||
249 | I = i + 1; return r -- other single-char tokens | ||
250 | end | ||
251 | return EOF -- end of stream | ||
252 | ---------------------------------------------------------------- | ||
253 | end--while inner | ||
254 | end--while outer | ||
255 | end | ||
256 | -------------------------------------------------------------------- | ||
257 | -- initial processing (shbang handling) | ||
258 | -------------------------------------------------------------------- | ||
259 | local p, q, r = find(z, "^#[^\n]*(\n?)") | ||
260 | if p then -- skip first line | ||
261 | I = q + 1 | ||
262 | if r == "\n" then luaX.ln = luaX.ln + 1 end | ||
263 | end | ||
264 | return luaX | ||
265 | -------------------------------------------------------------------- | ||
266 | end | ||