Skip to content

Commit

Permalink
Move imports to the top
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhoo committed Mar 9, 2020
1 parent 011d47f commit 20a683c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 66 deletions.
36 changes: 4 additions & 32 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ use std::ops::{
use crate::literal::{self, CChar};
use crate::token::{Kind as TokenKind, Token};
use crate::ToCexprResult;
use nom::branch::alt;
use nom::combinator::{complete, map, map_opt};
use nom::multi::{fold_many0, many0, separated_list};
use nom::sequence::{delimited, pair, preceded};
use nom::*;

/// Expression parser/evaluator that supports identifiers.
Expand Down Expand Up @@ -300,9 +304,6 @@ where

impl<'a> PRef<'a> {
fn unary<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::branch::alt;
use nom::combinator::map_opt;
use nom::sequence::{delimited, pair};
alt((
delimited(p("("), |i| self.numeric_expr(i), p(")")),
numeric(|i| self.literal(i)),
Expand All @@ -315,9 +316,6 @@ impl<'a> PRef<'a> {
}

fn mul_div_rem<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::combinator::complete;
use nom::multi::fold_many0;
use nom::sequence::pair;
let (input, acc) = self.unary(input)?;
fold_many0(
pair(complete(one_of_punctuation(&["*", "/", "%"][..])), |i| {
Expand All @@ -337,9 +335,6 @@ impl<'a> PRef<'a> {
}

fn add_sub<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::combinator::complete;
use nom::multi::fold_many0;
use nom::sequence::pair;
let (input, acc) = self.mul_div_rem(input)?;
fold_many0(
pair(complete(one_of_punctuation(&["+", "-"][..])), |i| {
Expand All @@ -358,9 +353,6 @@ impl<'a> PRef<'a> {
}

fn shl_shr<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::combinator::complete;
use nom::multi::fold_many0;
use nom::sequence::pair;
let (input, acc) = self.add_sub(input)?;
numeric(fold_many0(
pair(complete(one_of_punctuation(&["<<", ">>"][..])), |i| {
Expand All @@ -379,9 +371,6 @@ impl<'a> PRef<'a> {
}

fn and<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::combinator::complete;
use nom::multi::fold_many0;
use nom::sequence::preceded;
let (input, acc) = self.shl_shr(input)?;
numeric(fold_many0(
preceded(complete(p("&")), |i| self.shl_shr(i)),
Expand All @@ -394,9 +383,6 @@ impl<'a> PRef<'a> {
}

fn xor<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::combinator::complete;
use nom::multi::fold_many0;
use nom::sequence::preceded;
let (input, acc) = self.and(input)?;
numeric(fold_many0(
preceded(complete(p("^")), |i| self.and(i)),
Expand All @@ -409,9 +395,6 @@ impl<'a> PRef<'a> {
}

fn or<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::combinator::complete;
use nom::multi::fold_many0;
use nom::sequence::preceded;
let (input, acc) = self.xor(input)?;
numeric(fold_many0(
preceded(complete(p("|")), |i| self.xor(i)),
Expand Down Expand Up @@ -478,8 +461,6 @@ impl<'a> PRef<'a> {
}

fn string<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, Vec<u8>> {
use nom::branch::alt;
use nom::combinator::map_opt;
alt((
map_opt(|i| self.literal(i), EvalResult::as_str),
map_opt(|i| self.identifier(i), EvalResult::as_str),
Expand All @@ -489,10 +470,6 @@ impl<'a> PRef<'a> {

// "string1" "string2" etc...
fn concat_str<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::combinator::complete;
use nom::combinator::map;
use nom::multi::many0;
use nom::sequence::pair;
map(
pair(|i| self.string(i), many0(complete(|i| self.string(i)))),
|(first, v)| {
Expand All @@ -508,8 +485,6 @@ impl<'a> PRef<'a> {
}

fn expr<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, EvalResult> {
use nom::branch::alt;
use nom::sequence::delimited;
alt((
|i| self.numeric_expr(i),
delimited(p("("), |i| self.expr(i), p(")")),
Expand All @@ -521,7 +496,6 @@ impl<'a> PRef<'a> {
}

fn macro_definition<'t>(&'_ self, input: &'t [Token]) -> CResult<'t, (&'t [u8], EvalResult)> {
use nom::sequence::pair;
pair(typed_token!(Identifier), |i| self.expr(i))(input)
}
}
Expand Down Expand Up @@ -643,8 +617,6 @@ pub fn macro_definition<'a>(input: &'a [Token]) -> CResult<'a, (&'a [u8], EvalRe
/// assert_eq!(evaluated, EvalResult::Str(b"testsuffix".to_vec()));
/// ```
pub fn fn_macro_declaration(input: &[Token]) -> CResult<'_, (&[u8], Vec<&[u8]>)> {
use nom::multi::separated_list;
use nom::sequence::{delimited, pair};
pair(
typed_token!(Identifier),
delimited(
Expand Down
41 changes: 7 additions & 34 deletions src/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
use std::char;
use std::str::{self, FromStr};

use nom::branch::alt;
use nom::bytes::complete::is_not;
use nom::bytes::complete::tag;
use nom::character::complete::{char, one_of};
use nom::combinator::{complete, map, map_opt, opt, recognize};
use nom::multi::{fold_many0, many0, many1, many_m_n};
use nom::sequence::{delimited, pair, preceded, terminated, tuple};
use nom::*;

use crate::expr::EvalResult;
Expand Down Expand Up @@ -89,7 +96,6 @@ where
F: Fn(I) -> nom::IResult<I, O, (I, E)>,
{
move |input| {
use nom::lib::std::result::Result::*;
let res = f(input);
match res {
Ok((i, o)) => {
Expand Down Expand Up @@ -174,11 +180,6 @@ fn c_unicode_escape(n: Vec<u8>) -> Option<CChar> {
}

fn escaped_char(i: &[u8]) -> nom::IResult<&[u8], CChar> {
use nom::branch::alt;
use nom::character::complete::{char, one_of};
use nom::combinator::{map, map_opt};
use nom::multi::{many1, many_m_n};
use nom::sequence::preceded;
preceded(
char('\\'),
alt((
Expand All @@ -201,16 +202,10 @@ fn escaped_char(i: &[u8]) -> nom::IResult<&[u8], CChar> {
}

fn c_width_prefix(i: &[u8]) -> nom::IResult<&[u8], &[u8]> {
use nom::branch::alt;
use nom::bytes::complete::tag;
alt((tag("u8"), tag("u"), tag("U"), tag("L")))(i)
}

fn c_char(i: &[u8]) -> nom::IResult<&[u8], CChar> {
use nom::branch::alt;
use nom::character::complete::char;
use nom::combinator::{map, opt};
use nom::sequence::{delimited, terminated};
delimited(
terminated(opt(c_width_prefix), char('\'')),
alt((
Expand All @@ -222,12 +217,6 @@ fn c_char(i: &[u8]) -> nom::IResult<&[u8], CChar> {
}

fn c_string(i: &[u8]) -> nom::IResult<&[u8], Vec<u8>> {
use nom::branch::alt;
use nom::bytes::complete::is_not;
use nom::character::complete::char;
use nom::combinator::map;
use nom::multi::fold_many0;
use nom::sequence::{delimited, preceded};
delimited(
alt((preceded(c_width_prefix, char('"')), char('"'))),
fold_many0(
Expand Down Expand Up @@ -264,12 +253,6 @@ fn take_ul(input: &[u8]) -> IResult<&[u8], &[u8]> {
}

fn c_int(i: &[u8]) -> nom::IResult<&[u8], i64> {
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::char;
use nom::combinator::{complete, map, map_opt, opt};
use nom::multi::many1;
use nom::sequence::{preceded, terminated};
map(
terminated(
alt((
Expand Down Expand Up @@ -306,20 +289,13 @@ fn float_width(i: &[u8]) -> nom::IResult<&[u8], u8> {
}

fn float_exp(i: &[u8]) -> nom::IResult<&[u8], (Option<u8>, Vec<u8>)> {
use nom::combinator::{complete, opt};
use nom::multi::many1;
use nom::sequence::{pair, preceded};
preceded(
byte!(b'e' | b'E'),
pair(opt(byte!(b'-' | b'+')), many1(complete(decimal))),
)(i)
}

fn c_float(i: &[u8]) -> nom::IResult<&[u8], f64> {
use nom::branch::alt;
use nom::combinator::{complete, map_opt, opt, recognize};
use nom::multi::{many0, many1};
use nom::sequence::{terminated, tuple};
map_opt(
alt((
terminated(
Expand Down Expand Up @@ -367,9 +343,6 @@ fn c_float(i: &[u8]) -> nom::IResult<&[u8], f64> {
// ================================

fn one_literal(input: &[u8]) -> nom::IResult<&[u8], EvalResult, crate::Error<&[u8]>> {
use nom::branch::alt;
use nom::combinator::map;

alt((
map(full(c_char), EvalResult::Char),
map(full(c_int), |i| EvalResult::Int(::std::num::Wrapping(i))),
Expand Down

0 comments on commit 20a683c

Please sign in to comment.