forked from jakobhellermann/bevy_ecs_dynamic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_parser.rs
158 lines (151 loc) · 5.25 KB
/
query_parser.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use bevy_mod_dynamic_query::builder::{NamedDynamicBuilder, NamedOrBuilder};
use winnow::{
ascii::{alpha1, alphanumeric1, multispace0},
combinator::{alt, delimited, opt, preceded, repeat, separated1},
PResult, Parser,
};
#[derive(Clone, Copy, Debug)]
pub enum AndFilterStr<'a> {
With(&'a str),
Without(&'a str),
Changed(&'a str),
Added(&'a str),
}
impl AndFilterStr<'_> {
fn builder(&self, builder: &mut NamedOrBuilder) {
match self {
AndFilterStr::With(name) => builder.with(name),
AndFilterStr::Without(name) => builder.without(name),
AndFilterStr::Changed(name) => builder.changed(name),
AndFilterStr::Added(name) => builder.added(name),
};
}
}
#[derive(Clone, Debug)]
pub struct AndFiltersStr<'a>(pub Vec<AndFilterStr<'a>>);
impl AndFiltersStr<'_> {
pub fn build(&self, builder: &mut NamedDynamicBuilder) {
builder.or(|builder| {
for filter in &self.0 {
filter.builder(builder);
}
builder
});
}
}
#[derive(Clone, Debug)]
pub enum FetchStr<'a> {
Read(&'a str),
Mut(&'a str),
OptionRead(&'a str),
OptionMut(&'a str),
Entity,
}
impl FetchStr<'_> {
pub fn build(&self, builder: &mut NamedDynamicBuilder) {
match self {
FetchStr::Read(name) => builder.component(name),
FetchStr::Mut(name) => builder.component_mut(name),
FetchStr::OptionRead(name) => builder.optional(name),
FetchStr::OptionMut(name) => builder.optional_mut(name),
FetchStr::Entity => builder.entity(),
};
}
}
#[derive(Debug)]
pub struct QueryStr<'a> {
pub fetches: Vec<FetchStr<'a>>,
pub filters: Vec<AndFiltersStr<'a>>,
}
macro_rules! ws {
($parser:expr) => {
delimited(multispace0, $parser, multispace0)
};
}
pub fn query<'i>(input: &mut &'i str) -> PResult<QueryStr<'i>> {
let conjunction = || repeat(1.., ws!(filter)).map(AndFiltersStr);
let or_filters = || separated1(ws!(conjunction()), "|");
let fetches = repeat(1.., ws!(fetch));
let alt_query = (fetches, opt(preceded(",", ws!(or_filters()))))
.map(|(fetches, filters)| QueryStr { fetches, filters: filters.unwrap_or(Vec::new()) });
alt((rust_like::query, alt_query)).parse_next(input)
}
fn fetch<'i>(input: &mut &'i str) -> PResult<FetchStr<'i>> {
alt((
"Entity".map(|_| FetchStr::Entity),
preceded("mut ", component).map(FetchStr::Mut),
preceded("?mut ", component).map(FetchStr::OptionMut),
preceded("?", component).map(FetchStr::OptionRead),
component.map(FetchStr::Read),
))
.parse_next(input)
}
fn filter<'i>(input: &mut &'i str) -> PResult<AndFilterStr<'i>> {
alt((
preceded("!", component).map(AndFilterStr::Without),
preceded("+", component).map(AndFilterStr::Added),
preceded(">", component).map(AndFilterStr::Changed),
component.map(AndFilterStr::With),
))
.parse_next(input)
}
fn component<'i>(input: &mut &'i str) -> PResult<&'i str> {
let repeat = repeat::<_, _, (), _, _>;
(alt((alpha1, "_")), repeat(.., alt((alphanumeric1, "_"))))
.recognize()
.parse_next(input)
}
mod rust_like {
use super::{component, AndFilterStr, AndFiltersStr, FetchStr, QueryStr};
use winnow::{
ascii::multispace0,
combinator::{alt, delimited, opt, preceded, separated1},
PResult, Parser,
};
pub fn query<'i>(input: &mut &'i str) -> PResult<QueryStr<'i>> {
delimited(
"Query<",
(ws!(fetches), opt(preceded(",", ws!(or_filters)))),
">",
)
.map(|(fetches, filters)| QueryStr { fetches, filters: filters.unwrap_or(Vec::new()) })
.parse_next(input)
}
fn fetches<'i>(input: &mut &'i str) -> PResult<Vec<FetchStr<'i>>> {
alt((
delimited("(", separated1(ws!(fetch), ","), ")"),
ws!(fetch).map(|f| vec![f]),
))
.parse_next(input)
}
fn fetch<'i>(input: &mut &'i str) -> PResult<FetchStr<'i>> {
alt((
preceded("&mut ", component).map(FetchStr::Mut),
preceded("&", component).map(FetchStr::Read),
delimited("Option<&mut ", component, ">").map(FetchStr::OptionMut),
delimited("Option<&", component, ">").map(FetchStr::OptionRead),
"Entity".map(|_| FetchStr::Entity),
))
.parse_next(input)
}
fn or_filters<'i>(input: &mut &'i str) -> PResult<Vec<AndFiltersStr<'i>>> {
delimited("Or<(", separated1(ws!(conjunction), ","), ")>").parse_next(input)
}
fn conjunction<'i>(input: &mut &'i str) -> PResult<AndFiltersStr<'i>> {
alt((
delimited("(", separated1(ws!(filter), ","), ")"),
filter.map(|f| vec![f]),
))
.map(AndFiltersStr)
.parse_next(input)
}
fn filter<'i>(input: &mut &'i str) -> PResult<AndFilterStr<'i>> {
alt((
delimited("With<", component, ">").map(AndFilterStr::With),
delimited("Without<", component, ">").map(AndFilterStr::Without),
delimited("Added<", component, ">").map(AndFilterStr::Added),
delimited("Changed<", component, ">").map(AndFilterStr::Changed),
))
.parse_next(input)
}
}