"git@weirdboi.dev:microhacks/bevy-forks/kayak-ui.git" did not exist on "c8437b382b0cd1ce950c15cacd170a1c7c7fe5dc"
Newer
Older
use crate::lexer::{raw_decimal, raw_false, raw_true, ScriptToken, ScriptTokenType, Span};
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::{alpha1, alphanumeric1, one_of};
use nom::combinator::{opt, recognize};
use nom::multi::many0_count;
use nom::sequence::{pair, separated_pair};
use nom::{error_position, IResult};
use nom_locate::position;
pub fn token_ident(input: Span) -> IResult<Span, ScriptToken> {
let (input, pos) = position(input)?;
let (input, value) = recognize(pair(
alt((alpha1, tag("_"))),
many0_count(alt((alphanumeric1, tag("_")))),
))(input)?;
Ok((
input,
ScriptToken {
position: pos,
token_type: ScriptTokenType::Identifier(value.fragment().to_string()),
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
},
))
}
pub fn token_int(input: Span) -> IResult<Span, ScriptToken> {
let (input, pos) = position(input)?;
let (input, sign) = opt(one_of("+-"))(input)?;
let (input, value) = raw_decimal(input)?;
format!("{}{}", sign.map(String::from).unwrap_or_default(), value)
.parse::<i64>()
.map(|value| {
(
input,
ScriptToken {
token_type: ScriptTokenType::Integer(value),
position: pos,
},
)
})
.map_err(|_| nom::Err::Failure(error_position!(pos, nom::error::ErrorKind::Digit)))
}
pub fn token_float(input: Span) -> IResult<Span, ScriptToken> {
let (input, pos) = position(input)?;
let (input, sign) = opt(one_of("+-"))(input)?;
let (input, (before, after)) =
separated_pair(opt(raw_decimal), tag("."), opt(raw_decimal))(input)?;
let formatted_number = format!(
"{}{}.{}",
sign.map(String::from).unwrap_or_default(),
before
.map(|s| s.fragment().to_owned())
.unwrap_or_else(|| String::from("0")),
after
.map(|s| s.fragment().to_owned())
.unwrap_or_else(|| String::from("0"))
);
formatted_number
.parse::<f64>()
.map(|value| {
(
input,
ScriptToken {
token_type: ScriptTokenType::Float(value),
position: pos,
},
)
})
.map_err(|_| nom::Err::Failure(error_position!(pos, nom::error::ErrorKind::Digit)))
}
pub fn token_boolean(input: Span) -> IResult<Span, ScriptToken> {
let (input, pos) = position(input)?;
let (input, value) = alt((raw_true, raw_false))(input)?;
Ok((
input,
ScriptToken {
position: pos,
token_type: ScriptTokenType::Boolean(value),
},
))
}
#[cfg(test)]
mod parsing_tests {
use super::*;
use crate::map_result;
#[test]
fn parse_integer() {
let positive_cases = [
("1234", 1234),
("-1234", -1234),
("0", 0),
("1_000_000", 1000000),
("-12_34", -1234),
];
for (program, expected) in positive_cases {
map_result!(token_int(Span::new(program)), |_, token: ScriptToken| {
assert_eq!(token.token_type, ScriptTokenType::Integer(expected))
});
}
}
#[test]
fn parse_floats() {
let positive_cases = [
("12.34", 12.34),
("-12.34", -12.34),
("0.", 0.),
(".0", 0.),
(".0.", 0.),
(".0.1.2", 0.),
("1_000_000.1_23", 1000000.123),
("-12_34.0_0_0", -1234.0),
];
for (program, expected) in positive_cases {
map_result!(token_float(Span::new(program)), |_, token: ScriptToken| {
assert_eq!(token.token_type, ScriptTokenType::Float(expected))
});
}
}
#[test]
fn parse_bools() {
let positive_cases = [("true", true), ("false", false)];
for (program, expected) in positive_cases {
map_result!(
token_boolean(Span::new(program)),
|_, token: ScriptToken| {
assert_eq!(token.token_type, ScriptTokenType::Boolean(expected))
}
);
}
}
#[test]
fn parse_identifier() {
let positive_cases = ["BarBaz", "Foo", "foo", "foasd123", "_adad"];
for expected in positive_cases {
map_result!(token_ident(Span::new(expected)), |_, token: ScriptToken| {
assert_eq!(
token.token_type,
ScriptTokenType::Identifier(expected.to_string())
)