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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// lib.rs - MIT License
// MIT License
// Copyright (c) 2018 Tyler Laing (ZerothLaw)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//! # Introduction
//!
//! This crate implements a configurable and general-purpose Pratt parser.
//!
//! A Pratt parser is also known as a Top-Down Operator Precedence parser,
//! aka TDOP parser. The general algorithm was discovered and outlined by
//! Vaughn Pratt in 1973 in his paper[1].
//!
//! It differs from recursive-descent classes of parsers by associating
//! parsing rules to tokens rather than grammar rules.
//!
//! This is especially valuable when parsing expression grammars with
//! operator precedence without significant descent and type costs.
//!
//! Consider the following expression:
//!
//! > a + b * c
//!
//! So which variables are to be associated with which operators?
//! A recursive-descent parser would need to implement two layers of
//! grammar rules which are recursive with each other. This leads to
//! potential infinite loops if your parser follows the wrong rule and
//! doesn't backtrack effectively.
//!
//! Whereas with a Pratt Parser, you need just three rules and three
//! binding powers:
//! null denotation of Variable => Node::Simple(token);
//! left denotation of Add => Node::Composite(token: token, children: vec![node,
//! parser.parse_expr(5)]);
//! left denotation of Mul => Node::Composite(token: token, children: vec![node,
//! parser.parse_expr(10)]);
//! lbp of Variable = 0;
//! lbp of Add = 5;
//! lbp of Mul = 10;
//!
//! And now it will correctly associate 'b' and 'c' tokens with the mul operator, and
//! then the result of that with the 'a' token and the add operator.
//!
//! It also executes with far fewer parse calls, creating a minimal stack depth
//! during execution.
//!
//! ## Example
//!
//! ```rust
//! use std::fmt::{Display, Formatter, Error};
//!
//! extern crate prattle;
//! use prattle::prelude::*;
//!
//! #[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
//! enum CToken {
//! Var(String),
//! Add,
//! Mul
//! }
//!
//! impl Display for CToken {
//! fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
//! write!(f, "{:?}", self)
//! }
//! }
//!
//! fn main() {
//! let mut spec = ParserSpec::new();
//!
//! spec.add_null_assoc(
//! CToken::Var("".to_string()),
//! PrecedenceLevel::Root,
//! |parser: &mut dyn Parser<CToken>, token, _| {
//! Ok(Node::Simple(token.clone()))
//! }
//! );
//! spec.add_left_assoc(
//! CToken::Add,
//! PrecedenceLevel::First,
//! |parser, token, lbp, node| {
//! Ok(Node::Composite{token: token.clone(), children: vec![
//! node,
//! parser.parse_expr(lbp)?]})
//! });
//! spec.add_left_assoc(
//! CToken::Mul,
//! PrecedenceLevel::Second,
//! |parser, token, lbp, node| {
//! Ok(Node::Composite{token: token.clone(), children: vec![
//! node,
//! parser.parse_expr(lbp)?]})
//! });
//!
//! let lexer = LexerVec::new(vec![
//! CToken::Var("a".to_string()),
//! CToken::Add,
//! CToken::Var("b".to_string()),
//! CToken::Mul,
//! CToken::Var("c".to_string())
//! ]);
//!
//! let mut parser = GeneralParser::new(spec, lexer);
//!
//! let res = parser.parse();
//! println!("{:?}", res);
//! }
//! ```
//!
//! ## Capabilities
//!
//! This crate enables a very fast and simple parser, with simple rules, that is
//! easy to understand and maintain. Most of the work is in implementing the
//! required traits on your Token type.
//!
//! ## More complex examples
//!
//! Run:
//! > cargo run --example token_spec
//!
//! examples/token_spec.rs shows an example of how to implement the traits for
//! the token type so it can be used to lookup the parse rules (uses HashMap).
//!
//! ## Citations
//! > [1] Vaughan R. Pratt. 1973. Top down operator precedence. In Proceedings
//! > of the 1st annual ACM SIGACT-SIGPLAN symposium on Principles of
//! > programming languages (POPL '73). ACM, New York, NY, USA, 41-51.
//! > DOI=http://dx.doi.org/10.1145/512927.512931
//!
extern crate failure;
/// Handy prelude mod containing everything you need to get started.
//Little container mod for type aliases that are convenient and short