|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Text; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +namespace StandardAnchorTags; |
| 7 | + |
| 8 | +public record GrammarHeaders(string LexicalHeader, |
| 9 | + string SyntacticHeader, |
| 10 | + string UnsafeExtensionsHeader, |
| 11 | + string GrammarFooter); |
| 12 | + |
| 13 | +public class GenerateGrammar : IDisposable |
| 14 | +{ |
| 15 | + public static async Task<GrammarHeaders> ReadExistingHeaders(string pathToStandard, string grammarFile) |
| 16 | + { |
| 17 | + GrammarHeaders headers = new GrammarHeaders("", "", "", ""); |
| 18 | + StringBuilder headerBuffer = new StringBuilder(); |
| 19 | + |
| 20 | + using var reader = new StreamReader(Path.Combine(pathToStandard, grammarFile)); |
| 21 | + |
| 22 | + while (await reader.ReadLineAsync() is string inputLine) |
| 23 | + { |
| 24 | + headerBuffer.AppendLine(inputLine); |
| 25 | + if (inputLine.StartsWith("```ANTLR")) |
| 26 | + { |
| 27 | + if (string.IsNullOrWhiteSpace(headers.LexicalHeader)) |
| 28 | + { |
| 29 | + headers = headers with { LexicalHeader = headerBuffer.ToString() }; |
| 30 | + } |
| 31 | + else if (string.IsNullOrWhiteSpace(headers.SyntacticHeader)) |
| 32 | + { |
| 33 | + headers = headers with { SyntacticHeader = headerBuffer.ToString() }; |
| 34 | + } |
| 35 | + else if (string.IsNullOrWhiteSpace(headers.UnsafeExtensionsHeader)) |
| 36 | + { |
| 37 | + headers = headers with { UnsafeExtensionsHeader = headerBuffer.ToString() }; |
| 38 | + } |
| 39 | + } else if (inputLine.StartsWith("```")) |
| 40 | + { |
| 41 | + headerBuffer.Clear(); |
| 42 | + // Put the closing tag back: |
| 43 | + headerBuffer.AppendLine(inputLine); |
| 44 | + } |
| 45 | + } |
| 46 | + headers = headers with { GrammarFooter = headerBuffer.ToString() }; |
| 47 | + reader.Close(); |
| 48 | + |
| 49 | + return headers; |
| 50 | + } |
| 51 | + |
| 52 | + private readonly GrammarHeaders informativeTextBlocks; |
| 53 | + private readonly string pathToStandardFiles; |
| 54 | + private readonly StreamWriter grammarStream; |
| 55 | + |
| 56 | + public GenerateGrammar(string grammarPath, string pathToStandardFiles, GrammarHeaders headers) |
| 57 | + { |
| 58 | + grammarStream = new StreamWriter(Path.Combine(pathToStandardFiles, grammarPath), false); |
| 59 | + this.pathToStandardFiles = pathToStandardFiles; |
| 60 | + informativeTextBlocks = headers; |
| 61 | + } |
| 62 | + |
| 63 | + public async Task WriteHeader() => await grammarStream.WriteAsync(informativeTextBlocks.LexicalHeader); |
| 64 | + public async Task WriteSyntaxHeader() => await grammarStream.WriteAsync(informativeTextBlocks.SyntacticHeader); |
| 65 | + public async Task WriteUnsafeExtensionHeader() => await grammarStream.WriteAsync(informativeTextBlocks.UnsafeExtensionsHeader); |
| 66 | + |
| 67 | + public async Task WriteGrammarFooter() |
| 68 | + { |
| 69 | + await grammarStream.WriteAsync(informativeTextBlocks.GrammarFooter); |
| 70 | + await grammarStream.FlushAsync(); |
| 71 | + grammarStream.Close(); |
| 72 | + } |
| 73 | + |
| 74 | + public async Task ExtractGrammarFrom(string inputFileName) |
| 75 | + { |
| 76 | + string inputFilePath = $"{pathToStandardFiles}/{inputFileName}"; |
| 77 | + using var inputFile = new StreamReader(inputFilePath); |
| 78 | + string section = ""; |
| 79 | + bool inProduction = false; |
| 80 | + |
| 81 | + Console.OutputEncoding = Encoding.UTF8; |
| 82 | + |
| 83 | + while (await inputFile.ReadLineAsync() is string inputLine) |
| 84 | + { |
| 85 | + if (inProduction) |
| 86 | + { |
| 87 | + if (inputLine.StartsWith("```")) |
| 88 | + { |
| 89 | + inProduction = false; |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + await grammarStream.WriteLineAsync(inputLine); |
| 94 | + } |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + if (inputLine.StartsWith("#")) |
| 99 | + { |
| 100 | + section = inputLine.Trim('#', ' '); |
| 101 | + } |
| 102 | + else if (inputLine.StartsWith("```ANTLR", StringComparison.InvariantCultureIgnoreCase)) |
| 103 | + { |
| 104 | + await grammarStream.WriteLineAsync(); // write out blank line before each new production |
| 105 | + await grammarStream.WriteLineAsync($"// Source: §{section}"); |
| 106 | + inProduction = true; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public void Dispose() => grammarStream.Dispose(); |
| 113 | +} |
0 commit comments