Re: Lua syntax ambiguity
"'Martin Eden' via lua-l" <[email protected]>
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <[email protected]> |
On 2026-08-16 21:09, 'Martin Eden' via lua-l wrote:
> But it can't be inserted nicely in code generation. Because proper
> code just
> writes to output stream and can't (and shouldn't) read existing output
> stream.
I think I found acceptable solution for handling this.
We're doing AST tree serialization. AST node has field "type" which
is string with node type name (like "string" or "table").
Serialization function is recursive and writes node to output stream.
Core output stream has only one method: Write(str).
Problem is that when serializing index in table ("[" serialize(key) "]"),
serialize() can write something starting with "[" which will spoil
index bracket.
So we must know from what that next token will begin.
So we need stream that has method to peek at output.
And generally, when key node type is that problematic "string" --
we call serialize() for that peekable sub-stream, peeking
and it's first char, writing separator to main stream if needed,
writing sub-stream data to main stream:
local serialize
serialize =
function(Node, OutputStream)
-- ...
if (KeyNode.type == 'string') then
local StringStream = new(StringStream)
serialize(KeyNode, StringStream)
local node_str = StringStream:ToString()
if starts_with(node_str, '[') then
OutputStream:Write(' ')
end
OutputStream:Write(node_str)
else
serialize(KeyNode, OutputStream)
end
-- ...
end
-- Martin
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/13d77bb8-ea37-4bce-bc8f-51fa83ee8f16%40disroot.org.