Skip to main content

wowlab_parsers/parsers/spell_desc/parser/
mod.rs

1mod expr;
2mod segment;
3mod variables;
4
5use segment::Parser;
6
7use super::{lexer::tokenize, types::ParsedSpellDescription};
8
9/// Parse a spell description string into a typed AST.
10#[must_use]
11pub fn parse(input: &str) -> ParseResult {
12    let tokens = tokenize(input);
13    let mut parser = Parser::new(&tokens);
14    let nodes = parser.parse_description();
15
16    ParseResult {
17        ast: ParsedSpellDescription { nodes },
18        errors: parser.errors,
19    }
20}
21
22/// Result of parsing a spell description.
23#[derive(Clone, Debug)]
24// docref:start spell-desc-parse-result
25pub struct ParseResult {
26    pub ast: ParsedSpellDescription,
27    pub errors: Vec<ParseError>,
28}
29// docref:end spell-desc-parse-result
30
31wowlab_engine_macros::define_error! {
32/// A parse error.
33#[derive(Clone, Debug, PartialEq)]
34pub struct ParseError {
35    kind: ParseErrorKind,
36}
37
38#[derive(Clone, Debug, thiserror::Error, PartialEq)]
39enum ParseErrorKind {
40    #[error("{0}")]
41    Message(String),
42}
43}
44
45impl ParseError {
46    pub(super) fn new(message: String) -> Self {
47        Self {
48            kind: ParseErrorKind::Message(message),
49        }
50    }
51    pub fn message(&self) -> &str {
52        match &self.kind {
53            ParseErrorKind::Message(message) => message,
54        }
55    }
56}