Skip to content

Commit

Permalink
fixed pretty-print. changed URI to https
Browse files Browse the repository at this point in the history
  • Loading branch information
georghinkel committed Oct 25, 2024
1 parent 936e27e commit 3508038
Show file tree
Hide file tree
Showing 69 changed files with 249 additions and 350 deletions.
19 changes: 0 additions & 19 deletions UserInterfaces/AnyText.Core/Grammars/GrammarContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,5 @@ public LiteralRule ResolveKeyword(string keyword, params FormattingInstruction[]
}
return rule;
}

/// <summary>
/// Resolves the rule for the given keyword
/// </summary>
/// <param name="keyword">the keyword</param>
/// <param name="trailingWhitespaces">true, if trailing whitespaces are allowed, otherwise false</param>
/// <param name="formattingInstructions">formatting instructions for this keyword</param>
/// <returns>a literal rule that represents matching the provided keyword</returns>
public LiteralRule ResolveKeyword(string keyword, bool trailingWhitespaces, params FormattingInstruction[] formattingInstructions)
{
if (!_keywords.TryGetValue(keyword, out var rule))
{
rule = _grammar.CreateKeywordRule(keyword);
rule.TrailingWhitespaces = trailingWhitespaces;
rule.FormattingInstructions = formattingInstructions;
_keywords.Add(keyword, rule);
}
return rule;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public override bool CanSynthesize(object semanticElement)
{
if (semanticElement is ParseObject parseObject && parseObject.TryPeekModelToken<TSemanticElement, TReference>(Feature, GetCollection, null, out var assigned))
{
return assigned != null;
return true;
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion UserInterfaces/AnyText.Core/Model/AddAssignRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override bool CanSynthesize(object semanticElement)
{
if (semanticElement is ParseObject parseObject && parseObject.TryPeekModelToken<TSemanticElement, TProperty>(Feature, GetCollection, null, out var assigned))
{
return !EqualityComparer<TProperty>.Default.Equals(assigned, default);
return true;
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion UserInterfaces/AnyText.Core/Model/ExistsAssignRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public override bool CanSynthesize(object semanticElement)
/// <inheritdoc />
public override RuleApplication Synthesize(object semanticElement, ParsePosition position, ParseContext context)
{
if (semanticElement is ParseObject parseObject && parseObject.TryConsumeModelToken<TSemanticElement, bool>(Feature, GetValue, context, out var assigned))
if (semanticElement is ParseObject parseObject && parseObject.TryConsumeModelToken<TSemanticElement, bool>(Feature, GetValue, context, out var assigned) && assigned)
{
return base.Synthesize(semanticElement, position, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ public abstract class FormattingInstruction
/// Denotes a shared instance for a newline instruction
/// </summary>
public static readonly FormattingInstruction Newline = new NewlineInstruction();

/// <summary>
/// Denotes a shared instance for a space supression
/// </summary>
public static readonly FormattingInstruction SupressSpace = new SupressSpaceInstruction();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public class PrettyPrintWriter
private bool _lastWasNewline = true;
private bool _writeSpace = false;

/// <summary>
/// Supresses rendering a space character before the next token
/// </summary>
public void SupressSpace()
{
_writeSpace = false;
}

/// <summary>
/// Creates a new instance
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NMF.AnyText.PrettyPrinting
{
internal class SupressSpaceInstruction : FormattingInstruction
{
public override void Apply(PrettyPrintWriter writer)
{
writer.SupressSpace();
}
}
}
2 changes: 1 addition & 1 deletion UserInterfaces/AnyText.Core/Rules/SingleRuleApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public override void IterateLiterals<T>(Action<LiteralRuleApplication, T> action
public override void Write(PrettyPrintWriter writer, ParseContext context)
{
Inner?.Write(writer, context);

ApplyFormattingInstructions(writer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ private static CodeExpression CreateResolveKeyword(string keyword, IParserExpres
var res = new CodeMethodInvokeExpression(_contextRef, nameof(GrammarContext.ResolveKeyword), new CodePrimitiveExpression(unescaped));
if (parserExpression != null)
{
if (parserExpression.NoSpace)
{
res.Parameters.Add(new CodePrimitiveExpression(false));
}
foreach (var instruction in parserExpression.FormattingInstructions)
{
res.Parameters.Add(CreateFormattingInstruction(instruction));
Expand Down Expand Up @@ -174,18 +170,18 @@ private static CodeObjectCreateExpression AddFormattingInstructions(CodeObjectCr
return createRuleExpression;
}

private static void AddFormattingInstructions(CodeMemberMethod initializeMethod, IParserExpression parserExpression)
private static void AddFormattingInstructions(CodeMemberMethod initializeMethod, IEnumerable<FormattingInstruction> formattingInstructions)
{
if (parserExpression.FormattingInstructions.Count > 0)
if (formattingInstructions.Any())
{
var formattingInstructions = new CodeArrayCreateExpression(typeof(PrettyPrinting.FormattingInstruction).ToTypeReference());
var formattingInstructionsArray = new CodeArrayCreateExpression(typeof(PrettyPrinting.FormattingInstruction).ToTypeReference());
initializeMethod.Statements.Add(new CodeAssignStatement(
new CodePropertyReferenceExpression(null, nameof(AnyText.Rules.Rule.FormattingInstructions)),
formattingInstructions));
formattingInstructionsArray));

foreach (var instruction in parserExpression.FormattingInstructions)
foreach (var instruction in formattingInstructions)
{
formattingInstructions.Initializers.Add(CreateFormattingInstruction(instruction));
formattingInstructionsArray.Initializers.Add(CreateFormattingInstruction(instruction));
}
}
}
Expand All @@ -200,6 +196,9 @@ private static CodeExpression CreateFormattingInstruction(FormattingInstruction
return new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(PrettyPrinting.FormattingInstruction).ToTypeReference()), nameof(PrettyPrinting.FormattingInstruction.Unindent));
case FormattingInstruction.Newline:
return new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(PrettyPrinting.FormattingInstruction).ToTypeReference()), nameof(PrettyPrinting.FormattingInstruction.Newline));
case FormattingInstruction.ForbidSpace:
case FormattingInstruction.AvoidSpace:
return new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(PrettyPrinting.FormattingInstruction).ToTypeReference()), nameof(PrettyPrinting.FormattingInstruction.SupressSpace));
default:
throw new ArgumentOutOfRangeException(nameof(formattingInstruction));
}
Expand Down Expand Up @@ -639,13 +638,13 @@ public override void Transform(IFeatureExpression input, CodeTypeDeclaration out
initialize.Statements.Add(new CodeAssignStatement(
new CodePropertyReferenceExpression(null, nameof(QuoteRule.Inner)),
CreateParserExpression(input.Assigned, Rule<RuleToClass>(), Rule<AssignmentToClass>(), context)));
if (input.NoSpace)
if (input.FormattingInstructions.Contains(FormattingInstruction.ForbidSpace))
{
initialize.Statements.Add(new CodeAssignStatement(
new CodePropertyReferenceExpression(null, "TrailingWhitespaces"),
new CodePrimitiveExpression(false)));
}
AddFormattingInstructions(initialize, input);
AddFormattingInstructions(initialize, input.FormattingInstructions.Concat(input.Assigned.FormattingInstructions));
output.Members.Add(initialize);

var feature = new CodeMemberProperty
Expand Down
42 changes: 22 additions & 20 deletions UserInterfaces/AnyText/AnyText.anytext
Original file line number Diff line number Diff line change
@@ -1,52 +1,54 @@
grammar AnyText (anytext)
root Grammar

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

Grammar: 'grammar' Name=ID ('(' LanguageId=ID ')')?<nl> 'root' StartRule=[ClassRule]<nl><nl>
Imports+=MetamodelImport<nl>*<nl>
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;
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])+ ';' <unind> <nl> <nl>;
ModelRule: Name=ID RuleTypeFragment ':' <nl> <ind> Expression=ParserExpression ';' <unind> <nl> <nl>;
DataRule: 'terminal' Name=ID RuleTypeFragment ':' <nl> <ind> Regex=Regex ('surround' 'with' SurroundCharacter=Char)? ('escape' EscapeRules+=EscapeRule (',' EscapeRules+=EscapeRule)*)? ';'<unind> <nl> <nl>;
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 ';'<unind> <nl> <nl>;
ParanthesisRule: 'parantheses' Name=ID ':' <nl> <ind> OpeningParanthesis=KeywordExpression InnerRule=[ClassRule] ClosingParanthesis=KeywordExpression ';'<unind> <nl> <nl>;
EnumRule: 'enum' Name=ID RuleTypeFragment ':' <nl> <ind> (Literals+=LiteralRule)+ ';'<unind> <nl> <nl>;
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)?;
fragment FormattingInstructionFragment processes ParserExpression: (NoSpace?='<nsp>')?(FormattingInstructions+=FormattingInstruction)*;
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>';
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 '+' FormattingInstructionFragment;
StarExpression: Inner=BasicParserExpression '*' FormattingInstructionFragment;
MaybeExpression: Inner=BasicParserExpression '?' FormattingInstructionFragment;
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 '=' Assigned=BasicParserExpression FormattingInstructionFragment;
AddAssignExpression: Feature=ID '+=' Assigned=BasicParserExpression FormattingInstructionFragment;
ExistsAssignExpression: Feature=ID '?=' Assigned=BasicParserExpression FormattingInstructionFragment;
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: '[' ReferencedRule=[Rule] ']';
ReferenceExpression: '[' <nsp> ReferencedRule=[Rule] <nsp> ']';

terminal ID: /[a-zA-Z]\w*/;
terminal Keyword: /(\\\\|\\'|[^'\\])+/ surround with ' escape \ as '\\\\', ' as '\\\'';
Expand Down
5 changes: 3 additions & 2 deletions UserInterfaces/AnyText/AnyText.nmeta
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<nmeta:Namespace xmi:version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Uri="http://github.com/NMFCode/NMF/AnyText" Prefix="anytext" Name="AnyText" xmlns:xmi="http://www.omg.org/XMI" xmlns:nmeta="http://nmf.codeplex.com/nmeta/">
<nmeta:Namespace xmi:version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Uri="https://github.com/NMFCode/NMF/AnyText" Prefix="anytext" Name="AnyText" xmlns:xmi="http://www.omg.org/XMI" xmlns:nmeta="http://nmf.codeplex.com/nmeta/">
<Types xsi:type="nmeta:Class" IdentifierScope="Local" Identifier="#//Grammar/Name" Name="Grammar">
<References IsContainment="true" ReferenceType="nmeta:Class #//MetamodelImport" IsOrdered="true" IsUnique="true" UpperBound="-1" Name="Imports" />
<References IsContainment="true" ReferenceType="nmeta:Class #//Rule" IsOrdered="true" IsUnique="true" LowerBound="1" UpperBound="-1" Name="Rules" />
Expand Down Expand Up @@ -29,7 +29,6 @@
</Types>
<Types xsi:type="nmeta:Class" IsAbstract="true" Name="ParserExpression">
<Attributes IsOrdered="true" IsUnique="true" UpperBound="-1" Type="nmeta:Enumeration #//FormattingInstruction" Name="FormattingInstructions" />
<Attributes IsOrdered="true" IsUnique="true" LowerBound="1" Type="nmeta:PrimitiveType http://nmf.codeplex.com/nmeta/#//Boolean" Name="NoSpace" />
</Types>
<Types xsi:type="nmeta:Class" BaseTypes="#//Rule" Name="DataRule">
<References IsContainment="true" ReferenceType="nmeta:Class #//EscapeRule" IsOrdered="true" IsUnique="true" UpperBound="-1" Name="EscapeRules" />
Expand Down Expand Up @@ -73,6 +72,8 @@
<Literals Name="Newline" />
<Literals Value="1" Name="Indent" />
<Literals Value="2" Name="Unindent" />
<Literals Value="3" Name="AvoidSpace" />
<Literals Value="4" Name="ForbidSpace" />
</Types>
<Types xsi:type="nmeta:Class" BaseTypes="#//Rule" Name="EnumRule">
<References IsContainment="true" ReferenceType="nmeta:Class #//LiteralRule" IsOrdered="true" IsUnique="true" UpperBound="-1" Name="Literals" />
Expand Down
Loading

0 comments on commit 3508038

Please sign in to comment.