Skip to content

Commit

Permalink
Merge pull request #143 from b3b00/bugfix/better_error_reporting_on_p…
Browse files Browse the repository at this point in the history
…arser_build

bugfix : error reportin on parser build
  • Loading branch information
b3b00 authored Dec 17, 2019
2 parents 01d016d + 3f7cf3c commit 9906162
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 32 deletions.
97 changes: 71 additions & 26 deletions samples/ParserExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,34 +405,33 @@ public static void test104()
public static void testJSON()
{
try {
Console.WriteLine("starting json test.");

var instance = new EbnfJsonGenericParser();
var builder = new ParserBuilder<JsonTokenGeneric, JSon>();
var buildResult = builder.BuildParser(instance, ParserType.EBNF_LL_RECURSIVE_DESCENT, "root");
if (buildResult.IsOk)
{
Console.WriteLine("parser built.");
var parser = buildResult.Result;
var content = File.ReadAllText("test.json");
Console.WriteLine("test.json read.");
var jsonResult = parser.Parse(content);
Console.WriteLine("json parse done.");
if (jsonResult.IsOk)
{
Console.WriteLine("YES !");
}
else
{
Console.WriteLine("Ooh no !");
}
Console.WriteLine("Done.");

}
else
{
buildResult.Errors.ForEach(e => Console.WriteLine(e.Message));
}
// if (buildResult.IsOk)
// {
// Console.WriteLine("parser built.");
// var parser = buildResult.Result;
// var content = File.ReadAllText("test.json");
// Console.WriteLine("test.json read.");
// var jsonResult = parser.Parse(content);
// Console.WriteLine("json parse done.");
// if (jsonResult.IsOk)
// {
// Console.WriteLine("YES !");
// }
// else
// {
// Console.WriteLine("Ooh no !");
// }
// Console.WriteLine("Done.");
//
// }
// else
// {
// buildResult.Errors.ForEach(e => Console.WriteLine(e.Message));
// }
}
catch(Exception e) {
Console.WriteLine($"ERROR {e.Message} : \n {e.StackTrace}");
Expand Down Expand Up @@ -494,15 +493,61 @@ private static void TestChars()
}
}

private static void TestGrammarParser()
{
string productionRule = "clauses : clause (COMMA [D] clause)*";
var ruleparser = new RuleParser<TestGrammarToken>();
var builder = new ParserBuilder<EbnfTokenGeneric, GrammarNode<TestGrammarToken>>();
var grammarParser = builder.BuildParser(ruleparser, ParserType.LL_RECURSIVE_DESCENT, "rule").Result;
var result = grammarParser.Parse(productionRule);
grammarParser.Lexer.ResetLexer();
//(grammarParser.Lexer as GenericLexer<TestGrammarToken>).ResetLexer();
Console.WriteLine($"alors ? {string.Join('\n',result.Errors.Select(e => e.ErrorMessage))}");
result = grammarParser.Parse(productionRule);
Console.WriteLine($"alors ? {string.Join('\n',result.Errors.Select(e => e.ErrorMessage))}");
;

Console.WriteLine("starting");
ErroneousGrammar parserInstance = new ErroneousGrammar();
Console.WriteLine("new instance");

var builder2 = new ParserBuilder<TestGrammarToken, object>();
Console.WriteLine("builder");

var Parser = builder.BuildParser(parserInstance,ParserType.EBNF_LL_RECURSIVE_DESCENT,"rule");
Console.WriteLine($"built : {Parser.IsOk}");


}

private static void Main(string[] args)
{
//TestContextualParser();
//TestTokenCallBacks();
//test104();
//testJSON();
TestGraphViz();
TestGrammarParser();
// TestGraphViz();
//TestGraphViz();
TestChars();
//TestChars();

}
}

public enum TestGrammarToken
{
[Lexeme(GenericToken.SugarToken,",")]
COMMA = 1
}

public class ErroneousGrammar
{
[Production("clauses : clause (COMMA [D] clause)*")]

public object test()
{
return null;
}
}

}
6 changes: 4 additions & 2 deletions samples/jsonparser/JSONLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,11 @@ public LexerResult<JsonToken> Tokenize(ReadOnlyMemory<char> source)
position++;
}
}


return new LexerResult<JsonToken>(tokens);
}

public void ResetLexer()
{
}
}
}
5 changes: 5 additions & 0 deletions sly/lexer/GenericLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -752,5 +752,10 @@ public string ToGraphViz()
{
return LexerFsm.ToGraphViz();
}

public void ResetLexer()
{
LexerFsm.Reset();
}
}
}
2 changes: 2 additions & 0 deletions sly/lexer/ILexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public interface ILexer<T> where T : struct
LexerResult<T> Tokenize(string source);

LexerResult<T> Tokenize(ReadOnlyMemory<char> source);

void ResetLexer();
}
}
5 changes: 5 additions & 0 deletions sly/lexer/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,10 @@ public LexerResult<T> Tokenize(ReadOnlyMemory<char> source)
{
throw new NotImplementedException();
}

public void ResetLexer()
{

}
}
}
7 changes: 7 additions & 0 deletions sly/lexer/fsm/FSMLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,12 @@ private void ConsumeIgnored(ReadOnlyMemory<char> source)
}

#endregion

public void Reset()
{
CurrentColumn = 0;
CurrentLine = 0;
CurrentPosition = 0;
}
}
}
3 changes: 2 additions & 1 deletion sly/parser/generator/EBNFParserBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override BuildResult<Parser<IN, OUT>> BuildParser(object parserInstance,
}
catch (Exception e)
{
result.AddError(new ParserInitializationError(ErrorLevel.ERROR, e.Message+"\n"+e.StackTrace));
result.AddError(new ParserInitializationError(ErrorLevel.ERROR, e.Message));
return result;
}

Expand Down Expand Up @@ -128,6 +128,7 @@ protected virtual ParserConfiguration<IN, OUT> ExtractEbnfParserConfiguration(Ty
.Errors
.Select(e => e.ErrorMessage)
.Aggregate((e1, e2) => e1 + "\n" + e2);
message = $"rule error [{ruleString}] : {message}";
throw new ParserConfigurationException(message);
}
}
Expand Down
2 changes: 1 addition & 1 deletion sly/parser/parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public ParseResult<IN, OUT> Parse(string source, string startingNonTerminal = nu
public ParseResult<IN, OUT> ParseWithContext(string source, object context, string startingNonTerminal = null)
{
ParseResult<IN, OUT> result = null;

Lexer.ResetLexer();
var lexingResult = Lexer.Tokenize(source);
if (lexingResult.IsError)
{
Expand Down
4 changes: 2 additions & 2 deletions sly/sly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<Description>#LY is a parser generator halfway between parser combinators and parser generator like ANTLR</Description>
<Authors>b3b00</Authors>
<version>2.4.1.0</version>
<version>2.4.1.1</version>
<PackageProjectUrl>https://github.com/b3b00/sly</PackageProjectUrl>
<RepositoryUrl>https://github.com/b3b00/sly</RepositoryUrl>
<PackageLicenseUrl>https://github.com/b3b00/sly/blob/master/LICENSE</PackageLicenseUrl>
<PackageVersion>2.4.1.0</PackageVersion>
<PackageVersion>2.4.1.1</PackageVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;1705;1591</NoWarn>
Expand Down

0 comments on commit 9906162

Please sign in to comment.