Skip to content

Commit

Permalink
move precedence parsing to nom-language
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Dec 8, 2024
1 parent f1abba6 commit 1aebf89
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 14 deletions.
1 change: 1 addition & 0 deletions nom-language/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
//! at language parsing.
pub mod error;
pub mod precedence;
8 changes: 2 additions & 6 deletions src/precedence/mod.rs → nom-language/src/precedence/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
//! Combinators to parse expressions with operator precedence.
#![cfg(feature = "alloc")]
#![cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))]
#[cfg(test)]
mod tests;

use crate::error::{ErrorKind, FromExternalError, ParseError};
use crate::lib::std::vec::Vec;
use crate::{Err, IResult, Parser};
use nom::error::{ErrorKind, FromExternalError, ParseError};
use nom::{Err, IResult, Parser};

/// An unary operator.
pub struct Unary<V, Q: Ord + Copy> {
Expand Down Expand Up @@ -203,7 +200,6 @@ where
/// * It then reads the remaining input and evaluates the increment next in order to preserve its
/// position in the expression \
/// `((-a)++)**b`.
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))]
pub fn precedence<I, O, E, E2, F, G, H1, H3, H2, P1, P2, P3, Q>(
mut prefix: H1,
mut postfix: H2,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::precedence::{binary_op, unary_op, Assoc, Operation};
use crate::{
use nom::{
branch::alt,
bytes::complete::tag,
character::complete::digit1,
Expand All @@ -9,10 +9,8 @@ use crate::{
sequence::delimited,
};

#[cfg(feature = "alloc")]
use crate::precedence::precedence;

#[cfg(feature = "alloc")]
fn parser(i: &str) -> IResult<&str, i64> {
precedence(
unary_op(1, tag("-")),
Expand Down Expand Up @@ -42,7 +40,6 @@ fn parser(i: &str) -> IResult<&str, i64> {
}

#[test]
#[cfg(feature = "alloc")]
fn precedence_test() {
assert_eq!(parser("3"), Ok(("", 3)));
assert_eq!(parser("-3"), Ok(("", -3)));
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,6 @@ pub mod bytes;

pub mod character;

pub mod precedence;

mod str;

pub mod number;
Expand Down
4 changes: 2 additions & 2 deletions tests/expression_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use nom::{
character::complete::{alphanumeric1 as alphanumeric, digit1 as digit},
combinator::{map, map_res},
multi::separated_list0,
precedence::{binary_op, precedence, unary_op, Assoc, Operation},
sequence::delimited,
IResult, Parser,
};
use nom_language::precedence::{binary_op, precedence, unary_op, Assoc, Operation};

// Elements of the abstract syntax tree (ast) that represents an expression.
#[derive(Debug)]
Expand Down Expand Up @@ -119,7 +119,7 @@ fn expression(i: &str) -> IResult<&str, Expr> {
delimited(tag("("), expression, tag(")")),
)),
|op: Operation<PrefixOp, PostfixOp, BinaryOp, Expr>| -> Result<Expr, ()> {
use nom::precedence::Operation::*;
use nom_language::precedence::Operation::*;
use BinaryOp::*;
use PostfixOp::*;
use PrefixOp::*;
Expand Down

0 comments on commit 1aebf89

Please sign in to comment.