mech_syntax/
structures.rs

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
12// #### Structures
13
14pub 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
21// structure := empty_set | empty_table | table | matrix | tuple | tuple_struct | record | map | set ;
22pub 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    //Err(Failure(err)) => { return Err(Failure(err)); }, 
34    _ => (),
35  }
36  match matrix(input.clone()) {
37    Ok((input, mtrx)) => {return Ok((input, Structure::Matrix(mtrx)));},
38    //Err(Failure(err)) => { return Err(Failure(err)); }, 
39    _ => (),
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
63// tuple := "(", list0(",", expression), ")" ;
64pub 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
73// atom := "`", identifier ;
74pub fn atom(input: ParseString) -> ParseResult<Atom> {
75  let (input, _) = grave(input)?;
76  let (input, name) = identifier(input)?;
77  Ok((input, Atom{name}))
78}
79
80// tuple_struct = atom, "(", expression, ")" ;
81pub 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
92// binding := identifier, kind_annotation?, colon, expression, ","? ;
93
94pub fn binding(input: ParseString) -> ParseResult<Binding> {
95  let msg1 = "Unexpected space before colon ':'";
96  let msg2 = "Expects a value";
97  let msg3 = "Expects whitespace or comma followed by whitespace";
98  let msg4 = "Expects whitespace";
99  let (input, _) = whitespace0(input)?;
100  let (input, name) = identifier(input)?;
101  let (input, kind) = opt(kind_annotation)(input)?;
102  let (input, _) = whitespace0(input)?;
103  let (input, _) = colon(input)?;
104  let (input, _) = whitespace0(input)?;
105  let (input, value) = label!(expression, msg2)(input)?;
106  let (input, _) = whitespace0(input)?;
107  let (input, _) = opt(comma)(input)?;
108  let (input, _) = whitespace0(input)?;
109  Ok((input, Binding{name, kind, value}))
110}
111
112// table_column := (space | tab)*, expression, ((space | tab)*, ("," | table_separator)?, (space | tab)*) ;
113pub fn table_column(input: ParseString) -> ParseResult<TableColumn> {
114  let (input, _) = many0(space_tab)(input)?;
115  let (input, element) = match expression(input) {
116    Ok(result) => result,
117    Err(err) => {
118      return Err(err);
119    }
120  };
121  let (input, _) = nom_tuple((many0(space_tab),opt(alt((comma,table_separator))), many0(space_tab)))(input)?;
122  Ok((input, TableColumn{element}))
123}
124
125// matrix_column := (space | tab)*, expression, ((space | tab)*, ("," | table_separator)?, (space | tab)*) ;
126pub fn matrix_column(input: ParseString) -> ParseResult<MatrixColumn> {
127  let (input, _) = many0(space_tab)(input)?;
128  let (input, element) = match expression(input) {
129    Ok(result) => result,
130    Err(err) => {
131      return Err(err);
132    }
133  };
134  let (input, _) = nom_tuple((many0(space_tab),opt(alt((comma,table_separator))), many0(space_tab)))(input)?;
135  Ok((input, MatrixColumn{element}))
136}
137
138
139// table_row := table_separator?, (space | tab)*, table_column+, semicolon?, new_line?, (box_drawing_char+, new_line)? ;
140pub fn table_row(input: ParseString) -> ParseResult<TableRow> {
141  let (input, _) = opt(table_separator)(input)?;
142  let (input, _) = many0(space_tab)(input)?;
143  let (input, columns) = match many1(table_column)(input) {
144    Ok(result) => result,
145    Err(error) => {
146      return Err(error);
147    }
148  };
149  let (input, _) = nom_tuple((opt(semicolon), opt(new_line)))(input)?;
150  let (input, _) = opt(nom_tuple((many1(box_drawing_char),new_line)))(input)?;
151  Ok((input, TableRow{columns}))
152}
153
154// matrix_row := table_separator?, (space | tab)*, matrix_column+, semicolon?, new_line?, (box_drawing_char+, new_line)? ;
155pub fn matrix_row(input: ParseString) -> ParseResult<MatrixRow> {
156  let (input, _) = opt(table_separator)(input)?;
157  let (input, _) = many0(space_tab)(input)?;
158  let (input, columns) = match many1(matrix_column)(input) {
159    Ok(result) => result,
160    Err(error) => {
161      return Err(error);
162    }
163  };
164  let (input, _) = nom_tuple((opt(semicolon), opt(new_line)))(input)?;
165  let (input, _) = opt(nom_tuple((many1(box_drawing_char),new_line)))(input)?;
166  Ok((input, MatrixRow{columns}))
167}
168
169// table_header := list1(space_tab+, field), (space | tab)*, (bar| box_vert), whitespace* ;
170pub fn table_header(input: ParseString) -> ParseResult<Vec<Field>> {
171  let (input, fields) = separated_list1(many1(space_tab),field)(input)?;
172  let (input, _) = many0(space_tab)(input)?;
173  let (input, _) = alt((bar,box_vert,box_vert_bold))(input)?;
174  let (input, _) = whitespace0(input)?;
175  Ok((input, fields))
176}
177
178// field := identifier, kind_annotation? ;
179pub fn field(input: ParseString) -> ParseResult<Field> {
180  let (input, name) = identifier(input)?;
181  let (input, kind) = opt(kind_annotation)(input)?;
182  Ok((input, Field{name, kind}))
183}
184
185// box_drawing_char := 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 ;
186pub fn box_drawing_char(input: ParseString) -> ParseResult<Token> {
187  alt((box_tl, box_bl, box_tr, box_tl_bold, box_bl_bold, box_tr_bold, 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)
188}
189
190// box_drawing_emoji := 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 ;
191pub fn box_drawing_emoji(input: ParseString) -> ParseResult<Token> {
192  alt((box_tl, box_bl, box_tr, box_tl_bold, box_bl_bold, box_tr_bold, 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)
193}
194
195// matrix_start := box_tl_round | box_tl | left_bracket ;
196pub fn matrix_start(input: ParseString) -> ParseResult<Token> {
197  alt((box_tl_round, box_tl, box_tl_bold, left_bracket))(input)
198}
199
200// matrix_end := box_br_round | box_br | right_bracket ;
201pub fn matrix_end(input: ParseString) -> ParseResult<Token> {
202  let result = alt((box_br_round, box_br, box_br_bold, right_bracket))(input);
203  result
204}
205
206// table_start := box_tl_round | box_tl | left_brace ;
207pub fn table_start(input: ParseString) -> ParseResult<Token> {
208  alt((box_tl_round, box_tl, box_tl_bold, left_brace))(input)
209}
210
211// table_end := box_br_round | box_br | right_brace ;
212pub fn table_end(input: ParseString) -> ParseResult<Token> {
213  let result = alt((box_br_round, box_br, box_br_bold, right_brace))(input);
214  result
215}
216
217// table_separator := box_vert ;
218pub fn table_separator(input: ParseString) -> ParseResult<Token> {
219  let (input, token) = alt((box_vert,box_vert_bold))(input)?;
220  Ok((input, token))
221}
222
223// matrix := matrix_start, (box_drawing_char | whitespace)*, matrix_row*, box_drawing_char*, matrix_end ;
224pub fn matrix(input: ParseString) -> ParseResult<Matrix> {
225  let msg = "Expects right bracket ']' to finish the matrix";
226  let (input, (_, r)) = range(matrix_start)(input)?;
227  let (input, _) = many0(alt((box_drawing_char,whitespace)))(input)?;
228  let (input, rows) = many0(matrix_row)(input)?;
229  let (input, _) = many0(alt((box_drawing_char,whitespace)))(input)?;
230  let (input, _) = whitespace0(input)?;
231  let (input, _) = match label!(matrix_end, msg, r)(input) {
232    Ok(k) => k,
233    Err(err) => {
234      return Err(err);
235    }
236  };
237  Ok((input, Matrix{rows}))
238}
239
240// table := table_start, (box_drawing_char | whitespace)*, table_header, (box_drawing_char | whitespace)*, table_row+, box_drawing_char*, whitespace*, table_end ;
241pub fn table(input: ParseString) -> ParseResult<Table> {
242  let msg = "Expects right bracket '}' to finish the table";
243  let (input, (_, r)) = range(table_start)(input)?;
244  let (input, _) = many0(alt((box_drawing_char,whitespace)))(input)?;
245  let (input, header) = table_header(input)?;
246  let (input, _) = many0(alt((box_drawing_char,whitespace)))(input)?;
247  let (input, rows) = many1(table_row)(input)?;
248  let (input, _) = many0(box_drawing_char)(input)?;
249  let (input, _) = whitespace0(input)?;
250  let (input, _) = match label!(table_end, msg, r)(input) {
251    Ok(k) => k,
252    Err(err) => {
253      return Err(err);
254    }
255  };
256  Ok((input, Table{header,rows}))
257}
258
259// empty_table := table_start, whitespace*, table_end ;
260pub fn empty_map(input: ParseString) -> ParseResult<Map> {
261  let (input, _) = table_start(input)?;
262  let (input, _) = whitespace0(input)?;
263  let (input, _) = table_end(input)?;
264  Ok((input, Map{elements: vec![]}))
265}
266
267// empty_set := table_start, whitespace*, empty, whitespace*, table_end ;
268pub fn empty_set(input: ParseString) -> ParseResult<Set> {
269  let (input, _) = table_start(input)?;
270  let (input, _) = whitespace0(input)?;
271  let (input, _) = empty(input)?;
272  let (input, _) = whitespace0(input)?;
273  let (input, _) = table_end(input)?;
274  Ok((input,  Set{elements: vec![]}))
275}
276
277// record := table_start, whitespace*, binding+, whitespace*, table_end ;
278pub fn record(input: ParseString) -> ParseResult<Record> {
279  let msg = "Expects right bracket ']' to terminate inline table";
280  let (input, (_, r)) = range(table_start)(input)?;
281  let (input, _) = whitespace0(input)?;
282  let (input, bindings) = many1(binding)(input)?;
283  let (input, _) = whitespace0(input)?;
284  let (input, _) = label!(table_end, msg, r)(input)?;
285  Ok((input, Record{bindings}))
286}
287
288// map := "{", whitespace*, mapping*, whitespace*, "}" ;
289pub fn map(input: ParseString) -> ParseResult<Map> {
290  let msg = "Expects right bracket '}' to terminate inline table";
291  let (input, (_, r)) = range(left_brace)(input)?;
292  let (input, _) = whitespace0(input)?;
293  let (input, elements) = many0(mapping)(input)?;
294  let (input, _) = whitespace0(input)?;
295  let (input, _) = label!(right_brace, msg, r)(input)?;
296  Ok((input, Map{elements}))
297}
298
299// mapping :=  whitespace*, expression, whitespace*, ":", whitespace*, expression, comma?, whitespace* ;
300pub fn mapping(input: ParseString) -> ParseResult<Mapping> {
301  let msg1 = "Unexpected space before colon ':'";
302  let msg2 = "Expects a value";
303  let msg3 = "Expects whitespace or comma followed by whitespace";
304  let msg4 = "Expects whitespace";
305  let (input, _) = whitespace0(input)?;
306  let (input, key) = expression(input)?;
307  let (input, _) = whitespace0(input)?;
308  let (input, _) = colon(input)?;
309  let (input, _) = whitespace0(input)?;
310  let (input, value) = label!(expression, msg2)(input)?;
311  let (input, _) = whitespace0(input)?;
312  let (input, _) = opt(comma)(input)?;
313  let (input, _) = whitespace0(input)?;
314  Ok((input, Mapping{key, value}))
315}
316
317// set := "{", whitespace*, list0(("," | whitespace+), expression), whitespace*, "}" ;
318pub fn set(input: ParseString) -> ParseResult<Set> {
319  let msg = "Expects right bracket '}' to terminate inline table";
320  let (input, (_, r)) = range(left_brace)(input)?;
321  let (input, _) = whitespace0(input)?;
322  let (input, elements) = separated_list0(alt((list_separator,whitespace1)), expression)(input)?;
323  let (input, _) = whitespace0(input)?;
324  let (input, _) = label!(right_brace, msg, r)(input)?;
325  Ok((input, Set{elements}))
326}
327
328// #### State Machines
329
330// guard_operator := "|" | "│" | "├" | "└" ;
331pub fn guard_operator(input: ParseString) -> ParseResult<()> {
332  let (input, _) = whitespace0(input)?;
333  let (input, _) = alt((tag("|"),tag("│"),tag("├"),tag("└")))(input)?;
334  let (input, _) = whitespace0(input)?;
335  Ok((input, ()))
336}
337
338// fsm_implementation := "#", identifier, "(", list0(",", identifier), ")", transition_operator, fsm_pattern, whitespace*, fsm_arm+, "." ;
339pub fn fsm_implementation(input: ParseString) -> ParseResult<FsmImplementation> {
340  let (input, _) = hashtag(input)?;
341  let (input, name) = identifier(input)?;
342  let (input, _) = left_parenthesis(input)?;
343  let (input, input_vars) = separated_list0(list_separator, identifier)(input)?;
344  let (input, _) = right_parenthesis(input)?;
345  let (input, _) = transition_operator(input)?;
346  let (input, start) = fsm_pattern(input)?;
347  let (input, _) = whitespace0(input)?;
348  let (input, arms) = many1(fsm_arm)(input)?;
349  let (input, _) = period(input)?;
350  Ok((input, FsmImplementation{name,input: input_vars,start,arms}))
351}
352
353// fsm_arm := comment*, (fsm_transition | fsm_guard_arm), whitespace* ;
354pub fn fsm_arm(input: ParseString) -> ParseResult<FsmArm> {
355  let (input, _) = many0(comment)(input)?;
356  let (input, arm) = alt((fsm_guard_arm,fsm_transition))(input)?;
357  let (input, _) = whitespace0(input)?;
358  Ok((input, arm))
359}
360
361// fsm_guard_arm := comment*, fsm_pattern, fsm_guard+ ;
362pub fn fsm_guard_arm(input: ParseString) -> ParseResult<FsmArm> {
363  let (input, _) = many0(comment)(input)?;
364  let (input, start) = fsm_pattern(input)?;
365  let (input, grds) = many1(fsm_guard)(input)?;
366  Ok((input, FsmArm::Guard(start, grds)))
367}
368
369// fsm_guard := guard_operator, fsm_pattern, (fsm_statement_transition | fsm_state_transition | fsm_output | fsm_async_transition | fsm_block_transition)+ ;
370pub fn fsm_guard(input: ParseString) -> ParseResult<Guard> {
371  let (input, _) = guard_operator(input)?;
372  let (input, cnd) = fsm_pattern(input)?;
373  let (input, trns) = many1(alt((
374    fsm_statement_transition,
375    fsm_state_transition,
376    fsm_output,
377    fsm_async_transition,
378    fsm_block_transition)))(input)?;
379  Ok((input, Guard{condition: cnd, transitions: trns}))
380}
381
382// fsm_transition := comment*, fsm_pattern, (fsm_statement_transition | fsm_state_transition | fsm_output | fsm_async_transition | fsm_block_transition)+ ;
383pub fn fsm_transition(input: ParseString) -> ParseResult<FsmArm> {
384  let (input, _) = many0(comment)(input)?;
385  let (input, start) = fsm_pattern(input)?;
386  let (input, trns) = many1(alt((
387    fsm_state_transition,
388    fsm_output,
389    fsm_async_transition,
390    fsm_statement_transition,
391    fsm_block_transition)))(input)?;
392  Ok((input, FsmArm::Transition(start, trns)))
393}
394
395// fsm_state_transition := transition_operator, fsm_pattern ;
396pub fn fsm_state_transition(input: ParseString) -> ParseResult<Transition> {
397  let (input, _) = transition_operator(input)?;
398  let (input, ptrn) = fsm_pattern(input)?;
399  Ok((input, Transition::Next(ptrn)))
400}
401
402// fsm_async_transition := async_transition_operator, fsm_pattern ;
403pub fn fsm_async_transition(input: ParseString) -> ParseResult<Transition> {
404  let (input, _) = async_transition_operator(input)?;
405  let (input, ptrn) = fsm_pattern(input)?;
406  Ok((input, Transition::Async(ptrn)))
407}
408
409// fsm_statement_transition := transition_operator, statement ;
410pub fn fsm_statement_transition(input: ParseString) -> ParseResult<Transition> {
411  let (input, _) = transition_operator(input)?;
412  let (input, stmnt) = statement(input)?;
413  Ok((input, Transition::Statement(stmnt)))
414}
415
416// fsm_block_transition := transition_operator, left_brace, mech_code+, right_brace ;
417pub fn fsm_block_transition(input: ParseString) -> ParseResult<Transition> {
418  let (input, _) = transition_operator(input)?;
419  let (input, _) = left_brace(input)?;
420  let (input, code) = many1(mech_code)(input)?;
421  let (input, _) = right_brace(input)?;
422  Ok((input, Transition::CodeBlock(code)))
423}
424
425
426// fsm_output := output_operator, fsm_pattern ;
427pub fn fsm_output(input: ParseString) -> ParseResult<Transition> {
428  let (input, _) = output_operator(input)?;
429  let ((input, ptrn)) = fsm_pattern(input)?;
430  Ok((input, Transition::Output(ptrn)))
431}
432
433// fsm_specification := "#", identifier, "(", list0(",", var), ")", output_operator?, kind_annotation?, define_operator, fsm_state_definition+, "." ;
434pub fn fsm_specification(input: ParseString) -> ParseResult<FsmSpecification> {
435  let (input, _) = hashtag(input)?;
436  let (input, name) = identifier(input)?;
437  let (input, _) = left_parenthesis(input)?;
438  let (input, input_vars) = separated_list0(list_separator, var)(input)?;
439  let (input, _) = right_parenthesis(input)?;
440  let (input, _) = opt(output_operator)(input)?;
441  let (input, output) = opt(kind_annotation)(input)?;
442  let (input, _) = define_operator(input)?;
443  let (input, states) = many1(fsm_state_definition)(input)?;
444  let (input, _) = period(input)?;
445  Ok((input, FsmSpecification{name,input: input_vars, output, states}))
446}
447
448// fsm_pattern := fsm_tuple_struct | wildcard | formula ;
449pub fn fsm_pattern(input: ParseString) -> ParseResult<Pattern> {
450  match fsm_tuple_struct(input.clone()) {
451    Ok((input, tpl)) => {return Ok((input, Pattern::TupleStruct(tpl)))},
452    _ => ()
453  }
454  match wildcard(input.clone()) {
455    Ok((input, _)) => {return Ok((input, Pattern::Wildcard))},
456    _ => ()
457  }
458  match formula(input.clone()) {
459    Ok((input, Factor::Expression(expr))) => {return Ok((input, Pattern::Expression(*expr)))},
460    Ok((input, frmla)) => {return Ok((input, Pattern::Formula(frmla)))},
461    Err(err) => {return Err(err)},
462  }
463}
464
465// wildcard := "*" ;
466pub fn wildcard(input: ParseString) -> ParseResult<Pattern> {
467  let ((input, _)) = asterisk(input)?;
468  Ok((input, Pattern::Wildcard))
469}
470
471// fsm_tuple_struct := grave, identifier, "(", list1(",", fsm_pattern), ")" ;
472pub fn fsm_tuple_struct(input: ParseString) -> ParseResult<PatternTupleStruct> {
473  let (input, _) = grave(input)?;
474  let (input, id) = identifier(input)?;
475  let (input, _) = left_parenthesis(input)?;
476  let (input, patterns) = separated_list1(list_separator, fsm_pattern)(input)?;
477  let (input, _) = right_parenthesis(input)?;
478  Ok((input, PatternTupleStruct{name: id, patterns}))
479}
480
481// fsm_state_definition := guard_operator, grave, identifier, fsm_state_definition_variables? ;
482pub fn fsm_state_definition(input: ParseString) -> ParseResult<StateDefinition> {
483  let (input, _) = guard_operator(input)?;
484  let (input, _) = whitespace0(input)?;
485  let (input, _) = grave(input)?;
486  let (input, name) = identifier(input)?;
487  let (input, vars) = opt(fsm_state_definition_variables)(input)?;
488  Ok((input, StateDefinition{name,state_variables: vars}))
489}
490
491// fsm_state_definition_variables := "(", list0(list_separator, var), ")" ;
492pub fn fsm_state_definition_variables(input: ParseString) -> ParseResult<Vec<Var>> {
493  let (input, _) = left_parenthesis(input)?;
494  let (input, names) = separated_list1(list_separator, var)(input)?;
495  let (input, _) = right_parenthesis(input)?;
496  Ok((input, names))
497}
498
499// fsm_pipe := fsm_instance, (fsm_state_transition | fsm_async_transition | fsm_output)* ;
500pub fn fsm_pipe(input: ParseString) -> ParseResult<FsmPipe> {
501  let (input, start) = fsm_instance(input)?;
502  let (input, trns) = many0(alt((fsm_state_transition,fsm_async_transition,fsm_output)))(input)?;
503  Ok((input, FsmPipe{start, transitions: trns}))
504}
505
506// fsm_declare := fsm, define_operator, fsm_pipe ;
507pub fn fsm_declare(input: ParseString) -> ParseResult<FsmDeclare> {
508  let (input, fsm) = fsm(input)?;
509  let (input, _) = define_operator(input)?;
510  let (input, pipe) = fsm_pipe(input)?;
511  Ok((input, FsmDeclare{fsm,pipe}))
512}
513  
514// fsm := "#", identifier, argument_list?, kind_annotation? ;
515pub fn fsm(input: ParseString) -> ParseResult<Fsm> {
516  let ((input, _)) = hashtag(input)?;
517  let ((input, name)) = identifier(input)?;
518  let ((input, args)) = opt(argument_list)(input)?;
519  let ((input, kind)) = opt(kind_annotation)(input)?;
520  Ok((input, Fsm{ name, args, kind }))
521}
522
523// fsm_instance := "#", identifier, fsm_args? ;
524pub fn fsm_instance(input: ParseString) -> ParseResult<FsmInstance> {
525  let ((input, _)) = hashtag(input)?;
526  let (input, name) = identifier(input)?;
527  let (input, args) = opt(fsm_args)(input)?;
528  Ok((input, FsmInstance{name,args} ))
529}
530
531// fsm_args := "(", list0(list_separator, (call_arg_with_binding | call_arg)), ")" ;
532pub fn fsm_args(input: ParseString) -> ParseResult<Vec<(Option<Identifier>,Expression)>> {
533  let (input, _) = left_parenthesis(input)?;
534  let (input, args) = separated_list0(list_separator, alt((call_arg_with_binding,call_arg)))(input)?;
535  let (input, _) = right_parenthesis(input)?;
536  Ok((input, args))
537}