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
use super::{errors::*, ParseResult, Parser};
use crate::common::Span;
use crate::diagnostics::{Diagnostic, DiagnosticRecord};
use crate::grammar::*;
use crate::scanner::types::{Token, TokenType};
use crate::utils::{PeekIter, StringUtils};
pub fn parse(input: Vec<Token>, program: &str) -> ParseResult<StmtList> {
let mut parser = ExpressionParser::new(input, program);
let program = parser.parse();
let diagnostics = parser.diagnostics;
ParseResult {
program,
diagnostics,
}
}
pub struct ExpressionParser<'a> {
_input: PeekIter<Token>,
program: &'a str,
diagnostics: Vec<Diagnostic>,
}
impl<'a> ExpressionParser<'a> {
fn new(input: Vec<Token>, program: &'a str) -> Self {
Self {
_input: PeekIter::new(input.into_iter()),
program,
diagnostics: vec![],
}
}
fn parse_stmt(&mut self) -> Stmt {
#[allow(clippy::naive_bytecount)]
let vw = self
.peek_content()
.as_bytes()
.iter()
.filter(|&&c| c == b'\n')
.count()
.saturating_sub(1);
let mut next_2 = self.input().peek_map_n(2, |tok| (tok.ty.clone(), tok.span));
let kind = match (next_2.pop_front(), next_2.pop_front()) {
(
Some((TokenType::Variable(name), name_span)),
Some((asgn_ty @ TokenType::Equal, asgn_span)),
)
| (
Some((TokenType::Variable(name), name_span)),
Some((asgn_ty @ TokenType::AssignDefine, asgn_span)),
) => {
let Span { lo, .. } = self.input().next().unwrap().span;
self.input().next();
let asgn_op = if matches!(asgn_ty, TokenType::Equal) {
AssignmentOp::Equal(asgn_span)
} else {
AssignmentOp::AssignDefine(asgn_span)
};
let rhs = self.expr();
let span = (lo..rhs.span.hi).into();
StmtKind::Assignment(Assignment {
lhs: rc_expr!(Expr::Var(intern_str!(name)), name_span),
asgn_op,
rhs,
span,
})
}
_ => StmtKind::Expr(self.expr()),
};
Stmt::new(kind, vw)
}
fn parse_pattern(&mut self, name: String, span: Span) -> RcExpr {
self.push_diag(IllegalPattern!(span, name));
rc_expr!(Expr::Var(intern_str!(name)), span)
}
fn peek_content(&mut self) -> &str {
self.peek().full_span.clone().over(self.program)
}
}
impl<'a> Parser<StmtList> for ExpressionParser<'a> {
type Expr = RcExpr;
fn input(&mut self) -> &mut PeekIter<Token> {
&mut self._input
}
fn push_diag(&mut self, diagnostic: Diagnostic) {
self.diagnostics.push(diagnostic);
}
fn parse(&mut self) -> StmtList {
let mut stmts = Vec::new();
while !self.done() {
stmts.push(self.parse_stmt());
if !self.done() && !self.has_stmt_break() {
self.extra_tokens_diag(|diag, first_tok_span| {
diag.with_spanned_help(
first_tok_span,
"if you meant to specify another statement, add a newline before this token"
)
});
break;
}
}
StmtList::new(stmts)
}
fn parse_float(&mut self, f: f64, span: Span) -> Self::Expr {
rc_expr!(Expr::Const(f), span)
}
fn parse_variable(&mut self, name: String, span: Span) -> Self::Expr {
rc_expr!(Expr::Var(intern_str!(name)), span)
}
fn parse_var_pattern(&mut self, name: String, span: Span) -> Self::Expr {
self.parse_pattern(name, span)
}
fn parse_const_pattern(&mut self, name: String, span: Span) -> Self::Expr {
self.parse_pattern(name, span)
}
fn parse_any_pattern(&mut self, name: String, span: Span) -> Self::Expr {
self.parse_pattern(name, span)
}
fn has_stmt_break(&mut self) -> bool {
self.peek_content().contains('\n')
}
}
#[cfg(test)]
mod tests {
parser_tests! {
expr
variable: "a"
variable_in_op_left: "a + 1"
variable_in_op_right: "1 + a"
assignment_op: "a = 5"
assignment_op_expr: "a = 5 + 2 ^ 3"
}
}