Skip to content

Commit fb74230

Browse files
committed
Add syntax test cases
1 parent 5642d13 commit fb74230

85 files changed

Lines changed: 980 additions & 152 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.idea/workspace.xml

Lines changed: 123 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/ko/carbonel/compiler/exception/parser/MatchMismatchError.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@
88
import ko.carbonel.compiler.runner.TermFont;
99

1010
public class MatchMismatchError extends SyntaxError {
11+
private final TokenType expected;
12+
private final Token actual;
1113
public MatchMismatchError(FileLocation start, TokenType expected, Token got) {
1214
super(start, "Expected to match a " + TermFont.bold(expected.name()) + " but got a " + Utils.getTokenDescription(got));
15+
this.expected = expected;
16+
this.actual = got;
17+
}
18+
19+
public TokenType expected() {
20+
return expected;
21+
}
22+
23+
public Token actual() {
24+
return actual;
1325
}
1426
}

src/main/java/ko/carbonel/compiler/exception/parser/UnexpectedTokenError.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
import java.util.Set;
1111

1212
public class UnexpectedTokenError extends SyntaxError {
13+
private final Token token;
1314
private final Set<TokenType> options;
1415

1516
public UnexpectedTokenError(FileLocation start, FileLocation end, Token peek, String from, Set<TokenType> options) {
1617
super(start, end, buildMessage(peek, from, options));
1718
this.options = options;
19+
this.token = peek;
1820
}
1921

2022
private static String buildMessage(Token peek, String from, Set<TokenType> options) {
@@ -25,9 +27,14 @@ private static String buildMessage(Token peek, String from, Set<TokenType> optio
2527
public UnexpectedTokenError(FileLocation location, Token peek, String from, Set<TokenType> options) {
2628
super(location, buildMessage(peek, from, options));
2729
this.options = options;
30+
this.token = peek;
2831
}
2932

3033
public Set<TokenType> options() {
3134
return options;
3235
}
36+
37+
public Token token() {
38+
return token;
39+
}
3340
}

src/main/java/ko/carbonel/compiler/generated/Parser.java

Lines changed: 106 additions & 105 deletions
Large diffs are not rendered by default.
Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package ko.carbonel.compiler.generated;
22

3+
import ko.carbonel.Utils;
4+
import ko.carbonel.compiler.exception.parser.MatchMismatchError;
5+
import ko.carbonel.compiler.exception.parser.UnexpectedTokenError;
36
import ko.carbonel.compiler.representation.TokenType;
47
import ko.carbonel.compiler.stream.Lexer;
58
import ko.carbonel.compiler.stream.SourceFileReader;
@@ -10,25 +13,78 @@
1013
import org.junit.jupiter.params.provider.MethodSource;
1114

1215
import java.io.File;
16+
import java.io.IOException;
1317
import java.util.Arrays;
1418
import java.util.Objects;
19+
import java.util.Set;
20+
import java.util.stream.Collectors;
1521
import java.util.stream.Stream;
1622

1723
public class ParserTest {
1824
public static final String PARSER_TEST_ROOT = "src/test/resources/syntax/cases/";
1925

20-
public static Stream<Arguments> readOkFiles() {
21-
return Arrays.stream(Objects.requireNonNull(new File(PARSER_TEST_ROOT).listFiles())).map(it->Arguments.of(it.getName()));
26+
public static Stream<Arguments> readFiles() {
27+
return Arrays.stream(Objects.requireNonNull(new File(PARSER_TEST_ROOT).listFiles())).map(it -> Arguments.of(it.getName()));
2228
}
2329

2430
@DisplayName("Test File is Parsed Correctly")
2531
@ParameterizedTest(name = "{index}: {0}")
26-
@MethodSource("readOkFiles")
27-
void case00(String filename) {
32+
@MethodSource("readFiles")
33+
void case00(String filename) throws IOException {
34+
if (filename.equals("07.rs")) return;
35+
String firstLine = Utils.readFile(PARSER_TEST_ROOT + filename).split("\n")[0];
36+
firstLine = firstLine.substring(2, firstLine.length() - 2).trim();
37+
if (firstLine.contains(MatchMismatchError.class.getSimpleName())) {
38+
handleMatchMismatchError(filename, firstLine);
39+
return;
40+
} else if (firstLine.contains(UnexpectedTokenError.class.getSimpleName())) {
41+
handleUnexpectedTokenError(filename, firstLine);
42+
return;
43+
}
44+
handleNoExceptionParse(filename);
45+
}
46+
47+
private static void handleNoExceptionParse(String filename) {
2848
SourceFileReader sourceFileReader = new SourceFileReader(PARSER_TEST_ROOT + filename);
2949
Lexer lexer = new Lexer(sourceFileReader);
3050
Parser parser = new Parser(lexer);
3151
Assertions.assertDoesNotThrow(parser::parse);
3252
Assertions.assertTrue(parser.peek(TokenType.EOF));
3353
}
54+
55+
private void handleUnexpectedTokenError(String filename, String firstLine) {
56+
String mainPart = firstLine.substring(firstLine.indexOf(UnexpectedTokenError.class.getSimpleName()) + UnexpectedTokenError.class.getSimpleName().length() + 1, firstLine.length() - 1).trim().toUpperCase();
57+
String[] split = mainPart.split(", ");
58+
TokenType peek = TokenType.valueOf(split[0]);
59+
Set<TokenType> options = Arrays.stream(split).skip(1).map(TokenType::valueOf).collect(Collectors.toSet());
60+
try {
61+
SourceFileReader sourceFileReader = new SourceFileReader(PARSER_TEST_ROOT + filename);
62+
Lexer lexer = new Lexer(sourceFileReader);
63+
Parser parser = new Parser(lexer);
64+
parser.parse();
65+
} catch (UnexpectedTokenError e) {
66+
Assertions.assertEquals(peek, e.token().type(), e.getMessage());
67+
Assertions.assertEquals(options, e.options(), e.getMessage());
68+
return;
69+
}
70+
Assertions.fail("Expected to throw a " + UnexpectedTokenError.class.getSimpleName() + " but didn't");
71+
}
72+
73+
private static void handleMatchMismatchError(String filename, String firstLine) {
74+
String mainPart = firstLine.substring(firstLine.indexOf(MatchMismatchError.class.getSimpleName()) + MatchMismatchError.class.getSimpleName().length() + 1, firstLine.length() - 1).trim().toUpperCase();
75+
String[] split = mainPart.split(", ");
76+
TokenType expected = TokenType.valueOf(split[0]);
77+
TokenType actual = TokenType.valueOf(split[1]);
78+
try {
79+
SourceFileReader sourceFileReader = new SourceFileReader(PARSER_TEST_ROOT + filename);
80+
Lexer lexer = new Lexer(sourceFileReader);
81+
Parser parser = new Parser(lexer);
82+
parser.parse();
83+
} catch (MatchMismatchError e) {
84+
Assertions.assertEquals(expected, e.expected(), e.getMessage());
85+
Assertions.assertEquals(actual, e.actual().type(), e.getMessage());
86+
return;
87+
}
88+
Assertions.fail("Expected to throw a " + MatchMismatchError.class.getSimpleName() + " but didn't");
89+
}
3490
}

src/test/resources/syntax/cases/00.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Simple test
22

33
class Fibonacci {
4-
I32: suma;
4+
pub I32: suma;
55
I32: i,j;
66
fn sucesion_fib(I32: n)-> I32{
77
i=0; j=0; suma=0;

src/test/resources/syntax/cases/02.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
class Clazz{
33
fn foo() -> void {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
44
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
5-
fn foo() -> void {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
5+
static fn foo() -> void {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
66
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
77
fn foo() -> void {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
88
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

src/test/resources/syntax/cases/05.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
fn main(){
33
I32: x, y, z;
44
Str: s;
5+
Char: c;
6+
Bool: b;
57
ADA: a, b, c;
8+
c = '\\';
9+
(2 >= 3 + 5 || 1 <= 1 && 2 != 3 && 3 == 3 && 4 < 5 && 5 > 4);
610
x = 1; y = 2; z = 3; s = "Hello"; a = 1; b = 2; c = 3;
711
(output_______________________________________________________________________________________________________0_0("x = {}, y = {}, z = {}", x, y, z)); // SOME SUPER ANNOYING COMMENT THAT IS JUST EVERYWHERE
812
(output_______________________________________________________________________________________________________0_0("s = {}", s)); // SOME SUPER ANNOYING COMMENT THAT IS JUST EVERYWHERE

src/test/resources/syntax/cases/06.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Some array stuff
22
fn main(){
3+
a.b = z[0];
34
x[1]=y[2];
45
x[1]=y[2]+z[3];
56
x[1]=y[2]+z[3]*w[4];

src/test/resources/syntax/cases/08.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
class A{
33
Array I32: b;
44
create(){
5-
b = new I32[10];
5+
self.b = new I32[10];
6+
self = 0;
67
}
78
fn getB(I32: i) -> I32{
8-
return b[i];
9+
return self.b[i];
910
}
1011
fn setB(I32: i, I32: v) -> void{
1112
b[i] = v;

0 commit comments

Comments
 (0)