aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/testLua/yueliang-0.4.1/nat-5.1.3/test/test_lparser_mk2.lua
blob: 11f2827a5afa9c1811df30c22d7c6e1a086403c5 (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
--[[--------------------------------------------------------------------

  test_lparser_mk2.lua
  Test for lparser_mk2.lua
  This file is part of Yueliang.

  Copyright (c) 2008 Kein-Hong Man <khman@users.sf.net>
  The COPYRIGHT file describes the conditions
  under which this software may be distributed.

  See the ChangeLog for more information.

----------------------------------------------------------------------]]

------------------------------------------------------------------------
-- test the whole kaboodle
------------------------------------------------------------------------

package.path = "../?.lua;"..package.path
local llex = require "llex_mk2"
local lparser = require "lparser_mk2"

------------------------------------------------------------------------
-- dump contents of log table
------------------------------------------------------------------------

local function dump_log(fs)
  local log = fs.log
  for i = 1, #log do
    print(log[i])
  end
end

------------------------------------------------------------------------
-- try 1
------------------------------------------------------------------------

--[=[
llex.init("local a = 1", "=string")
lparser.init(llex)
-- nothing is returned, so hope there is an error if problem occurs
local fs = lparser.parser()
dump_log(fs)
--]=]

------------------------------------------------------------------------
-- try 2
------------------------------------------------------------------------

-- a slightly larger sample
--[=[
llex.init([[
local a = 47
local b = "hello, world!"
print(a, b)
]], "@sample.lua")
lparser.init(llex)
-- nothing is returned, so hope there is an error if problem occurs
local fs = lparser.parser()
dump_log(fs)
--]=]

------------------------------------------------------------------------
-- automatic dumper of output log data
------------------------------------------------------------------------

local test_case = {
-- 1 attempts to exercise most parts, extent of coverage not known
[[
]],
-- 2
[[
-- foobar
]],
-- 3
[[
do
end
]],
-- 4
[[
do end
do end
]],
-- 5
[[
foo()
foo{}
foo""
foo:bar()
foo=false
foo.bar=true
foo[true]=nil
foo,bar=1,"a"
]],
-- 6
[[
foo=true
foo=false
foo=nil
foo=1.23e45
foo=-1
foo=(0)
foo=1+2
foo=1+2*3-4/5
]],
-- 7
[[
if foo then foo=1 end
if foo then foo=1 else foo=0 end
if foo then foo=1 elseif not foo then foo=0 end
]],
-- 8
[[
do return end
do return 123 end
do return "foo","bar" end
]],
-- 9
[[
while true do foo=not foo end
while foo~=42 do foo=foo-1 end
while true do break end
]],
-- 10
[[
repeat foo=foo.."bar" until false
repeat foo=foo/2 until foo<1
repeat break until false
]],
-- 11
[[
for i=1,10 do foo=i end
for i=1,10,2 do break end
for i in foo do bar=0 end
for i,j in foo,bar do baz=0 end
]],
-- 12
[[
local foo
local foo,bar,baz
local foo,bar="foo","bar"
]],
-- 13
[[
local function foo() return end
local function foo(a) return end
local function foo(x,y,z) return end
local function foo(x,...) return end
]],
-- 14
[[
function foo() return end
function foo(a) return end
function foo(x,y,z) return end
function foo(x,...) return end
]],
-- 15
[[
function foo.bar(p) return end
function foo.bar.baz(p) return end
function foo:bar(p) return end
function foo.bar.baz(p) return end
]],
-- 16
[[
foo = function() return end
foo = function(x,y) return end
foo = function(...) return end
foo = function(...) local bar = ... return end
]],
-- 17
[[
foo = {}
foo = { 1,2,3; "foo"; }
foo = { bar=77, baz=88, }
foo = { ["bar"]=77, ["baz"]=88, }
]],
-- 18 simple tests for variable management follows
[[
  print(a)
]],
-- 19
[[
  local a
  print(a)
]],
-- 20
[[
  do
    local a
    print(a)
  end
  print(a)
]],
-- 21
[[
  local a,b,c
  do
    local b
    print(b)
  end
  print(b)
]],
-- 22
[[
  local function foo() end
  bar = foo
]],
-- 23
[[
  do
    local function foo() end
    bar = foo
  end
  baz = foo
]],
-- 24
[[
  local foo
  local function bar()
    baz = nil
    foo = bar()
  end
  foo = bar
]],
-- 25
[[
  local foo
  local function bar()
    local function baz()
      local foo, bar
      foo = bar
      foo = baz
    end
    foo = bar
    foo = baz
  end
  foo = bar
  foo = baz
]],
-- 26
[[
  function foo:bar()
    print(self)
  end
]],
-- 27
[[
  function foo(...)
    print(arg)
  end
]],
-- 28
[[
  local c,d
  function foo(a,b,c)
    print(a,c,d,e)
  end
]],
-- 29
[[
  function foo(a,b)
    local bar = function(c,d)
      print(a,b,c,d)
    end
  end
]],
-- 30
[[
  for i = 1,10 do
    print(i)
  end
  for i = 1,10,-2 do
    print(i)
  end
]],
-- 31
[[
  for foo in bar() do
    print(foo)
  end
  for foo,bar,baz in spring() do
    print(foo,bar,baz)
  end
]],
}

-- helps to skip old stuff during development of snippets
local do_beg, do_end = 1, #test_case

-- loop for all example snippets
for i = do_beg, do_end do
  local fname = "parser_log/sample_"..string.format("%02d", i)..".lua"
  local src = test_case[i]
  local OUTF = io.open(fname, "wb")
  if not OUTF then error("failed to write to file '"..fname.."'") end
  -- write out actual source for comparison
  OUTF:write(
    "-- START OF SOURCE --\n"..
    src..
    "-- END OF SOURCE --\n"..
    "\n"
  )
  -- attempt to parse
  llex.init(src, "=string")
  lparser.init(llex)
  local fs = lparser.parser()
  -- grab logged messages and write
  local log = fs.log
  local indent = 0
  for i = 1, #log do
    local ln = log[i]
    -- handle indentation
    local tag = string.sub(ln, 1, 2)
    if tag == ">>" or tag == "<<" then
      ln = string.sub(ln, 4)
    end
    if tag == ">>" then
      indent = indent + 1
    end
    OUTF:write(string.rep("  ", indent)..ln.."\n")
    if tag == "<<" then
      indent = indent - 1
    end
  end
  -- we're done
  OUTF:close()
end