Skip to content

Commit 80a4a86

Browse files
authored
Represent scanner tokens as enums internally (eclipse-jdt#3303) (eclipse-jdt#3772)
* Represent scanner tokens as enums internally (eclipse-jdt#3303) - Token production and processing is not type checkable from tokenization to parser tables - Renames `TerminalTokens` to `TerminalToken` - LexStream uses enums, but `DiagnoseParser`'s mix of terminals and non-terminal int id is retained.
1 parent ae098b1 commit 80a4a86

74 files changed

Lines changed: 2339 additions & 2111 deletions

File tree

Some content is hidden

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

org.eclipse.jdt.core.compiler.batch/scripts/GenerateParserScript.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,21 @@ public static void main(String[] args) throws IOException, InterruptedException
5959
Files.move(new File(filename).toPath(), new File(parserDir, filename).toPath(), StandardCopyOption.REPLACE_EXISTING);
6060
}
6161

62-
// Update TerminalTokens.java
62+
// Update terminalToken.java
6363
File javasymFile = new File(grammarDir, "javasym.java");
64-
File terminalTokensFile = new File(parserDir, "TerminalTokens.java");
64+
File terminalTokenFile = new File(parserDir, "TerminalToken.java");
6565
String javasymText = new String(Files.readAllBytes(javasymFile.toPath())).replace("\r\n", "\n");
66-
String terminalTokensText = new String(Files.readAllBytes(terminalTokensFile.toPath())).replace("\r\n", "\n");
66+
String terminalTokenText = new String(Files.readAllBytes(terminalTokenFile.toPath())).replace("\r\n", "\n");
6767
{
6868
String startTag = "// BEGIN_AUTOGENERATED_REGION\n";
69-
int start = terminalTokensText.indexOf(startTag);
69+
String endTag = "// END_AUTOGENERATED_REGION\n";
70+
int start = terminalTokenText.indexOf(startTag);
7071
assertTrue(start >= 0);
7172
start += startTag.length();
72-
String terminalTokensProlog = terminalTokensText.substring(0, start);
73+
int end = terminalTokenText.indexOf(endTag);
74+
assertTrue(end >= 0);
75+
String terminalTokenProlog = terminalTokenText.substring(0, start);
76+
String terminalTokenEpilog = terminalTokenText.substring(end);
7377

7478
String javasymProlog =
7579
"interface javasym\n" +
@@ -82,7 +86,10 @@ public static void main(String[] args) throws IOException, InterruptedException
8286
javasymText = javasymText.replace("TokenName$eof", "TokenNameEOF");
8387
javasymText = javasymText.replace("TokenName$error", "TokenNameERROR");
8488
javasymText = javasymText.replace("TokenNamenon-sealed", "TokenNamenon_sealed");
85-
Files.write(terminalTokensFile.toPath(), (terminalTokensProlog + "\tint " + javasymText).getBytes());
89+
javasymText = javasymText.replace("}", "");
90+
javasymText = javasymText.replaceAll("\\s*=\\s*(\\d+)", "($1)");
91+
92+
Files.write(terminalTokenFile.toPath(), (terminalTokenProlog + "\t\t\t\t\t\t\t" + javasymText + terminalTokenEpilog).getBytes());
8693
}
8794

8895
// Update ParserBasicInformation.java

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/AbstractCommentParser.java

Lines changed: 259 additions & 245 deletions
Large diffs are not rendered by default.

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/ConflictedParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public interface ConflictedParser {
2020
whether an @ begins a SE8 style type annotation or a SE5 declaration annotation. Where they can co-exist,
2121
we treat the type annotation as a declarative annotation.
2222
*/
23-
boolean atConflictScenario(int token);
23+
boolean atConflictScenario(TerminalToken token);
2424

2525
/* Return true if at the configuration the parser finds itself in, it would shift the token.
2626
It is axiomatic of the push down automaton that corresponds to the LALR grammar that it
2727
will never shift on invalid input.
2828
*/
29-
boolean automatonWillShift(int token);
29+
boolean automatonWillShift(TerminalToken token);
3030

3131
/*
3232
* Return true if the parser is parsing a module declaration. In Java 9, module, requires, exports,

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/JavadocParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,12 @@ protected void addSnippetInnerTag(Object tag, Object snippetTag) {
404404
}
405405

406406
@Override
407-
protected Object createTypeReference(int primitiveToken) {
407+
protected Object createTypeReference(TerminalToken primitiveToken) {
408408
return createTypeReference(primitiveToken, false);
409409
}
410410

411411
@Override
412-
protected Object createTypeReference(int primitiveToken, boolean canBeModule) {
412+
protected Object createTypeReference(TerminalToken primitiveToken, boolean canBeModule) {
413413
TypeReference typeRef = null;
414414
int size = this.identifierLengthStack[this.identifierLengthPtr];
415415
if (size == 1) { // Single Type ref
@@ -440,7 +440,7 @@ protected JavadocModuleReference createModuleReference(int moduleRefTokenCount)
440440
}
441441

442442
@Override
443-
protected Object createModuleTypeReference(int primitiveToken, int moduleRefTokenCount) {
443+
protected Object createModuleTypeReference(TerminalToken primitiveToken, int moduleRefTokenCount) {
444444
JavadocModuleReference moduleRef= createModuleReference(moduleRefTokenCount);
445445

446446
TypeReference typeRef = null;

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/JavadocScanner.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.internal.compiler.parser;
1515

16+
import static org.eclipse.jdt.internal.compiler.parser.TerminalToken.TokenNameSingleQuoteStringLiteral;
17+
import static org.eclipse.jdt.internal.compiler.parser.TerminalToken.TokenNameStringLiteral;
18+
1619
import org.eclipse.jdt.core.compiler.InvalidInputException;
1720
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
1821

@@ -123,7 +126,7 @@ public JavadocScanner(
123126
}
124127

125128
@Override
126-
protected int scanForStringLiteral() throws InvalidInputException {
129+
protected TerminalToken scanForStringLiteral() throws InvalidInputException {
127130
if (this.considerRegexInStringLiteral) {
128131
this.unicodeAsBackSlash = false;
129132
boolean isUnicode = false;
@@ -250,15 +253,15 @@ protected int scanForStringLiteral() throws InvalidInputException {
250253
}
251254

252255
@Override
253-
protected int processSingleQuotes(boolean checkIfUnicode) throws InvalidInputException{
256+
protected TerminalToken processSingleQuotes(boolean checkIfUnicode) throws InvalidInputException{
254257
if (this.tokenizeSingleQuotes) {
255258
return scanForSingleQuoteStringLiteral();
256259
} else {
257260
return super.processSingleQuotes(checkIfUnicode);
258261
}
259262
}
260263

261-
protected int scanForSingleQuoteStringLiteral() throws InvalidInputException {
264+
protected TerminalToken scanForSingleQuoteStringLiteral() throws InvalidInputException {
262265
this.unicodeAsBackSlash = false;
263266
boolean isUnicode = false;
264267
try {

0 commit comments

Comments
 (0)