Skip to content

Commit

Permalink
Closes #16 implement boolean value parsing
Browse files Browse the repository at this point in the history
Implemented `parse_boolean(input: &str)` returns `Value::True` for
'true' and `Value::False` for 'false'.
  • Loading branch information
atahanyorganci committed Jan 29, 2022
1 parent 56a8b24 commit 6b34426
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use std::num::{ParseFloatError, ParseIntError};

use nom::{
branch::alt,
bytes::complete::tag,
error::{context, VerboseError},
IResult,
};
use pest::{
error::LineColLocation,
iterators::{Pair, Pairs},
Parser, Span,
};
use thiserror::Error;

use crate::ast::statement::Statement;
use crate::ast::{statement::Statement, value::Value};

#[derive(Parser)]
#[grammar = "parser/alloy.pest"]
Expand Down Expand Up @@ -53,6 +59,8 @@ impl ParserError {
}
}

type Res<T, U> = IResult<T, U, VerboseError<T>>;

pub type ParseResult<T> = Result<T, ParserError>;

pub trait Parse<'a>: Sized {
Expand Down Expand Up @@ -98,3 +106,28 @@ pub fn parse(input: &str) -> Result<Vec<Statement>, ParserError> {
}),
}
}

fn parse_boolean(input: &str) -> Res<&str, Value> {
let result = context("boolean", alt((tag("true"), tag("false"))))(input);
result.map(|(next_input, res)| {
let value = if res == "true" {
Value::True
} else {
Value::False
};
(next_input, value)
})
}

#[cfg(test)]
mod tests {
use crate::ast::value::Value;

use super::parse_boolean;

#[test]
fn test_boolean() {
assert_eq!(parse_boolean("true"), Ok(("", Value::True)));
assert_eq!(parse_boolean("false"), Ok(("", Value::False)));
}
}

0 comments on commit 6b34426

Please sign in to comment.