rust-analyzer/crates/parser/src/lib.rs

126 lines
4.2 KiB
Rust
Raw Normal View History

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