Skip to content

Commit 6563ba6

Browse files
authored
Merge pull request #150 from svmukhin/147
147 Migrate from unmaintained antlr4ts to official ANTLR4 TypeScript target
2 parents 4204a7a + e68f4dd commit 6563ba6

File tree

10 files changed

+35
-50
lines changed

10 files changed

+35
-50
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ src/parser/
1010
.claude/
1111
Eo.g4
1212
compiled/
13+
antlr4/

Makefile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ SHELL := bash
88

99
VERSION := 0.58.8
1010
GRAMMAR_URL := https://raw.githubusercontent.com/objectionary/eo/refs/tags/$(VERSION)/eo-parser/src/main/antlr4/org/eolang/parser/Eo.g4
11+
ANTLR4_VERSION := 4.13.2
12+
13+
# a few URLs for download antlr4 tools
14+
# ANTLR4_URL := https://github.com/antlr/website-antlr4/blob/gh-pages/download/antlr-${ANTLR4_VERSION}-complete.jar
15+
# ANTLR4_URL := https://repo1.maven.org/maven2/org/antlr/antlr4/${ANTLR4_VERSION}/antlr4-${ANTLR4_VERSION}-complete.jar
16+
ANTLR4_URL := https://www.antlr.org/download/antlr-${ANTLR4_VERSION}-complete.jar
1117

1218
TSS := $(shell find src -name '*.ts')
1319

@@ -30,8 +36,12 @@ tsc-compiled: $(TSS) Makefile
3036
Eo.g4: Makefile
3137
curl --silent $(GRAMMAR_URL) > $@
3238

33-
src/parser: Eo.g4 Makefile
34-
npx antlr4ts -visitor $< -o src/parser
39+
antlr4: Makefile
40+
mkdir -p $@
41+
curl --silent ${ANTLR4_URL} > $@/antlr4-${ANTLR4_VERSION}-complete.jar
42+
43+
src/parser: Eo.g4 antlr4 Makefile
44+
java -jar antlr4/antlr4-${ANTLR4_VERSION}-complete.jar -o src/parser -visitor -Dlanguage=TypeScript Eo.g4
3545

3646
test: build
3747
npx jest --coverage

package-lock.json

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

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
},
1111
"dependencies": {
1212
"antlr4": "^4.13.2",
13-
"antlr4ts": "^0.5.0-alpha.4",
1413
"vscode-languageserver": "^9.0.0",
1514
"vscode-languageserver-textdocument": "^1.0.4"
1615
},
@@ -28,7 +27,6 @@
2827
"@typescript-eslint/parser": "^8.0.0",
2928
"@vercel/ncc": "^0.38.3",
3029
"@yao-pkg/pkg": "^6.4.0",
31-
"antlr4ts-cli": "^0.5.0-alpha.4",
3230
"babel-jest": "^30.0.0",
3331
"eslint": "^8.28.0",
3432
"eslint-config-eslint": "^7.0.0",

src/IndentationLexer.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 Objectionary.com
22
// SPDX-License-Identifier: MIT
33

4-
import { Token, CommonToken, Lexer, CharStream } from "antlr4ts";
5-
import { EoLexer } from "./parser/EoLexer";
6-
import { EoParser } from "./parser/EoParser";
4+
import { Token, CommonToken, Lexer, CharStream } from "antlr4";
5+
import EoLexer from "./parser/EoLexer";
6+
import EoParser from "./parser/EoParser";
77

88
/**
99
* Custom lexer that wraps EoLexer to handle indentation-based TAB/UNTAB tokens
@@ -42,7 +42,7 @@ export class IndentationLexer extends Lexer {
4242
*/
4343
private emitIndent(shift: number): void {
4444
for (let i = 0; i < shift; i++) {
45-
this.emitToken(IndentationLexer.TAB);
45+
this.emitIndentationToken(IndentationLexer.TAB);
4646
}
4747
}
4848

@@ -53,7 +53,7 @@ export class IndentationLexer extends Lexer {
5353
*/
5454
private emitDedent(shift: number): void {
5555
for (let i = 0; i < shift; i++) {
56-
this.emitToken(IndentationLexer.UNTAB);
56+
this.emitIndentationToken(IndentationLexer.UNTAB);
5757
}
5858
}
5959

@@ -63,10 +63,10 @@ export class IndentationLexer extends Lexer {
6363
* @param type - Token type (must be IndentationLexer.TAB or IndentationLexer.UNTAB)
6464
* @returns Void
6565
*/
66-
private emitToken(type: number) {
67-
const tkn = new CommonToken(type, type === IndentationLexer.TAB ? "TAB" : "UNTAB");
66+
private emitIndentationToken(type: number) {
67+
const tkn = new CommonToken([this.wrapped, this._input], type, Token.DEFAULT_CHANNEL, 0, 0);
6868
tkn.line = this.wrapped.line;
69-
tkn.charPositionInLine = 0;
69+
tkn.column = 0;
7070
this.tokens.push(tkn);
7171
}
7272

@@ -176,10 +176,7 @@ export class IndentationLexer extends Lexer {
176176
get ruleNames(): string[] {
177177
return this.wrapped.ruleNames;
178178
}
179-
get serializedATN(): string {
179+
get serializedATN(): number[] {
180180
return this.wrapped.serializedATN;
181181
}
182-
get vocabulary(): any {
183-
return this.wrapped.vocabulary;
184-
}
185182
}

src/errorListener.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
import {
1010
Token as AntlrToken,
11-
ANTLRErrorListener,
11+
ErrorListener as ANTLRErrorListener,
1212
Recognizer,
1313
RecognitionException
14-
} from "antlr4ts";
14+
} from "antlr4";
1515
import { ParserError } from "./parserError";
1616

1717
export class ErrorListener implements ANTLRErrorListener<AntlrToken> {
@@ -21,7 +21,7 @@ export class ErrorListener implements ANTLRErrorListener<AntlrToken> {
2121
*/
2222
errors: ParserError[] = [];
2323

24-
syntaxError(recognizer: Recognizer<AntlrToken, any>, symbol: AntlrToken | undefined, line: number,
24+
syntaxError(recognizer: Recognizer<AntlrToken>, symbol: AntlrToken | undefined, line: number,
2525
position: number, msg: string, e: RecognitionException | undefined) {
2626
this.errors.push(new ParserError(line, position, msg));
2727
}

src/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import * as fs from "fs";
55
import * as path from "path";
6-
import { Token as AntlrToken } from "antlr4ts";
6+
import { Token as AntlrToken } from "antlr4";
77
import { Processor } from "./processor";
88
import { ParserError } from "./parserError";
99
import { ErrorListener } from "./errorListener";
@@ -91,7 +91,7 @@ export function getTokenTypes(): Set<string> {
9191
export function tokenize(input: string): AntlrToken[] {
9292
const processor = new Processor(input);
9393
processor.tokens.fill();
94-
return processor.tokens.getTokens();
94+
return processor.tokens.tokens;
9595
}
9696

9797
/**

src/processor.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
import {
55
CharStreams,
66
CommonTokenStream,
7-
CodePointCharStream
8-
} from "antlr4ts";
9-
import { EoLexer } from "./parser/EoLexer";
10-
import { EoParser } from "./parser/EoParser";
7+
CharStream
8+
} from "antlr4";
119
import { IndentationLexer } from "./IndentationLexer";
10+
import EoParser from "./parser/EoParser";
1211

1312
export class Processor {
1413

1514
/**
1615
* Text to be lexed and parsed
1716
*/
18-
stream: CodePointCharStream;
17+
stream: CharStream;
1918

2019
/**
2120
* EO grammar lexer with indentation support

src/semantics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ export class SemanticTokensProvider {
128128
}
129129
tokens.push({
130130
line: tk.line - 1,
131-
start: tk.charPositionInLine,
132-
length: tk.stopIndex - tk.startIndex + 1,
131+
start: tk.column,
132+
length: tk.stop - tk.start + 1,
133133
tokenType: legend,
134134
tokenModifier: 0
135135
});

test/indentation-lexer.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 Objectionary.com
22
// SPDX-License-Identifier: MIT
33

4-
import { CharStreams, Token, VocabularyImpl } from "antlr4ts";
4+
import { CharStreams, Token } from "antlr4";
55
import { IndentationLexer } from "../src/IndentationLexer";
6-
import { EoLexer } from "../src/parser/EoLexer";
6+
import EoLexer from "../src/parser/EoLexer";
77

88
describe("IndentationLexer", () => {
99

@@ -37,8 +37,6 @@ describe("IndentationLexer", () => {
3737
expect(lexer.ruleNames).toEqual(EoLexer.ruleNames);
3838
/* eslint-disable-next-line no-underscore-dangle */
3939
expect(lexer.serializedATN).toEqual(EoLexer._serializedATN);
40-
expect(lexer.vocabulary).toBeInstanceOf(VocabularyImpl);
41-
expect(lexer.vocabulary).toEqual(EoLexer.VOCABULARY);
4240
});
4341

4442
});

0 commit comments

Comments
 (0)