Skip to content

Commit

Permalink
remove proc-macro dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
rrevenantt committed Aug 12, 2021
1 parent 12ba1d9 commit b52022b
Show file tree
Hide file tree
Showing 27 changed files with 121 additions and 149 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/antlr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ jobs:
repository: rrevenantt/antlr4
ref: rust-target
submodules: true
- run: git fetch origin $GITHUB_REF
- run: |
git fetch origin $GITHUB_REF
git checkout FETCH_HEAD
working-directory: runtime/Rust
- name: Set up JDK 1.8
uses: actions/setup-java@v1
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 0 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,6 @@ 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.

###### Long term improvements

things to consider for next versions:
- [ ] attempt a workaround to use stable rust
- [ ] remove proc macro dependency
- [ ] move to once_cell completely
- [ ] rustfmt in antlr tool
- [ ] make `Node` and associated type in visitors
- [ ] bulk import for context extension traits
- [ ] intradoc links
- [ ] move benches to repository
- [ ] LexerAtnSimulator position tracking customization
- [ ] Lexer position tracking customization
- [ ] thread local DFA(questionable because of quite slow threadlocals in rust)
- [ ] arena allocation for tree nodes
- [ ] tree utils
- [ ] features
- [ ] stateful lexer test
- [ ] better downcasting
- [ ] options for customizing downcast bounds
- [ ] token stream rewriter
- [ ] doctests + dummy parser for them
- [ ] actual examples
- [ ] normal visitor example/docs
- [ ] string interner test/example
- [ ] figure out base class api replacement
- [ ] rustfmt import config
- [ ] draft upstream PR
- [ ] add recursion limit
- [ ] test custom errors
- [ ] add fuzzer tests

### Usage

You should use the ANTLR4 "tool" to generate a parser, that will use the ANTLR
Expand Down
4 changes: 3 additions & 1 deletion src/common_token_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ use crate::token_stream::{TokenStream, UnbufferedTokenStream};
use better_any::{Tid, TidAble};

/// Default token stream that skips token that not correspond to current channel.
#[derive(Tid, Debug)]
#[derive(Debug)]
pub struct CommonTokenStream<'input, T: TokenSource<'input>> {
base: UnbufferedTokenStream<'input, T>,
channel: isize,
}

better_any::tid! { impl<'input,T> TidAble<'input> for CommonTokenStream<'input, T> where T: TokenSource<'input>}

impl<'input, T: TokenSource<'input>> IntStream for CommonTokenStream<'input, T> {
#[inline]
fn consume(&mut self) {
Expand Down
14 changes: 9 additions & 5 deletions src/error_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::token_factory::TokenFactory;
use crate::transition::RuleTransition;
use crate::tree::Tree;
use crate::utils::escape_whitespaces;
use better_any::{impl_tid, Tid, TidAble};
use better_any::{Tid, TidAble};

/// The interface for defining strategies to deal with syntax errors encountered
/// during a parse by ANTLR-generated parsers. We distinguish between three
Expand Down Expand Up @@ -94,8 +94,8 @@ pub trait ErrorStrategy<'a, T: Parser<'a>>: Tid<'a> {
// /// Supports downcasting.
// pub type DynHandler<'a, T> = Box<dyn ErrorStrategy<'a, T> + 'a>;

#[impl_tid]
impl<'a, T: Parser<'a> + TidAble<'a>> TidAble<'a> for Box<dyn ErrorStrategy<'a, T> + 'a> {}
// impl<'a, T: Parser<'a> + TidAble<'a>> TidAble<'a> for Box<dyn ErrorStrategy<'a, T> + 'a> {}
better_any::tid! { impl<'a, T> TidAble<'a> for Box<dyn ErrorStrategy<'a, T> + 'a> where T: Parser<'a>}

impl<'a, T: Parser<'a> + TidAble<'a>> ErrorStrategy<'a, T> for Box<dyn ErrorStrategy<'a, T> + 'a> {
#[inline(always)]
Expand Down Expand Up @@ -135,7 +135,7 @@ impl<'a, T: Parser<'a> + TidAble<'a>> ErrorStrategy<'a, T> for Box<dyn ErrorStra

/// This is the default implementation of `ErrorStrategy` used for
/// error reporting and recovery in ANTLR parsers.
#[derive(Debug, Tid)]
#[derive(Debug)]
pub struct DefaultErrorStrategy<'input, Ctx: ParserNodeType<'input>> {
error_recovery_mode: bool,
last_error_index: isize,
Expand All @@ -144,6 +144,8 @@ pub struct DefaultErrorStrategy<'input, Ctx: ParserNodeType<'input>> {
next_tokens_ctx: Option<Rc<Ctx::Type>>,
}

better_any::tid! { impl<'i,Ctx> TidAble<'i> for DefaultErrorStrategy<'i,Ctx> where Ctx: ParserNodeType<'i>}

impl<'input, Ctx: ParserNodeType<'input>> Default for DefaultErrorStrategy<'input, Ctx> {
fn default() -> Self { Self::new() }
}
Expand Down Expand Up @@ -545,11 +547,13 @@ impl<'a, T: Parser<'a>> ErrorStrategy<'a, T> for DefaultErrorStrategy<'a, T::Nod
///
/// [`ParserRuleContext.exception`]: todo
/// */
#[derive(Default, Debug, Tid)]
#[derive(Default, Debug)]
pub struct BailErrorStrategy<'input, Ctx: ParserNodeType<'input>>(
DefaultErrorStrategy<'input, Ctx>,
);

better_any::tid! {impl<'i,Ctx> TidAble<'i> for BailErrorStrategy<'i,Ctx> where Ctx:ParserNodeType<'i> }

impl<'input, Ctx: ParserNodeType<'input>> BailErrorStrategy<'input, Ctx> {
/// Creates new instance of `BailErrorStrategy`
pub fn new() -> Self { Self(DefaultErrorStrategy::new()) }
Expand Down
13 changes: 7 additions & 6 deletions src/input_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::char_stream::{CharStream, InputData};
use crate::int_stream::IntStream;
use std::ops::Deref;

use better_any::{impl_tid, TidAble};
use better_any::TidAble;

/// Default rust target input stream.
///
Expand All @@ -21,11 +21,12 @@ pub struct InputStream<Data: Deref> {
index: isize,
}

#[impl_tid]
impl<'a, T: ?Sized + 'static> TidAble<'a> for InputStream<&'a T> {}

#[impl_tid]
impl<'a, T: ?Sized + 'static> TidAble<'a> for InputStream<Box<T>> {}
// #[impl_tid]
// impl<'a, T: ?Sized + 'static> TidAble<'a> for InputStream<Box<T>> {}
// #[impl_tid]
// impl<'a, T: ?Sized + 'static> TidAble<'a> for InputStream<&'a T> {}
better_any::tid! {impl<'a, T: 'static> TidAble<'a> for InputStream<&'a T> where T: ?Sized}
better_any::tid! {impl<'a, T: 'static> TidAble<'a> for InputStream<Box<T>> where T: ?Sized}

impl<'a, T: From<&'a str>> CharStream<T> for InputStream<&'a str> {
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub use lazy_static::lazy_static;
pub use parking_lot::RwLock;

#[doc(hidden)]
pub use better_any::{impl_tid, type_id, Tid, TidAble, TidExt};
pub use better_any::{tid, Tid, TidAble, TidExt};

#[doc(inline)]
pub use error_strategy::{BailErrorStrategy, DefaultErrorStrategy, ErrorStrategy};
Expand Down
8 changes: 7 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ pub trait ParserNodeType<'input>: TidAble<'input> + Sized {
/// It is a member of generated parser struct, so
/// almost always you don't need to create it yourself.
/// Generated parser hides complexity of this struct and expose required flexibility via generic parameters
#[derive(Tid)]
pub struct BaseParser<
'input,
Ext: 'static, //: ParserRecog<'input, Self> + 'static, // user provided behavior, such as semantic predicates
Expand Down Expand Up @@ -151,6 +150,13 @@ pub struct BaseParser<
pd: PhantomData<fn() -> &'input str>,
}

better_any::tid! {
impl<'input, Ext:'static, I, Ctx, T> TidAble<'input> for BaseParser<'input,Ext, I, Ctx, T>
where I: TokenStream<'input>,
Ctx: ParserNodeType<'input, TF = I::TF>,
T: ParseTreeListener<'input, Ctx> + ?Sized
}

impl<'input, Ext, I, Ctx, T> Deref for BaseParser<'input, Ext, I, Ctx, T>
where
Ext: ParserRecog<'input, Self> + 'static,
Expand Down
3 changes: 2 additions & 1 deletion src/parser_rule_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ pub fn cast_mut<'a, T: ParserRuleContext<'a> + 'a + ?Sized, Result: 'a>(
// pub type ParserRuleContextType<'input,T> = ParseTreeNode<'input,T>;

/// Default rule context implementation that keeps everything provided by parser
#[derive(Tid)]
pub struct BaseParserRuleContext<'input, Ctx: CustomRuleContext<'input>> {
base: BaseRuleContext<'input, Ctx>,

Expand All @@ -245,6 +244,8 @@ pub struct BaseParserRuleContext<'input, Ctx: CustomRuleContext<'input>> {
pub(crate) children: RefCell<Vec<Rc<<Ctx::Ctx as ParserNodeType<'input>>::Type>>>,
}

better_any::tid! { impl<'i,Ctx> TidAble<'i> for BaseParserRuleContext<'i,Ctx> where Ctx:CustomRuleContext<'i> }

impl<'input, Ctx: CustomRuleContext<'input>> Debug for BaseParserRuleContext<'input, Ctx> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { f.write_str(type_name::<Self>()) }
}
Expand Down
11 changes: 8 additions & 3 deletions src/rule_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ where
// Self: Sized;
// }

#[derive(Tid, Debug)]
#[derive(Debug)]
#[doc(hidden)]
pub struct EmptyCustomRuleContext<'a, TF: TokenFactory<'a> + 'a>(
pub(crate) PhantomData<&'a TF::Tok>,
);

better_any::tid! { impl <'a,TF> TidAble<'a> for EmptyCustomRuleContext<'a,TF> where TF:TokenFactory<'a> + 'a}

impl<'a, TF: TokenFactory<'a> + 'a> CustomRuleContext<'a> for EmptyCustomRuleContext<'a, TF> {
type TF = TF;
type Ctx = EmptyContextType<'a, TF>;
Expand All @@ -88,10 +90,12 @@ impl<'a, TF: TokenFactory<'a> + 'a> CustomRuleContext<'a> for EmptyCustomRuleCon
pub type EmptyContext<'a, TF> =
dyn ParserRuleContext<'a, TF = TF, Ctx = EmptyContextType<'a, TF>> + 'a;

#[derive(Tid, Debug)]
#[derive(Debug)]
#[doc(hidden)] // public for implementation reasons
pub struct EmptyContextType<'a, TF: TokenFactory<'a>>(pub PhantomData<&'a TF>);

better_any::tid! { impl <'a,TF> TidAble<'a> for EmptyContextType<'a,TF> where TF:TokenFactory<'a> }

impl<'a, TF: TokenFactory<'a>> ParserNodeType<'a> for EmptyContextType<'a, TF> {
type TF = TF;
type Type = dyn ParserRuleContext<'a, TF = Self::TF, Ctx = Self> + 'a;
Expand Down Expand Up @@ -127,13 +131,14 @@ pub trait CustomRuleContext<'input> {
}

/// Minimal parse tree node implementation, that stores only data required for correct parsing
#[derive(Tid)]
pub struct BaseRuleContext<'input, ExtCtx: CustomRuleContext<'input>> {
pub(crate) parent_ctx: RefCell<Option<Weak<<ExtCtx::Ctx as ParserNodeType<'input>>::Type>>>,
invoking_state: Cell<isize>,
pub(crate) ext: ExtCtx,
}

better_any::tid! { impl <'input,Ctx> TidAble<'input> for BaseRuleContext<'input,Ctx> where Ctx:CustomRuleContext<'input>}

#[allow(missing_docs)]
impl<'input, ExtCtx: CustomRuleContext<'input>> BaseRuleContext<'input, ExtCtx> {
pub fn new_parser_ctx(
Expand Down
11 changes: 8 additions & 3 deletions src/token_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ pub trait TokenFactory<'a>: TidAble<'a> + Sized {
}

/// Default token factory
#[derive(Default, Tid, Debug)]
#[derive(Default, Debug)]
pub struct CommonTokenFactory;

better_any::tid! {CommonTokenFactory}

impl Default for &'_ CommonTokenFactory {
fn default() -> Self { &**COMMON_TOKEN_FACTORY_DEFAULT }
}
Expand Down Expand Up @@ -139,9 +141,11 @@ impl<'a> TokenFactory<'a> for CommonTokenFactory {

/// Token factory that produces heap allocated
/// `OwningToken`s
#[derive(Default, Tid, Debug)]
#[derive(Default, Debug)]
pub struct OwningTokenFactory;

better_any::tid! {OwningTokenFactory}

impl<'a> TokenFactory<'a> for OwningTokenFactory {
type Inner = OwningToken;
type Tok = Box<Self::Inner>;
Expand Down Expand Up @@ -220,13 +224,14 @@ pub type ArenaCommonFactory<'a> = ArenaFactory<'a, CommonTokenFactory, CommonTok
/// const INVALID_TOKEN:CustomToken = ...
/// ```
// Box is used here because it is almost always should be used for token factory
#[derive(Tid)]
pub struct ArenaFactory<'input, TF, T> {
arena: Arena<T>,
factory: TF,
pd: PhantomData<&'input str>,
}

better_any::tid! {impl<'input,TF,T> TidAble<'input> for ArenaFactory<'input,TF,T>}

impl<'input, TF: Debug, T> Debug for ArenaFactory<'input, TF, T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ArenaFactory")
Expand Down
3 changes: 2 additions & 1 deletion src/token_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ impl<'a, 'input: 'a, T: TokenStream<'input>> Iterator for TokenIter<'a, 'input,
}

/// Token stream that keeps all data in internal Vec
#[derive(Tid)]
pub struct UnbufferedTokenStream<'input, T: TokenSource<'input>> {
token_source: T,
pub(crate) tokens: Vec<<T::TF as TokenFactory<'input>>::Tok>,
Expand All @@ -77,6 +76,8 @@ pub struct UnbufferedTokenStream<'input, T: TokenSource<'input>> {
markers_count: isize,
pub(crate) p: isize,
}
better_any::tid! { impl<'input,T> TidAble<'input> for UnbufferedTokenStream<'input, T> where T: TokenSource<'input>}

impl<'input, T: TokenSource<'input>> Debug for UnbufferedTokenStream<'input, T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("UnbufferedTokenStream")
Expand Down
10 changes: 5 additions & 5 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,20 @@ pub trait ParseTree<'input>: Tree<'input> {
// }

#[doc(hidden)]
#[derive(Tid, Debug)]
#[derive(Debug)]
pub struct NoError;

#[doc(hidden)]
#[derive(Tid, Debug)]
#[derive(Debug)]
pub struct IsError;

/// Generic leaf AST node
#[derive(Tid)]
pub struct LeafNode<'input, Node: ParserNodeType<'input>, T: 'static> {
/// Token, this leaf consist of
pub symbol: <Node::TF as TokenFactory<'input>>::Tok,
iserror: PhantomData<T>,
}
better_any::tid! { impl <'input, Node, T:'static> TidAble<'input> for LeafNode<'input, Node, T> where Node:ParserNodeType<'input> }

impl<'input, Node: ParserNodeType<'input>, T: 'static> CustomRuleContext<'input>
for LeafNode<'input, Node, T>
Expand All @@ -132,8 +132,8 @@ impl<'input, Node: ParserNodeType<'input>, T: 'static> CustomRuleContext<'input>
}
}

impl<'input, Node: ParserNodeType<'input> + TidAble<'input>, T: 'static + TidAble<'input>>
ParserRuleContext<'input> for LeafNode<'input, Node, T>
impl<'input, Node: ParserNodeType<'input>, T: 'static> ParserRuleContext<'input>
for LeafNode<'input, Node, T>
{
}

Expand Down
Loading

0 comments on commit b52022b

Please sign in to comment.