Skip to content

Commit 528e206

Browse files
committed
переезд на antlr 4.13.1
1 parent ae01b3c commit 528e206

File tree

2 files changed

+23
-138
lines changed

2 files changed

+23
-138
lines changed

src/main/java/com/github/_1c_syntax/bsl/parser/CaseChangingCharStream.java

Lines changed: 0 additions & 133 deletions
This file was deleted.

src/main/java/com/github/_1c_syntax/bsl/parser/Tokenizer.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.antlr.v4.runtime.CommonToken;
2828
import org.antlr.v4.runtime.CommonTokenStream;
2929
import org.antlr.v4.runtime.ConsoleErrorListener;
30+
import org.antlr.v4.runtime.IntStream;
3031
import org.antlr.v4.runtime.Lexer;
3132
import org.antlr.v4.runtime.Parser;
3233
import org.antlr.v4.runtime.Token;
@@ -36,12 +37,13 @@
3637

3738
import java.io.IOException;
3839
import java.io.InputStream;
39-
import java.io.InputStreamReader;
40-
import java.io.Reader;
4140
import java.lang.reflect.InvocationTargetException;
41+
import java.lang.reflect.Method;
4242
import java.nio.charset.StandardCharsets;
4343
import java.util.ArrayList;
44+
import java.util.Arrays;
4445
import java.util.List;
46+
import java.util.Optional;
4547

4648
import static java.util.Objects.requireNonNull;
4749
import static org.antlr.v4.runtime.Token.EOF;
@@ -62,6 +64,8 @@ public abstract class Tokenizer<T extends BSLParserRuleContext, P extends Parser
6264
private final Class<P> parserClass;
6365
protected P parser;
6466

67+
private final Optional<Method> setInputStreamMethod;
68+
6569
protected Tokenizer(String content, Lexer lexer, Class<P> parserClass) {
6670
this(IOUtils.toInputStream(content, StandardCharsets.UTF_8), lexer, parserClass);
6771
}
@@ -72,6 +76,11 @@ protected Tokenizer(InputStream content, Lexer lexer, Class<P> parserClass) {
7276
this.content = content;
7377
this.lexer = lexer;
7478
this.parserClass = parserClass;
79+
var methods = lexer.getClass().getMethods();
80+
setInputStreamMethod = Arrays.stream(methods)
81+
.filter(method -> "setInputStream".equals(method.getName())
82+
&& method.getParameterCount() == 1
83+
&& method.getParameterTypes()[0] == IntStream.class).findFirst();
7584
}
7685

7786
/**
@@ -85,6 +94,7 @@ public List<Token> getTokens() {
8594

8695
/**
8796
* Возвращает абстрактное синтаксическое дерево, полученное на основании парсинга
97+
*
8898
* @return AST
8999
*/
90100
public T getAst() {
@@ -123,15 +133,23 @@ private CommonTokenStream computeTokenStream() {
123133

124134
try (
125135
var ubis = new UnicodeBOMInputStream(content);
126-
Reader inputStreamReader = new InputStreamReader(ubis, StandardCharsets.UTF_8)
127136
) {
128137
ubis.skipBOM();
129-
input = CharStreams.fromReader(inputStreamReader);
138+
input = CharStreams.fromStream(ubis);
130139
} catch (IOException e) {
131140
throw new RuntimeException(e);
132141
}
133142

134-
lexer.setInputStream(input);
143+
if (setInputStreamMethod.isPresent()) {
144+
try {
145+
setInputStreamMethod.get().invoke(lexer, input);
146+
} catch (IllegalAccessException | InvocationTargetException e) {
147+
throw new RuntimeException(e);
148+
}
149+
} else {
150+
lexer.setInputStream(input);
151+
}
152+
135153
lexer.removeErrorListener(ConsoleErrorListener.INSTANCE);
136154

137155
var tempTokenStream = new CommonTokenStream(lexer);

0 commit comments

Comments
 (0)