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
//! Primitive parsing: integers, string literals, identifiers
use nom::{
character::complete::{char, digit1},
combinator::{opt, recognize},
Parser,
};
pub fn parse_integer(input: &str) -> nom::IResult<&str, i64> {
let (input, s) = recognize(|i| {
let (i, _) = opt(char('-')).parse(i)?;
digit1(i)
})
.parse(input)?;
let n: i64 = s.parse().map_err(|_| {
nom::Err::Error(nom::error::Error::new(input, nom::error::ErrorKind::Digit))
})?;
Ok((input, n))
}
pub fn parse_string_literal(input: &str) -> nom::IResult<&str, String> {
let (input, _) = char('"').parse(input)?;
let mut result = String::new();
let mut chars = input.chars();
let mut consumed = 0;
loop {
match chars.next() {
None => {
return Err(nom::Err::Error(nom::error::Error::new(
input,
nom::error::ErrorKind::Char,
)));
}
Some('"') => {
consumed += 1;
break;
}
Some('\\') => {
consumed += 1;
match chars.next() {
None => {
return Err(nom::Err::Error(nom::error::Error::new(
input,
nom::error::ErrorKind::Char,
)));
}
Some('n') => {
result.push('\n');
consumed += 1;
}
Some('r') => {
result.push('\r');
consumed += 1;
}
Some('t') => {
result.push('\t');
consumed += 1;
}
Some('\\') => {
result.push('\\');
consumed += 1;
}
Some('"') => {
result.push('"');
consumed += 1;
}
Some('/') => {
result.push('/');
consumed += 1;
}
Some('b') => {
result.push('\u{0008}');
consumed += 1;
}
Some('f') => {
result.push('\u{000C}');
consumed += 1;
}
Some('u') => {
consumed += 1;
let mut hex = String::new();
for _ in 0..4 {
match chars.next() {
Some(c) if c.is_ascii_hexdigit() => {
hex.push(c);
consumed += c.len_utf8();
}
_ => {
return Err(nom::Err::Error(nom::error::Error::new(
input,
nom::error::ErrorKind::HexDigit,
)));
}
}
}
let code = u32::from_str_radix(&hex, 16).map_err(|_| {
nom::Err::Error(nom::error::Error::new(
input,
nom::error::ErrorKind::HexDigit,
))
})?;
let c = char::from_u32(code).ok_or_else(|| {
nom::Err::Error(nom::error::Error::new(
input,
nom::error::ErrorKind::Char,
))
})?;
result.push(c);
}
Some(_) => {
return Err(nom::Err::Error(nom::error::Error::new(
input,
nom::error::ErrorKind::Char,
)));
}
}
}
Some(c) => {
result.push(c);
consumed += c.len_utf8();
}
}
}
Ok((&input[consumed..], result))
}
pub fn identifier(input: &str) -> nom::IResult<&str, String> {
let mut chars = input.chars();
let first = chars.next().ok_or_else(|| {
nom::Err::Error(nom::error::Error::new(input, nom::error::ErrorKind::Alpha))
})?;
if !first.is_alphabetic() && first != '_' {
return Err(nom::Err::Error(nom::error::Error::new(
input,
nom::error::ErrorKind::Alpha,
)));
}
let mut len = first.len_utf8();
for c in chars {
if c.is_alphanumeric() || c == '_' {
len += c.len_utf8();
} else {
break;
}
}
Ok((&input[len..], input[..len].to_string()))
}