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