Skip to content

Commit 74b4eee

Browse files
committed
Fmt
1 parent adc8d11 commit 74b4eee

File tree

11 files changed

+148
-131
lines changed

11 files changed

+148
-131
lines changed

rustfmt.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tab_spaces = 2

src/grammar/mod.rs

+34-13
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,24 @@ pub enum Stmt<'a> {
8888

8989
#[derive(Debug, Clone)]
9090
pub enum FlowCtrl<'a> {
91-
Ret{ ret: Option<Box<Expr<'a>>> },
92-
Break{ label: Option<&'a str>, ret: Option<Box<Expr<'a>>> },
93-
Cont{ label: Option<&'a str> },
91+
Ret {
92+
ret: Option<Box<Expr<'a>>>,
93+
},
94+
Break {
95+
label: Option<&'a str>,
96+
ret: Option<Box<Expr<'a>>>,
97+
},
98+
Cont {
99+
label: Option<&'a str>,
100+
},
94101
}
95102

96103
#[derive(Debug, Clone)]
97104
pub enum Expr<'a> {
98105
Literal(Literal),
99106
Block {
100-
body: Vec<Stmt<'a>>,
101-
ret: Option<Box<Expr<'a>>>
107+
body: Vec<Stmt<'a>>,
108+
ret: Option<Box<Expr<'a>>>,
102109
},
103110
Tuple(Vec<Expr<'a>>),
104111
Array(Vec<Expr<'a>>),
@@ -107,8 +114,8 @@ pub enum Expr<'a> {
107114
count: Box<Expr<'a>>,
108115
},
109116
Call {
110-
recv: Box<Expr<'a>>,
111-
method: Option<&'a str>,
117+
recv: Box<Expr<'a>>,
118+
method: Option<&'a str>,
112119
params: Vec<Expr<'a>>,
113120
},
114121
Field {
@@ -190,8 +197,16 @@ pub enum BinaryOp {
190197

191198
#[derive(Debug, Clone)]
192199
pub enum ArithOp {
193-
Add, Sub, Mul, Div, Mod,
194-
BitAnd, BitOr, BitXor, BitLS, BitRS,
200+
Add,
201+
Sub,
202+
Mul,
203+
Div,
204+
Mod,
205+
BitAnd,
206+
BitOr,
207+
BitXor,
208+
BitLS,
209+
BitRS,
195210
}
196211

197212
impl ArithOp {
@@ -224,8 +239,14 @@ impl ArithOp {
224239

225240
#[derive(Debug, Clone)]
226241
pub enum LogicalOp {
227-
And, Or,
228-
Eq, Ne, L, G, Le, Ge,
242+
And,
243+
Or,
244+
Eq,
245+
Ne,
246+
L,
247+
G,
248+
Le,
249+
Ge,
229250
}
230251

231252
impl LogicalOp {
@@ -271,11 +292,11 @@ pub enum LoopCond<'a> {
271292
#[derive(Debug, Clone)]
272293
pub enum IfCond<'a> {
273294
Let(Pat<'a>, Box<Expr<'a>>),
274-
Bool(Box<Expr<'a>>)
295+
Bool(Box<Expr<'a>>),
275296
}
276297

277298
#[derive(Debug, Clone)]
278299
pub struct MatchArm<'a> {
279300
pub pats: Vec<Pat<'a>>,
280-
pub guard: Option<Box<Expr<'a>>>
301+
pub guard: Option<Box<Expr<'a>>>,
281302
}

src/main.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
mod parser;
21
mod grammar;
2+
mod parser;
33

4-
use std::path::PathBuf;
54
use failure::Error;
65
use std::fs::File;
76
use std::io::Read;
7+
use std::path::PathBuf;
88

99
#[derive(structopt::StructOpt)]
1010
struct Args {
11-
#[structopt(name = "FILE")]
12-
file: PathBuf,
11+
#[structopt(name = "FILE")]
12+
file: PathBuf,
1313
}
1414

1515
#[paw::main]
1616
fn main(args: Args) -> Result<(), Error> {
17-
println!("Processing file {}...", args.file.as_path().to_str().unwrap());
18-
let mut content = String::new();
19-
File::open(args.file)?.read_to_string(&mut content)?;
20-
let parsed = parser::parse(&content);
21-
println!("Success!");
22-
println!("{:#?}", parsed);
23-
Ok(())
17+
println!(
18+
"Processing file {}...",
19+
args.file.as_path().to_str().unwrap()
20+
);
21+
let mut content = String::new();
22+
File::open(args.file)?.read_to_string(&mut content)?;
23+
let parsed = parser::parse(&content);
24+
println!("Success!");
25+
println!("{:#?}", parsed);
26+
Ok(())
2427
}

src/parser/expr.rs

+38-46
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
use nom::{
2-
named, map, alt, complete, tag, opt, many0, terminated, separated_nonempty_list, value, one_of,
3-
call,
4-
IResult,
2+
alt, call, complete, many0, map, named, one_of, opt, separated_nonempty_list, tag, terminated,
3+
value, IResult,
54
};
65

7-
use super::literal::literal;
8-
use crate::grammar::*;
9-
use super::stmt::stmt;
106
use super::ident::*;
7+
use super::literal::literal;
118
use super::literal::tuple_idx;
129
use super::path::path_in_expr;
1310
use super::pattern::pat;
1411
use super::r#type::type_no_bound;
12+
use super::stmt::stmt;
13+
use crate::grammar::*;
1514

1615
named!(pub t1_expr<&str, Expr>, alt!(
1716
grouped_expr
@@ -52,26 +51,23 @@ pub fn t7_expr(input: &str) -> IResult<&str, Expr> {
5251
}
5352

5453
macro_rules! fall_through_expr {
55-
($i:expr, $first:tt, $tail:tt, $mapper:expr) => ({
56-
map!(
57-
$i,
58-
mrws!(tuple!(
59-
$first, opt!(complete!($tail))
60-
)),
61-
|(f, b)| match b {
62-
None => f,
63-
Some(bc) => $mapper(f, bc),
64-
}
65-
)
66-
})
54+
($i:expr, $first:tt, $tail:tt, $mapper:expr) => {{
55+
map!($i, mrws!(tuple!($first, opt!(complete!($tail)))), |(
56+
f,
57+
b,
58+
)| match b {
59+
None => f,
60+
Some(bc) => $mapper(f, bc),
61+
})
62+
}};
6763
}
6864

6965
macro_rules! bin_ltr_expr {
7066
($cur:ident, $op_parser:ident, $lower:ident) => {
7167
pub fn $cur(input: &str) -> IResult<&str, Expr> {
7268
let (sliced, init) = $lower(input)?;
7369

74-
use nom::{fold_many0};
70+
use nom::fold_many0;
7571

7672
fold_many0!(
7773
sliced,
@@ -85,7 +81,7 @@ macro_rules! bin_ltr_expr {
8581
}
8682
)
8783
}
88-
}
84+
};
8985
}
9086

9187
macro_rules! bin_rtl_expr {
@@ -94,34 +90,35 @@ macro_rules! bin_rtl_expr {
9490
dbg!(input);
9591
let (sliced, init) = $lower(input)?;
9692

97-
use nom::{fold_many0};
93+
use nom::fold_many0;
9894

99-
let (left, terms) = many0!(
100-
sliced,
101-
complete!(mrws!(tuple!($op_parser, $lower)))
102-
)?;
95+
let (left, terms) = many0!(sliced, complete!(mrws!(tuple!($op_parser, $lower))))?;
10396

104-
let ret = match terms.into_iter().rev().fold(None, |acc, (op, term)| {
105-
match acc {
97+
let ret = match terms
98+
.into_iter()
99+
.rev()
100+
.fold(None, |acc, (op, term)| match acc {
106101
None => Some((op, term)),
107-
Some((cop, dec)) => Some((op, Expr::BinaryOp {
108-
op: cop.into(),
109-
lhs: Box::new(term),
110-
rhs: Box::new(dec),
111-
}))
112-
}
113-
}) {
102+
Some((cop, dec)) => Some((
103+
op,
104+
Expr::BinaryOp {
105+
op: cop.into(),
106+
lhs: Box::new(term),
107+
rhs: Box::new(dec),
108+
},
109+
)),
110+
}) {
114111
None => init,
115112
Some((cop, dec)) => Expr::BinaryOp {
116113
op: cop.into(),
117114
lhs: Box::new(init),
118115
rhs: Box::new(dec),
119-
}
116+
},
120117
};
121118

122119
Ok((left, ret))
123120
}
124-
}
121+
};
125122
}
126123

127124
bin_ltr_expr!(arith_t1_expr, arith_op_t1, t7_expr);
@@ -234,15 +231,15 @@ impl<'a> T3Args<'a> {
234231
Self::Field(field) => Expr::Field {
235232
owner: Box::new(base),
236233
field,
237-
}
234+
},
238235
}
239236
}
240237
}
241238

242239
pub fn t3_full(input: &str) -> IResult<&str, Expr> {
243240
let (sliced, init) = t2_expr(input)?;
244241

245-
use nom::{fold_many0};
242+
use nom::fold_many0;
246243

247244
fold_many0!(
248245
sliced,
@@ -274,18 +271,13 @@ impl<'a> T4Args<'a> {
274271
recv: Box::new(base),
275272
method,
276273
params,
277-
}
274+
},
278275
}
279276
}
280277
}
281278

282279
fn t4_full<'a>(input: &'a str) -> IResult<&'a str, Expr<'a>> {
283-
fall_through_expr!(
284-
input,
285-
t3_expr,
286-
t4_tail,
287-
|b, t: T4Args<'a>| t.finalize(b)
288-
)
280+
fall_through_expr!(input, t3_expr, t4_tail, |b, t: T4Args<'a>| t.finalize(b))
289281
}
290282

291283
named!(t4_tail<&str, T4Args>, alt!(
@@ -562,7 +554,7 @@ named!(type_cast_expr<&str, Expr>, fall_through_expr!(
562554
to: t,
563555
}
564556
));
565-
557+
566558
// TODO: split this to confirm assoc
567559
named!(arith_op_t1<&str, ArithOp>,
568560
map!(one_of!("*/%"), |v| ArithOp::from_char(v))

src/parser/ident.rs

+34-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use nom::{
2-
named, take_while, preceded, tag, alt, map,
3-
IResult,
4-
};
1+
use nom::{alt, map, named, preceded, tag, take_while, IResult};
52

63
#[allow(dead_code)]
74
pub mod keywords {
@@ -41,12 +38,36 @@ pub mod keywords {
4138
pub const KW_WHILE: &'static str = "while";
4239

4340
pub const ALL: [&'static str; 30] = [
44-
"as", "break", "pub const", "continue", "else",
45-
"enum", "false", "fn", "for", "if",
46-
"impl", "in", "let", "loop", "match",
47-
"mod", "mut", "ref", "return", "self",
48-
"Self", "static", "super", "trait", "true",
49-
"type", "unsafe", "use", "where", "while",
41+
"as",
42+
"break",
43+
"pub const",
44+
"continue",
45+
"else",
46+
"enum",
47+
"false",
48+
"fn",
49+
"for",
50+
"if",
51+
"impl",
52+
"in",
53+
"let",
54+
"loop",
55+
"match",
56+
"mod",
57+
"mut",
58+
"ref",
59+
"return",
60+
"self",
61+
"Self",
62+
"static",
63+
"super",
64+
"trait",
65+
"true",
66+
"type",
67+
"unsafe",
68+
"use",
69+
"where",
70+
"while",
5071
];
5172
}
5273

@@ -63,15 +84,15 @@ fn ident_or_kw(input: &str) -> IResult<&str, &str> {
6384
if input.starts_with('_') {
6485
let (left, ident) = unchecked_ident(input)?;
6586
if ident.len() < 2 {
66-
return Err(nom::Err::Error((input, nom::error::ErrorKind::Char)))
87+
return Err(nom::Err::Error((input, nom::error::ErrorKind::Char)));
6788
}
6889

6990
return Ok((left, ident));
7091
}
7192

7293
match input.chars().next() {
7394
Some('a'..='z') | Some('A'..='Z') => unchecked_ident(input),
74-
_ => Err(nom::Err::Error((input, nom::error::ErrorKind::Alt)))
95+
_ => Err(nom::Err::Error((input, nom::error::ErrorKind::Alt))),
7596
}
7697
}
7798

@@ -83,7 +104,7 @@ fn non_kw_ident(input: &str) -> IResult<&str, &str> {
83104
let (left, ident) = ident_or_kw(input)?;
84105
for kw in keywords::ALL.iter() {
85106
if ident == *kw {
86-
return Err(nom::Err::Error((input, nom::error::ErrorKind::Alt)))
107+
return Err(nom::Err::Error((input, nom::error::ErrorKind::Alt)));
87108
}
88109
}
89110

0 commit comments

Comments
 (0)