Files
rust/crates/parser/src/lib.rs

130 lines
4.2 KiB
Rust
Raw Normal View History

2019-02-21 15:24:42 +03:00
//! The Rust parser.
//!
2021-12-12 19:06:40 +03:00
//! NOTE: The crate is undergoing refactors, don't believe everything the docs
//! say :-)
//!
2019-02-21 15:24:42 +03:00
//! The parser doesn't know about concrete representation of tokens and syntax
2021-12-12 19:06:40 +03:00
//! trees. Abstract [`TokenSource`] and [`TreeSink`] traits are used instead. As
//! a consequence, this crate does not contain a lexer.
2019-02-21 15:24:42 +03:00
//!
//! The [`Parser`] struct from the [`parser`] module is a cursor into the
//! sequence of tokens. Parsing routines use [`Parser`] to inspect current
//! state and advance the parsing.
2019-02-21 15:24:42 +03:00
//!
//! The actual parsing happens in the [`grammar`] module.
2019-02-21 15:24:42 +03:00
//!
//! Tests for this crate live in the `syntax` crate.
//!
//! [`Parser`]: crate::parser::Parser
#![allow(rustdoc::private_intra_doc_links)]
2021-09-06 18:42:07 +03:00
2021-12-12 21:32:58 +03:00
mod lexer_token;
2019-02-21 13:27:45 +03:00
mod token_set;
mod syntax_kind;
mod event;
mod parser;
mod grammar;
2021-11-14 16:47:13 +03:00
mod tokens;
2019-02-21 13:27:45 +03:00
2021-12-12 21:32:58 +03:00
#[cfg(test)]
mod tests;
2019-02-21 13:27:45 +03:00
pub(crate) use token_set::TokenSet;
2021-12-12 21:32:58 +03:00
pub use crate::{lexer_token::LexerToken, syntax_kind::SyntaxKind, tokens::Tokens};
2021-11-14 22:13:44 +03:00
2019-02-21 13:27:45 +03:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ParseError(pub Box<String>);
2019-02-21 13:27:45 +03:00
/// `TreeSink` abstracts details of a particular syntax tree implementation.
pub trait TreeSink {
2019-03-30 13:25:53 +03:00
/// Adds new token to the current branch.
fn token(&mut self, kind: SyntaxKind, n_tokens: u8);
2019-02-21 13:27:45 +03:00
/// Start new branch and make it current.
2019-03-30 13:25:53 +03:00
fn start_node(&mut self, kind: SyntaxKind);
2019-02-21 13:27:45 +03:00
/// Finish current branch and restore previous
/// branch as current.
2019-03-30 13:25:53 +03:00
fn finish_node(&mut self);
2019-02-21 13:27:45 +03:00
fn error(&mut self, error: ParseError);
}
/// 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.
2020-03-02 14:05:15 +08:00
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum ParserEntryPoint {
SourceFile,
2019-09-02 18:51:03 +03:00
Path,
Expr,
Statement,
StatementOptionalSemi,
2019-09-02 18:51:03 +03:00
Type,
Pattern,
Item,
Block,
Visibility,
MetaItem,
Items,
Statements,
2020-12-18 18:58:42 +01:00
Attr,
2019-09-02 18:51:03 +03:00
}
/// Parse given tokens into the given sink as a rust file.
2021-11-14 22:13:44 +03:00
pub fn parse_source_file(tokens: &Tokens, tree_sink: &mut dyn TreeSink) {
parse(tokens, tree_sink, ParserEntryPoint::SourceFile);
}
2021-11-14 22:13:44 +03:00
pub fn parse(tokens: &Tokens, tree_sink: &mut dyn TreeSink, entry_point: ParserEntryPoint) {
let entry_point: fn(&'_ mut parser::Parser) = match entry_point {
ParserEntryPoint::SourceFile => grammar::entry_points::source_file,
ParserEntryPoint::Path => grammar::entry_points::path,
ParserEntryPoint::Expr => grammar::entry_points::expr,
ParserEntryPoint::Type => grammar::entry_points::type_,
ParserEntryPoint::Pattern => grammar::entry_points::pattern,
ParserEntryPoint::Item => grammar::entry_points::item,
ParserEntryPoint::Block => grammar::entry_points::block_expr,
ParserEntryPoint::Visibility => grammar::entry_points::visibility,
ParserEntryPoint::MetaItem => grammar::entry_points::meta_item,
ParserEntryPoint::Statement => grammar::entry_points::stmt,
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,
2019-09-02 18:51:03 +03:00
};
2021-11-14 22:13:44 +03:00
let mut p = parser::Parser::new(tokens);
entry_point(&mut p);
let events = p.finish();
event::process(tree_sink, events);
2019-04-19 03:49:56 +08:00
}
2019-02-21 15:24:42 +03:00
/// A parsing function for a specific braced-block.
2019-02-21 13:27:45 +03:00
pub struct Reparser(fn(&mut parser::Parser));
impl Reparser {
2019-02-21 15:24:42 +03:00
/// If the node is a braced block, return the corresponding `Reparser`.
2019-02-21 13:27:45 +03:00
pub fn for_node(
node: SyntaxKind,
first_child: Option<SyntaxKind>,
parent: Option<SyntaxKind>,
) -> Option<Reparser> {
grammar::reparser(node, first_child, parent).map(Reparser)
}
2019-02-21 15:24:42 +03:00
/// Re-parse given tokens using this `Reparser`.
///
/// Tokens must start with `{`, end with `}` and form a valid brace
/// sequence.
2021-11-14 22:13:44 +03:00
pub fn parse(self, tokens: &Tokens, tree_sink: &mut dyn TreeSink) {
2019-02-21 13:37:32 +03:00
let Reparser(r) = self;
2021-11-14 22:13:44 +03:00
let mut p = parser::Parser::new(tokens);
2019-02-21 13:37:32 +03:00
r(&mut p);
let events = p.finish();
event::process(tree_sink, events);
}
2019-02-21 13:27:45 +03:00
}