Skip to content

Merge upstream 0.3.0 beta into our fork for less unsafe #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions .github/workflows/antlr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
name: ANTLR4 testsuite
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
repository: rrevenantt/antlr4
ref: rust-target
Expand All @@ -30,16 +30,17 @@ jobs:
git checkout FETCH_HEAD
working-directory: runtime/Rust
- name: Set up JDK 1.8
uses: actions/setup-java@v1
uses: actions/setup-java@v3
with:
java-version: 1.8
distribution: 'temurin'
java-version: '8'
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ env.RUST_VERSION }}
toolchain: stable
default: true
- name: Cache local Maven repository
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
Expand All @@ -54,7 +55,7 @@ jobs:
cat target/surefire-reports/*.dumpstream || true;
exit $rc
working-directory: runtime-testsuite
- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v3
if: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
with:
name: antlr-tool
Expand All @@ -65,8 +66,12 @@ jobs:
runs-on: ubuntu-latest
outputs:
new_version: ${{ steps.check_ver.outputs.new_version }}
strategy:
fail-fast: false
matrix:
rust_version: [ 1.52, stable ]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
Expand All @@ -79,10 +84,9 @@ jobs:
run: cargo test --verbose
- name: Formatting
run: cargo fmt -- --check
- uses: actions/checkout@v2
- uses: actions/checkout@v3
if: ${{ github.event_name == 'push' }}
with:
# todo change to `fetch-depth: 2` and HEAD^
ref: ${{ github.event.before }}
path: base_branch
- name: check version bump
Expand All @@ -92,7 +96,7 @@ jobs:
NEW_VER=$(cat Cargo.toml | sed -nE "s/^version.*\"([0-9a-z.\-]+)\".*$/\1/p")
BASE_VER=$(cat base_branch/Cargo.toml | sed -nE "s/^version.*\"([0-9a-z.\-]+)\".*$/\1/p")
if [[ "$NEW_VER" != "$BASE_VER" ]]; then
echo "::set-output name=new_version::$NEW_VER"
echo "{new_version}={$NEW_VER}" >> $GITHUB_OUTPUT
fi


Expand All @@ -107,38 +111,30 @@ jobs:
RELEASE_NAME: ${{ format('antlr4-4.8-2-Rust{0}',needs.tests-rust.outputs.new_version) }}
PRERELEASE: ${{ contains(needs.tests-rust.outputs.new_version, '-') }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ env.RUST_VERSION }}
toolchain: stable
default: true
- name: Cargo publish check
run: |
cargo publish --dry-run
- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v3
with:
name: antlr-tool
- name: Create release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ env.RELEASE_NAME }}
release_name: ${{ env.RELEASE_NAME }}
name: ${{ env.RELEASE_NAME }}
token: ${{ secrets.GITHUB_TOKEN }}
# make release a draft first in case `cargo publish` still fail
draft: true
prerelease: ${{ env.PRERELEASE }}
- name: Upload Release Asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: antlr4-4.8-2-SNAPSHOT-complete.jar
asset_name: antlr4-4.8-2-SNAPSHOT-complete.jar
asset_content_type: application/java-archive
files: |
antlr4-4.8-2-SNAPSHOT-complete.jar
- name: Cargo publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_PUBLISH_TOKEN }}
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "antlr-rust"
version = "0.2.2"
version = "0.3.0-beta"
authors = ["Konstantin Anisimov <[email protected]>"]
homepage = "https://github.com/rrevenantt/antlr4rust"
repository = "https://github.com/rrevenantt/antlr4rust"
Expand All @@ -23,7 +23,8 @@ bit-set = "=0.5.*"
once_cell = "^1.2"
#backtrace = "=0.3"
typed-arena = "^2.0"
better_any = "=0.1"
better_any = "0.2.0-dev.1"
#better_any = "=0.1"
parking_lot = "0.11"
#qcell = { path="../qcell" }

Expand Down
39 changes: 11 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
This is a fork of https://github.com/rrevenantt/antlr4rust because rrevenantt does not appear to be maintaining it.

# antlr4rust
[![Crate](https://flat.badgen.net/crates/v/antlr-rust)](https://crates.io/crates/antlr_rust/0.2.0)
[![docs](https://flat.badgen.net/badge/docs.rs/v0.2.0)](https://docs.rs/antlr-rust/0.2.0)
[![Crate](https://flat.badgen.net/crates/v/antlr-rust)](https://crates.io/crates/antlr_rust/0.3.0-beta)
[![docs](https://flat.badgen.net/badge/docs.rs/v0.3.0-beta)](https://docs.rs/antlr-rust/0.3.0-beta)
![ANTLR4 testsuite](https://github.com/rrevenantt/antlr4rust/workflows/ANTLR4%20testsuite/badge.svg?event=push)
![cargo test](https://github.com/rrevenantt/antlr4rust/workflows/cargo%20test/badge.svg)
[![](https://tokei.rs/b1/github/rrevenantt/antlr4rust)](https://github.com/rrevenantt/antlr4rust)
Expand All @@ -23,37 +23,18 @@ or if you want to contribute to only runtime part you don't have to do build it

But if you want to build or change generator yourself:
* `git clone -b rust-target https://github.com/rrevenantt/antlr4` - clone my antlr4 fork
* `git submodule update --init --recursive` - update Rust target submodule
* `git submodule update --init --recursive --remote` - update Rust target submodule
* `mvn -DskipTests install` - build generator

### Implementation status

For now development is going on in this repository
but eventually it will be merged to main ANTLR4 repo

Currently, requires nightly version of rust.
This likely will be the case until `coerce_unsized` or some kind of coercion trait is stabilized.
There are other unstable features in use but only `CoerceUnsized` is essential.

Remaining things before merge:
- API stabilization
- [ ] Rust api guidelines compliance
- [ ] more tests for API because it is quite different from Java

Can be done after merge:
- Documentation
- [ ] Some things are already documented but still far from perfect, also more links needed.
- cfg to not build potentially unnecessary parts
(no Lexer if custom token stream, no ParserATNSimulator if LL(1) grammar)
- run rustfmt on generated parser
###### Long term improvements
- generate enum for labeled alternatives without redundant `Error` option
- option to generate fields instead of getters by default and make visiting based on fields
- make tree generic over pointer type and allow tree nodes to arena.
(requires GAT, otherwise it would be a problem for users that want ownership for parse tree)
- support stable rust
- support no_std(although alloc would still be required)

Since version `0.3` works on stable rust.
Previous versions are not maintained any more
so in case of nightly breakage you should migrate to the latest version.

### Usage

You should use the ANTLR4 "tool" to generate a parser, that will use the ANTLR
Expand All @@ -71,9 +52,8 @@ Then add following to `Cargo.toml` of the crate from which generated parser
is going to be used:
```toml
[dependencies]
antlr-rust = "=0.2"
antlr-rust = "0.3"
```
and `#![feature(try_blocks)]` in your project root module.

### Parse Tree structure

Expand Down Expand Up @@ -121,9 +101,11 @@ Also while structs used by generated lexer and parser were customized to track a
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.
So there is still room for improvement.
```text
lexers:
large/large_xmlparser time: [1.8598 ms 1.8607 ms 1.8619 ms]
large/large_quick_xml time: [1.4623 ms 1.4645 ms 1.4675 ms]
large/large_antlr_xml_lexer time: [5.7866 ms 5.7877 ms 5.7891 ms]
parsers:
large/large_xmlrs time: [16.734 ms 16.748 ms 16.766 ms]
large/large_minidom time: [7.0639 ms 7.0792 ms 7.0975 ms]
large/large_roxmltree time: [4.9341 ms 4.9360 ms 4.9380 ms]
Expand All @@ -144,3 +126,4 @@ BSD 3-clause.
Unless you explicitly state otherwise,
any contribution intentionally submitted for inclusion in this project by you
shall be licensed as above, without any additional terms or conditions.

12 changes: 11 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ use std::process::Command;

fn main() {
let grammars = vec![
"VisitorBasic",
"VisitorCalc",
"CSV",
"ReferenceToATN",
"XMLLexer",
"SimpleLR",
"Labels",
"FHIRPath",
];
let additional_args = vec![Some("-visitor"), None, None, None, None];
let additional_args = vec![
Some("-visitor"),
Some("-visitor"),
Some("-visitor"),
None,
None,
None,
None,
];
let antlr_path = "/home/rrevenantt/dev/antlr4/tool/target/antlr4-4.8-2-SNAPSHOT-complete.jar";

for (grammar, arg) in grammars.into_iter().zip(additional_args) {
Expand Down
7 changes: 7 additions & 0 deletions grammars/VisitorBasic.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
grammar VisitorBasic;

s
: 'A' EOF
;

A : 'A';
18 changes: 18 additions & 0 deletions grammars/VisitorCalc.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
grammar VisitorCalc;

s
: expr EOF
;

expr
: INT # number
| expr (MUL | DIV) expr # multiply
| expr (ADD | SUB) expr # add
;

INT : [0-9]+;
MUL : '*';
DIV : '/';
ADD : '+';
SUB : '-';
WS : [ \t]+ -> channel(HIDDEN);
4 changes: 2 additions & 2 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
edition = "2018"
fn_single_line = true
fn_params_layout = "Compressed"
#fn_single_line = true
#fn_args_layout = "Compressed"
36 changes: 26 additions & 10 deletions src/atn_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl PartialEq for ATNConfig {
fn eq(&self, other: &Self) -> bool {
self.get_state() == other.get_state()
&& self.get_alt() == other.get_alt()
&& (Arc::ptr_eq(self.get_context().unwrap(), other.get_context().unwrap())
|| self.get_context() == other.get_context())
// Arc is optimized to not do a deep equalitiy if arc pointers are equal so that's enough
&& self.context == other.context
&& self.get_type() == other.get_type()
&& self.semantic_context == other.semantic_context
&& self.precedence_filter_suppressed == other.precedence_filter_suppressed
Expand Down Expand Up @@ -202,25 +202,41 @@ impl ATNConfig {
new
}

pub fn get_state(&self) -> ATNStateRef { self.state }
pub fn get_state(&self) -> ATNStateRef {
self.state
}

pub fn get_alt(&self) -> isize { self.alt }
pub fn get_alt(&self) -> isize {
self.alt
}

pub(crate) fn get_type(&self) -> &ATNConfigType { &self.config_type }
pub(crate) fn get_type(&self) -> &ATNConfigType {
&self.config_type
}

pub fn get_context(&self) -> Option<&Arc<PredictionContext>> { self.context.as_ref() }
pub fn get_context(&self) -> Option<&Arc<PredictionContext>> {
self.context.as_ref()
}

pub fn take_context(&mut self) -> Arc<PredictionContext> { self.context.take().unwrap() }
pub fn take_context(&mut self) -> Arc<PredictionContext> {
self.context.take().unwrap()
}

pub fn set_context(&mut self, _v: Arc<PredictionContext>) { self.context = Some(_v); }
pub fn set_context(&mut self, _v: Arc<PredictionContext>) {
self.context = Some(_v);
}

pub fn get_reaches_into_outer_context(&self) -> isize { self.reaches_into_outer_context }
pub fn get_reaches_into_outer_context(&self) -> isize {
self.reaches_into_outer_context
}

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

pub fn is_precedence_filter_suppressed(&self) -> bool { self.precedence_filter_suppressed }
pub fn is_precedence_filter_suppressed(&self) -> bool {
self.precedence_filter_suppressed
}

pub fn set_precedence_filter_suppressed(&mut self, _v: bool) {
self.precedence_filter_suppressed = _v;
Expand Down
Loading
Loading