Full cratera support

This commit is contained in:
SoniEx2 2019-07-30 23:37:42 -03:00
parent 4b365cdab1
commit 5aa22fbbb1
2 changed files with 52 additions and 21 deletions

View File

@ -160,14 +160,8 @@ defs[parser.FALLBACK] = function(state, token)
-- figure out whether we're in funcname -- figure out whether we're in funcname
local i = results.n - 1 -- skip the ':' local i = results.n - 1 -- skip the ':'
local find_statement = true local find_statement = true
--while is_tk(results, i, '\n') do -- ignore newlines
-- i = i - 1
--end
i = ignore_newlines(results, i) i = ignore_newlines(results, i)
while results[i-1] == TK.NAME do while results[i-1] == TK.NAME do
--while is_tk(results, i-2, '\n') do -- ignore newlines
-- i = i - 1
--end
i = ignore_newlines(results, i-2) + 2 i = ignore_newlines(results, i-2) + 2
if is_tk(results, i-2, '.') then if is_tk(results, i-2, '.') then
-- keep going -- keep going
@ -194,9 +188,6 @@ defs[parser.FALLBACK] = function(state, token)
end end
if find_statement then if find_statement then
while true do while true do
--while is_tk(results, i, '\n') do -- ignore newlines
-- i = i - 1
--end
i = ignore_newlines(results, i) i = ignore_newlines(results, i)
if is_tk(results, i, ')') then if is_tk(results, i, ')') then
-- (prefixexp) or (funcargs) -- (prefixexp) or (funcargs)
@ -208,6 +199,8 @@ defs[parser.FALLBACK] = function(state, token)
depth = depth - 1 depth = depth - 1
elseif is_tk(results, i, ')') then elseif is_tk(results, i, ')') then
depth = depth + 1 depth = depth + 1
elseif not results[i] then
error("syntax error (unbalanced '()')")
end end
until depth == 0 until depth == 0
elseif is_tk(results, i, ']') then elseif is_tk(results, i, ']') then
@ -220,6 +213,8 @@ defs[parser.FALLBACK] = function(state, token)
depth = depth - 1 depth = depth - 1
elseif is_tk(results, i, ']') then elseif is_tk(results, i, ']') then
depth = depth + 1 depth = depth + 1
elseif not results[i] then
error("syntax error (unbalanced '[]')")
end end
until depth == 0 until depth == 0
elseif results[i-1] == TK.NAME then elseif results[i-1] == TK.NAME then
@ -243,6 +238,8 @@ defs[parser.FALLBACK] = function(state, token)
depth = depth - 1 depth = depth - 1
elseif is_tk(results, newi, ']') then elseif is_tk(results, newi, ']') then
depth = depth + 1 depth = depth + 1
elseif not results[i] then
error("syntax error (unbalanced '{}')")
end end
until depth == 0 until depth == 0
local checki = ignore_newlines(results, newi-1) local checki = ignore_newlines(results, newi-1)
@ -328,7 +325,6 @@ defs[parser.FALLBACK] = function(state, token)
local tk_mytrait = i-1 local tk_mytrait = i-1
i = ignore_newlines(results, i-2) i = ignore_newlines(results, i-2)
if results[i-1] == END_OF_STMT then if results[i-1] == END_OF_STMT then
assert(token == '(', "unimplemented")
-- definitely cratera (stmt ':' Name '.' Name '(') -- definitely cratera (stmt ':' Name '.' Name '(')
-- convert into '(' stmt ',' String ',' String -- convert into '(' stmt ',' String ',' String
-- convert names into strings -- convert names into strings
@ -344,13 +340,14 @@ defs[parser.FALLBACK] = function(state, token)
depth = depth - 1 depth = depth - 1
elseif is_tk(results, i, ']') then elseif is_tk(results, i, ']') then
depth = depth + 1 depth = depth + 1
elseif not results[i] then
error("syntax error (unbalanced '[]')")
end end
until depth == 0 until depth == 0
local tk_left = i local tk_left = i
i = ignore_newlines(results, i-1) i = ignore_newlines(results, i-1)
if results[i-1] == END_OF_STMT then if results[i-1] == END_OF_STMT then
assert(token == '(', "unimplemented") -- definitely cratera (stmt ':' '[' exp ']' '.' Name '(')
-- definitely cratera (':' '[' exp ']' '.' Name '(')
-- convert into '(' stmt ',' '(' exp ')' ',' String -- convert into '(' stmt ',' '(' exp ')' ',' String
-- replace '[' and ']' -- replace '[' and ']'
results[tk_right] = ')' results[tk_right] = ')'
@ -359,6 +356,7 @@ defs[parser.FALLBACK] = function(state, token)
end -- else not cratera end -- else not cratera
end end
if inject_cratera then if inject_cratera then
--assert(token == '(', "unimplemented")
-- convert name into string -- convert name into string
results[tk_myfunction] = TK.STRING results[tk_myfunction] = TK.STRING
-- replace '.' with ',' -- replace '.' with ','
@ -370,8 +368,16 @@ defs[parser.FALLBACK] = function(state, token)
results.n = results.n - 2 results.n = results.n - 2
-- replace ':' with ',' -- replace ':' with ','
results[ignore_newlines(results, i-2)] = ',' results[ignore_newlines(results, i-2)] = ','
-- replace START_OF_STMT with '(', and '(' with ',' -- replace START_OF_STMT with '('
results[pos], results[results.n] = '(', ',' results[pos] = '('
if token == '(' then
-- replace '(' with ','
results[results.n] = ','
else
-- insert ',' before argument
table.insert(results, results.n, ',')
results.n = results.n + 1
end
-- inject cratera -- inject cratera
table.insert(results, pos, ')') table.insert(results, pos, ')')
table.insert(results, pos, CRATERA_FUNCTION) table.insert(results, pos, CRATERA_FUNCTION)
@ -386,19 +392,38 @@ defs[parser.FALLBACK] = function(state, token)
results.n = results.n + 1 results.n = results.n + 1
end end
results.n = results.n + 3 results.n = results.n + 3
-- tag it for '(' ')' (no argument) calls -- tag it so we know to insert a ')' to close our '('
-- and to handle '(' ')' (no argument) calls
-- we add the tag before TK.STRING/'{'/','
table.insert(results, results.n, END_OF_CRATERA)
results.n = results.n + 1 results.n = results.n + 1
results[results.n] = END_OF_CRATERA
end end
end -- else not cratera end -- else not cratera
end end
elseif token == '}' then elseif token == '}' then
-- TODO unimplemented local i = results.n -- we'll be subtracting anyway, see below
local depth = 1
repeat
i = i - 1
if is_tk(results, i, '{') then
depth = depth - 1
elseif is_tk(results, i, '}') then
depth = depth + 1
elseif not results[i] then
error("syntax error (unbalanced '{}')")
end
until depth == 0
assert(is_tk(results, i, '{'))
if results[i-1] == END_OF_CRATERA then
-- need to add ')' to close our '('
table.remove(results, i-1)
results[results.n] = ')'
end
elseif token == ')' then elseif token == ')' then
local i = results.n - 1 -- skip the ')' local i = results.n - 1 -- skip the ')'
i = ignore_newlines(results, i) i = ignore_newlines(results, i)
if results[i] == END_OF_CRATERA then if results[i] == ',' and results[i-1] == END_OF_CRATERA then
-- '(' CRATERA_FUNCTION ')' '(' something ',' END_OF_CRATERA ')' -- '(' CRATERA_FUNCTION ')' '(' something END_OF_CRATERA ',' ')'
-- need to fix it up into -- need to fix it up into
-- '(' CRATERA_FUNCTION ')' '(' something ')' -- '(' CRATERA_FUNCTION ')' '(' something ')'
table.remove(results, i-1) table.remove(results, i-1)
@ -419,11 +444,17 @@ defs[parser.FALLBACK] = function(state, token)
results.n = results.n - 1 results.n = results.n - 1
break break
elseif not results[i] then elseif not results[i] then
error("syntax error") error("syntax error (unbalanced '()')")
end end
until depth == 0 until depth == 0
end end
end end
else -- we skipped a string literal
if results[results.n-1] == TK.STRING and results[results.n-2] == END_OF_CRATERA then
-- need to add ')' to close our '('
table.remove(results, results.n-2)
results[results.n] = ')'
end
end end
results.skip = EXTRA_DATA[token] results.skip = EXTRA_DATA[token]
return SELF return SELF

View File

@ -8,7 +8,7 @@ t.tt = t
t[T] = t t[T] = t
t.f = print t.f = print
t.ff = print t.ff = print
t.g = function(a) print(a[1]) end t.g = function(self, a) print(self, a[1]) end
t[F] = print t[F] = print
local _f="f" local _f="f"
local _t="t" local _t="t"