Skip to content

LuaSyntaxTree.Parse fails on any comment that precedes a statement, although LuaState.Load accepts the same source #334

Description

@timkrueger1993-lab

Summary

LuaSyntaxTree.Parse (and the underlying Lexer) throws a LuaParseException whenever a -- comment appears before any statement — a leading comment, a comment between two statements, or a comment inside a function body before its first statement. Only a trailing comment after the last statement of the chunk parses. The compiler path (LuaState.Load) accepts all of these sources, so the two front ends disagree about what is valid Lua.

Comments are valid in every Lua version, so this affects effectively every real-world script once it is fed through the syntax-tree API.

Reproduction (LuaCSharp 0.5.6)

using Lua;
using Lua.CodeAnalysis.Syntax;

(string label, string source)[] cases =
[
    ("leading comment", "-- comment\nfunction f() return 1 end"),
    ("between statements", "local a = 1\n-- comment\nfunction f() return a end"),
    ("inside a body", "function f()\n    -- comment\n    return 1\nend"),
    ("trailing only", "function f() return 1 end\n-- comment"),
];

var state = LuaState.Create();
foreach (var (label, source) in cases)
{
    try { state.Load(source, label); Console.WriteLine($"Load {label}: OK"); }
    catch (Exception e) { Console.WriteLine($"Load {label}: {e.Message}"); }

    try { LuaSyntaxTree.Parse(source, label); Console.WriteLine($"Parse {label}: OK"); }
    catch (Exception e) { Console.WriteLine($"Parse {label}: {e.Message}"); }
}

Actual output

Load leading comment: OK
Parse leading comment: leading comment:1: unexpected symbol <Subtraction> near '-'
Load between statements: OK
Parse between statements: between statements:3: 'LParen' expected
Load inside a body: OK
Parse inside a body: inside a body:2: unexpected symbol <Subtraction> near '-'
Load trailing only: OK
Parse trailing only: OK

Expected

LuaSyntaxTree.Parse accepts every chunk the compiler accepts; comments are skipped as trivia wherever they appear.

Environment

  • LuaCSharp 0.5.5 and 0.5.6 (NuGet), .NET 10, Windows x64 (also reproduced on Linux x64)

Context

Possibly related to #112 (lexer + consecutive single-line comments), but this is a different failure: a single comment in the wrong place is enough, and the exception is a parse error rather than a stack overflow.

We use LuaSyntaxTree/Lexer for static source inspection in a server runtime (Limes) next to Load as the syntax gate; we currently work around this by blanking comments (position-preserving) before handing the source to the tree/lexer APIs. Happy to provide more repro material if useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions