Skip to content

Parse (and format) ANTLR fragment productions in the Word converter #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion tools/MarkdownConverter/Grammar/Antlr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ private static IEnumerable<ColorizedWord> ColorizeAntlr(Production p)
}
else
{
if (p.Fragment)
{
yield return Col("fragment ", "PlainText");
}
yield return Col(p.Name, "Production");
yield return Col(":", "PlainText");
if (p.RuleStartsOnNewLine) { yield return null; yield return Col("\t| ", "PlainText"); }
Expand Down Expand Up @@ -343,6 +347,16 @@ private static IEnumerable<Production> ReadInternal(string src)
}
else
{
bool fragment = t == "fragment";
if (fragment)
{
while (tokens.Any() && string.IsNullOrWhiteSpace(tokens.First.Value))
{
tokens.RemoveFirst();
}
t = tokens.First.Value;
tokens.RemoveFirst();
}
var whitespace = "";
var comment = "";
var newline = false;
Expand Down Expand Up @@ -375,7 +389,7 @@ private static IEnumerable<Production> ReadInternal(string src)
tokens.RemoveFirst();
}

var production = new Production { Comment = comment, Ebnf = p, Name = t, RuleStartsOnNewLine = newline };
var production = new Production { Fragment = fragment, Comment = comment, Ebnf = p, Name = t, RuleStartsOnNewLine = newline };
while (tokens.Any() && tokens.First.Value.StartsWith("//"))
{
production.Comment += tokens.First.Value.Substring(2); tokens.RemoveFirst();
Expand Down
7 changes: 6 additions & 1 deletion tools/MarkdownConverter/Grammar/Production.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ internal class Production
/// </summary>
public EbnfNode Ebnf { get; set; }

/// <summary>
/// Whether or not this production is a fragment
/// </summary>
public bool Fragment { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary, but it looks like this could be an init only property once we move to C# 9. I only see it set in the property initializer.

Suggested change
public bool Fragment { get; set; }
public bool Fragment { get; init; }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I suspect that may well be fine. I was mostly following the rest of the properties. I'd prefer to leave it as it is for now, and then revisit holistically.


/// <summary>
/// Optional. Contains no whitespace and is not delimited by '
/// </summary>
Expand All @@ -32,6 +37,6 @@ internal class Production
/// </summary>
public string LinkName { get; set; }

public override string ToString() => $"{Name} := {Ebnf}";
public override string ToString() => $"{(Fragment ? "fragment " : "")}{Name} := {Ebnf}";
}
}