1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
extern crate nom;
use std::env;
pub use nom::{
IResult,
multi::{many0, many1},
branch::alt,
sequence::{delimited, preceded},
character::complete::{one_of, none_of, anychar, char}
};
use Node::{Tree, Literal};
type Forest = Vec<Node>;
#[derive(Debug)]
enum Node {
Tree(Forest),
Literal(String),
}
fn parse_literal(input: &str) -> IResult<&str, Node> {
alt((many1(one_of(" \n")),
many1(alt((preceded(char('\\'), one_of(" \n()\\")),
none_of(" \n()\\"))))))
(input)
.map(|(next_input, v)|
(next_input, Literal(v.into_iter().collect())))
}
fn parse_tree(input: &str) -> IResult<&str, Node> {
delimited(char('('), parse_forest, char(')'))(input)
.map(|(ni, f)| (ni, Tree(f)))
}
fn parse_elem(input: &str) -> IResult<&str, Node> {
alt((parse_tree, parse_literal))(input)
}
fn parse_forest(input: &str) -> IResult<&str, Forest> {
many0(parse_elem)(input)
}
fn main() {
match env::args().nth(1) {
None => println!("Argumetns: <input>"),
Some(input) => println!("Result: {:?}", parse_forest(input.as_ref()))
}
}
|