Skip to content

Commit 4e58e0a

Browse files
committed
Merge whitespace into a single token, errors now report with 1 length
1 parent 7d7b3ce commit 4e58e0a

File tree

185 files changed

+1200
-1418
lines changed

Some content is hidden

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

185 files changed

+1200
-1418
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</option>
1111
<option name="taskNames">
1212
<list>
13-
<option value=":test" />
13+
<option value=":ScriptingExample:test" />
1414
</list>
1515
</option>
1616
<option name="vmOptions" />

Parser/src/main/java/org/openzen/zenscript/lexer/ZSTokenType.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ public enum ZSTokenType implements TokenType {
44
T_COMMENT_SCRIPT("#[^\n]*[\n\\e]", true),
55
T_COMMENT_SINGLELINE("//[^\n]*", true),
66
T_COMMENT_MULTILINE("/\\*([^\\*]|(\\*+([^\\*/])))*\\*+/", true, true),
7-
T_WHITESPACE_SPACE(true, " ", " "),
8-
T_WHITESPACE_TAB(true, "\t", "\t"),
9-
T_WHITESPACE_NEWLINE(true, "\n", "\n", true),
10-
T_WHITESPACE_CARRIAGE_RETURN(true, "\r", "\r"),
7+
T_WHITESPACE("[ \t\r\n]+", true, true),
118
T_IDENTIFIER("@?[a-zA-Z_][a-zA-Z_0-9]*"),
129
T_LOCAL_IDENTIFIER("$[a-zA-Z_][a-zA-Z_0-9]*"),
1310
T_FLOAT("\\-?(0|[1-9][0-9]*)\\.[0-9]+([eE][\\+\\-]?[0-9]+)?[a-zA-Z_]*"),

Parser/src/main/java/org/openzen/zenscript/tree/Tree.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package org.openzen.zenscript.tree;
22

33
import org.openzen.zencode.shared.SourceFile;
4-
import org.openzen.zenscript.lexer.ParseException;
54
import org.openzen.zenscript.lexer.ZSToken;
65
import org.openzen.zenscript.lexer.ZSTokenType;
76
import org.openzen.zenscript.tree.lexer.DFALexer;
87
import org.openzen.zenscript.tree.lexer.PositionedToken;
98
import org.openzen.zenscript.tree.lexer.ZSPosTokenFactory;
109

11-
import java.io.IOException;
1210
import java.util.ArrayList;
1311
import java.util.List;
1412

@@ -27,13 +25,13 @@ public Tree(TreeKind kind) {
2725
this.children = new ArrayList<>();
2826
}
2927

30-
public static Tree parse(SourceFile sourceFile) throws IOException, ParseException {
28+
public static Tree parse(SourceFile sourceFile) {
3129
DFALexer dfaLexer = new DFALexer(new ZSPosTokenFactory());
3230
List<PositionedToken<ZSTokenType, ZSToken>> tokens = dfaLexer.tokenize(sourceFile);
3331
return parse(tokens);
3432
}
3533

36-
public static Tree parse(List<PositionedToken<ZSTokenType, ZSToken>> tokens) throws IOException, ParseException {
34+
public static Tree parse(List<PositionedToken<ZSTokenType, ZSToken>> tokens) {
3735
TreeParser p = new TreeParser(tokens);
3836
p.file();
3937
return p.buildTree();

Parser/src/main/java/org/openzen/zenscript/tree/TreeKind.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,7 @@ public enum TreeKind {
164164
T_COMMENT_SCRIPT,
165165
T_COMMENT_SINGLELINE,
166166
T_COMMENT_MULTILINE,
167-
T_WHITESPACE_SPACE,
168-
T_WHITESPACE_TAB,
169-
T_WHITESPACE_NEWLINE,
167+
T_WHITESPACE,
170168
T_IDENTIFIER,
171169
T_LOCAL_IDENTIFIER,
172170
T_FLOAT,
@@ -310,9 +308,7 @@ public boolean isToken() {
310308
case T_COMMENT_SCRIPT:
311309
case T_COMMENT_SINGLELINE:
312310
case T_COMMENT_MULTILINE:
313-
case T_WHITESPACE_SPACE:
314-
case T_WHITESPACE_TAB:
315-
case T_WHITESPACE_NEWLINE:
311+
case T_WHITESPACE:
316312
case T_IDENTIFIER:
317313
case T_LOCAL_IDENTIFIER:
318314
case T_FLOAT:
@@ -464,13 +460,8 @@ public static TreeKind fromTokenType(ZSTokenType type) {
464460
return TreeKind.T_COMMENT_SINGLELINE;
465461
case T_COMMENT_MULTILINE:
466462
return TreeKind.T_COMMENT_MULTILINE;
467-
case T_WHITESPACE_SPACE:
468-
return TreeKind.T_WHITESPACE_SPACE;
469-
case T_WHITESPACE_TAB:
470-
return TreeKind.T_WHITESPACE_TAB;
471-
case T_WHITESPACE_NEWLINE:
472-
case T_WHITESPACE_CARRIAGE_RETURN:
473-
return TreeKind.T_WHITESPACE_NEWLINE;
463+
case T_WHITESPACE:
464+
return TreeKind.T_WHITESPACE;
474465
case T_IDENTIFIER:
475466
return TreeKind.T_IDENTIFIER;
476467
case T_LOCAL_IDENTIFIER:

Parser/src/main/java/org/openzen/zenscript/tree/TreeParser.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,11 @@ public void advance() {
158158
}
159159

160160
public void error(String message) {
161-
events.add(Event.error(message, this.tokens.get(pos - 1).position()));
161+
int reportingPos = pos -1;
162+
// while(this.tokens.get(reportingPos).getType().isWhitespace() && reportingPos > 0) {
163+
// reportingPos--;
164+
// }
165+
events.add(Event.error(message, this.tokens.get(reportingPos).position().withLength(1)));
162166
}
163167

164168
public void advanceWithError(String error) {

Parser/src/test/java/org/openzen/zenscript/test/framework/ParsingTestCase.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,20 @@ public ParsingTestCase(Path zcFile, Path zastFile) {
2121

2222
public void execute() throws IOException {
2323
// Read file content
24-
try {
25-
if (Files.notExists(zcFile)) {
26-
Assertions.fail("File " + zcFile + " does not exist");
27-
}
28-
if (Files.notExists(zastFile)) {
29-
Assertions.fail("File " + zastFile + " does not exist");
30-
}
31-
String zcContent = new String(Files.readAllBytes(zcFile), StandardCharsets.UTF_8).replace("\r\n", "\n");
32-
String zastContent = new String(Files.readAllBytes(zastFile), StandardCharsets.UTF_8).replace("\r\n", "\n");
33-
Tree tree = Tree.parse(new LiteralSourceFile(zcFile.getFileName().toString(), zcContent));
34-
35-
if (!zastContent.equals(tree.toString())) {
36-
Assertions.fail(
37-
"Error in \nfile://" + zcFile.toUri().getPath() + "\nfile://" + zastFile.toUri().getPath() + "\n Expected tree:\n'" + zastContent + "'\nbut got:\n'" + tree + "'\n"
38-
);
39-
}
40-
} catch (ParseException e) {
41-
Assertions.fail(e);
24+
if (Files.notExists(zcFile)) {
25+
Assertions.fail("File " + zcFile + " does not exist");
26+
}
27+
if (Files.notExists(zastFile)) {
28+
Assertions.fail("File " + zastFile + " does not exist");
29+
}
30+
String zcContent = new String(Files.readAllBytes(zcFile), StandardCharsets.UTF_8).replace("\r\n", "\n");
31+
String zastContent = new String(Files.readAllBytes(zastFile), StandardCharsets.UTF_8).replace("\r\n", "\n");
32+
Tree tree = Tree.parse(new LiteralSourceFile(zcFile.getFileName().toString(), zcContent));
33+
34+
if (!zastContent.equals(tree.toString())) {
35+
Assertions.fail(
36+
"Error in \nfile://" + zcFile.toUri().getPath() + "\nfile://" + zastFile.toUri().getPath() + "\n Expected tree:\n'" + zastContent + "'\nbut got:\n'" + tree + "'\n"
37+
);
4238
}
4339

4440
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
FILE
22
DEF_ALIAS
33
K_ALIAS "alias"
4-
T_WHITESPACE_NEWLINE "\n"
5-
T_WHITESPACE_NEWLINE "\n"
4+
T_WHITESPACE "\n\n"
65
DEF_ALIAS
76
K_ALIAS "alias"
8-
T_WHITESPACE_SPACE " "
7+
T_WHITESPACE " "
98
NAME
109
T_IDENTIFIER "foo"
11-
T_WHITESPACE_SPACE " "
10+
T_WHITESPACE " "
1211
TYPE_DECLARATION
1312
K_AS "as"
14-
T_WHITESPACE_SPACE " "
13+
T_WHITESPACE " "
1514
TYPE
1615
NAME
1716
T_IDENTIFIER "Bar"
1817
T_SEMICOLON ";"
19-
(2:0->3:0)expected a name
20-
(2:0->3:0)expected as
18+
(1:5->1:6)expected a name
19+
(1:5->1:6)expected as
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
FILE
22
DEF_CLASS
33
K_CLASS "class"
4-
T_WHITESPACE_NEWLINE "\n"
5-
T_WHITESPACE_NEWLINE "\n"
4+
T_WHITESPACE "\n\n"
65
DEF_CLASS
76
K_CLASS "class"
8-
T_WHITESPACE_SPACE " "
7+
T_WHITESPACE " "
98
NAME
109
T_IDENTIFIER "Foo"
11-
T_WHITESPACE_SPACE " "
10+
T_WHITESPACE " "
1211
MEMBERS
1312
T_AOPEN "{"
1413
T_ACLOSE "}"
15-
(2:0->3:0)expected a name
16-
(2:0->3:0)expected '{'
14+
(1:5->1:6)expected a name
15+
(1:5->1:6)expected '{'
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
FILE
22
DEF_ENUM
33
K_ENUM "enum"
4-
T_WHITESPACE_NEWLINE "\n"
5-
T_WHITESPACE_NEWLINE "\n"
4+
T_WHITESPACE "\n\n"
65
DEF_ENUM
76
K_ENUM "enum"
8-
T_WHITESPACE_SPACE " "
7+
T_WHITESPACE " "
98
NAME
109
T_IDENTIFIER "Foo"
11-
T_WHITESPACE_SPACE " "
10+
T_WHITESPACE " "
1211
T_AOPEN "{"
1312
T_ACLOSE "}"
14-
(2:0->3:0)expected a name
15-
(2:0->3:0)expected '{'
13+
(1:4->1:5)expected a name
14+
(1:4->1:5)expected '{'
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
FILE
22
DEF_EXPANSION
33
K_EXPAND "expand"
4-
T_WHITESPACE_NEWLINE "\n"
5-
T_WHITESPACE_NEWLINE "\n"
4+
T_WHITESPACE "\n\n"
65
DEF_EXPANSION
76
K_EXPAND "expand"
8-
T_WHITESPACE_SPACE " "
7+
T_WHITESPACE " "
98
TYPE
109
NAME
1110
T_IDENTIFIER "Foo"
12-
T_WHITESPACE_SPACE " "
11+
T_WHITESPACE " "
1312
MEMBERS
1413
T_AOPEN "{"
1514
T_ACLOSE "}"
16-
(2:0->3:0)expected type
15+
(1:6->1:7)expected type

0 commit comments

Comments
 (0)