Skip to content

Commit 4779134

Browse files
authored
v0.3 prerelease (#29)
1 parent 4eecc87 commit 4779134

Some content is hidden

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

69 files changed

+4320
-809
lines changed

.github/workflows/antlr.yml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@ on:
44
push:
55
branches: [ master ]
66
pull_request:
7-
branches:
8-
- master
9-
- v0.3
10-
11-
workflow_dispatch:
7+
branches: [ master ]
128

139
env:
1410
CARGO_TERM_COLOR: always
15-
RUST_VERSION: nightly-2021-11-30
1611

1712
jobs:
1813
tests-antlr:
@@ -35,7 +30,7 @@ jobs:
3530
- name: Install Rust
3631
uses: actions-rs/toolchain@v1
3732
with:
38-
toolchain: ${{ env.RUST_VERSION }}
33+
toolchain: stable
3934
default: true
4035
- name: Cache local Maven repository
4136
uses: actions/cache@v2
@@ -64,12 +59,15 @@ jobs:
6459
runs-on: ubuntu-latest
6560
outputs:
6661
new_version: ${{ steps.check_ver.outputs.new_version }}
62+
strategy:
63+
matrix:
64+
rust_version: [ 1.52 , stable]
6765
steps:
6866
- uses: actions/checkout@v2
6967
- name: Install Rust
7068
uses: actions-rs/toolchain@v1
7169
with:
72-
toolchain: ${{ env.RUST_VERSION }}
70+
toolchain: ${{ matrix.rust_version }}
7371
default: true
7472
components: rustfmt
7573
- name: Build
@@ -81,7 +79,6 @@ jobs:
8179
- uses: actions/checkout@v2
8280
if: ${{ github.event_name == 'push' }}
8381
with:
84-
# todo change to `fetch-depth: 2` and HEAD^
8582
ref: ${{ github.event.before }}
8683
path: base_branch
8784
- name: check version bump
@@ -110,7 +107,7 @@ jobs:
110107
- name: Install Rust
111108
uses: actions-rs/toolchain@v1
112109
with:
113-
toolchain: ${{ env.RUST_VERSION }}
110+
toolchain: stable
114111
default: true
115112
- name: Cargo publish check
116113
run: |

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "antlr-rust"
3-
version = "0.2.2"
3+
version = "0.3.0-beta"
44
authors = ["Konstantin Anisimov <[email protected]>"]
55
homepage = "https://github.com/rrevenantt/antlr4rust"
66
repository = "https://github.com/rrevenantt/antlr4rust"
@@ -23,7 +23,8 @@ bit-set = "=0.5.*"
2323
once_cell = "^1.2"
2424
#backtrace = "=0.3"
2525
typed-arena = "^2.0"
26-
better_any = "=0.1"
26+
better_any = "0.2.0-dev.1"
27+
#better_any = "=0.1"
2728
parking_lot = "0.11"
2829
#qcell = { path="../qcell" }
2930

README.md

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# antlr4rust
2-
[![Crate](https://flat.badgen.net/crates/v/antlr-rust)](https://crates.io/crates/antlr_rust/0.2.0)
3-
[![docs](https://flat.badgen.net/badge/docs.rs/v0.2.0)](https://docs.rs/antlr-rust/0.2.0)
2+
[![Crate](https://flat.badgen.net/crates/v/antlr-rust)](https://crates.io/crates/antlr_rust/0.3.0-beta)
3+
[![docs](https://flat.badgen.net/badge/docs.rs/v0.3.0-beta)](https://docs.rs/antlr-rust/0.3.0-beta)
44
![ANTLR4 testsuite](https://github.com/rrevenantt/antlr4rust/workflows/ANTLR4%20testsuite/badge.svg?event=push)
55
![cargo test](https://github.com/rrevenantt/antlr4rust/workflows/cargo%20test/badge.svg)
66
[![](https://tokei.rs/b1/github/rrevenantt/antlr4rust)](https://github.com/rrevenantt/antlr4rust)
@@ -27,29 +27,10 @@ But if you want to build or change generator yourself:
2727
For now development is going on in this repository
2828
but eventually it will be merged to main ANTLR4 repo
2929

30-
Currently, requires nightly version of rust.
31-
This likely will be the case until `coerce_unsized` or some kind of coercion trait is stabilized.
32-
There are other unstable features in use but only `CoerceUnsized` is essential.
33-
34-
Remaining things before merge:
35-
- API stabilization
36-
- [ ] Rust api guidelines compliance
37-
- [ ] more tests for API because it is quite different from Java
38-
39-
Can be done after merge:
40-
- Documentation
41-
- [ ] Some things are already documented but still far from perfect, also more links needed.
42-
- cfg to not build potentially unnecessary parts
43-
(no Lexer if custom token stream, no ParserATNSimulator if LL(1) grammar)
44-
- run rustfmt on generated parser
45-
###### Long term improvements
46-
- generate enum for labeled alternatives without redundant `Error` option
47-
- option to generate fields instead of getters by default and make visiting based on fields
48-
- make tree generic over pointer type and allow tree nodes to arena.
49-
(requires GAT, otherwise it would be a problem for users that want ownership for parse tree)
50-
- support stable rust
51-
- support no_std(although alloc would still be required)
52-
30+
Since version `0.3` works on stable rust.
31+
Previous versions are not maintained any more
32+
so in case of nightly breakage you should migrate to the latest version.
33+
5334
### Usage
5435

5536
You should use the ANTLR4 "tool" to generate a parser, that will use the ANTLR
@@ -67,9 +48,8 @@ Then add following to `Cargo.toml` of the crate from which generated parser
6748
is going to be used:
6849
```toml
6950
[dependencies]
70-
antlr-rust = "=0.2"
51+
antlr-rust = "0.3"
7152
```
72-
and `#![feature(try_blocks)]` in your project root module.
7353

7454
### Parse Tree structure
7555

@@ -117,9 +97,11 @@ Also while structs used by generated lexer and parser were customized to track a
11797
internals of the lexer cannot be customized enough yet and still track quite a lot of data that might not be used in particular case.
11898
So there is still room for improvement.
11999
```text
100+
lexers:
120101
large/large_xmlparser time: [1.8598 ms 1.8607 ms 1.8619 ms]
121102
large/large_quick_xml time: [1.4623 ms 1.4645 ms 1.4675 ms]
122103
large/large_antlr_xml_lexer time: [5.7866 ms 5.7877 ms 5.7891 ms]
104+
parsers:
123105
large/large_xmlrs time: [16.734 ms 16.748 ms 16.766 ms]
124106
large/large_minidom time: [7.0639 ms 7.0792 ms 7.0975 ms]
125107
large/large_roxmltree time: [4.9341 ms 4.9360 ms 4.9380 ms]
@@ -140,3 +122,4 @@ BSD 3-clause.
140122
Unless you explicitly state otherwise,
141123
any contribution intentionally submitted for inclusion in this project by you
142124
shall be licensed as above, without any additional terms or conditions.
125+

build.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,24 @@ use std::process::Command;
99

1010
fn main() {
1111
let grammars = vec![
12+
"VisitorBasic",
13+
"VisitorCalc",
1214
"CSV",
1315
"ReferenceToATN",
1416
"XMLLexer",
1517
"SimpleLR",
1618
"Labels",
1719
"FHIRPath",
1820
];
19-
let additional_args = vec![Some("-visitor"), None, None, None, None];
21+
let additional_args = vec![
22+
Some("-visitor"),
23+
Some("-visitor"),
24+
Some("-visitor"),
25+
None,
26+
None,
27+
None,
28+
None,
29+
];
2030
let antlr_path = "/home/rrevenantt/dev/antlr4/tool/target/antlr4-4.8-2-SNAPSHOT-complete.jar";
2131

2232
for (grammar, arg) in grammars.into_iter().zip(additional_args) {

grammars/VisitorBasic.g4

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
grammar VisitorBasic;
2+
3+
s
4+
: 'A' EOF
5+
;
6+
7+
A : 'A';

grammars/VisitorCalc.g4

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
grammar VisitorCalc;
2+
3+
s
4+
: expr EOF
5+
;
6+
7+
expr
8+
: INT # number
9+
| expr (MUL | DIV) expr # multiply
10+
| expr (ADD | SUB) expr # add
11+
;
12+
13+
INT : [0-9]+;
14+
MUL : '*';
15+
DIV : '/';
16+
ADD : '+';
17+
SUB : '-';
18+
WS : [ \t]+ -> channel(HIDDEN);

rustfmt.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
edition = "2018"
2-
fn_single_line = true
3-
fn_params_layout = "Compressed"
2+
#fn_single_line = true
3+
#fn_args_layout = "Compressed"

src/atn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Debug for ATN {
5555
}
5656

5757
impl ATN {
58-
crate fn new_atn(grammar_type: ATNType, max_token_type: isize) -> ATN {
58+
pub(crate) fn new_atn(grammar_type: ATNType, max_token_type: isize) -> ATN {
5959
ATN {
6060
decision_to_state: Vec::new(),
6161
grammar_type,
@@ -93,7 +93,7 @@ impl ATN {
9393
analyzer.look::<Ctx>(s, None, _ctx)
9494
}
9595

96-
crate fn add_state(&mut self, state: Box<dyn ATNState>) {
96+
pub(crate) fn add_state(&mut self, state: Box<dyn ATNState>) {
9797
debug_assert_eq!(state.get_state_number(), self.states.len());
9898
self.states.push(state)
9999
}

src/atn_config.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ impl PartialEq for ATNConfig {
3232
fn eq(&self, other: &Self) -> bool {
3333
self.get_state() == other.get_state()
3434
&& self.get_alt() == other.get_alt()
35-
&& (Arc::ptr_eq(self.get_context().unwrap(), other.get_context().unwrap())
36-
|| self.get_context() == other.get_context())
35+
// Arc is optimized to not do a deep equalitiy if arc pointers are equal so that's enough
36+
&& self.context == other.context
3737
&& self.get_type() == other.get_type()
3838
&& self.semantic_context == other.semantic_context
3939
&& self.precedence_filter_suppressed == other.precedence_filter_suppressed
@@ -202,25 +202,41 @@ impl ATNConfig {
202202
new
203203
}
204204

205-
pub fn get_state(&self) -> ATNStateRef { self.state }
205+
pub fn get_state(&self) -> ATNStateRef {
206+
self.state
207+
}
206208

207-
pub fn get_alt(&self) -> isize { self.alt }
209+
pub fn get_alt(&self) -> isize {
210+
self.alt
211+
}
208212

209-
pub(crate) fn get_type(&self) -> &ATNConfigType { &self.config_type }
213+
pub(crate) fn get_type(&self) -> &ATNConfigType {
214+
&self.config_type
215+
}
210216

211-
pub fn get_context(&self) -> Option<&Arc<PredictionContext>> { self.context.as_ref() }
217+
pub fn get_context(&self) -> Option<&Arc<PredictionContext>> {
218+
self.context.as_ref()
219+
}
212220

213-
pub fn take_context(&mut self) -> Arc<PredictionContext> { self.context.take().unwrap() }
221+
pub fn take_context(&mut self) -> Arc<PredictionContext> {
222+
self.context.take().unwrap()
223+
}
214224

215-
pub fn set_context(&mut self, _v: Arc<PredictionContext>) { self.context = Some(_v); }
225+
pub fn set_context(&mut self, _v: Arc<PredictionContext>) {
226+
self.context = Some(_v);
227+
}
216228

217-
pub fn get_reaches_into_outer_context(&self) -> isize { self.reaches_into_outer_context }
229+
pub fn get_reaches_into_outer_context(&self) -> isize {
230+
self.reaches_into_outer_context
231+
}
218232

219233
pub fn set_reaches_into_outer_context(&mut self, _v: isize) {
220234
self.reaches_into_outer_context = _v
221235
}
222236

223-
pub fn is_precedence_filter_suppressed(&self) -> bool { self.precedence_filter_suppressed }
237+
pub fn is_precedence_filter_suppressed(&self) -> bool {
238+
self.precedence_filter_suppressed
239+
}
224240

225241
pub fn set_precedence_filter_suppressed(&mut self, _v: bool) {
226242
self.precedence_filter_suppressed = _v;

src/atn_config_set.rs

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ impl PartialEq for ATNConfigSet {
8686
impl Eq for ATNConfigSet {}
8787

8888
impl Hash for ATNConfigSet {
89-
fn hash<H: Hasher>(&self, state: &mut H) { self.configs.hash(state) }
89+
fn hash<H: Hasher>(&self, state: &mut H) {
90+
self.configs.hash(state)
91+
}
9092
}
9193

9294
impl ATNConfigSet {
@@ -113,7 +115,9 @@ impl ATNConfigSet {
113115
a
114116
}
115117

116-
fn full_hash_key(config: &ATNConfig) -> Key { Key::Full(config.clone()) }
118+
fn full_hash_key(config: &ATNConfig) -> Key {
119+
Key::Full(config.clone())
120+
}
117121

118122
fn local_hash_key(config: &ATNConfig) -> Key {
119123
let mut hasher = MurmurHasher::default();
@@ -176,7 +180,9 @@ impl ATNConfigSet {
176180
true
177181
}
178182

179-
pub fn add(&mut self, config: Box<ATNConfig>) -> bool { self.add_cached(config, None) }
183+
pub fn add(&mut self, config: Box<ATNConfig>) -> bool {
184+
self.add_cached(config, None)
185+
}
180186

181187
pub fn get_items(&self) -> impl Iterator<Item = &ATNConfig> {
182188
self.configs.iter().map(|c| c.as_ref())
@@ -197,19 +203,33 @@ impl ATNConfigSet {
197203
}
198204
}
199205

200-
pub fn length(&self) -> usize { self.configs.len() }
206+
pub fn length(&self) -> usize {
207+
self.configs.len()
208+
}
201209

202-
pub fn is_empty(&self) -> bool { self.configs.is_empty() }
210+
pub fn is_empty(&self) -> bool {
211+
self.configs.is_empty()
212+
}
203213

204-
pub fn has_semantic_context(&self) -> bool { self.has_semantic_context }
214+
pub fn has_semantic_context(&self) -> bool {
215+
self.has_semantic_context
216+
}
205217

206-
pub fn set_has_semantic_context(&mut self, _v: bool) { self.has_semantic_context = _v; }
218+
pub fn set_has_semantic_context(&mut self, _v: bool) {
219+
self.has_semantic_context = _v;
220+
}
207221

208-
pub fn read_only(&self) -> bool { self.read_only }
222+
pub fn read_only(&self) -> bool {
223+
self.read_only
224+
}
209225

210-
pub fn set_read_only(&mut self, _read_only: bool) { self.read_only = _read_only; }
226+
pub fn set_read_only(&mut self, _read_only: bool) {
227+
self.read_only = _read_only;
228+
}
211229

212-
pub fn full_context(&self) -> bool { self.full_ctx }
230+
pub fn full_context(&self) -> bool {
231+
self.full_ctx
232+
}
213233

214234
//duplicate of the self.conflicting_alts???
215235
pub fn get_alts(&self) -> BitSet {
@@ -219,11 +239,19 @@ impl ATNConfigSet {
219239
})
220240
}
221241

222-
pub fn get_unique_alt(&self) -> isize { self.unique_alt }
242+
pub fn get_unique_alt(&self) -> isize {
243+
self.unique_alt
244+
}
223245

224-
pub fn set_unique_alt(&mut self, _v: isize) { self.unique_alt = _v }
246+
pub fn set_unique_alt(&mut self, _v: isize) {
247+
self.unique_alt = _v
248+
}
225249

226-
pub fn get_dips_into_outer_context(&self) -> bool { self.dips_into_outer_context }
250+
pub fn get_dips_into_outer_context(&self) -> bool {
251+
self.dips_into_outer_context
252+
}
227253

228-
pub fn set_dips_into_outer_context(&mut self, _v: bool) { self.dips_into_outer_context = _v }
254+
pub fn set_dips_into_outer_context(&mut self, _v: bool) {
255+
self.dips_into_outer_context = _v
256+
}
229257
}

src/atn_deserialization_options.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ pub struct ATNDeserializationOptions {
66
}
77

88
impl ATNDeserializationOptions {
9-
pub fn is_verify(&self) -> bool { self.verify_atn }
9+
pub fn is_verify(&self) -> bool {
10+
self.verify_atn
11+
}
1012
}
1113

1214
impl Default for ATNDeserializationOptions {

0 commit comments

Comments
 (0)