Skip to content

Commit 5f5ea3b

Browse files
committed
Fix/bug 49 (#117)
* #49 - рефакторинг на строго типизированные теории * #49 - исправление бага с помощью lookahead и lookbehind
1 parent 1b19b61 commit 5f5ea3b

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

src/Domain/HydraScript.Domain.FrontEnd/Lexer/TokenTypesJson.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public static class TokenTypesJson
3131
},
3232
{
3333
"tag": "NullLiteral",
34-
"pattern": "null",
34+
"pattern": "(?<![a-zA-Z0-9])(null)(?![a-zA-Z0-9])",
3535
"priority": 4
3636
},
3737
{
3838
"tag": "BooleanLiteral",
39-
"pattern": "true|false",
39+
"pattern": "(?<![a-zA-Z0-9])(true|false)(?![a-zA-Z0-9])",
4040
"priority": 5
4141
},
4242
{
@@ -46,7 +46,7 @@ public static class TokenTypesJson
4646
},
4747
{
4848
"tag": "Keyword",
49-
"pattern": "let|const|function|if|else|while|break|continue|return|as|type",
49+
"pattern": "(?<![a-zA-Z0-9])(let|const|function|if|else|while|break|continue|return|as|type)(?![a-zA-Z0-9])",
5050
"priority": 11
5151
},
5252
{
Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1-
using System.Collections;
1+
using Xunit;
22

33
namespace HydraScript.Tests.TestData;
44

5-
public class LexerSuccessData : IEnumerable<object[]>
5+
public class LexerSuccessData : TheoryData<string>
66
{
7-
public IEnumerator<object[]> GetEnumerator()
7+
public LexerSuccessData()
88
{
9-
yield return new object[] { "a + b - c return while do" };
10-
yield return new object[] { "=> abc null true false" };
11-
yield return new object[] { "{ . } , ( ) [] =?:" };
9+
Add("a + b - c return while do");
10+
Add("=> abc null true false");
11+
Add("{ . } , ( ) [] =?:");
1212
}
13-
14-
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
1513
}
1614

17-
public class LexerFailData : IEnumerable<object[]>
15+
public class LexerFailData : TheoryData<string>
1816
{
19-
public IEnumerator<object[]> GetEnumerator()
17+
public LexerFailData()
2018
{
21-
yield return new object[] { "a + v $$$" };
22-
yield return new object[] { "kkk &" };
23-
yield return new object[] { "|| |" };
19+
Add("a + v $$$");
20+
Add("kkk &");
21+
Add("|| |");
2422
}
23+
}
2524

26-
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
25+
public class LexerKeywordInsideIdentData : TheoryData<string>
26+
{
27+
public LexerKeywordInsideIdentData()
28+
{
29+
Add("constExpr");
30+
Add("letVarConst");
31+
Add("nullObj");
32+
Add("trueStmt");
33+
Add("falseStmt");
34+
Add("returnResult");
35+
}
2736
}

tests/HydraScript.Tests/Unit/FrontEnd/RegexLexerTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using HydraScript.Domain.FrontEnd.Lexer;
22
using HydraScript.Domain.FrontEnd.Lexer.Impl;
3+
using HydraScript.Domain.FrontEnd.Lexer.TokenTypes;
34
using HydraScript.Infrastructure;
45
using HydraScript.Tests.TestData;
56
using Xunit;
@@ -44,4 +45,12 @@ public void GetTokensSkipIgnorableTypesTest()
4445
var tokens = _regexLexer.GetTokens(text);
4546
Assert.DoesNotContain(_regexLexer.Structure.FindByTag("Comment"), tokens.Select(x => x.Type));
4647
}
48+
49+
[Theory, ClassData(typeof(LexerKeywordInsideIdentData))]
50+
public void GetTokens_KeywordInsideIdent_Ident(string input)
51+
{
52+
var tokens = _regexLexer.GetTokens(input);
53+
var token = tokens.First();
54+
token.Type.Should().Be(new TokenType("Ident"));
55+
}
4756
}

0 commit comments

Comments
 (0)