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
//! Number parsing for Nash.
//!
//! Ported from Elm's `Parse/Number.hs`.
//! Currently only supports integers (no floats).
use crate::error;
use crate::{Col, Parser, Row};
impl<'a> Parser<'a> {
/// Parse an integer literal with custom error constructors.
///
/// Mirrors Elm's `Number.number`:
/// ```haskell
/// number :: (Row -> Col -> x) -> (E.Number -> Row -> Col -> x) -> Parser x Number
/// ```
///
/// Takes two error constructors:
/// - `to_expectation`: called when no digit is found (empty error, no input consumed)
/// - `to_error`: called when parsing fails after consuming input
///
/// Handles:
/// - Decimal integers: `42`, `123`
/// - Hex integers: `0xFF`, `0x1A2B`
///
/// # Example
/// ```ignore
/// // From expression parsing:
/// self.number_literal(error::Expr::Start, error::Expr::Number)
/// ```
pub fn number_literal<E>(
&mut self,
to_expectation: impl FnOnce(Row, Col) -> E,
to_error: impl FnOnce(error::Number, Row, Col) -> E,
) -> Result<i128, E> {
let (row, col) = self.position();
// Check first - if not a digit, return expectation error WITHOUT consuming
let first = match self.peek() {
Some(b) if b.is_ascii_digit() => b,
_ => return Err(to_expectation(row, col)),
};
// Now we're committed - consume the first digit
self.advance();
let result = if first == b'0' {
self.chomp_zero()
} else {
self.chomp_int((first - b'0') as i128)
};
result.map_err(|e| to_error(e, self.row(), self.col()))
}
/// Continue parsing after seeing a leading '0'.
fn chomp_zero(&mut self) -> Result<i128, error::Number> {
match self.peek() {
None => Ok(0),
Some(b'x') | Some(b'X') => {
self.advance();
self.chomp_hex()
}
Some(b) if b.is_ascii_digit() => {
// Leading zeros not allowed: 007, 00, etc.
Err(error::Number::NoLeadingZero)
}
Some(b'.') if matches!(self.peek_at(1), Some(d) if d.is_ascii_digit()) => {
Err(error::Number::Dot(0))
}
Some(b) if is_ident_inner(b) => {
// 0abc - dirty end
Err(error::Number::End)
}
Some(_) => Ok(0),
}
}
/// Parse remaining decimal digits after the first non-zero digit.
fn chomp_int(&mut self, mut n: i128) -> Result<i128, error::Number> {
loop {
match self.peek() {
Some(b) if b.is_ascii_digit() => {
n = n * 10 + (b - b'0') as i128;
self.advance();
}
Some(b'.') if matches!(self.peek_at(1), Some(d) if d.is_ascii_digit()) => {
return Err(error::Number::Dot(n));
}
Some(b) if is_ident_inner(b) => {
// 123abc - dirty end
return Err(error::Number::End);
}
_ => return Ok(n),
}
}
}
/// Parse hex digits after `0x`.
fn chomp_hex(&mut self) -> Result<i128, error::Number> {
let mut n: i128 = 0;
let mut has_digits = false;
loop {
match self.peek() {
Some(b) if b.is_ascii_hexdigit() => {
has_digits = true;
n = n * 16 + hex_value(b) as i128;
self.advance();
}
Some(b) if is_ident_inner(b) => {
// 0xGG or 0x1G - invalid hex followed by ident char
return Err(error::Number::HexDigit);
}
_ => {
if has_digits {
return Ok(n);
} else {
// 0x without any hex digits
return Err(error::Number::HexDigit);
}
}
}
}
}
}
/// Check if a byte is a valid inner identifier character.
/// Used to detect "dirty end" like `123abc`.
#[inline]
fn is_ident_inner(b: u8) -> bool {
b.is_ascii_alphabetic() || b == b'_'
}
/// Get the numeric value of a hex digit.
#[inline]
pub(crate) fn hex_value(b: u8) -> u8 {
match b {
b'0'..=b'9' => b - b'0',
b'a'..=b'f' => b - b'a' + 10,
b'A'..=b'F' => b - b'A' + 10,
_ => unreachable!(),
}
}