-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.rs
132 lines (118 loc) · 3.26 KB
/
grammar.rs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use crate::ast::*;
use combine::{parser as parse_fn, Parser};
use combine::parser::choice::{choice, optional};
use combine::parser::repeat::{many, many1, sep_by};
use combine_proc_macro::parser;
use combine_proc_macro::parser::{delim, ident, keyword, literal, punct};
use either::Either;
parser!(fn grammar() -> Grammar {
( keyword("grammar")
, ident()
, punct(';')
, many1::<Vec<_>, _>(rule())
).map(|(_, name, _, rules)| Grammar { name, rules })
});
parser!(fn rule() -> Rule {
( optional((punct('#'), delim('['), many(attribute()), delim(']')).map(|(_, _, list, _)| list))
, optional(keyword("fragment"))
, ident()
, punct(':')
, pattern()
, punct(';')
).map(|(attrs, _, name, _, pattern, _)| {
Rule {
name,
pattern,
attributes: attrs.unwrap_or_default()
}
})
});
parser!(fn attribute() -> Attribute {
parse_fn(attribute_recursive)
});
parser!(fn attribute_recursive(input: &mut Input) -> Attribute {
( ident()
, optional((delim('('), many1(attribute()), delim(')')))
)
.map(|(word, list)| match list {
Some((_, attrs, _)) => Attribute::Group(word, attrs),
None => Attribute::Word(word)
})
.parse_stream(input)
});
parser!(fn pattern() -> Pattern {
parse_fn(pattern_recursive)
});
parser!(fn pattern_recursive(input: &mut Input) -> Pattern {
sep_by(series(), punct('|'))
.map(Pattern::Choice)
.map(Pattern::flatten_once)
.parse_stream(input)
});
parser!(fn series() -> Pattern {
many1(atom())
.map(Pattern::Series)
.map(Pattern::flatten_once)
});
parser!(fn atom() -> Pattern {
parse_fn(atom_recursive)
});
parser!(fn atom_recursive(input: &mut Input) -> Pattern {
choice((
ident().map(Pattern::Ident),
(
literal(),
// TODO: Add a `Pattern::Range`
optional((
// TODO: Implement joined punct parser
punct('.'),
punct('.'),
literal(),
)),
).map(|(lit, _)| Pattern::Literal(lit)),
group().map(Pattern::flatten_once),
))
.parse_stream(input)
});
parser!(fn group() -> Pattern {
( delim('(')
, pattern()
, delim(')')
, optional(repeat_or_predicate())
).map(|(_, pattern, _, special)| {
match special {
Some(Either::Left(repeat)) => {
Pattern::Repeat {
pattern: Box::new(pattern),
repeat,
}
}
Some(Either::Right(predicate_then)) => {
Pattern::Predicate {
pred: Box::new(pattern),
expr: Box::new(predicate_then),
}
}
None => pattern,
}
})
});
parser!(fn repeat_or_predicate() -> Either<Repeat, Pattern> {
choice((
repeat().map(Either::Left),
predicate().map(Either::Right),
))
});
parser!(fn repeat() -> Repeat {
choice((
punct('?').map(|_| Repeat::ZeroOrOne),
punct('*').map(|_| Repeat::ZeroOrMore),
punct('+').map(|_| Repeat::OneOrMore),
))
});
parser!(fn predicate() -> Pattern {
( punct('=')
, punct('>')
, series()
).map(|(_, _, pattern)| pattern)
});