Skip to content

Commit

Permalink
Upgrade nom to 8.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vinc committed Jan 27, 2025
1 parent 1ab0158 commit 48f65bb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 49 deletions.
11 changes: 2 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ lazy_static = { version = "1.5.0", features = ["spin_no_std"] }
libm = "0.2.11"
linked_list_allocator = "0.10.5"
littlewing = { version = "0.7.0", default-features = false }
nom = { version = "7.1.3", default-features = false, features = ["alloc"] }
nom = { version = "8.0.0", default-features = false, features = ["alloc"] }
num-bigint = { version = "0.4.6", default-features = false }
num-traits = { version = "0.2.19", default-features = false }
object = { version = "0.36.7", default-features = false, features = ["read"] }
Expand Down
22 changes: 13 additions & 9 deletions src/usr/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use nom::character::complete::{char, space0};
use nom::combinator::map;
use nom::multi::many0;
use nom::number::complete::double;
use nom::sequence::{delimited, tuple};
use nom::sequence::delimited;
use nom::IResult;
use nom::Parser;

// Adapted from Basic Calculator
// Copyright 2021 Balaji Sivaraman
Expand All @@ -35,32 +36,35 @@ pub enum Exp {
fn parse(input: &str) -> IResult<&str, Exp> {
let (input, num1) = parse_term(input)?;
let (input, exps) = many0(
tuple((alt((char('+'), char('-'))), parse_term))
)(input)?;
(alt((char('+'), char('-'))), parse_term)
).parse(input)?;
Ok((input, parse_exp(num1, exps)))
}

fn parse_term(input: &str) -> IResult<&str, Exp> {
let (input, num1) = parse_factor(input)?;
let (input, exps) = many0(tuple((
let (input, exps) = many0((
alt((char('%'), char('/'), char('*'))),
parse_factor,
)))(input)?;
)).parse(input)?;
Ok((input, parse_exp(num1, exps)))
}

fn parse_factor(input: &str) -> IResult<&str, Exp> {
let (input, num1) = alt((parse_parens, parse_num))(input)?;
let (input, exps) = many0(tuple((char('^'), parse_factor)))(input)?;
let (input, num1) = alt((parse_parens, parse_num)).parse(input)?;
let (input, exps) = many0((char('^'), parse_factor)).parse(input)?;
Ok((input, parse_exp(num1, exps)))
}

fn parse_parens(input: &str) -> IResult<&str, Exp> {
delimited(space0, delimited(char('('), parse, char(')')), space0)(input)
delimited(
space0,
delimited(char('('), parse, char(')')), space0
).parse(input)
}

fn parse_num(input: &str) -> IResult<&str, Exp> {
map(delimited(space0, double, space0), Exp::Num)(input)
map(delimited(space0, double, space0), Exp::Num).parse(input)
}

fn parse_exp(exp: Exp, rem: Vec<(char, Exp)>) -> Exp {
Expand Down
62 changes: 32 additions & 30 deletions src/usr/lisp/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use nom::multi::many1;
use nom::sequence::delimited;
use nom::sequence::preceded;
use nom::sequence::terminated;
use nom::sequence::tuple;
use nom::Err::Error;
use nom::IResult;
use nom::Parser;

// https://docs.rs/nom/latest/nom/recipes/index.html#hexadecimal
fn hexadecimal(input: &str) -> IResult<&str, &str> {
Expand All @@ -34,48 +34,50 @@ fn hexadecimal(input: &str) -> IResult<&str, &str> {
one_of("0123456789abcdefABCDEF"),
many0(char('_')),
))),
)(input)
).parse(input)
}

// https://docs.rs/nom/latest/nom/recipes/index.html#decimal
fn decimal(input: &str) -> IResult<&str, &str> {
recognize(many1(terminated(one_of("0123456789"), many0(char('_')))))(input)
recognize(
many1(terminated(one_of("0123456789"), many0(char('_'))))
).parse(input)
}

// https://docs.rs/nom/latest/nom/recipes/index.html#binary
fn binary(input: &str) -> IResult<&str, &str> {
preceded(
tag("0b"),
recognize(many1(terminated(one_of("01"), many0(char('_'))))),
)(input)
recognize(many1(terminated(one_of("01"), many0(char('_')))))
).parse(input)
}

// https://docs.rs/nom/latest/nom/recipes/index.html#floating-point-numbers
fn float(input: &str) -> IResult<&str, &str> {
alt((
recognize(
// .42
tuple((
(
char('.'),
decimal,
opt(tuple((one_of("eE"), opt(one_of("+-")), decimal))),
)),
opt((one_of("eE"), opt(one_of("+-")), decimal))
),
),
recognize(
// 42e42 and 42.42e42
tuple((
(
decimal,
opt(preceded(char('.'), decimal)),
one_of("eE"),
opt(one_of("+-")),
decimal,
)),
decimal
),
),
recognize(
// 42. and 42.42
tuple((decimal, char('.'), opt(decimal))),
(decimal, char('.'), opt(decimal))
),
))(input)
)).parse(input)
}

fn is_symbol_letter(c: char) -> bool {
Expand All @@ -95,76 +97,76 @@ fn parse_str(input: &str) -> IResult<&str, Exp> {
value("\r", tag("r")),
value("\t", tag("t")),
value("\x08", tag("b")),
value("\x1B", tag("e")),
value("\x1B", tag("e"))
)),
)),
|inner| inner.unwrap_or("".to_string()),
);
let (input, s) = delimited(char('"'), escaped, char('"'))(input)?;
let (input, s) = delimited(char('"'), escaped, char('"')).parse(input)?;
Ok((input, Exp::Str(s)))
}

fn parse_sym(input: &str) -> IResult<&str, Exp> {
let (input, sym) = take_while1(is_symbol_letter)(input)?;
let (input, sym) = take_while1(is_symbol_letter).parse(input)?;
Ok((input, Exp::Sym(sym.to_string())))
}

fn parse_num(input: &str) -> IResult<&str, Exp> {
let (input, num) = recognize(tuple((
let (input, num) = recognize((
opt(alt((char('+'), char('-')))),
alt((float, hexadecimal, binary, decimal)),
)))(input)?;
)).parse(input)?;
Ok((input, Exp::Num(Number::from(num))))
}

fn parse_bool(input: &str) -> IResult<&str, Exp> {
let (input, s) = alt((tag("true"), tag("false")))(input)?;
let (input, s) = alt((tag("true"), tag("false"))).parse(input)?;
Ok((input, Exp::Bool(s == "true")))
}

fn parse_list(input: &str) -> IResult<&str, Exp> {
let (input, list) = delimited(
char('('), many0(parse_exp), char(')')
)(input)?;
).parse(input)?;
Ok((input, Exp::List(list)))
}

fn parse_quote(input: &str) -> IResult<&str, Exp> {
let (input, list) = preceded(char('\''), parse_exp)(input)?;
let (input, list) = preceded(char('\''), parse_exp).parse(input)?;
let list = vec![Exp::Sym("quote".to_string()), list];
Ok((input, Exp::List(list)))
}

fn parse_unquote_splice(input: &str) -> IResult<&str, Exp> {
let (input, list) = preceded(tag(",@"), parse_exp)(input)?;
let (input, list) = preceded(tag(",@"), parse_exp).parse(input)?;
let list = vec![Exp::Sym("unquote-splice".to_string()), list];
Ok((input, Exp::List(list)))
}

fn parse_splice(input: &str) -> IResult<&str, Exp> {
let (input, list) = preceded(tag("@"), parse_exp)(input)?;
let (input, list) = preceded(tag("@"), parse_exp).parse(input)?;
let list = vec![Exp::Sym("splice".to_string()), list];
Ok((input, Exp::List(list)))
}

fn parse_unquote(input: &str) -> IResult<&str, Exp> {
let (input, list) = preceded(char(','), parse_exp)(input)?;
let (input, list) = preceded(char(','), parse_exp).parse(input)?;
let list = vec![Exp::Sym("unquote".to_string()), list];
Ok((input, Exp::List(list)))
}

fn parse_quasiquote(input: &str) -> IResult<&str, Exp> {
let (input, list) = preceded(char('`'), parse_exp)(input)?;
let (input, list) = preceded(char('`'), parse_exp).parse(input)?;
let list = vec![Exp::Sym("quasiquote".to_string()), list];
Ok((input, Exp::List(list)))
}

fn parse_comment(input: &str) -> IResult<&str, &str> {
preceded(multispace0, preceded(char('#'), is_not("\n")))(input)
preceded(multispace0, preceded(char('#'), is_not("\n"))).parse(input)
}

fn parse_exp(input: &str) -> IResult<&str, Exp> {
let (input, _) = opt(many0(parse_comment))(input)?;
let (input, _) = opt(many0(parse_comment)).parse(input)?;
delimited(
multispace0,
alt((
Expand All @@ -177,10 +179,10 @@ fn parse_exp(input: &str) -> IResult<&str, Exp> {
parse_unquote_splice,
parse_unquote,
parse_splice,
parse_sym,
parse_sym
)),
alt((parse_comment, multispace0)),
)(input)
alt((parse_comment, multispace0))
).parse(input)
}

pub fn parse(input: &str) -> Result<(String, Exp), Err> {
Expand Down

0 comments on commit 48f65bb

Please sign in to comment.