From e8bf0dc0bbfb47547ed006a853e841fab23ed057 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 24 Jun 2021 17:25:06 +0100 Subject: [PATCH] Parse (and format) ANTLR fragment productions in the Word converter This should enable #339 and #342 in terms of Word conversion. --- tools/MarkdownConverter/Grammar/Antlr.cs | 16 +++++++++++++++- tools/MarkdownConverter/Grammar/Production.cs | 7 ++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/tools/MarkdownConverter/Grammar/Antlr.cs b/tools/MarkdownConverter/Grammar/Antlr.cs index 622e96de2..59da095f5 100644 --- a/tools/MarkdownConverter/Grammar/Antlr.cs +++ b/tools/MarkdownConverter/Grammar/Antlr.cs @@ -167,6 +167,10 @@ private static IEnumerable 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"); } @@ -343,6 +347,16 @@ private static IEnumerable 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; @@ -375,7 +389,7 @@ private static IEnumerable 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(); diff --git a/tools/MarkdownConverter/Grammar/Production.cs b/tools/MarkdownConverter/Grammar/Production.cs index 55fd09379..4e998dc9b 100644 --- a/tools/MarkdownConverter/Grammar/Production.cs +++ b/tools/MarkdownConverter/Grammar/Production.cs @@ -7,6 +7,11 @@ internal class Production /// public EbnfNode Ebnf { get; set; } + /// + /// Whether or not this production is a fragment + /// + public bool Fragment { get; set; } + /// /// Optional. Contains no whitespace and is not delimited by ' /// @@ -32,6 +37,6 @@ internal class Production /// public string LinkName { get; set; } - public override string ToString() => $"{Name} := {Ebnf}"; + public override string ToString() => $"{(Fragment ? "fragment " : "")}{Name} := {Ebnf}"; } }