fe_parser/lib.rs
1pub mod ast;
2pub mod grammar;
3pub mod lexer;
4pub use lexer::{Token, TokenKind};
5mod parser;
6pub use parser::{Label, ParseFailed, ParseResult, Parser};
7pub mod node;
8
9use ast::Module;
10use fe_common::diagnostics::Diagnostic;
11use fe_common::files::SourceFileId;
12
13/// Parse a [`Module`] from the file content string.
14///
15/// Returns a `Module` (which may be incomplete), and a vec of [`Diagnostic`]s
16/// (which may be empty) to display to the user. If any of the returned
17/// diagnostics are errors, the compilation of this file should ultimately fail.
18///
19/// If a fatal parse error occurred, the last element of the `Module::body` will
20/// be a `ModuleStmt::ParseError`. The parser currently has very limited ability
21/// to recover from syntax errors; this is just a first meager attempt at returning a
22/// useful AST when there are syntax errors.
23///
24/// A [`SourceFileId`] is required to associate any diagnostics with the
25/// underlying file.
26pub fn parse_file(file_id: SourceFileId, src: &str) -> (Module, Vec<Diagnostic>) {
27 let mut parser = Parser::new(file_id, src);
28 let node = crate::grammar::module::parse_module(&mut parser);
29 (node.kind, parser.diagnostics)
30}