mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 20:09:19 +00:00
add TopEntryPoint
This commit is contained in:
parent
8e7fc7be65
commit
afffa096f6
8 changed files with 96 additions and 52 deletions
|
@ -83,43 +83,47 @@ pub(crate) mod entry {
|
|||
attributes::meta(p);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod top {
|
||||
use super::*;
|
||||
|
||||
pub(crate) fn source_file(p: &mut Parser) {
|
||||
let m = p.start();
|
||||
p.eat(SHEBANG);
|
||||
items::mod_contents(p, false);
|
||||
m.complete(p, SOURCE_FILE);
|
||||
}
|
||||
|
||||
pub(crate) fn macro_stmts(p: &mut Parser) {
|
||||
let m = p.start();
|
||||
|
||||
while !p.at(EOF) {
|
||||
if p.at(T![;]) {
|
||||
p.bump(T![;]);
|
||||
continue;
|
||||
}
|
||||
|
||||
expressions::stmt(p, expressions::StmtWithSemi::Optional, true);
|
||||
}
|
||||
|
||||
m.complete(p, MACRO_STMTS);
|
||||
}
|
||||
|
||||
pub(crate) fn macro_items(p: &mut Parser) {
|
||||
let m = p.start();
|
||||
items::mod_contents(p, false);
|
||||
m.complete(p, MACRO_ITEMS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod entry_points {
|
||||
use super::*;
|
||||
|
||||
pub(crate) fn source_file(p: &mut Parser) {
|
||||
let m = p.start();
|
||||
p.eat(SHEBANG);
|
||||
items::mod_contents(p, false);
|
||||
m.complete(p, SOURCE_FILE);
|
||||
}
|
||||
|
||||
pub(crate) fn stmt_optional_semi(p: &mut Parser) {
|
||||
expressions::stmt(p, expressions::StmtWithSemi::Optional, false);
|
||||
}
|
||||
|
||||
pub(crate) fn macro_items(p: &mut Parser) {
|
||||
let m = p.start();
|
||||
items::mod_contents(p, false);
|
||||
m.complete(p, MACRO_ITEMS);
|
||||
}
|
||||
|
||||
pub(crate) fn macro_stmts(p: &mut Parser) {
|
||||
let m = p.start();
|
||||
|
||||
while !p.at(EOF) {
|
||||
if p.at(T![;]) {
|
||||
p.bump(T![;]);
|
||||
continue;
|
||||
}
|
||||
|
||||
expressions::stmt(p, expressions::StmtWithSemi::Optional, true);
|
||||
}
|
||||
|
||||
m.complete(p, MACRO_STMTS);
|
||||
}
|
||||
|
||||
pub(crate) fn attr(p: &mut Parser) {
|
||||
attributes::outer_attrs(p);
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ pub use crate::{
|
|||
syntax_kind::SyntaxKind,
|
||||
};
|
||||
|
||||
/// Parse a syntactic construct at the *start* of the input.
|
||||
/// Parse a prefix of the input as a given syntactic construct.
|
||||
///
|
||||
/// This is used by macro-by-example parser to implement things like `$i:item`
|
||||
/// and the naming of variants follows the naming of macro fragments.
|
||||
|
@ -83,13 +83,61 @@ impl PrefixEntryPoint {
|
|||
}
|
||||
}
|
||||
|
||||
/// Parse the whole of the input as a given syntactic construct.
|
||||
///
|
||||
/// This covers two main use-cases:
|
||||
///
|
||||
/// * Parsing a Rust file.
|
||||
/// * Parsing a result of macro expansion.
|
||||
///
|
||||
/// That is, for something like
|
||||
///
|
||||
/// ```
|
||||
/// quick_check! {
|
||||
/// fn prop() {}
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// the input to the macro will be parsed with [`PrefixEntryPoint::Item`], and
|
||||
/// the result will be [`TopEntryPoint::Items`].
|
||||
///
|
||||
/// This *should* (but currently doesn't) guarantee that all input is consumed.
|
||||
#[derive(Debug)]
|
||||
pub enum TopEntryPoint {
|
||||
SourceFile,
|
||||
MacroStmts,
|
||||
MacroItems,
|
||||
Pattern,
|
||||
Type,
|
||||
Expr,
|
||||
MetaItem,
|
||||
}
|
||||
|
||||
impl TopEntryPoint {
|
||||
pub fn parse(&self, input: &Input) -> Output {
|
||||
let entry_point: fn(&'_ mut parser::Parser) = match self {
|
||||
TopEntryPoint::SourceFile => grammar::entry::top::source_file,
|
||||
TopEntryPoint::MacroStmts => grammar::entry::top::macro_stmts,
|
||||
TopEntryPoint::MacroItems => grammar::entry::top::macro_items,
|
||||
// FIXME
|
||||
TopEntryPoint::Pattern => grammar::entry::prefix::pat,
|
||||
TopEntryPoint::Type => grammar::entry::prefix::ty,
|
||||
TopEntryPoint::Expr => grammar::entry::prefix::expr,
|
||||
TopEntryPoint::MetaItem => grammar::entry::prefix::meta_item,
|
||||
};
|
||||
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.
|
||||
///
|
||||
/// The primary consumer of this API are declarative macros, `$x:expr` matchers
|
||||
/// are implemented by calling into the parser with non-standard entry point.
|
||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
||||
pub enum ParserEntryPoint {
|
||||
SourceFile,
|
||||
Path,
|
||||
Expr,
|
||||
StatementOptionalSemi,
|
||||
|
@ -97,14 +145,12 @@ pub enum ParserEntryPoint {
|
|||
Pattern,
|
||||
Item,
|
||||
MetaItem,
|
||||
Items,
|
||||
Statements,
|
||||
Attr,
|
||||
}
|
||||
|
||||
/// Parse given tokens into the given sink as a rust file.
|
||||
pub fn parse_source_file(inp: &Input) -> Output {
|
||||
parse(inp, ParserEntryPoint::SourceFile)
|
||||
pub fn parse_source_file(input: &Input) -> Output {
|
||||
TopEntryPoint::SourceFile.parse(input)
|
||||
}
|
||||
|
||||
/// Parses the given [`Input`] into [`Output`] assuming that the top-level
|
||||
|
@ -117,7 +163,6 @@ pub fn parse_source_file(inp: &Input) -> Output {
|
|||
/// indices between the four stages.
|
||||
pub fn parse(inp: &Input, entry_point: ParserEntryPoint) -> Output {
|
||||
let entry_point: fn(&'_ mut parser::Parser) = match entry_point {
|
||||
ParserEntryPoint::SourceFile => grammar::entry_points::source_file,
|
||||
ParserEntryPoint::Path => grammar::entry::prefix::path,
|
||||
ParserEntryPoint::Expr => grammar::entry::prefix::expr,
|
||||
ParserEntryPoint::Type => grammar::entry::prefix::ty,
|
||||
|
@ -125,8 +170,6 @@ pub fn parse(inp: &Input, entry_point: ParserEntryPoint) -> Output {
|
|||
ParserEntryPoint::Item => grammar::entry::prefix::item,
|
||||
ParserEntryPoint::MetaItem => grammar::entry::prefix::meta_item,
|
||||
ParserEntryPoint::StatementOptionalSemi => grammar::entry_points::stmt_optional_semi,
|
||||
ParserEntryPoint::Items => grammar::entry_points::macro_items,
|
||||
ParserEntryPoint::Statements => grammar::entry_points::macro_stmts,
|
||||
ParserEntryPoint::Attr => grammar::entry_points::attr,
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue