Skip to main content

welly_parser/
lib.rs

1mod tree;
2pub use tree::{Tree, EndOfFile};
3
4mod stream;
5pub use stream::{Location, Loc, Token, Stream, Characters};
6
7mod parser;
8pub use parser::{Context, Parse};
9
10pub mod lexer;
11pub mod word;
12pub mod bracket;
13pub mod expr;
14pub mod stmt;
15
16/// Re-exports all the Welly [`Parse`] implementations and [`Brackets`].
17pub mod parsers {
18    use super::*;
19    use bracket::{Round, Brace};
20
21    pub const LEXER: lexer::Parser = lexer::Parser;
22
23    pub type Word = word::Parser;
24
25    /// Returns a parser that replaces all Welly keywords with their [`Tree`]s.
26    pub fn word() -> Word {
27        let mut ret = Word::default();
28        ret.add_keywords::<expr::Operator>();
29        ret.add_keywords::<expr::Keyword>();
30        ret.add_keywords::<stmt::Keyword>();
31        ret.add_keywords::<stmt::AssignOp>();
32        ret
33    }
34
35    pub type Brackets<I, F> = bracket::Brackets<I, F>;
36
37    /// Returns a [`Brackets`] that recognises [`Round`]s and parses their
38    /// contents into [`Expr`]s.
39    ///
40    /// It parses a [`Stream`] containing [`Brace`]s, words, lexemes and
41    /// [`char`]s.
42    ///
43    /// [`Expr`]: expr::Expr
44    pub fn round(input: impl Stream) -> impl Stream {
45        STMT.parse_stream(EXPR.parse_stream(Brackets::new('(', ')', |contents| {
46            let contents = EXPR.parse_stream(contents.into_iter()).read_all();
47            Round::new(contents)
48        }, input)))
49    }
50
51    /// Returns a [`Brackets`] that recognises [`Brace`]s and parses their
52    /// contents into [`Stmt`]s.
53    ///
54    /// It parses a [`Stream`] containing words, lexemes and [`char`]s.
55    ///
56    /// [`Stmt`]: stmt::Stmt
57    pub fn brace(input: impl Stream) -> impl Stream {
58        round(Brackets::new('{', '}', |contents| {
59            let contents = round(contents.into_iter()).read_all();
60            Brace::new(contents)
61        }, input))
62    }
63
64    pub const EXPR: expr::Parser = expr::Parser;
65    pub const STMT: stmt::Parser = stmt::Parser;
66}
67
68mod valid;
69pub use valid::{Invalid, AST};
70
71pub mod ast;
72
73mod buffer;
74pub use buffer::{Buffer};
75
76// ----------------------------------------------------------------------------
77
78#[cfg(test)]
79mod tests {}