nitra
nitra copied to clipboard
Runtime Exception when pretty printing grammar
I have this grammar:
namespace Test
{
syntax module Test
{
using Nitra.Core;
[StartRule]
syntax Expression = (Foo; nl)+;
syntax Foo = "foo";
}
}
When I'm pretty-printing any accepted string that consists of more than one "foo" I get an System.ArgumentOutOfRangeException
. Input that produces the error is e.g. "foofoo", "foo foo" or "foofoo foo". The only input that can be pretty printed is "foo".
If I change the rule syntax Expression = (Foo; nl)+;
to syntax Expression = Foo+;
or syntax Expression = (Foo nl)+;
, then it works correctly. Other pretty-print markers like sm
and inl
produce the same result.
Some more infos for debugging purposes
The stack trace (might not be that useful because of the generated sources):
Unhandled Exception: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: id
at Test.TestParseTree.Expression.__Parsed__Node.__ReadSequenceParseTreeStandardMode(Int32 id, Int32& pos) in C:\Users\<...>\AppData\Local\Temp\_N_GeneratedSource_Experiments.n:Line 392.
at Nitra.Internal.ParseTreeFactory.CreateListWithSeparatorOnlyItems[TItemLoader,TItem,TSeparatorLoader](ParseResult parseResult, ISequenceParseTreeLoader`1itemLoader, Int32 itemSequenceId, ParseTreeCastHelper`2 itemConverter, ISequenceParseTreeLoader`1 separatorLoader, Int32 separatorSequenceId, Int32 rawTreePtr,Int32 offset, Int32& pos) in D:\<...>\Nitra\Nitra\Nitra.Runtime\ParseTree\ParseTreeFactory.n:Line 187.
at Test.TestParseTree.Expression.__Parsed__Node.get_Foos() in C:\Users\<...>\AppData\Local\Temp\_N_GeneratedSource_Experiments.n:Line 260.
at Test.TestParseTree.Expression.PrettyPrint(PrettyPrintWriter writer, Int32 callerBindingPower, SpanClass spanClass) in C:\Users\<...>\AppData\Local\Temp\_N_GeneratedSource_Experiments.n:Line 380.
at Nitra.ParseTree.ToString(PrettyPrintOptions options) in D:\<...>\Nitra\Nitra\Nitra.Runtime\ParseTree\ParseTree.n:Line 45.
at Nitra.ParseTree.ToString() in D:\<...>\Nitra\Nitra\Nitra.Runtime\ParseTree\ParseTree.n:Line 32.
at System.String.Concat(Object arg0, Object arg1)
at TesstApp.Program.Main(String[] args) in d:\<...>\Program.cs:Line 23.
The test program I used:
using System;
using Nitra;
using Nitra.ProjectSystem;
namespace TesstApp
{
public class Program
{
public static void Main(string[] args)
{
Console.Write("> ");
var input = Console.ReadLine();
while (input != string.Empty)
{
var session = new ParseSession(Test.Test.Expression, compilerMessages: new ConsoleCompilerMessages());
var result = session.Parse(input);
if (result.IsSuccess)
{
Console.WriteLine("Parsing succeeded!");
Console.WriteLine("PrettyPrint: " + result.CreateParseTree());
}
else
{
Console.Error.WriteLine("Parsing failed!");
}
Console.Write("> ");
input = Console.ReadLine();
}
}
}
}
As a workaround one can write syntax Expression = (Foo; !Any nl)+;
. This does not produce the error, but (according to my understanding) should be equivalent to the original rule.