make bool handling case insensitive

This commit is contained in:
Robin Appelman 2020-12-07 21:18:09 +01:00
commit 575d5be572
2 changed files with 9 additions and 2 deletions

View file

@ -4,7 +4,7 @@ use logos::Logos;
pub enum Token { pub enum Token {
#[token("array")] #[token("array")]
Array, Array,
#[regex("true|false")] #[regex("(?i:true|false)")]
Bool, Bool,
#[regex("null")] #[regex("null")]
Null, Null,

View file

@ -47,7 +47,13 @@ pub fn parse_lexer<'source>(
]) ])
.with_span(lexer.span(), source)?; .with_span(lexer.span(), source)?;
let value = match token { let value = match token {
Token::Bool => Value::Bool(lexer.slice().parse().with_span(lexer.span(), source)?), Token::Bool => Value::Bool(
lexer
.slice()
.to_ascii_lowercase()
.parse()
.with_span(lexer.span(), source)?,
),
Token::Integer => Value::Int(parse_int(lexer.slice()).with_span(lexer.span(), source)?), Token::Integer => Value::Int(parse_int(lexer.slice()).with_span(lexer.span(), source)?),
Token::Float => Value::Float(lexer.slice().parse().with_span(lexer.span(), source)?), Token::Float => Value::Float(lexer.slice().parse().with_span(lexer.span(), source)?),
Token::LiteralString => { Token::LiteralString => {
@ -282,4 +288,5 @@ fn test_parse() {
assert_eq!(Value::Int(26), parse(r#"0x1A"#).unwrap()); assert_eq!(Value::Int(26), parse(r#"0x1A"#).unwrap());
assert_eq!(Value::Int(3), parse(r#"0b11"#).unwrap()); assert_eq!(Value::Int(3), parse(r#"0b11"#).unwrap());
assert_eq!(Value::Int(12345), parse(r#"12_34_5"#).unwrap()); assert_eq!(Value::Int(12345), parse(r#"12_34_5"#).unwrap());
assert_eq!(Value::Bool(true), parse(r#"True"#).unwrap());
} }