Skip to content

Commit 3b0d8e6

Browse files
authored
Add 'Assert' consistency to algorithm format check (#1168)
* Add 'Assert' consistency to algorithm format check * Fix detection of grammar rules * Enforce more asserts * Standardise assert format
1 parent 646f937 commit 3b0d8e6

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

.github/algorithm-format-check.mjs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ for (const filename of filenames) {
2828
const matches = line.match(/^([a-z0-9A-Z]+)(\s*)\(([^)]*)\)(\s*):(\s*)$/);
2929
const grammarMatches =
3030
filename === "Section 2 -- Language.md" &&
31-
line.match(/^([A-Za-z0-9]+) :\s+((\S).*)$/);
31+
line.match(/^([A-Za-z0-9]+) ::?\s+((\S).*)$/);
3232
if (matches) {
3333
const [, algorithmName, ns1, _args, ns2, ns3] = matches;
3434
if (ns1 || ns2 || ns3) {
@@ -67,14 +67,23 @@ for (const filename of filenames) {
6767
console.log();
6868
process.exitCode = 1;
6969
}
70-
if (step.match(/^\s*(-|[0-9]\.)\s+[a-z]/)) {
70+
if (step.match(/^\s*(-|[0-9]+\.)\s+[a-z]/)) {
7171
console.log(
7272
`Bad formatting of '${algorithmName}' step (should start with a capital) in '${filename}':`
7373
);
7474
console.dir(step);
7575
console.log();
7676
process.exitCode = 1;
7777
}
78+
const assertMatch = step.match(/^\s*(-|[0-9]+\.)\s*Assert([^:])/);
79+
if (assertMatch) {
80+
console.log(
81+
`Bad formatting of '${algorithmName}' step (Assert should be immediately followed by ':'; found '${assertMatch[2]}') in '${filename}':`
82+
);
83+
console.dir(step);
84+
console.log();
85+
process.exitCode = 1;
86+
}
7887

7988
const stepWithoutValueLiterals = step.replace(
8089
valueLiteralsRegexp,
@@ -131,6 +140,10 @@ for (const filename of filenames) {
131140
console.log();
132141
process.exitCode = 1;
133142
}
143+
while (lines[i + 1].trim() !== "") {
144+
// Continuation of definition
145+
i++;
146+
}
134147
if (!lines[i + 2].startsWith("- ")) {
135148
// Not an algorithm; probably more grammar
136149
continue;
@@ -176,6 +189,15 @@ for (const filename of filenames) {
176189
console.log();
177190
process.exitCode = 1;
178191
}
192+
const assertMatch = step.match(/^\s*(-|[0-9]+\.)\s*Assert([^:])/);
193+
if (assertMatch) {
194+
console.log(
195+
`Bad formatting of '${grammarName}' step (Assert should be immediately followed by ':'; found '${assertMatch[2]}') in '${filename}':`
196+
);
197+
console.dir(step);
198+
console.log();
199+
process.exitCode = 1;
200+
}
179201
}
180202
}
181203
}

spec/Section 2 -- Language.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ StringCharacter :: `\u` EscapedUnicode
972972

973973
- Let {value} be the hexadecimal value represented by the sequence of {HexDigit}
974974
within {EscapedUnicode}.
975-
- Assert {value} is a within the _Unicode scalar value_ range (>= 0x0000 and <=
975+
- Assert: {value} is a within the _Unicode scalar value_ range (>= 0x0000 and <=
976976
0xD7FF or >= 0xE000 and <= 0x10FFFF).
977977
- Return the _Unicode scalar value_ {value}.
978978

@@ -984,12 +984,12 @@ HexDigit HexDigit HexDigit
984984
- Let {trailingValue} be the hexadecimal value represented by the second
985985
sequence of {HexDigit}.
986986
- If {leadingValue} is >= 0xD800 and <= 0xDBFF (a _Leading Surrogate_):
987-
- Assert {trailingValue} is >= 0xDC00 and <= 0xDFFF (a _Trailing Surrogate_).
987+
- Assert: {trailingValue} is >= 0xDC00 and <= 0xDFFF (a _Trailing Surrogate_).
988988
- Return ({leadingValue} - 0xD800) × 0x400 + ({trailingValue} - 0xDC00) +
989989
0x10000.
990990
- Otherwise:
991-
- Assert {leadingValue} is within the _Unicode scalar value_ range.
992-
- Assert {trailingValue} is within the _Unicode scalar value_ range.
991+
- Assert: {leadingValue} is within the _Unicode scalar value_ range.
992+
- Assert: {trailingValue} is within the _Unicode scalar value_ range.
993993
- Return the sequence of the _Unicode scalar value_ {leadingValue} followed by
994994
the _Unicode scalar value_ {trailingValue}.
995995

spec/Section 6 -- Execution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ coerce result values.
757757

758758
CoerceResult(leafType, value):
759759

760-
- Assert {value} is not {null}.
760+
- Assert: {value} is not {null}.
761761
- Return the result of calling the internal method provided by the type system
762762
for determining the "result coercion" of {leafType} given the value {value}.
763763
This internal method must return a valid value for the type and not {null}.

0 commit comments

Comments
 (0)