mech_syntax/
expressions.rs

1#[macro_use]
2use crate::*;
3use crate::structures::tuple;
4
5#[cfg(not(feature = "no-std"))] use core::fmt;
6#[cfg(feature = "no-std")] use alloc::fmt;
7#[cfg(feature = "no-std")] use alloc::string::String;
8#[cfg(feature = "no-std")] use alloc::vec::Vec;
9use nom::{
10  IResult,
11  branch::alt,
12  sequence::{tuple as nom_tuple, preceded, pair},
13  combinator::{opt, eof, cut},
14  multi::{many1, many_till, many0, separated_list1,separated_list0},
15  Err,
16  Err::Failure
17};
18
19use std::collections::HashMap;
20use colored::*;
21
22use crate::*;
23
24// Expressions
25// ============================================================================
26
27/*
28Defines how expressions are parsed using a recursive structure hat reflects 
29operator precedence. Parsing begins at the top-level (`formula`) and proceeds 
30through increasingly tightly-binding operations, down to the basic elements 
31like literals and variables.
32
33- `formula`: entry point
34- `l1`: addition and subtraction (`+`, `-`)
35- `l2`: multiplication, division, matrix operations
36- `l3`: exponentiation (`^`)
37- `l4`: logical operators (e.g., `and`, `or`)
38- `l5`: comparisons (e.g., `==`, `<`, `>`)
39- `l6`: table operations (e.g., joins)
40- `l7`: set operations (e.g., union, intersection)
41- `factor`: atomic units (literals, function calls, variables, etc.)
42*/
43
44// expression := range-expression | formula ;
45pub fn expression(input: ParseString) -> ParseResult<Expression> {
46  let (input, expr) = match range_expression(input.clone()) {
47    Ok((input, rng)) => (input, Expression::Range(Box::new(rng))),
48    Err(_) => match formula(input.clone()) {
49      Ok((input, Factor::Expression(expr))) => (input, *expr),
50      Ok((input, fctr)) => (input, Expression::Formula(fctr)),
51      Err(err) => {
52        return Err(err);},
53    } 
54  };
55  Ok((input, expr))
56}
57
58// formula := l1 ;
59pub fn formula(input: ParseString) -> ParseResult<Factor> {
60  let (input, factor) = l1(input)?;
61  Ok((input, factor))
62}
63
64// l1 := l2, (add-sub-operator, l2)* ;
65pub fn l1(input: ParseString) -> ParseResult<Factor> {
66  let (input, lhs) = l2(input)?;
67  let (input, rhs) = many0(pair(add_sub_operator,cut(l2)))(input)?;
68  let factor = if rhs.is_empty() { lhs } else { Factor::Term(Box::new(Term { lhs, rhs })) };
69  Ok((input, factor))
70}
71
72// l2 := l3, (mul-div-operator | matrix-operator, l3)* ;
73pub fn l2(input: ParseString) -> ParseResult<Factor> {
74  let (input, lhs) = l3(input)?;
75  let (input, rhs) = many0(pair(alt((mul_div_operator, matrix_operator)),cut(l3)))(input)?;
76  let factor = if rhs.is_empty() { lhs } else { Factor::Term(Box::new(Term { lhs, rhs })) };
77  Ok((input, factor))
78}
79
80// l3 := l4, (exponent-operator, l4)* ;
81pub fn l3(input: ParseString) -> ParseResult<Factor> {
82  let (input, lhs) = l4(input)?;
83  let (input, rhs) = many0(pair(exponent_operator,cut(l4)))(input)?;
84  let factor = if rhs.is_empty() { lhs } else { Factor::Term(Box::new(Term { lhs, rhs })) };
85  Ok((input, factor))
86}
87
88// l4 := l5, (logic-operator, l5)* ;
89pub fn l4(input: ParseString) -> ParseResult<Factor> {
90  let (input, lhs) = l5(input)?;
91  let (input, rhs) = many0(pair(logic_operator,cut(l5)))(input)?;
92  let factor = if rhs.is_empty() { lhs } else { Factor::Term(Box::new(Term { lhs, rhs })) };
93  Ok((input, factor))
94}
95
96// l5 := factor, (comparison-operator, factor)* ;
97pub fn l5(input: ParseString) -> ParseResult<Factor> {
98  let (input, lhs) = l6(input)?;
99  let (input, rhs) = many0(pair(comparison_operator,cut(l6)))(input)?;
100  let factor = if rhs.is_empty() { lhs } else { Factor::Term(Box::new(Term { lhs, rhs })) };
101  Ok((input, factor))
102}
103
104// l6 := factor, (table-operator, factor)* ;
105pub fn l6(input: ParseString) -> ParseResult<Factor> {
106  let (input, lhs) = l7(input)?;
107  let (input, rhs) = many0(pair(table_operator,cut(l7)))(input)?;
108  let factor = if rhs.is_empty() { lhs } else { Factor::Term(Box::new(Term { lhs, rhs })) };
109  Ok((input, factor))
110}
111
112// l7 := factor, (set-operator, factor)* ;
113pub fn l7(input: ParseString) -> ParseResult<Factor> {
114  let (input, lhs) = factor(input)?;
115  let (input, rhs) = many0(pair(set_operator,cut(factor)))(input)?;
116  let factor = if rhs.is_empty() { lhs } else { Factor::Term(Box::new(Term { lhs, rhs })) };
117  Ok((input, factor))
118}
119
120// factor := parenthetical-term | negate-factor | not-factor | structure | function-call | literal | slice | var ;
121pub fn factor(input: ParseString) -> ParseResult<Factor> {
122  let parsers: Vec<(&str, Box<dyn Fn(ParseString) -> ParseResult<Factor>>)> = vec![
123    ("parenthetical_term", Box::new(|i| parenthetical_term(i))),
124    ("negate_factor", Box::new(|i| negate_factor(i))),
125    ("not_factor", Box::new(|i| not_factor(i))),
126    ("structure", Box::new(|i| structure(i).map(|(i, s)| (i, Factor::Expression(Box::new(Expression::Structure(s))))))),
127    ("function_call", Box::new(|i| function_call(i).map(|(i, f)| (i, Factor::Expression(Box::new(Expression::FunctionCall(f))))))),
128    ("literal", Box::new(|i| literal(i).map(|(i, l)| (i, Factor::Expression(Box::new(Expression::Literal(l))))))),
129    ("slice", Box::new(|i| slice(i).map(|(i, s)| (i, Factor::Expression(Box::new(Expression::Slice(s))))))),
130    ("var", Box::new(|i| var(i).map(|(i, v)| (i, Factor::Expression(Box::new(Expression::Var(v))))))),
131  ];
132  let (input, fctr) = alt_best(input, &parsers)?;
133  let (input, transpose) = opt(transpose)(input)?;
134  let fctr = match transpose {
135    Some(_) => Factor::Transpose(Box::new(fctr)),
136    None => fctr,
137  };
138  Ok((input, fctr))
139}
140
141// parenthetical-term := left-parenthesis, space-tab0, formula, space-tab0, right-parenthesis ;
142pub fn parenthetical_term(input: ParseString) -> ParseResult<Factor> {
143  let msg1 = "parenthetical_term: Expects expression";
144  let msg2 = "parenthetical_term: Expects right parenthesis `)`";
145  let (input, (_, r)) = range(left_parenthesis)(input)?;
146  let (input, _) = space_tab0(input)?;
147  let (input, frmla) = label!(formula, msg1)(input)?;
148  let (input, _) = space_tab0(input)?;
149  let (input, _) = label!(right_parenthesis, msg2, r)(input)?;
150  Ok((input, Factor::Parenthetical(Box::new(frmla))))
151}
152
153// var := identifier, ?kind-annotation ;
154pub fn var(input: ParseString) -> ParseResult<Var> {
155  let ((input, name)) = identifier(input)?;
156  let ((input, kind)) = opt(kind_annotation)(input)?;
157  Ok((input, Var{ name, kind }))
158}
159
160// statement-separator := ";" ;
161pub fn statement_separator(input: ParseString) -> ParseResult<()> {
162  let (input,_) = nom_tuple((whitespace0,semicolon,whitespace0))(input)?;
163  Ok((input, ()))
164}
165
166// Math Expressions
167// ----------------------------------------------------------------------------
168
169// add-sub-operator := add | subtract ;
170pub fn add_sub_operator(input: ParseString) -> ParseResult<FormulaOperator> {
171  let (input, op) = alt((add, subtract))(input)?;
172  Ok((input, FormulaOperator::AddSub(op)))
173}
174
175
176// mul-div-operator := multiply | divide | modulus ;
177pub fn mul_div_operator(input: ParseString) -> ParseResult<FormulaOperator> {
178  let (input, op) = alt((multiply, divide, modulus))(input)?;
179  Ok((input, FormulaOperator::MulDiv(op)))
180}
181
182// exponent-operator := exponent ;
183pub fn exponent_operator(input: ParseString) -> ParseResult<FormulaOperator> {
184  let (input, op) = exponent(input)?;
185  Ok((input, FormulaOperator::Exponent(op)))
186}
187
188// negate-factor := "-", factor ;
189pub fn negate_factor(input: ParseString) -> ParseResult<Factor> {
190  let (input, _) = dash(input)?;
191  let (input, expr) = factor(input)?;
192  Ok((input, Factor::Negate(Box::new(expr))))
193}
194
195// not-factor := not, factor ;
196pub fn not_factor(input: ParseString) -> ParseResult<Factor> {
197  let (input, _) = not(input)?;
198  let (input, expr) = factor(input)?;
199  Ok((input, Factor::Not(Box::new(expr))))
200}
201
202// add := "+" ;
203pub fn add(input: ParseString) -> ParseResult<AddSubOp> {
204  let (input, _) = ws0e(input)?;
205  let (input, _) = tag("+")(input)?;
206  let (input, _) = ws0e(input)?;
207  Ok((input, AddSubOp::Add))
208}
209
210pub fn subtract(input: ParseString) -> ParseResult<AddSubOp> {
211  let (input, _) = alt((spaced_subtract, raw_subtract))(input)?;
212  Ok((input, AddSubOp::Sub))
213}
214
215// subtract := "-" ;
216pub fn raw_subtract(input: ParseString) -> ParseResult<AddSubOp> {
217  let (input, _) = pair(is_not(comment_sigil), tag("-"))(input)?;
218  Ok((input, AddSubOp::Sub))
219}
220
221pub fn spaced_subtract(input: ParseString) -> ParseResult<AddSubOp> {
222  let (input, _) = ws1e(input)?;
223  let (input, _) = raw_subtract(input)?;
224  let (input, _) = ws1e(input)?;
225  Ok((input, AddSubOp::Sub))
226}
227
228// multiply := "*" | "×" ;
229pub fn multiply(input: ParseString) -> ParseResult<MulDivOp> {
230  let (input, _) = ws0e(input)?;
231  let (input, _) = pair(is_not(matrix_multiply),alt((tag("*"), tag("×"))))(input)?;
232  let (input, _) = ws0e(input)?;
233  Ok((input, MulDivOp::Mul))
234}
235
236// divide := "/" | "÷" ;
237pub fn divide(input: ParseString) -> ParseResult<MulDivOp> {
238  let (input, _) = ws0e(input)?;
239  let (input, _) = pair(is_not(comment_sigil),alt((tag("/"),tag("÷"))))(input)?;
240  let (input, _) = ws0e(input)?;
241  Ok((input, MulDivOp::Div))
242}
243
244// modulus := "%" ;
245pub fn modulus(input: ParseString) -> ParseResult<MulDivOp> {
246  let (input, _) = ws0e(input)?;
247  let (input, _) = tag("%")(input)?;
248  let (input, _) = ws0e(input)?;
249  Ok((input, MulDivOp::Mod))
250}
251
252// exponent := "^" ;
253pub fn exponent(input: ParseString) -> ParseResult<ExponentOp> {
254  let (input, _) = ws0e(input)?;
255  let (input, _) = tag("^")(input)?;
256  let (input, _) = ws0e(input)?;
257  Ok((input, ExponentOp::Exp))
258}
259
260// Matrix Operations
261// ----------------------------------------------------------------------------
262
263// matrix-operator := matrix-multiply | multiply | divide | matrix-solve ;
264pub fn matrix_operator(input: ParseString) -> ParseResult<FormulaOperator> {
265  let (input, op) = alt((matrix_multiply, matrix_solve, dot_product, cross_product))(input)?;
266  Ok((input, FormulaOperator::Vec(op)))
267}
268
269// matrix-multiply := "**" ;
270pub fn matrix_multiply(input: ParseString) -> ParseResult<VecOp> {
271  let (input, _) = ws0e(input)?;
272  let (input, _) = tag("**")(input)?;
273  let (input, _) = ws0e(input)?;
274  Ok((input, VecOp::MatMul))
275}
276
277// matrix-solve := "\" ;
278pub fn matrix_solve(input: ParseString) -> ParseResult<VecOp> {
279  let (input, _) = ws0e(input)?;
280  let (input, _) = tag("\\")(input)?;
281  let (input, _) = ws0e(input)?;
282  Ok((input, VecOp::Solve))
283}
284
285// dot-product := "·" | "•" ;
286pub fn dot_product(input: ParseString) -> ParseResult<VecOp> {
287  let (input, _) = ws0e(input)?;
288  let (input, _) = alt((tag("·"),tag("•")))(input)?;
289  let (input, _) = ws0e(input)?;
290  Ok((input, VecOp::Dot))
291}
292
293// cross-product := "⨯" ;
294pub fn cross_product(input: ParseString) -> ParseResult<VecOp> {
295  let (input, _) = ws0e(input)?;
296  let (input, _) = tag("⨯")(input)?;
297  let (input, _) = ws0e(input)?;
298  Ok((input, VecOp::Cross))
299}
300
301// transpose := "'" ;
302pub fn transpose(input: ParseString) -> ParseResult<()> {
303  let (input, _) = tag("'")(input)?;
304  Ok((input, ()))
305}
306
307// Range Expressions
308// ----------------------------------------------------------------------------
309
310// range := formula, range-operator, formula, (range-operator, formula)? ;
311pub fn range_expression(input: ParseString) -> ParseResult<RangeExpression> {
312  let (input, start) = formula(input)?;
313  let (input, op) = range_operator(input)?;
314  let (input, x) = formula(input)?;
315  let (input, y) = opt(nom_tuple((range_operator,formula)))(input)?;
316  let range = match y {
317    Some((op2,terminal)) => RangeExpression{start, increment: Some((op,x)), operator: op2, terminal},
318    None => RangeExpression{start, increment: None, operator: op, terminal: x},
319  };
320  Ok((input, range))
321}
322
323// range-inclusive := "..=" ;
324pub fn range_inclusive(input: ParseString) -> ParseResult<RangeOp> {
325  let (input, _) = tag("..=")(input)?;
326  Ok((input, RangeOp::Inclusive))
327}
328
329// range-exclusive := ".." ;
330pub fn range_exclusive(input: ParseString) -> ParseResult<RangeOp> {
331  let (input, _) = tag("..")(input)?;
332  Ok((input, RangeOp::Exclusive))
333}
334
335// range-operator := range-inclusive | range-exclusive ;
336pub fn range_operator(input: ParseString) -> ParseResult<RangeOp> {
337  let (input, op) = alt((range_inclusive,range_exclusive))(input)?;
338  Ok((input, op))
339}
340
341// Comparison expressions
342// ----------------------------------------------------------------------------
343
344// comparison-operator := strict-equal | strict-not-equal | not-equal | equal-to | greater-than-equal | greater-than | less-than-equal | less-than ;
345pub fn comparison_operator(input: ParseString) -> ParseResult<FormulaOperator> {
346  let (input, op) = alt((strict_equal, strict_not_equal, not_equal, equal_to, greater_than_equal, greater_than, less_than_equal, less_than))(input)?;
347  Ok((input, FormulaOperator::Comparison(op)))
348}
349
350// not-equal := "!=" | "¬=" | "≠" ;
351pub fn not_equal(input: ParseString) -> ParseResult<ComparisonOp> {
352  let (input, _) = ws0e(input)?;
353  let (input, _) = alt((tag("!="),tag("¬="),tag("≠")))(input)?;
354  let (input, _) = ws0e(input)?;
355  Ok((input, ComparisonOp::NotEqual))
356}
357
358// equal-to := "==" ;
359pub fn equal_to(input: ParseString) -> ParseResult<ComparisonOp> {
360  let (input, _) = ws0e(input)?;
361  let (input, _) = tag("==")(input)?;
362  let (input, _) = ws0e(input)?;
363  Ok((input, ComparisonOp::Equal))
364}
365
366// strict-not-equal := "=!=" | "=¬=" ;
367pub fn strict_not_equal(input: ParseString) -> ParseResult<ComparisonOp> {
368  let (input, _) = ws0e(input)?;
369  let (input, _) = alt((tag("=!="),tag("=¬=")))(input)?;
370  let (input, _) = ws0e(input)?;
371  Ok((input, ComparisonOp::StrictNotEqual))
372}
373
374// strict-equal := "=:=" | "≡" ;
375pub fn strict_equal(input: ParseString) -> ParseResult<ComparisonOp> {
376  let (input, _) = ws0e(input)?;
377  let (input, _) = alt((tag("=:="),tag("≡")))(input)?;
378  let (input, _) = ws0e(input)?;
379  Ok((input, ComparisonOp::StrictEqual))
380}
381
382// greater-than := ">" ;
383pub fn greater_than(input: ParseString) -> ParseResult<ComparisonOp> {
384  let (input, _) = ws0e(input)?;
385  let (input, _) = tag(">")(input)?;
386  let (input, _) = ws0e(input)?;
387  Ok((input, ComparisonOp::GreaterThan))
388}
389
390// less_than := "<" ;
391pub fn less_than(input: ParseString) -> ParseResult<ComparisonOp> {
392  let (input, _) = ws0e(input)?;
393  let (input, _) = tag("<")(input)?;
394  let (input, _) = ws0e(input)?;
395  Ok((input, ComparisonOp::LessThan))
396}
397
398// greater-than-equal := ">=" | "≥" ;
399pub fn greater_than_equal(input: ParseString) -> ParseResult<ComparisonOp> {
400  let (input, _) = ws0e(input)?;
401  let (input, _) = alt((tag(">="),tag("≥")))(input)?;
402  let (input, _) = ws0e(input)?;
403  Ok((input, ComparisonOp::GreaterThanEqual))
404}
405
406// less-than-equal := "<=" | "≤" ;
407pub fn less_than_equal(input: ParseString) -> ParseResult<ComparisonOp> {
408  let (input, _) = ws0e(input)?;
409  let (input, _) = alt((tag("<="),tag("≤")))(input)?;
410  let (input, _) = ws0e(input)?;
411  Ok((input, ComparisonOp::LessThanEqual))
412}
413
414// Logic expressions
415// ----------------------------------------------------------------------------
416
417// logic-operator := and | or | xor ;
418pub fn logic_operator(input: ParseString) -> ParseResult<FormulaOperator> {
419  let (input, op) = alt((and, or, xor))(input)?;
420  Ok((input, FormulaOperator::Logic(op)))
421}
422
423// or := "|" ;
424pub fn or(input: ParseString) -> ParseResult<LogicOp> {
425  let (input, _) = ws0e(input)?;
426  let (input, _) = alt((tag("||"), tag("∨"), tag("⋁")))(input)?;
427  let (input, _) = ws0e(input)?;
428  Ok((input, LogicOp::Or))
429}
430
431// and := "&" ;
432pub fn and(input: ParseString) -> ParseResult<LogicOp> {
433  let (input, _) = ws0e(input)?;
434  let (input, _) = alt((tag("&&"), tag("∧"), tag("⋀")))(input)?;
435  let (input, _) = ws0e(input)?;
436  Ok((input, LogicOp::And))
437}
438
439// not := "!" | "¬" ;
440pub fn not(input: ParseString) -> ParseResult<LogicOp> {
441  let (input, _) = alt((tag("!"), tag("¬")))(input)?;
442  Ok((input, LogicOp::Not))
443}
444
445// xor := "xor" | "⊕" | "⊻" ;
446pub fn xor(input: ParseString) -> ParseResult<LogicOp> {
447  let (input, _) = ws0e(input)?;
448  let (input, _) = alt((tag("^^"), tag("⊕"), tag("⊻")))(input)?;
449  let (input, _) = ws0e(input)?;
450  Ok((input, LogicOp::Xor))
451}
452
453// Table Operations
454// ----------------------------------------------------------------------------
455
456// table-operator := join | left-join | right-join | full-join | left-semi-join | left-anti-join ;
457fn table_operator(input: ParseString) -> ParseResult<FormulaOperator> {
458  let (input, op) = alt((join,left_join,right_join,full_join,left_semi_join,left_anti_join))(input)?;
459  Ok((input, FormulaOperator::Table(op)))
460}
461
462// join := "⋈" ;
463fn join(input: ParseString) -> ParseResult<TableOp> {
464  let (input, _) = ws0e(input)?;
465  let (input, _) = tag("⋈")(input)?;
466  let (input, _) = ws0e(input)?;
467  Ok((input, TableOp::InnerJoin))
468}
469
470// left-join := "⟕" ;
471fn left_join(input: ParseString) -> ParseResult<TableOp> {
472  let (input, _) = ws0e(input)?;
473  let (input, _) = tag("⟕")(input)?;
474  let (input, _) = ws0e(input)?;
475  Ok((input, TableOp::LeftOuterJoin))
476}
477
478// right-join := "⟖" ;
479fn right_join(input: ParseString) -> ParseResult<TableOp> {
480  let (input, _) = ws0e(input)?;
481  let (input, _) = tag("⟖")(input)?;
482  let (input, _) = ws0e(input)?;
483  Ok((input, TableOp::RightOuterJoin))
484}
485
486// full-join := "⟗" ;
487fn full_join(input: ParseString) -> ParseResult<TableOp> {
488  let (input, _) = ws0e(input)?;
489  let (input, _) = tag("⟗")(input)?;
490  let (input, _) = ws0e(input)?;
491  Ok((input, TableOp::FullOuterJoin))
492}
493
494// left-semi-join := "⋉" ;
495fn left_semi_join(input: ParseString) -> ParseResult<TableOp> {
496  let (input, _) = ws0e(input)?;
497  let (input, _) = tag("⋉")(input)?;
498  let (input, _) = ws0e(input)?;
499  Ok((input, TableOp::LeftSemiJoin))
500}
501
502// left-anti-join := "▷" ;
503fn left_anti_join(input: ParseString) -> ParseResult<TableOp> {
504  let (input, _) = ws0e(input)?;
505  let (input, _) = tag("▷")(input)?;
506  let (input, _) = ws0e(input)?;
507  Ok((input, TableOp::LeftAntiJoin))
508}
509
510
511// Set Operations
512// ----------------------------------------------------------------------------
513
514// set-operator := union | intersection | difference | complement | subset | superset | proper-subset | proper-superset | element-of | not-element-of ;
515pub fn set_operator(input: ParseString) -> ParseResult<FormulaOperator> {
516  let (input, op) = alt((union_op,intersection,difference,complement,subset,superset,proper_subset,proper_superset,element_of,not_element_of))(input)?;
517  Ok((input, FormulaOperator::Set(op)))
518}
519
520// union := "∪" ;
521pub fn union_op(input: ParseString) -> ParseResult<SetOp> {
522  let (input, _) = ws0e(input)?;
523  let (input, _) = tag("∪")(input)?;
524  let (input, _) = ws0e(input)?;
525  Ok((input, SetOp::Union))
526}
527
528// intersection := "∩" ;
529pub fn intersection(input: ParseString) -> ParseResult<SetOp> {
530  let (input, _) = ws0e(input)?;
531  let (input, _) = tag("∩")(input)?;
532  let (input, _) = ws0e(input)?;
533  Ok((input, SetOp::Intersection))
534}
535
536// difference := "∖" ;
537pub fn difference(input: ParseString) -> ParseResult<SetOp> {
538  let (input, _) = ws0e(input)?;
539  let (input, _) = tag("∖")(input)?;
540  let (input, _) = ws0e(input)?;
541  Ok((input, SetOp::Difference))
542}
543
544// complement := "∁" ;
545pub fn complement(input: ParseString) -> ParseResult<SetOp> {
546  let (input, _) = ws0e(input)?;
547  let (input, _) = tag("∁")(input)?;
548  let (input, _) = ws0e(input)?;
549  Ok((input, SetOp::Complement))
550}
551
552// subset := "⊆" ;
553pub fn subset(input: ParseString) -> ParseResult<SetOp> { 
554  let (input, _) = ws0e(input)?;
555  let (input, _) = tag("⊆")(input)?;
556  let (input, _) = ws0e(input)?;
557  Ok((input, SetOp::Subset))
558}
559
560// superset := "⊇" ;
561pub fn superset(input: ParseString) -> ParseResult<SetOp> {
562  let (input, _) = ws0e(input)?;
563  let (input, _) = tag("⊇")(input)?;
564  let (input, _) = ws0e(input)?;
565  Ok((input, SetOp::Superset))
566}
567
568// proper-subset := "⊊" ;
569pub fn proper_subset(input: ParseString) -> ParseResult<SetOp> {
570  let (input, _) = ws0e(input)?;
571  let (input, _) = alt((tag("⊊"), tag("⊂")))(input)?;
572  let (input, _) = ws0e(input)?;
573  Ok((input, SetOp::ProperSubset))
574}
575
576// proper-superset := "⊋" ;
577pub fn proper_superset(input: ParseString) -> ParseResult<SetOp> {
578  let (input, _) = ws0e(input)?;
579  let (input, _) = alt((tag("⊋"), tag("⊃")))(input)?;
580  let (input, _) = ws0e(input)?;
581  Ok((input, SetOp::ProperSuperset))
582}
583
584// element-of := "∈" ;
585pub fn element_of(input: ParseString) -> ParseResult<SetOp> { 
586  let (input, _) = ws0e(input)?;
587  let (input, _) = tag("∈")(input)?;
588  let (input, _) = ws0e(input)?;
589  Ok((input, SetOp::ElementOf))
590}
591
592// not-element-of := "∉" ;
593pub fn not_element_of(input: ParseString) -> ParseResult<SetOp> {
594  let (input, _) = ws0e(input)?;
595  let (input, _) = tag("∉")(input)?;
596  let (input, _) = ws0e(input)?;
597  Ok((input, SetOp::NotElementOf))
598}
599
600// Subscript Operations
601// ----------------------------------------------------------------------------
602
603// subscript := (swizzle-subscript | dot-subscript-int | dot-subscript | bracket-subscript | brace-subscript)+ ; 
604pub fn subscript(input: ParseString) -> ParseResult<Vec<Subscript>> {
605  let (input, subscripts) = many1(alt((swizzle_subscript,dot_subscript,dot_subscript_int,bracket_subscript,brace_subscript)))(input)?;
606  Ok((input, subscripts))
607}
608
609// slice := identifier, subscript ;
610pub fn slice(input: ParseString) -> ParseResult<Slice> {
611  let (input, name) = identifier(input)?;
612  let (input, ixes) = subscript(input)?;
613  Ok((input, Slice{name, subscript: ixes}))
614}
615
616// slice-ref := identifier, subscript? ;
617pub fn slice_ref(input: ParseString) -> ParseResult<SliceRef> {
618  let (input, name) = identifier(input)?;
619  let (input, ixes) = opt(subscript)(input)?;
620  Ok((input, SliceRef{name, subscript: ixes}))
621}
622
623// swizzle-subscript := ".", identifier, ",", list1(",", identifier) ;
624pub fn swizzle_subscript(input: ParseString) -> ParseResult<Subscript> {
625  let (input, _) = period(input)?;
626  let (input, first) = identifier(input)?;
627  let (input, _) = comma(input)?;
628  let (input, mut name) = separated_list1(tag(","),identifier)(input)?;
629  let mut subscripts = vec![first];
630  subscripts.append(&mut name);
631  Ok((input, Subscript::Swizzle(subscripts)))
632}
633
634// dot-subscript := ".", identifier ;
635pub fn dot_subscript(input: ParseString) -> ParseResult<Subscript> {
636  let (input, _) = period(input)?;
637  let (input, name) = identifier(input)?;
638  Ok((input, Subscript::Dot(name)))
639}
640
641// dot-subscript-int := ".", integer-literal ;
642pub fn dot_subscript_int(input: ParseString) -> ParseResult<Subscript> {
643  let (input, _) = period(input)?;
644  let (input, name) = integer_literal(input)?;
645  Ok((input, Subscript::DotInt(name)))
646}
647
648// bracket-subscript := "[", list1(",", select-all | range-subscript | formula-subscript), "]" ;
649pub fn bracket_subscript(input: ParseString) -> ParseResult<Subscript> {
650  let (input, _) = left_bracket(input)?;
651  let (input, subscripts) = separated_list1(list_separator,alt((select_all,range_subscript,formula_subscript)))(input)?;
652  let (input, _) = right_bracket(input)?;
653  Ok((input, Subscript::Bracket(subscripts)))
654}
655
656// brace-subscript := "{", list1(",", select-all | range-subscript | formula-subscript), "}" ;
657pub fn brace_subscript(input: ParseString) -> ParseResult<Subscript> {
658  let (input, _) = left_brace(input)?;
659  let (input, subscripts) = separated_list1(list_separator,alt((select_all,range_subscript,formula_subscript)))(input)?;
660  let (input, _) = right_brace(input)?;
661  Ok((input, Subscript::Brace(subscripts)))
662}
663
664// select-all := ":" ;
665pub fn select_all(input: ParseString) -> ParseResult<Subscript> {
666  let (input, lhs) = colon(input)?;
667  Ok((input, Subscript::All))
668}
669
670// formula-subscript := formula ;
671pub fn formula_subscript(input: ParseString) -> ParseResult<Subscript> {
672  let (input, factor) = formula(input)?;
673  Ok((input, Subscript::Formula(factor)))
674}
675
676// range-subscript := range-expression ;
677pub fn range_subscript(input: ParseString) -> ParseResult<Subscript> {
678  let (input, rng) = range_expression(input)?;
679  Ok((input, Subscript::Range(rng)))
680}