Skip to content

Commit ffc2a59

Browse files
committed
переезд на свой антлр
1 parent d292f16 commit ffc2a59

File tree

7 files changed

+157
-37
lines changed

7 files changed

+157
-37
lines changed

.github/workflows/check.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
fail-fast: false
1010
matrix:
11-
java_version: ['17', '20']
11+
java_version: ['17', '21']
1212
os: [ubuntu-latest, windows-latest, macOS-latest]
1313
steps:
1414
- uses: actions/checkout@v4

build.gradle.kts

+8-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ plugins {
2121
repositories {
2222
mavenLocal()
2323
mavenCentral()
24-
maven(url = "https://jitpack.io")
24+
maven(url = "https://s01.oss.sonatype.org/content/repositories/snapshots")
2525
}
2626

2727
group = "io.github.1c-syntax"
@@ -43,8 +43,8 @@ gitVersioning.apply {
4343
val isSnapshot = gitVersioning.gitVersionDetails.refType != GitRefType.TAG
4444

4545
dependencies {
46-
implementation("org.antlr", "antlr4", "4.13.1")
47-
implementation("com.github.1c-syntax", "utils", "0.5.1")
46+
implementation("io.github.1c-syntax", "antlr4", "0.1.1")
47+
implementation("io.github.1c-syntax", "utils", "0.6.2")
4848
implementation("commons-io", "commons-io", "2.15.1")
4949
}
5050

@@ -78,7 +78,7 @@ tasks.check {
7878
tasks.jacocoTestReport {
7979
reports {
8080
xml.required.set(true)
81-
xml.outputLocation.set(File("$buildDir/reports/jacoco/test/jacoco.xml"))
81+
xml.outputLocation.set(File("${layout.buildDirectory.get()}/reports/jacoco/test/jacoco.xml"))
8282
}
8383
}
8484

@@ -105,7 +105,10 @@ sonar {
105105
property("sonar.projectKey", "1c-syntax_bsl-parser-core")
106106
property("sonar.projectName", "BSL Parser Core")
107107
property("sonar.scm.exclusions.disabled", "true")
108-
property("sonar.coverage.jacoco.xmlReportPaths", "$buildDir/reports/jacoco/test/jacoco.xml")
108+
property(
109+
"sonar.coverage.jacoco.xmlReportPaths",
110+
"${layout.buildDirectory.get()}/reports/jacoco/test/jacoco.xml"
111+
)
109112
}
110113
}
111114

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is a part of BSL Parser Core.
33
*
4-
* Copyright (c) 2018-2024
4+
* Copyright (c) 2018-2025
55
* Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]>, Valery Maximov <[email protected]>
66
*
77
* SPDX-License-Identifier: LGPL-3.0-or-later

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is a part of BSL Parser Core.
33
*
4-
* Copyright (c) 2018-2024
4+
* Copyright (c) 2018-2025
55
* Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]>, Valery Maximov <[email protected]>
66
*
77
* SPDX-License-Identifier: LGPL-3.0-or-later
@@ -25,12 +25,14 @@
2525
import org.antlr.v4.runtime.Lexer;
2626
import org.antlr.v4.runtime.atn.ATN;
2727
import org.antlr.v4.runtime.atn.LexerATNSimulator;
28-
import org.antlr.v4.runtime.atn.PredictionContextCache;
29-
import org.antlr.v4.runtime.dfa.DFA;
3028

3129
public class CRAwareLexerATNSimulator extends LexerATNSimulator {
32-
public CRAwareLexerATNSimulator(Lexer recog, ATN atn, DFA[] dfas, PredictionContextCache sharedContextCache) {
33-
super(recog, atn, dfas, sharedContextCache);
30+
public CRAwareLexerATNSimulator(ATN atn) {
31+
super(atn);
32+
}
33+
34+
public CRAwareLexerATNSimulator(Lexer recog, ATN atn) {
35+
super(recog, atn);
3436
}
3537

3638
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* This file is a part of BSL Parser Core.
3+
*
4+
* Copyright (c) 2018-2025
5+
* Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]>, Valery Maximov <[email protected]>
6+
*
7+
* SPDX-License-Identifier: LGPL-3.0-or-later
8+
*
9+
* BSL Parser Core is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation; either
12+
* version 3.0 of the License, or (at your option) any later version.
13+
*
14+
* BSL Parser Core is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public
20+
* License along with BSL Parser Core.
21+
*/
22+
/*
23+
Originally released by BSD 3-clause license:
24+
25+
Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
26+
27+
Redistribution and use in source and binary forms, with or without
28+
modification, are permitted provided that the following conditions
29+
are met:
30+
31+
1. Redistributions of source code must retain the above copyright
32+
notice, this list of conditions and the following disclaimer.
33+
2. Redistributions in binary form must reproduce the above copyright
34+
notice, this list of conditions and the following disclaimer in the
35+
documentation and/or other materials provided with the distribution.
36+
3. Neither the name of the copyright holder nor the names of its contributors
37+
may be used to endorse or promote products derived from this software
38+
without specific prior written permission.
39+
40+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
41+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42+
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
43+
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
44+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
46+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
47+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
48+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
49+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50+
*/
51+
package com.github._1c_syntax.bsl.parser;
52+
53+
import org.antlr.v4.runtime.CharStream;
54+
import org.antlr.v4.runtime.UnicodeCharStream;
55+
import org.antlr.v4.runtime.misc.Interval;
56+
57+
/**
58+
* This class supports case-insensitive lexing by wrapping an existing
59+
* {@link CharStream} and forcing the lexer to see either upper or
60+
* lowercase characters. Grammar literals should then be either upper or
61+
* lower case such as 'BEGIN' or 'begin'. The text of the character
62+
* stream is unaffected. Example: input 'BeGiN' would match lexer rule
63+
* 'BEGIN' if constructor parameter upper=true but getText() would return
64+
* 'BeGiN'.
65+
*/
66+
public class CaseChangingCharStream implements UnicodeCharStream {
67+
68+
private final UnicodeCharStream stream;
69+
70+
/**
71+
* Constructs a new CaseChangingCharStream wrapping the given {@link UnicodeCharStream} forcing
72+
* all characters to upper case.
73+
*
74+
* @param stream The stream to wrap.
75+
*/
76+
public CaseChangingCharStream(UnicodeCharStream stream) {
77+
this.stream = stream;
78+
}
79+
80+
@Override
81+
public String getText(Interval interval) {
82+
return stream.getText(interval);
83+
}
84+
85+
@Override
86+
public void consume() {
87+
stream.consume();
88+
}
89+
90+
@Override
91+
public int LA(int i) {
92+
int c = stream.LA(i);
93+
if (c <= 0) {
94+
return c;
95+
}
96+
return Character.toUpperCase(c);
97+
}
98+
99+
@Override
100+
public int mark() {
101+
return stream.mark();
102+
}
103+
104+
@Override
105+
public void release(int marker) {
106+
stream.release(marker);
107+
}
108+
109+
@Override
110+
public int index() {
111+
return stream.index();
112+
}
113+
114+
@Override
115+
public void seek(int index) {
116+
stream.seek(index);
117+
}
118+
119+
@Override
120+
public int size() {
121+
return stream.size();
122+
}
123+
124+
@Override
125+
public String getSourceName() {
126+
return stream.getSourceName();
127+
}
128+
129+
@Override
130+
public boolean supportsUnicodeCodePoints() {
131+
return stream.supportsUnicodeCodePoints();
132+
}
133+
}

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

+6-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is a part of BSL Parser Core.
33
*
4-
* Copyright (c) 2018-2024
4+
* Copyright (c) 2018-2025
55
* Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]>, Valery Maximov <[email protected]>
66
*
77
* SPDX-License-Identifier: LGPL-3.0-or-later
@@ -27,7 +27,6 @@
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;
3130
import org.antlr.v4.runtime.Lexer;
3231
import org.antlr.v4.runtime.Parser;
3332
import org.antlr.v4.runtime.Token;
@@ -37,13 +36,12 @@
3736

3837
import java.io.IOException;
3938
import java.io.InputStream;
39+
import java.io.InputStreamReader;
40+
import java.io.Reader;
4041
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;
4544
import java.util.List;
46-
import java.util.Optional;
4745

4846
import static java.util.Objects.requireNonNull;
4947
import static org.antlr.v4.runtime.Token.EOF;
@@ -64,8 +62,6 @@ public abstract class Tokenizer<T extends BSLParserRuleContext, P extends Parser
6462
private final Class<P> parserClass;
6563
protected P parser;
6664

67-
private final Optional<Method> setInputStreamMethod;
68-
6965
protected Tokenizer(String content, Lexer lexer, Class<P> parserClass) {
7066
this(IOUtils.toInputStream(content, StandardCharsets.UTF_8), lexer, parserClass);
7167
}
@@ -76,11 +72,6 @@ protected Tokenizer(InputStream content, Lexer lexer, Class<P> parserClass) {
7672
this.content = content;
7773
this.lexer = lexer;
7874
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();
8475
}
8576

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

9586
/**
9687
* Возвращает абстрактное синтаксическое дерево, полученное на основании парсинга
97-
*
9888
* @return AST
9989
*/
10090
public T getAst() {
@@ -133,23 +123,15 @@ private CommonTokenStream computeTokenStream() {
133123

134124
try (
135125
var ubis = new UnicodeBOMInputStream(content);
126+
Reader inputStreamReader = new InputStreamReader(ubis, StandardCharsets.UTF_8)
136127
) {
137128
ubis.skipBOM();
138-
input = CharStreams.fromStream(ubis);
129+
input = CharStreams.fromReader(inputStreamReader);
139130
} catch (IOException e) {
140131
throw new RuntimeException(e);
141132
}
142133

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-
134+
lexer.setInputStream(input);
153135
lexer.removeErrorListener(ConsoleErrorListener.INSTANCE);
154136

155137
var tempTokenStream = new CommonTokenStream(lexer);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is a part of BSL Parser Core.
33
*
4-
* Copyright (c) 2018-2024
4+
* Copyright (c) 2018-2025
55
* Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]>, Valery Maximov <[email protected]>
66
*
77
* SPDX-License-Identifier: LGPL-3.0-or-later

0 commit comments

Comments
 (0)