-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate code for inputs with errors
- Loading branch information
1 parent
17ecd30
commit 66f2ad2
Showing
8 changed files
with
153 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/Abc.Zebus.MessageDsl/Dsl/MessageContractsErrorStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Antlr4.Runtime; | ||
|
||
namespace Abc.Zebus.MessageDsl.Dsl; | ||
|
||
internal class MessageContractsErrorStrategy : DefaultErrorStrategy | ||
{ | ||
public override void Sync(Parser recognizer) | ||
{ | ||
if (InErrorRecoveryMode(recognizer)) | ||
return; | ||
|
||
base.Sync(recognizer); | ||
|
||
if (InErrorRecoveryMode(recognizer) && IsInDefinitionRule(recognizer)) | ||
throw new GoToNextDefinitionException(recognizer); | ||
} | ||
|
||
public override void ReportError(Parser recognizer, RecognitionException e) | ||
{ | ||
if (e is GoToNextDefinitionException) | ||
return; | ||
|
||
base.ReportError(recognizer, e); | ||
|
||
if (IsInDefinitionRule(recognizer)) | ||
throw new GoToNextDefinitionException(recognizer); | ||
} | ||
|
||
public override void Recover(Parser recognizer, RecognitionException e) | ||
{ | ||
if (e is GoToNextDefinitionException) | ||
{ | ||
if (recognizer.Context.RuleIndex != MessageContractsParser.RULE_definition) | ||
throw e; | ||
|
||
while (true) | ||
{ | ||
// Consume tokens until we reach a possible separator between definitions | ||
if (recognizer.InputStream.La(1) is MessageContractsParser.SEP or MessageContractsParser.Eof | ||
|| ((MessageContractsParser)recognizer).IsAtImplicitSeparator()) | ||
{ | ||
EndErrorCondition(recognizer); | ||
return; | ||
} | ||
|
||
recognizer.Consume(); | ||
} | ||
} | ||
|
||
base.Recover(recognizer, e); | ||
} | ||
|
||
private static bool IsInDefinitionRule(Parser recognizer) | ||
{ | ||
RuleContext ctx = recognizer.Context; | ||
|
||
while (ctx != null) | ||
{ | ||
if (ctx.RuleIndex == MessageContractsParser.RULE_definition) | ||
return true; | ||
|
||
ctx = ctx.Parent; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private class GoToNextDefinitionException(Parser recognizer) : RecognitionException(recognizer, recognizer.InputStream, recognizer.Context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters