Skip to content

Commit

Permalink
update readme and use rustfmt for formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rrevenantt committed Sep 20, 2020
1 parent d287361 commit f8da12f
Show file tree
Hide file tree
Showing 58 changed files with 5,284 additions and 4,501 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ bit-set = "=0.5.*"
once_cell = "^1.2.*"
backtrace = "=0.3"
typed-arena = "^2.0.*"
git2 = "0.12.*"

[lib]

Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and [tests/my_tests.rs](tests/my_test.rs) for actual usage examples
### Implementation status

Everything is implemented, "business" logic is quite stable and well tested, but user facing
API is not very robust yet an very likely will have some changes.
API is not very robust yet and very likely will have some changes.

For now development is going on in this repository
but eventually it will be merged to main ANTLR4 repo
Expand Down Expand Up @@ -46,7 +46,7 @@ Can be done after merge:

### Usage

You use the ANTLR4 "tool" to generate a parser, that will use the ANTLR
You should use the ANTLR4 "tool" to generate a parser, that will use the ANTLR
runtime, located here. You can run it with the following command:
```bash
java -jar <path to ANTLR4 tool> -Dlanguage=Rust MyGrammar.g4
Expand Down Expand Up @@ -94,15 +94,19 @@ there are quite some differences because Rust is not an OOP language and is much
otherwise `ParseTreeWalker` should be used.
- In embedded actions to access parser you should use `recog` variable instead of `self`/`this`.
This is because predicate have to be inserted into two syntactically different places in generated parser
- `InputStream`s have different index behavior for unicode characters.
- String `InputStream` have different index behavior when there are unicode characters.
If you need exactly the same behavior, use `[u32]` based `InputStream`, or implement custom `CharStream`.
- In actions you have to escape `'` in rust lifetimes with `\ ` because ANTLR considers them as strings: `Struct<\'lifetime>`
- In actions you have to escape `'` in rust lifetimes with `\ ` because ANTLR considers them as strings, e.g. `Struct<\'lifetime>`
- For custom tokens you should use `@tokenfactory` custom action, instead of usual `TokenLabelType` parser option
- All rule context variables (rule argument or rule return) should implement `Default + Clone`.

### Unsafe
Currently unsafe is used only to cast from trait object back to original type
and to update data inside Rc via `get_mut_unchecked`(returned mutable reference is used immediately and not stored anywhere)

### Versioning
In addition to usual Rust semantic versioning,
patch version changes of the crate should not require updating of generator part

## Licence

Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::convert::TryInto;
use std::env;
use std::env::VarError;
use std::error::Error;
use std::fs::{DirEntry, File, read_dir};
use std::fs::{read_dir, DirEntry, File};
use std::io::Write;
use std::path::Path;
use std::process::Command;
Expand Down
3 changes: 1 addition & 2 deletions src/atn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ impl ATN {
pub fn get_expected_tokens(
&self,
state_number: isize,
states_stack:impl Iterator<Item=isize>
// _ctx: &Rc<Ctx::Type>,
states_stack: impl Iterator<Item = isize>, // _ctx: &Rc<Ctx::Type>,
) -> IntervalSet {
let s = self.states[state_number as usize].as_ref();
let mut following = self.next_tokens(s);
Expand Down
2 changes: 1 addition & 1 deletion src/atn_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl PartialEq for ATNConfig {
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())
|| self.get_context() == other.get_context())
&& self.get_type() == other.get_type()
&& self.semantic_context == other.semantic_context
&& self.precedence_filter_suppressed == other.precedence_filter_suppressed
Expand Down
2 changes: 1 addition & 1 deletion src/atn_config_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl ATNConfigSet {

pub fn add(&mut self, config: Box<ATNConfig>) -> bool { self.add_cached(config, None) }

pub fn get_items(&self) -> impl Iterator<Item=&ATNConfig> {
pub fn get_items(&self) -> impl Iterator<Item = &ATNConfig> {
self.configs.iter().map(|c| c.as_ref())
}

Expand Down
4 changes: 1 addition & 3 deletions src/atn_deserialization_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ impl ATNDeserializationOptions {
) -> ATNDeserializationOptions {
unimplemented!()
}
pub fn is_verify(&self) -> bool {
self.verify_atn
}
pub fn is_verify(&self) -> bool { self.verify_atn }
}

impl Default for ATNDeserializationOptions {
Expand Down
62 changes: 31 additions & 31 deletions src/atn_deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ use uuid::Uuid;

use crate::atn::ATN;
use crate::atn_deserialization_options::ATNDeserializationOptions;
use crate::atn_state::*;
use crate::atn_state::ATNBlockStart;
use crate::atn_state::ATNDecisionState;
use crate::atn_state::ATNState;
use crate::atn_state::ATNStateType;
use crate::atn_state::BaseATNState;
use crate::atn_state::*;
use crate::atn_type::ATNType;
use crate::int_stream::EOF;
use crate::interval_set::IntervalSet;
use crate::lexer_action::*;
use crate::lexer_action::LexerAction::*;
use crate::lexer_action::*;
use crate::rule_context::CustomRuleContext;
use crate::transition::*;
use crate::transition::Transition;
use crate::transition::*;

lazy_static! {
static ref BASE_SERIALIZED_UUID: Uuid =
Expand Down Expand Up @@ -122,7 +122,7 @@ impl ATNDeserializer {
}
}

fn check_uuid(&self, data: &mut dyn Iterator<Item=isize>) -> Uuid {
fn check_uuid(&self, data: &mut dyn Iterator<Item = isize>) -> Uuid {
//rust uses UTF-8 encoding so we need explicitly convert unicode
//codepoint numbers to bytes
let mut bytes = Vec::new();
Expand All @@ -138,7 +138,7 @@ impl ATNDeserializer {
uuid
}

fn read_atn(&self, data: &mut dyn Iterator<Item=isize>) -> ATN {
fn read_atn(&self, data: &mut dyn Iterator<Item = isize>) -> ATN {
let atn = ATN::new_atn(
match data.next() {
Some(0) => ATNType::LEXER,
Expand All @@ -151,7 +151,7 @@ impl ATNDeserializer {
atn
}

fn read_states(&self, atn: &mut ATN, data: &mut dyn Iterator<Item=isize>) {
fn read_states(&self, atn: &mut ATN, data: &mut dyn Iterator<Item = isize>) {
// let loop_back_states = Vec::<(BaseATNState,isize)>::new();
// let end_states = Vec::<(BaseATNState,isize)>::new();
let states_count = data.next().unwrap() as usize;
Expand Down Expand Up @@ -187,7 +187,7 @@ impl ATNDeserializer {
for _ in 0..num_non_greedy {
let st = data.next().unwrap() as usize;
if let ATNStateType::DecisionState { nongreedy: ng, .. } =
atn.states[st].get_state_type_mut()
atn.states[st].get_state_type_mut()
{
*ng = true
}
Expand All @@ -209,7 +209,7 @@ impl ATNDeserializer {
}
}

fn read_rules(&self, atn: &mut ATN, data: &mut dyn Iterator<Item=isize>) {
fn read_rules(&self, atn: &mut ATN, data: &mut dyn Iterator<Item = isize>) {
let nrules = data.next().unwrap() as usize;
// if atn.grammar_type == ATNType::LEXER {
// atn.rule_to_token_type.resize(nrules, 0)
Expand Down Expand Up @@ -248,14 +248,14 @@ impl ATNDeserializer {
}
}

fn read_modes(&self, atn: &mut ATN, data: &mut dyn Iterator<Item=isize>) {
fn read_modes(&self, atn: &mut ATN, data: &mut dyn Iterator<Item = isize>) {
let nmodes = data.next().unwrap();
for _i in 0..nmodes {
atn.mode_to_start_state.push(data.next().unwrap() as usize);
}
}

fn read_sets<T: Iterator<Item=isize>>(
fn read_sets<T: Iterator<Item = isize>>(
&self,
_atn: &mut ATN,
data: &mut T,
Expand Down Expand Up @@ -285,7 +285,7 @@ impl ATNDeserializer {
fn read_edges(
&self,
atn: &mut ATN,
data: &mut dyn Iterator<Item=isize>,
data: &mut dyn Iterator<Item = isize>,
sets: &Vec<IntervalSet>,
) {
let nedges = data.next().unwrap();
Expand Down Expand Up @@ -352,10 +352,10 @@ impl ATNDeserializer {
match atn_state.get_state_type() {
ATNStateType::DecisionState {
state:
ATNDecisionState::BlockStartState {
end_state: _,
en: _,
},
ATNDecisionState::BlockStartState {
end_state: _,
en: _,
},
..
} => {

Expand All @@ -379,7 +379,7 @@ impl ATNDeserializer {
}
}

fn read_decisions(&self, atn: &mut ATN, _data: &mut dyn Iterator<Item=isize>) {
fn read_decisions(&self, atn: &mut ATN, _data: &mut dyn Iterator<Item = isize>) {
let ndecisions = _data.next().unwrap();
for i in 0..ndecisions {
let s = _data.next().unwrap() as usize;
Expand All @@ -391,7 +391,7 @@ impl ATNDeserializer {
}
}

fn read_lexer_actions(&self, atn: &mut ATN, _data: &mut dyn Iterator<Item=isize>) {
fn read_lexer_actions(&self, atn: &mut ATN, _data: &mut dyn Iterator<Item = isize>) {
//lexer actions are always supported here
let nactions = _data.next().unwrap() as usize;

Expand All @@ -416,15 +416,15 @@ impl ATNDeserializer {
fn generate_rule_bypass_transitions(
&self,
_atn: &mut ATN,
_data: &mut dyn Iterator<Item=isize>,
_data: &mut dyn Iterator<Item = isize>,
) {
unimplemented!()
}

fn generate_rule_bypass_transition(
&self,
_atn: &mut ATN,
_data: &mut dyn Iterator<Item=isize>,
_data: &mut dyn Iterator<Item = isize>,
_idx: isize,
) {
unimplemented!()
Expand All @@ -434,23 +434,23 @@ impl ATNDeserializer {
unimplemented!()
}

fn mark_precedence_decisions(&self, _atn: &mut ATN, _data: &mut dyn Iterator<Item=isize>) {
fn mark_precedence_decisions(&self, _atn: &mut ATN, _data: &mut dyn Iterator<Item = isize>) {
let mut precedence_states = Vec::new();
for state in _atn.states.iter() {
if let ATNStateType::DecisionState {
state:
ATNDecisionState::StarLoopEntry {
loop_back_state,
is_precedence,
},
ATNDecisionState::StarLoopEntry {
loop_back_state,
is_precedence,
},
..
} = state.get_state_type()
{
if let ATNStateType::RuleStartState {
is_left_recursive: true,
..
} =
_atn.states[_atn.rule_to_start_state[state.get_rule_index()]].get_state_type()
_atn.states[_atn.rule_to_start_state[state.get_rule_index()]].get_state_type()
{
let maybe_loop_end =
state.get_transitions().iter().last().unwrap().get_target();
Expand All @@ -459,7 +459,7 @@ impl ATNDeserializer {
if maybe_loop_end.has_epsilon_only_transitions() {
if let ATNStateType::RuleStopState = _atn.states
[maybe_loop_end.get_transitions()[0].get_target()]
.get_state_type()
.get_state_type()
{
precedence_states.push(state.get_state_number())
}
Expand All @@ -471,10 +471,10 @@ impl ATNDeserializer {
for st in precedence_states {
if let ATNStateType::DecisionState {
state:
ATNDecisionState::StarLoopEntry {
loop_back_state,
is_precedence,
},
ATNDecisionState::StarLoopEntry {
loop_back_state,
is_precedence,
},
..
} = _atn.states[st].get_state_type_mut()
{
Expand All @@ -483,7 +483,7 @@ impl ATNDeserializer {
}
}

fn verify_atn(&self, _atn: &mut ATN, _data: &mut dyn Iterator<Item=isize>) {
fn verify_atn(&self, _atn: &mut ATN, _data: &mut dyn Iterator<Item = isize>) {
//TODO
}

Expand Down
13 changes: 3 additions & 10 deletions src/atn_simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub trait IATNSimulator {
fn decision_to_dfa(&self) -> &Vec<DFA>;
}


pub struct BaseATNSimulator {
pub atn: Arc<ATN>,
pub shared_context_cache: Arc<PredictionContextCache>,
Expand Down Expand Up @@ -45,15 +44,9 @@ impl BaseATNSimulator {
}

impl IATNSimulator for BaseATNSimulator {
fn shared_context_cache(&self) -> &PredictionContextCache {
self.shared_context_cache.deref()
}
fn shared_context_cache(&self) -> &PredictionContextCache { self.shared_context_cache.deref() }

fn atn(&self) -> &ATN {
self.atn.as_ref()
}
fn atn(&self) -> &ATN { self.atn.as_ref() }

fn decision_to_dfa(&self) -> &Vec<DFA> {
self.decision_to_dfa.as_ref()
}
fn decision_to_dfa(&self) -> &Vec<DFA> { self.decision_to_dfa.as_ref() }
}
20 changes: 11 additions & 9 deletions src/char_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ pub trait CharStream<Data>: IntStream {
/// Trait for input that can be accepted by `InputStream` to be able to provide lexer with data.
/// Is sealed for now just in case.
pub trait InputData:
Index<Range<usize>, Output=Self>
+ Index<RangeFrom<usize>, Output=Self>
+ ToOwned
+ 'static
Index<Range<usize>, Output = Self> + Index<RangeFrom<usize>, Output = Self> + ToOwned + 'static
{
// fn to_indexed_vec(&self) -> Vec<(u32, u32)>;

Expand All @@ -39,7 +36,10 @@ Index<Range<usize>, Output=Self>
fn to_display(&self) -> String;
}

impl<T: Into<u32> + From<u8> + TryFrom<u32> + Copy + Debug + 'static> InputData for [T] where <T as TryFrom<u32>>::Error:Debug {
impl<T: Into<u32> + From<u8> + TryFrom<u32> + Copy + Debug + 'static> InputData for [T]
where
<T as TryFrom<u32>>::Error: Debug,
{
// fn to_indexed_vec(&self) -> Vec<(u32, u32)> {
// self.into_iter()
// .enumerate()
Expand Down Expand Up @@ -69,7 +69,11 @@ impl<T: Into<u32> + From<u8> + TryFrom<u32> + Copy + Debug + 'static> InputData
fn len(&self) -> usize { self.len() }

#[inline]
fn from_text(text: &str) -> Self::Owned { text.chars().map(|it| T::try_from(it as u32).unwrap()).collect() }
fn from_text(text: &str) -> Self::Owned {
text.chars()
.map(|it| T::try_from(it as u32).unwrap())
.collect()
}

#[inline]
// default
Expand Down Expand Up @@ -135,9 +139,7 @@ impl InputData for str {
#[inline]
fn len(&self) -> usize { self.len() }

fn from_text(text: &str) -> Self::Owned {
text.to_owned()
}
fn from_text(text: &str) -> Self::Owned { text.to_owned() }

// #[inline]
// fn from_text(text: &str) -> Self::Owned { text.to_owned() }
Expand Down
Loading

0 comments on commit f8da12f

Please sign in to comment.