Skip to content

Commit 5d8600f

Browse files
committed
Fixed 'ambiguous syntax' detection.
Added more macro tests.
1 parent 8ea2f48 commit 5d8600f

File tree

3 files changed

+106
-84
lines changed

3 files changed

+106
-84
lines changed

preprocess.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1942,7 +1942,7 @@ local function doLateExpansionsResources(tokensToExpand, fileBuffers, params, st
19421942

19431943
if isTokenAndNotNil(tokNext, "punctuation", "(") then
19441944
-- Apply the same 'ambiguous syntax' rule as Lua.
1945-
if isLastToken(tokenStack, "whitespace") and tokenStack[#tokenStack].value:find"\n" then
1945+
if isTokenAndNotNil(tokenStack[iNext+1], "whitespace") and tokenStack[iNext+1].value:find"\n" then
19461946
errorAtToken(fileBuffers, tokNext, nil, "Macro", "Ambiguous syntax near '(' - part of macro, or new statement?")
19471947
end
19481948
elseif not (isTokenAndNotNil(tokNext, "string") or isTokenAndNotNil(tokNext, "punctuation", "{")) then

tests/quickTest.lua2p

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -129,54 +129,10 @@ local f = @@PASS_THROUGH(function(a, b)
129129
return "", nil
130130
end)
131131

132-
local x = @@PASS_THROUGH( @@"tests/quickTest_expression.txt" )
133-
134-
local x1 = @@PASS_THROUGH( 1 + !!( "2" ) + 3 )
135-
local x2 = @@PASS_THROUGH{ 1 + !!( "2" ) + 3 }
136-
local y1 = @@PASS_THROUGH( 1 + !( 2 ) + 3 )
137-
local y2 = @@PASS_THROUGH{ 1 + !( 2 ) + 3 }
138-
139-
local a1 = @@PASS_THROUGH( !(1) )
140-
local a2 = @@PASS_THROUGH{ !(1) }
141-
local b1 = @@PASS_THROUGH( !(1) + 2 )
142-
local b2 = @@PASS_THROUGH{ !(1) + 2 }
143-
local c1 = @@PASS_THROUGH( 1 + !(2) )
144-
local c2 = @@PASS_THROUGH{ 1 + !(2) }
145-
local d1 = @@PASS_THROUGH( 1 + !(2) + 3 )
146-
local d2 = @@PASS_THROUGH{ 1 + !(2) + 3 }
147-
148-
local m1 = @@PASS_THROUGH( !!("1")!!("+")!!("2") )
149-
local m2 = @@PASS_THROUGH{ !!("1")!!("+")!!("2") }
150-
151-
local nest_x1 = @@PASS_THROUGH(@@PASS_THROUGH(@@PASS_THROUGH( 1 + !!( "2" ) + 3 )))
152-
local nest_x2 = @@PASS_THROUGH{@@PASS_THROUGH{@@PASS_THROUGH{ 1 + !!( "2" ) + 3 }}}
153-
local nest_y1 = @@PASS_THROUGH(@@PASS_THROUGH(@@PASS_THROUGH( 1 + !( 2 ) + 3 )))
154-
local nest_y2 = @@PASS_THROUGH{@@PASS_THROUGH{@@PASS_THROUGH{ 1 + !( 2 ) + 3 }}}
155-
156-
local nest_a1 = @@PASS_THROUGH(@@PASS_THROUGH(@@PASS_THROUGH( !(1) )))
157-
local nest_a2 = @@PASS_THROUGH{@@PASS_THROUGH{@@PASS_THROUGH{ !(1) }}}
158-
local nest_b1 = @@PASS_THROUGH(@@PASS_THROUGH(@@PASS_THROUGH( !(1) + 2 )))
159-
local nest_b2 = @@PASS_THROUGH{@@PASS_THROUGH{@@PASS_THROUGH{ !(1) + 2 }}}
160-
local nest_c1 = @@PASS_THROUGH(@@PASS_THROUGH(@@PASS_THROUGH( 1 + !(2) )))
161-
local nest_c2 = @@PASS_THROUGH{@@PASS_THROUGH{@@PASS_THROUGH{ 1 + !(2) }}}
162-
local nest_d1 = @@PASS_THROUGH(@@PASS_THROUGH(@@PASS_THROUGH( 1 + !(2) + 3 )))
163-
local nest_d2 = @@PASS_THROUGH{@@PASS_THROUGH{@@PASS_THROUGH{ 1 + !(2) + 3 }}}
164-
165-
local nest_m1 = @@PASS_THROUGH(@@PASS_THROUGH(@@PASS_THROUGH( !!("1")!!("+")!!("2") )))
166-
local nest_m2 = @@PASS_THROUGH{@@PASS_THROUGH{@@PASS_THROUGH{ !!("1")!!("+")!!("2") }}}
167-
168-
-- local no = @@PASS_THROUGH( @@654 ) -- Error: Bad token after @insert.
169-
-- local no = @@PASS_THROUGH{ @@654 } -- Error: Bad token after @insert.
170-
-- local no = @@PASS_THROUGH( @@foo ) -- Error: Bad token after macro name.
171-
-- local no = @@PASS_THROUGH{ @@foo } -- Error: Bad token after macro name.
172-
-- local no = @@PASS_THROUGH( !() ) -- Error: Invalid expression.
173-
-- local no = @@PASS_THROUGH{ !() } -- Error: Invalid expression.
174-
-- local no = @@PASS_THROUGH( !!() ) -- Error: Invalid expression.
175-
-- local no = @@PASS_THROUGH{ !!() } -- Error: Invalid expression.
176-
-- local no = @@PASS_THROUGH( !!( 1 ) ) -- Error: Value is not Lua code.
177-
-- local no = @@PASS_THROUGH{ !!( 1 ) } -- Error: Value is not Lua code.
178-
-- local no = @@PASS_THROUGH( !!( !(3) ) ) -- Error: Nested code block.
179-
-- local no = @@PASS_THROUGH{ !!( !(3) ) } -- Error: Nested code block.
132+
local a = @@PASS_THROUGH( @@"tests/quickTest_expression.txt" )
133+
local b = @@PASS_THROUGH( !!("2") )
134+
local c = @@PASS_THROUGH{ 1 + !(2) + 3 }
135+
local d = @@PASS_THROUGH(@@PASS_THROUGH{@@PASS_THROUGH( !!("1")!!("+")!!("2") )})
180136

181137

182138

tests/suite.lua

Lines changed: 101 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
io.stdout:setvbuf("no")
66
io.stderr:setvbuf("no")
77

8+
local ppChunk = assert(loadfile"preprocess.lua")
89
local results = {}
910

1011
local function doTest(description, f, ...)
@@ -59,7 +60,7 @@ end
5960
addLabel("Preprocessor code")
6061

6162
doTest("Inline block with simple expression", function()
62-
local pp = assert(loadfile"preprocess.lua")()
63+
local pp = ppChunk()
6364
local luaIn = [[
6465
local x = !(1+2*3)
6566
]]
@@ -69,7 +70,7 @@ doTest("Inline block with simple expression", function()
6970
end)
7071

7172
doTest("Static branch", function()
72-
local pp = assert(loadfile"preprocess.lua")()
73+
local pp = ppChunk()
7374
local luaIn = [[
7475
!if FLAG then
7576
print("Yes")
@@ -88,7 +89,7 @@ doTest("Static branch", function()
8889
end)
8990

9091
doTest("Output value from metaprogram", function()
91-
local pp = assert(loadfile"preprocess.lua")()
92+
local pp = ppChunk()
9293
local luaIn = [[
9394
!local t = {
9495
z = 99,
@@ -103,7 +104,7 @@ doTest("Output value from metaprogram", function()
103104
end)
104105

105106
doTest("Generate code", function()
106-
local pp = assert(loadfile"preprocess.lua")()
107+
local pp = ppChunk()
107108
local luaIn = [[
108109
!(
109110
outputLua("local s = ")
@@ -116,7 +117,7 @@ doTest("Generate code", function()
116117
end)
117118

118119
doTest("Parsing extended preprocessor line", function()
119-
local pp = assert(loadfile"preprocess.lua")()
120+
local pp = ppChunk()
120121
local luaIn = [[
121122
!local str = "foo\
122123
"; local arr = {
@@ -136,7 +137,7 @@ doTest("Parsing extended preprocessor line", function()
136137
end)
137138

138139
doTest("Dual code", function()
139-
local pp = assert(loadfile"preprocess.lua")()
140+
local pp = ppChunk()
140141

141142
local luaOut = assert(pp.processString{ code=[[
142143
!local one = 1
@@ -152,7 +153,7 @@ doTest("Dual code", function()
152153
end)
153154

154155
doTest("Expression or not?", function()
155-
local pp = assert(loadfile"preprocess.lua")()
156+
local pp = ppChunk()
156157

157158
local luaOut = assert(pp.processString{ code=[[
158159
foo(!( math.floor(1.5) ))
@@ -171,7 +172,7 @@ doTest("Expression or not?", function()
171172
end)
172173

173174
doTest("Output values of different types", function()
174-
local pp = assert(loadfile"preprocess.lua")()
175+
local pp = ppChunk()
175176

176177
-- Valid: Numbers, strings, tables, booleans, nil.
177178

@@ -195,7 +196,7 @@ doTest("Output values of different types", function()
195196
end)
196197

197198
doTest("Preprocessor keywords", function()
198-
local pp = assert(loadfile"preprocess.lua")()
199+
local pp = ppChunk()
199200

200201
local luaOut = assert(pp.processString{ code=[[ filename = @file ]]})
201202
assertCodeOutput(luaOut, [[filename = "<code>"]]) -- Note: The dummy value when we have no real path may change in the future.
@@ -207,68 +208,133 @@ doTest("Preprocessor keywords", function()
207208
assertCodeOutput(luaOut, [[lnStr = "<code>".. 1 .. 1 .."<code>"]])
208209

209210
local luaOut = assert(pp.processString{
210-
code = [[
211-
v = @insert "foo"
212-
]],
213-
onInsert = function(name)
214-
return '"bar"'
215-
end,
211+
code = [[ v = @insert "foo" ]],
212+
onInsert = function(name) return name end,
216213
})
217-
assertCodeOutput(luaOut, [[v = "bar"]])
214+
assertCodeOutput(luaOut, [[v = foo]])
215+
216+
local luaOut = assert(pp.processString{
217+
code = [[ v = @@"foo" ]],
218+
onInsert = function(name) return name end,
219+
})
220+
assertCodeOutput(luaOut, [[v = foo]])
221+
222+
-- Invalid: Bad keyword.
223+
assert(not pp.processString{ code=[[ @bad ]]})
224+
225+
-- Invalid: Bad insert value.
226+
assert(not pp.processString{ code=[[ @insert 1 ]]})
227+
assert(not pp.processString{ code=[[ @insert {} ]]})
228+
assert(not pp.processString{ code=[[ @insert (1) ]]})
229+
assert(not pp.processString{ code=[[ @insert nil ]]})
230+
end)
231+
232+
doTest("Macros", function()
233+
local pp = ppChunk()
218234

219235
local luaOut = assert(pp.processString{ code=[[
220-
!function join(ident1, ident2) return ident1..ident2 end
221-
v = @insert join(foo, bar)
236+
!function JOIN(ident1, ident2) return ident1..ident2 end
237+
v = @insert JOIN(foo, bar)
222238
]]})
223239
assertCodeOutput(luaOut, [[v = foobar]])
224240

225241
local luaOut = assert(pp.processString{ code=[[
226-
!function echo(v) return v end
227-
s = @insert echo""
242+
!function JOIN(ident1, ident2) return ident1..ident2 end
243+
v = @@JOIN(foo, bar)
244+
]]})
245+
assertCodeOutput(luaOut, [[v = foobar]])
246+
247+
-- Macro variants.
248+
local luaOut = assert(pp.processString{ code=[[
249+
!function ECHO(v) return v end
250+
s = @@ECHO""
228251
]]})
229252
assertCodeOutput(luaOut, [[s = ""]])
230253

231254
local luaOut = assert(pp.processString{ backtickStrings=true, code=[[
232-
!function echo(v) return v end
233-
s = @insert echo``
255+
!function ECHO(v) return v end
256+
s = @@ECHO``
234257
]]})
235258
assertCodeOutput(luaOut, [[s = ""]])
236259

237260
local luaOut = assert(pp.processString{ code=[[
238-
!function echo(v) return v end
239-
t = @insert echo{}
261+
!function ECHO(v) return v end
262+
t = @@ECHO{}
240263
]]})
241264
assertCodeOutput(luaOut, [[t = {}]])
242265

266+
-- Function as an argument.
243267
local luaOut = assert(pp.processString{ code=[[
244-
!function echo(v) return v end
245-
f = @insert echo(function() return a,b end)
268+
!function ECHO(v) return v end
269+
f = @@ECHO(function() return a,b end)
246270
]]})
247271
assertCodeOutput(luaOut, [[f = function() return a,b end]])
248272

249273
local luaOut = assert(pp.processString{ backtickStrings=true, code=[[
250-
!function echo(v) return v end
251-
f = @insert echo(function() return a,`b` end)
274+
!function ECHO(v) return v end
275+
f = @@ECHO(function() return a,`b` end)
252276
]]})
253277
assertCodeOutput(luaOut, [[f = function() return a,"b" end]])
254278

255-
-- Invalid: Bad keyword.
256-
assert(not pp.processString{ code=[[ @bad ]]})
279+
-- Nested macros.
280+
local luaOut = assert(pp.processString{ code=[[
281+
!function DOUBLE(ident) return ident.."_"..ident end
282+
v = @@DOUBLE(@@DOUBLE(@@DOUBLE(woof)))
283+
]]})
284+
assertCodeOutput(luaOut, [[v = woof_woof_woof_woof_woof_woof_woof_woof]])
285+
286+
-- Code blocks in macros.
287+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO( !( 1 ) ) ]]}), [[n = 1]] )
288+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO( !!("1") ) ]]}), [[n = 1]] )
289+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO{ !( 1 ) } ]]}), [[n = { 1 }]] )
290+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO{ !!("1") } ]]}), [[n = { 1 }]] )
291+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO( !( 1 ) + 2 ) ]]}), [[n = 1 + 2]] )
292+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO( !!("1") + 2 ) ]]}), [[n = 1 + 2]] )
293+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO{ !( 1 ) + 2 } ]]}), [[n = { 1 + 2 }]] )
294+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO{ !!("1") + 2 } ]]}), [[n = { 1 + 2 }]] )
295+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO( 1 + !( 2 ) ) ]]}), [[n = 1 + 2]] )
296+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO( 1 + !!("2") ) ]]}), [[n = 1 + 2]] )
297+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO{ 1 + !( 2 ) } ]]}), [[n = { 1 + 2 }]] )
298+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO{ 1 + !!("2") } ]]}), [[n = { 1 + 2 }]] )
299+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO( 1 + !( 2 ) + 3 ) ]]}), [[n = 1 + 2 + 3]] )
300+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO( 1 + !!("2") + 3 ) ]]}), [[n = 1 + 2 + 3]] )
301+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO{ 1 + !( 2 ) + 3 } ]]}), [[n = { 1 + 2 + 3 }]])
302+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO{ 1 + !!("2") + 3 } ]]}), [[n = { 1 + 2 + 3 }]])
303+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO( !!("1")!!("+")!!("2") ) ]]}), [[n = 1+2]] )
304+
assertCodeOutput(assert(pp.processString{ code=[[ !(function ECHO(v) return v end) n = @@ECHO{ !!("1")!!("+")!!("2") } ]]}), [[n = { 1+2 }]] )
257305

258306
-- Invalid: Ambiguous syntax.
259307
assert(not pp.processString{ code=[[
260-
!function void() return "" end
261-
v = @insert void
262-
()
308+
!function VOID() return "" end
309+
v = @@VOID
310+
() 1
263311
]]})
312+
313+
-- Invalid: Bad macro arguments format.
314+
assert(not pp.processString{ code=[[ @insert type[] ]]})
315+
assert(not pp.processString{ code=[[ @insert type + 1 ]]})
316+
317+
-- Invalid: Non-expression code block in macro.
318+
assert(not pp.processString{ code=[[ !(function ECHO(v) return v end) v = @@ECHO(!(do end)) ]]})
319+
assert(not pp.processString{ code=[[ !(function ECHO(v) return v end) v = @@ECHO{!(do end)} ]]})
320+
assert(not pp.processString{ code=[[ !(function ECHO(v) return v end) v = @@ECHO(!( )) ]]})
321+
assert(not pp.processString{ code=[[ !(function ECHO(v) return v end) v = @@ECHO{!( )} ]]})
322+
assert(not pp.processString{ code=[[ !(function ECHO(v) return v end) v = @@ECHO(!!( )) ]]})
323+
assert(not pp.processString{ code=[[ !(function ECHO(v) return v end) v = @@ECHO{!!( )} ]]})
324+
325+
-- Invalid: Invalid value from code block in macro.
326+
assert(not pp.processString{ code=[[ !(function ECHO(v) return v end) v = @@ECHO(!!(1)) ]]})
327+
328+
-- Invalid: Nested code block in macro.
329+
assert(not pp.processString{ code=[[ !(function ECHO(v) return v end) v = @@ECHO( !!( !(1) ) ) ]]})
264330
end)
265331

266332

267333

268334
addLabel("Library API")
269335

270336
doTest("Get useful tokens", function()
271-
local pp = assert(loadfile"preprocess.lua")()
337+
local pp = ppChunk()
272338
local tokens = pp.tokenize[[local x = 5 -- Foo!]]
273339

274340
pp.removeUselessTokens(tokens)
@@ -285,7 +351,7 @@ doTest("Get useful tokens", function()
285351
end)
286352

287353
doTest("Serialize", function()
288-
local pp = assert(loadfile"preprocess.lua")()
354+
local pp = ppChunk()
289355

290356
local t = {
291357
z = 99,

0 commit comments

Comments
 (0)