Skip to content

Commit 263207e

Browse files
committed
Fix TerminalGenerator.cs
1 parent b2c91a6 commit 263207e

File tree

3 files changed

+16
-44
lines changed

3 files changed

+16
-44
lines changed

Parsers/DotParser/DotTerminals.cs

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,14 @@ public sealed partial class DotTerminals
88
[Regex(@"[\l_]\w*")]
99
public static partial Terminal Identifier();
1010

11-
//[Regex(@"""[^""]*""")]
12-
//public static partial Terminal QuotedString();
13-
public static Terminal QuotedString() => _quotedString;
11+
[Regex("""
12+
"(\\.|[^"])*"
13+
""")]
14+
public static partial Terminal QuotedString();
1415

1516
[Regex(@"\d+")]
1617
public static partial Terminal Number();
1718

1819
[Regex(@"(//[^\n]*|\s)*")]
1920
public static partial Terminal Trivia();
20-
21-
private sealed record QuotedStringMatcher() : Terminal(Kind: "QuotedString")
22-
{
23-
public override int TryMatch(string input, int startPos)
24-
{
25-
if (startPos >= input.Length)
26-
return -1;
27-
28-
var i = startPos;
29-
var c = input[i];
30-
31-
if (c != '\"')
32-
return -1;
33-
34-
for (i++; i < input.Length; i++)
35-
{
36-
c = input[i];
37-
38-
if (c == '\\' && peek() != '\0')
39-
i++;
40-
else if (c == '\n')
41-
return -1;
42-
else if (c == '\"')
43-
{
44-
i++;
45-
break;
46-
}
47-
}
48-
49-
return i - startPos;
50-
char peek() => i + 1 < input.Length ? input[i + 1] : '\0';
51-
}
52-
53-
public override string ToString() => @"QuotedString";
54-
}
55-
56-
private static readonly Terminal _quotedString = new QuotedStringMatcher();
5721
}

TerminalGenerator/TerminalGenerator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,13 @@ static string generateTransitionCondition(RegexNode node)
244244
RegexChar rc => $"""c == '{EscapeChar(rc.Value)}' /* {rc} */""",
245245
RegexAnyChar => "true /* RegexAnyChar */",
246246
NegatedCharClassGroup rcc when rcc.ToString() == $@"[^[\n]]" => $@"c is not '\n' and not '\0' /* {rcc} */",
247+
NegatedCharClassGroup x when x.Classes.Any(x => x is RangesCharClass) => $@"!({string.Join(" && ", x.Classes.Select(generateTransitionCondition))}) && c != '\0'",
247248
RangesCharClass rcc => GenerateRangeCondition(rcc),
248249
WordCharClass wcc => $"""{(wcc.Negated ? "!" : "")}(char.IsLetterOrDigit(c) || c == '_')""",
249250
DigitCharClass dcc => $"""{(dcc.Negated ? "!" : "")}char.IsDigit(c)""",
250251
WhitespaceCharClass scc => $"""{(scc.Negated ? "!" : "")}char.IsWhiteSpace(c)""",
251252
LetterCharClass lcc => $"""{(lcc.Negated ? "!" : "")}char.IsLetter(c)""",
253+
NegatedCharClassGroup x => $@"false /* {x} ({x.GetType().Name}) {string.Join(" && ", x.Classes.Select(x => $"{x} ({x.GetType().Name})"))} */",
252254
_ => $"false /* {node} ({node.GetType().Name}) */"
253255
};
254256
}

Tests/ParaserTests/Dot/DotTests.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@ public void QuotedStringMatcherTest()
4040
{
4141
var matcher = DotTerminals.QuotedString();
4242

43+
test(startPos: 1, expectedLen: 16, """
44+
="23\"\n4\r56789"
45+
""");
46+
test(startPos: 1, expectedLen: 12, """
47+
"23\"456789""23\"456789"
48+
""");
4349
test(startPos: 0, expectedLen: -1, "123");
4450
test(startPos: 0, expectedLen: 10, "\"23456789\"");
4551
test(startPos: 1, expectedLen: 10, " \"23456789\"");
46-
test(startPos: 1, expectedLen: 12, """
47-
"23\"456789""23\"456789"
52+
test(startPos: 1, expectedLen: -1, """
53+
"23456789\
4854
""");
49-
test(startPos: 1, expectedLen: 16, """
50-
="23\"\n4\r56789"
55+
test(startPos: 1, expectedLen: -1, """
56+
"23456789
5157
""");
5258

5359
return;

0 commit comments

Comments
 (0)