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
//! Operator/symbol parsing for Nash.
//!
//! Ported from Elm's `Parse/Symbol.hs`.
use nash_region::{Located, Region};
use crate::Parser;
use crate::error::BadOperator;
impl<'a> Parser<'a> {
/// Parse an operator (binary op or symbolic identifier).
///
/// Mirrors Elm's `Symbol.operator`:
/// ```haskell
/// operator :: (Row -> Col -> x) -> (BadOperator -> Row -> Col -> x) -> Parser x Name.Name
/// ```
///
/// Valid operator characters: `+-/*=.<>:&|^?%!$`
///
/// Reserved operators that cannot be parsed standalone:
/// - `.` (dot)
/// - `|` (pipe - reserved for case branches)
/// - `->` (arrow - reserved for lambdas/types)
/// - `=` (equals - reserved for definitions)
/// - `:` (colon - reserved for type annotations)
pub(crate) fn operator<E>(
&mut self,
to_expectation: impl FnOnce(usize, usize) -> E,
to_error: impl FnOnce(BadOperator, usize, usize) -> E,
) -> Result<&'a str, E> {
let (row, col) = self.position();
let start_pos = self.pos;
// Chomp all operator characters
while let Some(b) = self.peek() {
if is_binop_char(b) {
self.advance();
} else {
break;
}
}
// No operator characters found
if self.pos == start_pos {
return Err(to_expectation(row, col));
}
let op = self.slice_from(start_pos);
// Check for reserved operators
match op {
"." => Err(to_error(BadOperator::Dot, row, col)),
"|" => Err(to_error(BadOperator::Pipe, row, col)),
"->" => Err(to_error(BadOperator::Arrow, row, col)),
"=" => Err(to_error(BadOperator::Equals, row, col)),
":" => Err(to_error(BadOperator::HasType, row, col)),
"=>" => Err(to_error(BadOperator::FatArrow, row, col)),
"<-" => Err(to_error(BadOperator::LeftArrow, row, col)),
_ => Ok(op),
}
}
/// Parse an operator and wrap it in a Located.
pub(crate) fn add_location_operator<E>(
&mut self,
to_expectation: impl FnOnce(usize, usize) -> E,
to_error: impl FnOnce(BadOperator, usize, usize) -> E,
) -> Result<&'a Located<&'a str>, E> {
let start = self.get_position();
let op = self.operator(to_expectation, to_error)?;
let end = self.get_position();
Ok(self.alloc(Located::at(Region::new(start, end), op)))
}
}
/// Check if a byte is a valid operator character.
///
/// Valid: `+-/*=.<>:&|^?%!$`
#[inline]
pub fn is_binop_char(b: u8) -> bool {
matches!(
b,
b'+' | b'-'
| b'/'
| b'*'
| b'='
| b'.'
| b'<'
| b'>'
| b':'
| b'&'
| b'|'
| b'^'
| b'?'
| b'%'
| b'!'
| b'$'
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_binop_char() {
assert!(is_binop_char(b'+'));
assert!(is_binop_char(b'-'));
assert!(is_binop_char(b'*'));
assert!(is_binop_char(b'/'));
assert!(is_binop_char(b'<'));
assert!(is_binop_char(b'>'));
assert!(is_binop_char(b'|'));
assert!(is_binop_char(b'&'));
assert!(!is_binop_char(b'a'));
assert!(!is_binop_char(b' '));
}
}