Skip to content

Commit 2fb8425

Browse files
authored
Combine merge tools (#977)
* Update clauses to separate unsafe code For the grammar, we separate the unsafe code extensions. Make that distinction in the clauses.json, the code that reads it, and the section renumbering tool. * Replicate current functionality The section renumbering tool now replicates the output from the grammar extraction tool. Refactoring to come. * remove update grammar tool It's no longer needed because it's functionality has been replicated in the section renumber tool. * Refactoring, part 1 Simplify the loop that reads all the grammar productions from parts of the standard, and writes them to the grammar annex. * refactoring, part 2 Read the existing text outside of the ANTLR productions from the existing annex. That way, we can make any edits to this text (including any changing section numbers) in the markdown file, rather than in the program source code. * some final refactoring * respond to feedback * one final small lint fix
1 parent cf8b7b6 commit 2fb8425

14 files changed

+206
-213
lines changed

.github/workflows/update-on-merge.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@ jobs:
5353
./run-section-renumber.sh
5454
shell: bash
5555

56-
- name: Update grammar annex
57-
id: update-grammar
58-
run: |
59-
cd tools
60-
./update-grammar-annex.sh
61-
shell: bash
62-
6356
- name: Create pull request
6457
uses: peter-evans/[email protected]
6558
if: ${{ steps.renumber-sections.outputs.status }} == 'success' && ${{ steps.update-grammar.outputs.status }} == 'success'

standard/clauses.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
"foreword.md",
44
"introduction.md"
55
],
6-
"MainBody": [
6+
"ScopeAndConformance": [
77
"scope.md",
88
"normative-references.md",
99
"terms-and-definitions.md",
1010
"general-description.md",
11-
"conformance.md",
12-
"lexical-structure.md",
11+
"conformance.md"
12+
],
13+
"LexicalStructure": [
14+
"lexical-structure.md"
15+
],
16+
"MainBody": [
1317
"basic-concepts.md",
1418
"types.md",
1519
"variables.md",
@@ -25,7 +29,9 @@
2529
"enums.md",
2630
"delegates.md",
2731
"exceptions.md",
28-
"attributes.md",
32+
"attributes.md"
33+
],
34+
"UnsafeClauses": [
2935
"unsafe-code.md"
3036
],
3137
"Annexes": [

tools/GetGrammar/GetGrammar.csproj

Lines changed: 0 additions & 9 deletions
This file was deleted.

tools/GetGrammar/Program.cs

Lines changed: 0 additions & 87 deletions
This file was deleted.

tools/GetGrammar/grammar-eof-insert.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

tools/GetGrammar/grammar-general-lexical-insert.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

tools/GetGrammar/grammar-lexer-members.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

tools/GetGrammar/grammar-syntactic-insert.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

tools/GetGrammar/grammar-unsafe-extensions-insert.md

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)