welly_parser/
lib.rs

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
mod tree;
pub use tree::{Tree, EndOfFile};

mod stream;
pub use stream::{Location, Loc, Token, Stream, Characters};

mod parser;
pub use parser::{Context, Parse};

pub mod lexer;
pub mod word;
pub mod bracket;
pub mod expr;
pub mod stmt;

mod buffer;
pub use buffer::{Buffer};

/// Re-exports all the Welly parse [`Tree`] types (except [`char`]) and [`Op`].
///
/// [`Op`]: expr::Op
pub mod welly {
    use super::*;

    pub type Comment = lexer::Comment;
    pub type CharacterLiteral = lexer::CharacterLiteral;
    pub type StringLiteral = lexer::StringLiteral;
    pub type Whitespace = word::Whitespace;
    pub type Symbol = word::Symbol;
    pub type Alphanumeric = word::Alphanumeric;
    pub type Round = bracket::Round;
    pub type Square = bracket::Square;
    pub type Brace = bracket::Brace;
    pub type Op = expr::Op;
    pub type Expr = expr::Expr;
    pub type MaybeExpr = expr::MaybeExpr;
    pub type AssignOp = stmt::AssignOp;
    pub type Stmt = stmt::Stmt;
}

/// Re-exports all the Welly [`Parse`] implementations and [`Brackets`].
pub mod parsers {
    use super::*;
    use welly::{Round, Brace};

    pub const LEXER: lexer::Parser = lexer::Parser;

    pub type Word = word::Parser;

    /// Returns a parser that replaces all Welly keywords with their [`Tree`]s.
    pub fn word() -> Word {
        let mut ret = Word::default();
        ret.add_keywords::<expr::Operator>();
        ret.add_keywords::<expr::Keyword>();
        ret.add_keywords::<stmt::Keyword>();
        ret.add_keywords::<stmt::AssignOp>();
        ret
    }

    pub type Brackets<I, F> = bracket::Brackets<I, F>;

    /// Returns a [`Brackets`] that recognises [`Round`]s and parses their
    /// contents into [`Expr`]s.
    ///
    /// It parses a [`Stream`] containing [`Brace`]s, words, lexemes and
    /// [`char`]s.
    ///
    /// [`Expr`]: expr::Expr
    pub fn round(input: impl Stream) -> impl Stream {
        STMT.parse_stream(EXPR.parse_stream(Brackets::new('(', ')', |contents| {
            let contents = EXPR.parse_stream(contents.into_iter()).read_all();
            Round::new(contents)
        }, input)))
    }

    /// Returns a [`Brackets`] that recognises [`Brace`]s and parses their
    /// contents into [`Stmt`]s.
    ///
    /// Parse a [`Stream`] containing words, lexemes and [`char`]s.
    ///
    /// [`Stmt`]: stmt::Stmt
    pub fn brace(input: impl Stream) -> impl Stream {
        round(Brackets::new('{', '}', |contents| {
            let contents = round(contents.into_iter()).read_all();
            Brace::new(contents)
        }, input))
    }

    pub const EXPR: expr::Parser = expr::Parser;
    pub const STMT: stmt::Parser = stmt::Parser;
}

// ----------------------------------------------------------------------------

#[cfg(test)]
mod tests {}