internal: add prefix entry points

This commit is contained in:
Aleksey Kladov 2021-12-27 15:17:48 +03:00
parent d3ba55bd06
commit abc658aad0
2 changed files with 36 additions and 0 deletions

View file

@ -44,6 +44,18 @@ use crate::{
TokenSet, T, TokenSet, T,
}; };
pub(crate) mod entry {
use super::*;
pub(crate) mod prefix {
use super::*;
pub(crate) fn vis(p: &mut Parser) {
let _ = opt_visibility(p, false);
}
}
}
pub(crate) mod entry_points { pub(crate) mod entry_points {
use super::*; use super::*;

View file

@ -41,6 +41,30 @@ pub use crate::{
syntax_kind::SyntaxKind, syntax_kind::SyntaxKind,
}; };
/// Parse a syntactic construct at the *start* of the input.
///
/// This is used by macro-by-example parser to implement things like `$i:item`.
///
/// Note that this is generally non-optional -- the result is intentionally not
/// `Option<Output>`. The way MBE work, by the time we *try* to parse `$e:expr`
/// we already commit to expression. In other words, this API by design can't be
/// used to implement "rollback and try another alternative" logic.
pub enum PrefixEntryPoint {
Vis,
}
impl PrefixEntryPoint {
pub fn parse(self, input: &Input) -> Output {
let entry_point: fn(&'_ mut parser::Parser) = match self {
PrefixEntryPoint::Vis => grammar::entry::prefix::vis,
};
let mut p = parser::Parser::new(input);
entry_point(&mut p);
let events = p.finish();
event::process(events)
}
}
/// rust-analyzer parser allows you to choose one of the possible entry points. /// rust-analyzer parser allows you to choose one of the possible entry points.
/// ///
/// The primary consumer of this API are declarative macros, `$x:expr` matchers /// The primary consumer of this API are declarative macros, `$x:expr` matchers