1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
//! Parser for SML '97.
//!
//! Currently, this only covers the Core language (no support for modules).
//!
//! The design of this module is heavily influenced by
//! [`rust-analyzer/crates/parser`](https://github.com/rust-lang/rust-analyzer/tree/master/crates/parser),
//! and uses [`rowan`](https://docs.rs/rowan/latest/rowan/) to construct the syntax tree.
//! It is also influenced by
//! [`apollo_parser`](https://docs.rs/apollo-parser/latest/apollo_parser/), a GraphQL parser in
//! Rust that also draws from `rust-analyzer`.
//!
//! TODO: Maybe add some examples here?
mod grammar;
mod syntax;
pub use syntax::SyntaxKind;
pub mod language;
mod parser;
pub use parser::{Parser, SyntaxTree};
pub mod ast;
pub use ast::{AstNode, AstPtr, AstToken};
pub mod passes;
#[cfg(test)]
pub(crate) mod tests;
use thiserror::Error;
/// An parsing error.
#[derive(Debug, Clone, Error, PartialEq, Eq)]
#[error("Error@{pos}: {msg}")]
pub struct Error {
msg: String,
text: String,
pos: usize,
}
impl Error {
pub fn new(msg: impl Into<String>, text: impl Into<String>, pos: usize) -> Self {
Self {
msg: msg.into(),
text: text.into(),
pos,
}
}
}
/// Lex and parse an input string.
///
/// Eventually, this can also apply validation passes
/// (currently, `passes::apply_passes` does nothing).
pub fn parse(input: &str) -> SyntaxTree {
let tree = Parser::new(input).parse();
passes::apply_passes(&tree);
tree
}