1#[macro_use]
2use crate::*;
3use crate::base::*;
4use crate::label;
5use crate::labelr;
6use nom::{
7 multi::separated_list0,
8 sequence::tuple as nom_tuple,
9};
10use crate::nodes::Matrix;
11
12pub fn max_err<'a>(x: Option<ParseError<'a>>, y: ParseError<'a>) -> ParseError<'a> {
15 match (x,&y) {
16 (None, y) => y.clone(),
17 _ => y.clone(),
18 }
19}
20
21pub fn structure(input: ParseString) -> ParseResult<Structure> {
23 match empty_set(input.clone()) {
24 Ok((input, set)) => {return Ok((input, Structure::Set(set)));},
25 _ => (),
26 }
27 match empty_map(input.clone()) {
28 Ok((input, map)) => {return Ok((input, Structure::Map(map)));},
29 _ => (),
30 }
31 match table(input.clone()) {
32 Ok((input, tbl)) => {return Ok((input, Structure::Table(tbl)));},
33 _ => (),
35 }
36 match matrix(input.clone()) {
37 Ok((input, mtrx)) => {return Ok((input, Structure::Matrix(mtrx)));},
38 _ => (),
40 }
41 match tuple(input.clone()) {
42 Ok((input, tpl)) => {return Ok((input, Structure::Tuple(tpl)));},
43 _ => (),
44 }
45 match tuple_struct(input.clone()) {
46 Ok((input, tpl)) => {return Ok((input, Structure::TupleStruct(tpl)));},
47 _ => (),
48 }
49 match record(input.clone()) {
50 Ok((input, table)) => {return Ok((input, Structure::Record(table)));},
51 _ => (),
52 }
53 match map(input.clone()) {
54 Ok((input, map)) => {return Ok((input, Structure::Map(map)));},
55 _ => (),
56 }
57 match set(input.clone()) {
58 Ok((input, set)) => {return Ok((input, Structure::Set(set)));},
59 Err(err) => {return Err(err);}
60 }
61}
62
63pub fn tuple(input: ParseString) -> ParseResult<Tuple> {
65 let (input, _) = left_parenthesis(input)?;
66 let (input, _) = whitespace0(input)?;
67 let (input, exprs) = separated_list0(list_separator, expression)(input)?;
68 let (input, _) = whitespace0(input)?;
69 let (input, _) = right_parenthesis(input)?;
70 Ok((input, Tuple{elements: exprs}))
71}
72
73pub fn atom(input: ParseString) -> ParseResult<Atom> {
75 let (input, _) = grave(input)?;
76 let (input, name) = identifier(input)?;
77 Ok((input, Atom{name}))
78}
79
80pub fn tuple_struct(input: ParseString) -> ParseResult<TupleStruct> {
82 let (input, _) = grave(input)?;
83 let (input, name) = identifier(input)?;
84 let (input, _) = left_parenthesis(input)?;
85 let (input, _) = whitespace0(input)?;
86 let (input, value) = expression(input)?;
87 let (input, _) = whitespace0(input)?;
88 let (input, _) = right_parenthesis(input)?;
89 Ok((input, TupleStruct{name, value: Box::new(value)}))
90}
91
92pub fn binding(input: ParseString) -> ParseResult<Binding> {
104 let msg1 = "Unexpected space before colon ':'";
105 let msg2 = "Expects a value";
106 let msg3 = "Expects whitespace or comma followed by whitespace";
107 let msg4 = "Expects whitespace";
108 let (input, _) = whitespace0(input)?;
109 let (input, name) = identifier(input)?;
110 let (input, kind) = opt(kind_annotation)(input)?;
111 let (input, _) = whitespace0(input)?;
112 let (input, _) = colon(input)?;
113 let (input, _) = whitespace0(input)?;
114 let (input, value) = label!(expression, msg2)(input)?;
115 let (input, _) = whitespace0(input)?;
116 let (input, _) = opt(comma)(input)?;
117 let (input, _) = whitespace0(input)?;
118 Ok((input, Binding{name, kind, value}))
119}
120
121pub fn table_column(input: ParseString) -> ParseResult<TableColumn> {
123 let (input, _) = many0(space_tab)(input)?;
124 let (input, element) = match expression(input) {
125 Ok(result) => result,
126 Err(err) => {
127 return Err(err);
128 }
129 };
130 let (input, _) = nom_tuple((many0(space_tab),opt(alt((comma,table_separator))), many0(space_tab)))(input)?;
131 Ok((input, TableColumn{element}))
132}
133
134pub fn matrix_column(input: ParseString) -> ParseResult<MatrixColumn> {
136 let (input, _) = many0(space_tab)(input)?;
137 let (input, element) = match expression(input) {
138 Ok(result) => result,
139 Err(err) => {
140 return Err(err);
141 }
142 };
143 let (input, _) = nom_tuple((many0(space_tab),opt(alt((comma,table_separator))), many0(space_tab)))(input)?;
144 Ok((input, MatrixColumn{element}))
145}
146
147
148pub fn table_row(input: ParseString) -> ParseResult<TableRow> {
150 let (input, _) = opt(table_separator)(input)?;
151 let (input, _) = many0(space_tab)(input)?;
152 let (input, columns) = match many1(table_column)(input) {
153 Ok(result) => result,
154 Err(error) => {
155 return Err(error);
156 }
157 };
158 let (input, _) = nom_tuple((opt(semicolon), opt(new_line)))(input)?;
159 let (input, _) = opt(nom_tuple((many1(box_drawing_char),new_line)))(input)?;
160 Ok((input, TableRow{columns}))
161}
162
163pub fn matrix_row(input: ParseString) -> ParseResult<MatrixRow> {
165 let (input, _) = opt(table_separator)(input)?;
166 let (input, _) = many0(space_tab)(input)?;
167 let (input, columns) = match many1(matrix_column)(input) {
168 Ok(result) => result,
169 Err(error) => {
170 return Err(error);
171 }
172 };
173 let (input, _) = nom_tuple((opt(semicolon), opt(new_line)))(input)?;
174 let (input, _) = opt(nom_tuple((many1(box_drawing_char),new_line)))(input)?;
175 Ok((input, MatrixRow{columns}))
176}
177
178pub fn table_header(input: ParseString) -> ParseResult<Vec<Field>> {
180 let (input, fields) = separated_list1(many1(space_tab),field)(input)?;
181 let (input, _) = many0(space_tab)(input)?;
182 let (input, _) = alt((bar,box_vert))(input)?;
183 let (input, _) = whitespace0(input)?;
184 Ok((input, fields))
185}
186
187pub fn field(input: ParseString) -> ParseResult<Field> {
189 let (input, name) = identifier(input)?;
190 let (input, kind) = opt(kind_annotation)(input)?;
191 Ok((input, Field{name, kind}))
192}
193
194pub fn box_drawing_char(input: ParseString) -> ParseResult<Token> {
196 alt((box_tl, box_br, box_bl, box_tr, box_tr_round, box_bl_round, box_vert, box_cross, box_horz, box_t_left, box_t_right, box_t_top, box_t_bottom))(input)
197}
198
199pub fn box_drawing_emoji(input: ParseString) -> ParseResult<Token> {
201 alt((box_tl, box_br, box_bl, box_tr, box_tl_round, box_br_round, box_tr_round, box_bl_round, box_vert, box_cross, box_horz, box_t_left, box_t_right, box_t_top, box_t_bottom))(input)
202}
203
204pub fn matrix_start(input: ParseString) -> ParseResult<Token> {
206 alt((box_tl_round, box_tl, left_bracket))(input)
207}
208
209pub fn matrix_end(input: ParseString) -> ParseResult<Token> {
211 let result = alt((box_br_round, box_br, right_bracket))(input);
212 result
213}
214
215pub fn table_start(input: ParseString) -> ParseResult<Token> {
217 alt((box_tl_round, box_tl, left_brace))(input)
218}
219
220pub fn table_end(input: ParseString) -> ParseResult<Token> {
222 let result = alt((box_br_round, box_br, right_brace))(input);
223 result
224}
225
226pub fn table_separator(input: ParseString) -> ParseResult<Token> {
228 let (input, token) = box_vert(input)?;
229 Ok((input, token))
230}
231
232pub fn matrix(input: ParseString) -> ParseResult<Matrix> {
234 let msg = "Expects right bracket ']' to finish the matrix";
235 let (input, (_, r)) = range(matrix_start)(input)?;
236 let (input, _) = many0(alt((box_drawing_char,whitespace)))(input)?;
237 let (input, rows) = many0(matrix_row)(input)?;
238 let (input, _) = many0(box_drawing_char)(input)?;
239 let (input, _) = whitespace0(input)?;
240 let (input, _) = match label!(matrix_end, msg, r)(input) {
241 Ok(k) => k,
242 Err(err) => {
243 return Err(err);
244 }
245 };
246 Ok((input, Matrix{rows}))
247}
248
249pub fn table(input: ParseString) -> ParseResult<Table> {
251 let msg = "Expects right bracket '}' to finish the table";
252 let (input, (_, r)) = range(table_start)(input)?;
253 let (input, _) = many0(alt((box_drawing_char,whitespace)))(input)?;
254 let (input, header) = table_header(input)?;
255 let (input, _) = many0(alt((box_drawing_char,whitespace)))(input)?;
256 let (input, rows) = many1(table_row)(input)?;
257 let (input, _) = many0(box_drawing_char)(input)?;
258 let (input, _) = whitespace0(input)?;
259 let (input, _) = match label!(table_end, msg, r)(input) {
260 Ok(k) => k,
261 Err(err) => {
262 return Err(err);
263 }
264 };
265 Ok((input, Table{header,rows}))
266}
267
268pub fn empty_map(input: ParseString) -> ParseResult<Map> {
270 let (input, _) = table_start(input)?;
271 let (input, _) = whitespace0(input)?;
272 let (input, _) = table_end(input)?;
273 Ok((input, Map{elements: vec![]}))
274}
275
276pub fn empty_set(input: ParseString) -> ParseResult<Set> {
278 let (input, _) = table_start(input)?;
279 let (input, _) = whitespace0(input)?;
280 let (input, _) = empty(input)?;
281 let (input, _) = whitespace0(input)?;
282 let (input, _) = table_end(input)?;
283 Ok((input, Set{elements: vec![]}))
284}
285
286pub fn record(input: ParseString) -> ParseResult<Record> {
288 let msg = "Expects right bracket ']' to terminate inline table";
289 let (input, (_, r)) = range(table_start)(input)?;
290 let (input, _) = whitespace0(input)?;
291 let (input, bindings) = many1(binding)(input)?;
292 let (input, _) = whitespace0(input)?;
293 let (input, _) = label!(table_end, msg, r)(input)?;
294 Ok((input, Record{bindings}))
295}
296
297pub fn map(input: ParseString) -> ParseResult<Map> {
299 let msg = "Expects right bracket '}' to terminate inline table";
300 let (input, (_, r)) = range(left_brace)(input)?;
301 let (input, _) = whitespace0(input)?;
302 let (input, elements) = many0(mapping)(input)?;
303 let (input, _) = whitespace0(input)?;
304 let (input, _) = label!(right_brace, msg, r)(input)?;
305 Ok((input, Map{elements}))
306}
307
308pub fn mapping(input: ParseString) -> ParseResult<Mapping> {
310 let msg1 = "Unexpected space before colon ':'";
311 let msg2 = "Expects a value";
312 let msg3 = "Expects whitespace or comma followed by whitespace";
313 let msg4 = "Expects whitespace";
314 let (input, _) = whitespace0(input)?;
315 let (input, key) = expression(input)?;
316 let (input, _) = whitespace0(input)?;
317 let (input, _) = colon(input)?;
318 let (input, _) = whitespace0(input)?;
319 let (input, value) = label!(expression, msg2)(input)?;
320 let (input, _) = whitespace0(input)?;
321 let (input, _) = opt(comma)(input)?;
322 let (input, _) = whitespace0(input)?;
323 Ok((input, Mapping{key, value}))
324}
325
326pub fn set(input: ParseString) -> ParseResult<Set> {
328 let msg = "Expects right bracket '}' to terminate inline table";
329 let (input, (_, r)) = range(left_brace)(input)?;
330 let (input, _) = whitespace0(input)?;
331 let (input, elements) = separated_list0(alt((list_separator,whitespace1)), expression)(input)?;
332 let (input, _) = whitespace0(input)?;
333 let (input, _) = label!(right_brace, msg, r)(input)?;
334 Ok((input, Set{elements}))
335}
336
337pub fn define_operator(input: ParseString) -> ParseResult<()> {
341 let (input, _) = whitespace0(input)?;
342 let (input, _) = tag(":=")(input)?;
343 let (input, _) = whitespace0(input)?;
344 Ok((input, ()))
345}
346
347pub fn output_operator(input: ParseString) -> ParseResult<()> {
349 let (input, _) = whitespace0(input)?;
350 let (input, _) = tag("=>")(input)?;
351 let (input, _) = whitespace0(input)?;
352 Ok((input, ()))
353}
354
355pub fn async_transition_operator(input: ParseString) -> ParseResult<()> {
357 let (input, _) = whitespace0(input)?;
358 let (input, _) = tag("~>")(input)?;
359 let (input, _) = whitespace0(input)?;
360 Ok((input, ()))
361}
362
363pub fn transition_operator(input: ParseString) -> ParseResult<()> {
365 let (input, _) = whitespace0(input)?;
366 let (input, _) = tag("->")(input)?;
367 let (input, _) = whitespace0(input)?;
368 Ok((input, ()))
369}
370
371pub fn guard_operator(input: ParseString) -> ParseResult<()> {
373 let (input, _) = whitespace0(input)?;
374 let (input, _) = alt((tag("|"),tag("│"),tag("├"),tag("└")))(input)?;
375 let (input, _) = whitespace0(input)?;
376 Ok((input, ()))
377}
378
379pub fn fsm_implementation(input: ParseString) -> ParseResult<FsmImplementation> {
381 let (input, _) = hashtag(input)?;
382 let (input, name) = identifier(input)?;
383 let (input, _) = left_parenthesis(input)?;
384 let (input, input_vars) = separated_list0(list_separator, identifier)(input)?;
385 let (input, _) = right_parenthesis(input)?;
386 let (input, _) = transition_operator(input)?;
387 let (input, start) = fsm_pattern(input)?;
388 let (input, _) = whitespace0(input)?;
389 let (input, arms) = many1(fsm_arm)(input)?;
390 let (input, _) = period(input)?;
391 Ok((input, FsmImplementation{name,input: input_vars,start,arms}))
392}
393
394pub fn fsm_arm(input: ParseString) -> ParseResult<FsmArm> {
396 let (input, _) = many0(comment)(input)?;
397 let (input, arm) = alt((fsm_guard_arm,fsm_transition))(input)?;
398 let (input, _) = whitespace0(input)?;
399 Ok((input, arm))
400}
401
402pub fn fsm_guard_arm(input: ParseString) -> ParseResult<FsmArm> {
404 let (input, _) = many0(comment)(input)?;
405 let (input, start) = fsm_pattern(input)?;
406 let (input, grds) = many1(fsm_guard)(input)?;
407 Ok((input, FsmArm::Guard(start, grds)))
408}
409
410pub fn fsm_guard(input: ParseString) -> ParseResult<Guard> {
411 let (input, _) = guard_operator(input)?;
412 let (input, cnd) = fsm_pattern(input)?;
413 let (input, trns) = many1(alt((
414 fsm_statement_transition,
415 fsm_state_transition,
416 fsm_output,
417 fsm_async_transition,
418 fsm_block_transition)))(input)?;
419 Ok((input, Guard{condition: cnd, transitions: trns}))
420}
421
422pub fn fsm_transition(input: ParseString) -> ParseResult<FsmArm> {
423 let (input, _) = many0(comment)(input)?;
424 let (input, start) = fsm_pattern(input)?;
425 let (input, trns) = many1(alt((
426 fsm_state_transition,
427 fsm_output,
428 fsm_async_transition,
429 fsm_statement_transition,
430 fsm_block_transition)))(input)?;
431 Ok((input, FsmArm::Transition(start, trns)))
432}
433
434pub fn fsm_state_transition(input: ParseString) -> ParseResult<Transition> {
436 let (input, _) = transition_operator(input)?;
437 let (input, ptrn) = fsm_pattern(input)?;
438 Ok((input, Transition::Next(ptrn)))
439}
440
441pub fn fsm_async_transition(input: ParseString) -> ParseResult<Transition> {
443 let (input, _) = async_transition_operator(input)?;
444 let (input, ptrn) = fsm_pattern(input)?;
445 Ok((input, Transition::Async(ptrn)))
446}
447
448pub fn fsm_statement_transition(input: ParseString) -> ParseResult<Transition> {
449 let (input, _) = transition_operator(input)?;
450 let (input, stmnt) = statement(input)?;
451 Ok((input, Transition::Statement(stmnt)))
452}
453
454pub fn fsm_block_transition(input: ParseString) -> ParseResult<Transition> {
455 let (input, _) = transition_operator(input)?;
456 let (input, _) = left_brace(input)?;
457 let (input, code) = many1(mech_code)(input)?;
458 let (input, _) = right_brace(input)?;
459 Ok((input, Transition::CodeBlock(code)))
460}
461
462pub fn fsm_output(input: ParseString) -> ParseResult<Transition> {
464 let (input, _) = output_operator(input)?;
465 let ((input, ptrn)) = fsm_pattern(input)?;
466 Ok((input, Transition::Output(ptrn)))
467}
468
469pub fn fsm_specification(input: ParseString) -> ParseResult<FsmSpecification> {
471 let (input, _) = hashtag(input)?;
472 let (input, name) = identifier(input)?;
473 let (input, _) = left_parenthesis(input)?;
474 let (input, input_vars) = separated_list0(list_separator, var)(input)?;
475 let (input, _) = right_parenthesis(input)?;
476 let (input, _) = opt(output_operator)(input)?;
477 let (input, output) = opt(kind_annotation)(input)?;
478 let (input, _) = define_operator(input)?;
479 let (input, states) = many1(fsm_state_definition)(input)?;
480 let (input, _) = period(input)?;
481 Ok((input, FsmSpecification{name,input: input_vars, output, states}))
482}
483
484pub fn fsm_pattern(input: ParseString) -> ParseResult<Pattern> {
486 match fsm_tuple_struct(input.clone()) {
487 Ok((input, tpl)) => {return Ok((input, Pattern::TupleStruct(tpl)))},
488 _ => ()
489 }
490 match wildcard(input.clone()) {
491 Ok((input, _)) => {return Ok((input, Pattern::Wildcard))},
492 _ => ()
493 }
494 match formula(input.clone()) {
495 Ok((input, Factor::Expression(expr))) => {return Ok((input, Pattern::Expression(*expr)))},
496 Ok((input, frmla)) => {return Ok((input, Pattern::Formula(frmla)))},
497 Err(err) => {return Err(err)},
498 }
499}
500
501pub fn wildcard(input: ParseString) -> ParseResult<Pattern> {
503 let ((input, _)) = asterisk(input)?;
504 Ok((input, Pattern::Wildcard))
505}
506
507pub fn fsm_tuple_struct(input: ParseString) -> ParseResult<PatternTupleStruct> {
509 let (input, _) = grave(input)?;
510 let (input, id) = identifier(input)?;
511 let (input, _) = left_parenthesis(input)?;
512 let (input, patterns) = separated_list1(list_separator, fsm_pattern)(input)?;
513 let (input, _) = right_parenthesis(input)?;
514 Ok((input, PatternTupleStruct{name: id, patterns}))
515}
516
517pub fn fsm_state_definition(input: ParseString) -> ParseResult<StateDefinition> {
519 let (input, _) = guard_operator(input)?;
520 let (input, _) = whitespace0(input)?;
521 let (input, _) = grave(input)?;
522 let (input, name) = identifier(input)?;
523 let (input, vars) = opt(fsm_state_definition_variables)(input)?;
524 Ok((input, StateDefinition{name,state_variables: vars}))
525}
526
527pub fn fsm_state_definition_variables(input: ParseString) -> ParseResult<Vec<Var>> {
529 let (input, _) = left_parenthesis(input)?;
530 let (input, names) = separated_list1(list_separator, var)(input)?;
531 let (input, _) = right_parenthesis(input)?;
532 Ok((input, names))
533}
534
535pub fn fsm_pipe(input: ParseString) -> ParseResult<FsmPipe> {
537 let (input, start) = fsm_instance(input)?;
538 let (input, trns) = many0(alt((fsm_state_transition,fsm_async_transition,fsm_output)))(input)?;
539 Ok((input, FsmPipe{start, transitions: trns}))
540}
541
542pub fn fsm_declare(input: ParseString) -> ParseResult<FsmDeclare> {
544 let (input, fsm) = fsm(input)?;
545 let (input, _) = define_operator(input)?;
546 let (input, pipe) = fsm_pipe(input)?;
547 Ok((input, FsmDeclare{fsm,pipe}))
548}
549
550pub fn fsm(input: ParseString) -> ParseResult<Fsm> {
552 let ((input, _)) = hashtag(input)?;
553 let ((input, name)) = identifier(input)?;
554 let ((input, args)) = opt(argument_list)(input)?;
555 let ((input, kind)) = opt(kind_annotation)(input)?;
556 Ok((input, Fsm{ name, args, kind }))
557}
558
559pub fn fsm_instance(input: ParseString) -> ParseResult<FsmInstance> {
561 let ((input, _)) = hashtag(input)?;
562 let (input, name) = identifier(input)?;
563 let (input, args) = opt(fsm_args)(input)?;
564 Ok((input, FsmInstance{name,args} ))
565}
566
567pub fn fsm_args(input: ParseString) -> ParseResult<Vec<(Option<Identifier>,Expression)>> {
569 let (input, _) = left_parenthesis(input)?;
570 let (input, args) = separated_list0(list_separator, alt((call_arg_with_binding,call_arg)))(input)?;
571 let (input, _) = right_parenthesis(input)?;
572 Ok((input, args))
573}