Skip to content

Commit

Permalink
slight improvements of formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
georghinkel committed Oct 25, 2024
1 parent 3508038 commit 5ec1264
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 51 deletions.
161 changes: 111 additions & 50 deletions UserInterfaces/AnyText/AnyText.anytext
Original file line number Diff line number Diff line change
@@ -1,57 +1,118 @@
grammar AnyText (anytext)
grammar AnyText ( anytext )
root Grammar

imports https://github.com/NMFCode/NMF/AnyText

Grammar: 'grammar' Name=ID ('(' LanguageId=ID ')')? <nl> 'root' StartRule=[ClassRule] <nl> <nl>
Imports+=MetamodelImport* <nl>
Rules+=Rule<nl>+;

MetamodelImport: 'imports' (Prefix=ID 'from')? File=Uri <nl>;
Rule: ClassRule | DataRule | FragmentRule | ParanthesisRule | EnumRule;
ClassRule: InheritanceRule | ModelRule;

InheritanceRule: Name=ID RuleTypeFragment ':' <nl> <ind> Subtypes+=[ClassRule] ('|' Subtypes+=[ClassRule])+ <nsp> ';' <unind> <nl> <nl>;
ModelRule: Name=ID RuleTypeFragment ':' <nl> <ind> Expression=ParserExpression <nsp> ';' <unind> <nl> <nl>;
DataRule: 'terminal' Name=ID RuleTypeFragment ':' <nl> <ind> Regex=Regex ('surround' 'with' SurroundCharacter=Char)? ('escape' EscapeRules+=EscapeRule (',' EscapeRules+=EscapeRule)*)? <nsp> ';'<unind> <nl> <nl>;
EscapeRule: Character=Char 'as' Escape=Keyword;
FragmentRule: 'fragment' Name=ID 'processes' (Prefix=ID <nsp> '.'<nsp>)? TypeName=ID ':' <nl> <ind> Expression=ParserExpression <nsp> ';'<unind> <nl> <nl>;
ParanthesisRule: 'parantheses' Name=ID ':' <nl> <ind> OpeningParanthesis=KeywordExpression InnerRule=[ClassRule] ClosingParanthesis=KeywordExpression <nsp> ';'<unind> <nl> <nl>;
EnumRule: 'enum' Name=ID RuleTypeFragment ':' <nl> <ind> (Literals+=LiteralRule)+ <nsp> ';'<unind> <nl> <nl>;
LiteralRule: Literal=ID '=>' Keyword=Keyword <nl>;

fragment RuleTypeFragment processes Rule: ('returns' (Prefix=ID <nsp> '.'<nsp>)? TypeName=ID)? <nsp>;
fragment FormattingInstructionFragment processes ParserExpression: (FormattingInstructions+=FormattingInstruction)*;
enum FormattingInstruction:
Grammar:
'grammar' Name=ID ( '(' LanguageId=ID ')' )? <nl> 'root' StartRule=[ClassRule] <nl> Imports+=MetamodelImport* <nl> Rules+=Rule <nl>+;

MetamodelImport:
'imports' ( Prefix=ID 'from' )? File=Uri <nl>;

Rule:
ClassRule | DataRule | FragmentRule | ParanthesisRule | EnumRule;

ClassRule:
InheritanceRule | ModelRule;

InheritanceRule:
Name=ID RuleTypeFragment ':' <nl> <ind> Subtypes+=[ClassRule] ( '|' Subtypes+=[ClassRule] )+ <nsp> ';' <unind> <nl>;

ModelRule:
Name=ID RuleTypeFragment ':' <nl> <ind> Expression=ParserExpression <nsp> ';' <unind> <nl>;

DataRule:
'terminal' Name=ID RuleTypeFragment ':' <nl> <ind> Regex=Regex ( 'surround' 'with' SurroundCharacter=Char )? ( 'escape' EscapeRules+=EscapeRule ( ',' EscapeRules+=EscapeRule )* )? <nsp> ';' <unind> <nl>;

EscapeRule:
Character=Char 'as' Escape=Keyword;

FragmentRule:
'fragment' Name=ID 'processes' ( Prefix=ID <nsp> '.' <nsp> )? TypeName=ID ':' <nl> <ind> Expression=ParserExpression <nsp> ';' <unind> <nl>;

ParanthesisRule:
'parantheses' Name=ID ':' <nl> <ind> OpeningParanthesis=KeywordExpression InnerRule=[ClassRule] ClosingParanthesis=KeywordExpression <nsp> ';' <unind> <nl>;

EnumRule:
'enum' Name=ID RuleTypeFragment ':' <nl> <ind> Literals+=LiteralRule+ <nsp> ';' <unind> <nl>;

LiteralRule:
Literal=ID '=>' Keyword=Keyword <nl>;

fragment RuleTypeFragment processes Rule :
( 'returns' ( Prefix=ID <nsp> '.' <nsp> )? TypeName=ID )? <nsp>;

fragment FormattingInstructionFragment processes ParserExpression :
FormattingInstructions+=FormattingInstruction*;

enum FormattingInstruction:
Newline => '<nl>'
Indent => '<ind>'
Unindent => '<unind>'
AvoidSpace => '<nsp>'
ForbidSpace => '<!nsp>';

ParserExpression: ChoiceExpression | SequenceExpression | ConjunctiveParserExpression;
ConjunctiveParserExpression returns ParserExpression: PlusExpression | StarExpression | MaybeExpression | BasicParserExpression;
BasicParserExpression returns ParserExpression: NegativeLookaheadExpression | KeywordExpression | ReferenceExpression | AssignExpression | AddAssignExpression | ExistsAssignExpression | RuleExpression | ParanthesisExpression;

parantheses ParanthesisExpression: '(' ParserExpression ')';
SequenceExpression: InnerExpressions+=ConjunctiveParserExpression (InnerExpressions+=ConjunctiveParserExpression)+;
PlusExpression: Inner=BasicParserExpression <nsp> '+' FormattingInstructionFragment;
StarExpression: Inner=BasicParserExpression <nsp> '*' FormattingInstructionFragment;
MaybeExpression: Inner=BasicParserExpression <nsp> '?' FormattingInstructionFragment;

KeywordExpression: Keyword=Keyword FormattingInstructionFragment;

ChoiceExpression: (Alternatives+=ConjunctiveParserExpression '|')+ Alternatives+=ConjunctiveParserExpression;

AssignExpression: Feature=ID <nsp> '=' <nsp> Assigned=BasicParserExpression FormattingInstructionFragment;
AddAssignExpression: Feature=ID <nsp> '+=' <nsp> Assigned=BasicParserExpression FormattingInstructionFragment;
ExistsAssignExpression: Feature=ID <nsp> '?=' <nsp> Assigned=BasicParserExpression FormattingInstructionFragment;
NegativeLookaheadExpression: '!' Inner=BasicParserExpression;
RuleExpression: Rule=[Rule] FormattingInstructionFragment !'=' !'+=' !'?=' ;
ReferenceExpression: '[' <nsp> ReferencedRule=[Rule] <nsp> ']';

terminal ID: /[a-zA-Z]\w*/;
terminal Keyword: /(\\\\|\\'|[^'\\])+/ surround with ' escape \ as '\\\\', ' as '\\\'';
terminal Regex: /(\\\/|[^\/])*/ surround with / escape / as '\\/';
terminal Uri: /.+/;
terminal Char: /\S/;
ForbidSpace => '<!nsp>'
;

ParserExpression:
ChoiceExpression | SequenceExpression | ConjunctiveParserExpression;

ConjunctiveParserExpression returns ParserExpression:
PlusExpression | StarExpression | MaybeExpression | BasicParserExpression;

BasicParserExpression returns ParserExpression:
NegativeLookaheadExpression | KeywordExpression | ReferenceExpression | AssignExpression | AddAssignExpression | ExistsAssignExpression | RuleExpression | ParanthesisExpression;

parantheses ParanthesisExpression :
'(' ParserExpression ')';

SequenceExpression:
InnerExpressions+=ConjunctiveParserExpression InnerExpressions+=ConjunctiveParserExpression+;

PlusExpression:
Inner=BasicParserExpression <nsp> '+' FormattingInstructionFragment;

StarExpression:
Inner=BasicParserExpression <nsp> '*' FormattingInstructionFragment;

MaybeExpression:
Inner=BasicParserExpression <nsp> '?' FormattingInstructionFragment;

KeywordExpression:
Keyword=Keyword FormattingInstructionFragment;

ChoiceExpression:
( Alternatives+=ConjunctiveParserExpression '|' )+ Alternatives+=ConjunctiveParserExpression;

AssignExpression:
Feature=ID <nsp> '=' <nsp> Assigned=BasicParserExpression FormattingInstructionFragment;

AddAssignExpression:
Feature=ID <nsp> '+=' <nsp> Assigned=BasicParserExpression FormattingInstructionFragment;

ExistsAssignExpression:
Feature=ID <nsp> '?=' <nsp> Assigned=BasicParserExpression FormattingInstructionFragment;

NegativeLookaheadExpression:
'!' <nsp> Inner=BasicParserExpression;

RuleExpression:
Rule=[Rule] FormattingInstructionFragment !'='!'+='!'?=';

ReferenceExpression:
'[' <nsp> ReferencedRule=[Rule] <nsp> ']';

terminal ID:
/[a-zA-Z]\w*/;

terminal Keyword:
/(\\\\|\\'|[^'\\])+/ surround with ' escape \ as '\\\\' , ' as '\\\'';

terminal Regex:
/(\\\/|[^\/])*/ surround with / escape / as '\\/';

terminal Uri:
/.+/;

terminal Char:
/\S/;


2 changes: 1 addition & 1 deletion UserInterfaces/AnyText/Grammars/AnyTextGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public partial class NegativeLookaheadExpressionRule : ModelElementRule<Negative
public override void Initialize(GrammarContext context)
{
Rules = new NMF.AnyText.Rules.Rule[] {
context.ResolveKeyword("!"),
context.ResolveKeyword("!", NMF.AnyText.PrettyPrinting.FormattingInstruction.SupressSpace),
context.ResolveRule<UnaryParserExpressionInnerRule>()};
}
}
Expand Down
2 changes: 2 additions & 0 deletions UserInterfaces/Tests/AnyText.Tests/AnyTextGrammarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public void AnyText_PrettyPrintItself()

var synthesized = anyText.GetRule<AnyTextGrammar.GrammarRule>().Synthesize(parsed, parser.Context);
Assert.That(synthesized, Is.Not.Null);
var joined = string.Join(Environment.NewLine, grammar);
Assert.That(synthesized, Is.EqualTo(joined));
}

private static string[] SplitIntoLines(string grammar)
Expand Down

0 comments on commit 5ec1264

Please sign in to comment.