3 //! The parser doesn't know about concrete representation of tokens and syntax
4 //! trees. Abstract [`TokenSource`] and [`TreeSink`] traits are used instead.
5 //! As a consequence, this crate does not contain a lexer.
7 //! The [`Parser`] struct from the [`parser`] module is a cursor into the
8 //! sequence of tokens. Parsing routines use [`Parser`] to inspect current
9 //! state and advance the parsing.
11 //! The actual parsing happens in the [`grammar`] module.
13 //! Tests for this crate live in the `syntax` crate.
15 //! [`Parser`]: crate::parser::Parser
16 #![allow(rustdoc::private_intra_doc_links)]
24 pub(crate) use token_set::TokenSet;
26 pub use syntax_kind::SyntaxKind;
28 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
29 pub struct ParseError(pub Box<String>);
31 /// `TokenSource` abstracts the source of the tokens parser operates on.
33 /// Hopefully this will allow us to treat text and token trees in the same way!
34 pub trait TokenSource {
35 fn current(&self) -> Token;
38 fn lookahead_nth(&self, n: usize) -> Token;
40 /// bump cursor to next token
43 /// Is the current token a specified keyword?
44 fn is_keyword(&self, kw: &str) -> bool;
47 /// `Token` abstracts the cursor of `TokenSource` operates on.
48 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
50 /// What is the current token?
53 /// Is the current token joined to the next one (`> >` vs `>>`).
54 pub is_jointed_to_next: bool,
57 /// `TreeSink` abstracts details of a particular syntax tree implementation.
59 /// Adds new token to the current branch.
60 fn token(&mut self, kind: SyntaxKind, n_tokens: u8);
62 /// Start new branch and make it current.
63 fn start_node(&mut self, kind: SyntaxKind);
65 /// Finish current branch and restore previous
66 /// branch as current.
67 fn finish_node(&mut self);
69 fn error(&mut self, error: ParseError);
72 /// rust-analyzer parser allows you to choose one of the possible entry points.
74 /// The primary consumer of this API are declarative macros, `$x:expr` matchers
75 /// are implemented by calling into the parser with non-standard entry point.
76 #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
77 pub enum ParserEntryPoint {
82 StatementOptionalSemi,
94 /// Parse given tokens into the given sink as a rust file.
95 pub fn parse_source_file(token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) {
96 parse(token_source, tree_sink, ParserEntryPoint::SourceFile);
100 token_source: &mut dyn TokenSource,
101 tree_sink: &mut dyn TreeSink,
102 entry_point: ParserEntryPoint,
104 let entry_point: fn(&'_ mut parser::Parser) = match entry_point {
105 ParserEntryPoint::SourceFile => grammar::entry_points::source_file,
106 ParserEntryPoint::Path => grammar::entry_points::path,
107 ParserEntryPoint::Expr => grammar::entry_points::expr,
108 ParserEntryPoint::Type => grammar::entry_points::type_,
109 ParserEntryPoint::Pattern => grammar::entry_points::pattern,
110 ParserEntryPoint::Item => grammar::entry_points::item,
111 ParserEntryPoint::Block => grammar::entry_points::block_expr,
112 ParserEntryPoint::Visibility => grammar::entry_points::visibility,
113 ParserEntryPoint::MetaItem => grammar::entry_points::meta_item,
114 ParserEntryPoint::Statement => grammar::entry_points::stmt,
115 ParserEntryPoint::StatementOptionalSemi => grammar::entry_points::stmt_optional_semi,
116 ParserEntryPoint::Items => grammar::entry_points::macro_items,
117 ParserEntryPoint::Statements => grammar::entry_points::macro_stmts,
118 ParserEntryPoint::Attr => grammar::entry_points::attr,
121 let mut p = parser::Parser::new(token_source);
123 let events = p.finish();
124 event::process(tree_sink, events);
127 /// A parsing function for a specific braced-block.
128 pub struct Reparser(fn(&mut parser::Parser));
131 /// If the node is a braced block, return the corresponding `Reparser`.
134 first_child: Option<SyntaxKind>,
135 parent: Option<SyntaxKind>,
136 ) -> Option<Reparser> {
137 grammar::reparser(node, first_child, parent).map(Reparser)
140 /// Re-parse given tokens using this `Reparser`.
142 /// Tokens must start with `{`, end with `}` and form a valid brace
144 pub fn parse(self, token_source: &mut dyn TokenSource, tree_sink: &mut dyn TreeSink) {
145 let Reparser(r) = self;
146 let mut p = parser::Parser::new(token_source);
148 let events = p.finish();
149 event::process(tree_sink, events);