main
annieversary 2021-09-26 17:05:30 +02:00
parent 9390fb4fd5
commit a66e769b5a
1 changed files with 12 additions and 23 deletions

View File

@ -1,17 +1,12 @@
use nom::{ use nom::{
branch::alt, branch::alt,
bytes::complete::{escaped, is_not}, bytes::complete::{escaped, is_not, tag, take_until},
bytes::complete::{tag, take_until}, character::complete::{alpha1, alphanumeric1, char, multispace0, one_of, space0},
character::complete::char, combinator::{cut, map, recognize, value},
character::complete::multispace0, error::ParseError,
character::complete::{alpha1, alphanumeric1, digit1, one_of, space0},
combinator::value,
combinator::{cut, map, map_res, recognize},
error::{context, convert_error, ContextError, ErrorKind, ParseError, VerboseError},
multi::{fold_many0, many0, separated_list0}, multi::{fold_many0, many0, separated_list0},
number::complete::double, number::complete::double,
sequence::pair, sequence::{delimited, pair, preceded, terminated, tuple},
sequence::{delimited, preceded, terminated, tuple},
IResult, Parser, IResult, Parser,
}; };
@ -28,17 +23,11 @@ fn pinline_comment<'a, E>(i: &'a str) -> IResult<&'a str, (), E>
where where
E: ParseError<&'a str>, E: ParseError<&'a str>,
{ {
value( value((), tuple((tag("/*"), take_until("*/"), tag("*/"))))(i)
(), // Output is thrown away.
tuple((tag("/*"), take_until("*/"), tag("*/"))),
)(i)
} }
fn peol_comment<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, (), E> { fn peol_comment<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, (), E> {
value( value((), pair(char('/'), is_not("\n\r")))(i)
(), // Output is thrown away.
pair(char('/'), is_not("\n\r")),
)(i)
} }
fn braces<'a, P, O, E>(a: P) -> impl FnMut(&'a str) -> IResult<&'a str, O, E> fn braces<'a, P, O, E>(a: P) -> impl FnMut(&'a str) -> IResult<&'a str, O, E>
@ -145,13 +134,13 @@ fn bin_equal_not(i: &str) -> IResult<&str, Expr> {
let (i, init) = bin_less_greater(i)?; let (i, init) = bin_less_greater(i)?;
fold_many0( fold_many0(
pair(alt((char('<'), char('>'))), bin_less_greater), pair(alt((tag("=="), tag("!="))), bin_less_greater),
move || init.clone(), move || init.clone(),
|acc, (op, val): (char, Expr)| { |acc, (op, val)| {
if op == '<' { if op == "==" {
Expr::Binary(BinOp::LessThan, Box::new(acc), Box::new(val)) Expr::Binary(BinOp::Equals, Box::new(acc), Box::new(val))
} else { } else {
Expr::Binary(BinOp::GreaterThan, Box::new(acc), Box::new(val)) Expr::Binary(BinOp::NotEquals, Box::new(acc), Box::new(val))
} }
}, },
)(i) )(i)