forked from ricosjp/ruststep
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'parse-literal' into 'master'
Parse literal See merge request ricos/truck/ruststep!7
- Loading branch information
Showing
4 changed files
with
123 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use nom::{ | ||
branch::*, bytes::complete::*, character::complete::*, combinator::*, number::complete::double, | ||
IResult, Parser, | ||
}; | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub enum Logical { | ||
False, | ||
True, | ||
Unknown, | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub enum Literal { | ||
Integer(u64), | ||
Real(f64), | ||
Logial(Logical), | ||
} | ||
|
||
/// 251 literal = binary_literal | logical_literal | real_literal | string_literal . | ||
pub fn literal(input: &str) -> IResult<&str, Literal> { | ||
alt(( | ||
logical_literal.map(|val| Literal::Logial(val)), | ||
real_literal.map(|val| Literal::Real(val)), | ||
// FIXME binary_literal, | ||
// FIXME string_literal | ||
)) | ||
.parse(input) | ||
} | ||
|
||
/// 255 logical_literal = `FALSE` | `TRUE` | `UNKNOWN` . | ||
pub fn logical_literal(input: &str) -> IResult<&str, Logical> { | ||
alt(( | ||
value(Logical::True, tag("TRUE")), | ||
value(Logical::False, tag("FALSE")), | ||
value(Logical::Unknown, tag("UNKNOWN")), | ||
)) | ||
.parse(input) | ||
} | ||
|
||
/// 141 integer_literal = digits . | ||
/// | ||
/// Negative integer, e.g. `-23`, | ||
/// will be represented by the combination of `-` unary operator and integer literal `23` | ||
/// | ||
/// Example | ||
/// -------- | ||
/// | ||
/// ``` | ||
/// use exp2rs::parser; | ||
/// use nom::Finish; | ||
/// | ||
/// let (residual, value) = parser::integer_literal("123").finish().unwrap(); | ||
/// assert_eq!(value, 123); | ||
/// assert_eq!(residual, ""); | ||
/// ``` | ||
pub fn integer_literal(input: &str) -> IResult<&str, u64> { | ||
digit1.map(|d: &str| d.parse().unwrap()).parse(input) | ||
} | ||
|
||
/// 142 real_literal = integer_literal | ( digits `.` [ digits ] [ `e` [ sign ] digits ] ) . | ||
/// | ||
/// Example | ||
/// -------- | ||
/// | ||
/// ``` | ||
/// use exp2rs::parser; | ||
/// use nom::Finish; | ||
/// | ||
/// let (residual, value) = parser::real_literal("123").finish().unwrap(); | ||
/// assert_eq!(value, 123.0); | ||
/// assert_eq!(residual, ""); | ||
/// | ||
/// let (residual, value) = parser::real_literal("123.456").finish().unwrap(); | ||
/// assert_eq!(value, 123.456); | ||
/// assert_eq!(residual, ""); | ||
/// | ||
/// let (residual, value) = parser::real_literal("1.23e-5").finish().unwrap(); | ||
/// assert_eq!(value, 1.23e-5); | ||
/// assert_eq!(residual, ""); | ||
/// ``` | ||
pub fn real_literal(input: &str) -> IResult<&str, f64> { | ||
double.parse(input) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters