mech_syntax/
structures.rs

1#[macro_use]
2use crate::*;
3
4#[cfg(not(feature = "no-std"))] use core::fmt;
5#[cfg(feature = "no-std")] use alloc::fmt;
6#[cfg(feature = "no-std")] use alloc::string::String;
7#[cfg(feature = "no-std")] use alloc::vec::Vec;
8use nom::{
9  IResult,
10  branch::alt,
11  sequence::{tuple as nom_tuple, delimited},
12  combinator::{opt, eof, peek},
13  multi::{many1, many_till, many0, separated_list1,separated_list0},
14  bytes::complete::{take_until, take_while},
15  Err,
16  Err::Failure
17};
18use std::collections::HashMap;
19use colored::*;
20
21use crate::*;
22use crate::nodes::Matrix;
23
24// Structures
25// =============================================================================
26
27pub fn max_err<'a>(x: Option<ParseError<'a>>, y: ParseError<'a>) -> ParseError<'a> {
28  match (x,&y) {
29    (None, y) => y.clone(),
30    _ => y.clone(),
31  }
32}
33
34// structure := empty-set | empty-table | table | matrix | tuple | tuple-struct | record | map | set ;
35pub fn structure(input: ParseString) -> ParseResult<Structure> {
36  match empty_set(input.clone()) {
37    Ok((input, set)) => {return Ok((input, Structure::Set(set)));},
38    _ => (),
39  }
40  match empty_map(input.clone()) {
41    Ok((input, map)) => {return Ok((input, Structure::Map(map)));},
42    _ => (),
43  }
44  match table(input.clone()) {
45    Ok((input, tbl)) => {
46      return Ok((input, Structure::Table(tbl)));
47    },
48    //Err(Failure(err)) => { return Err(Failure(err)); }, 
49    _ => (),
50  }
51  match matrix(input.clone()) {
52    Ok((input, mtrx)) => {return Ok((input, Structure::Matrix(mtrx)));},
53    //Err(Failure(err)) => { return Err(Failure(err)); }, 
54    _ => (),
55  }
56  match tuple(input.clone()) {
57    Ok((input, tpl)) => {return Ok((input, Structure::Tuple(tpl)));},
58    _ => (),
59  }
60  match tuple_struct(input.clone()) {
61    Ok((input, tpl)) => {return Ok((input, Structure::TupleStruct(tpl)));},
62    _ => (),
63  }
64  match record(input.clone()) {
65    Ok((input, table)) => {return Ok((input, Structure::Record(table)));},
66    _ => (),
67  }
68  match map(input.clone()) {
69    Ok((input, map)) => {return Ok((input, Structure::Map(map)));},
70    _ => (),
71  }
72  match set(input.clone()) {
73    Ok((input, set)) => {return Ok((input, Structure::Set(set)));},
74    Err(err) => {return Err(err);}
75  }
76}
77
78// Matrix
79// ----------------------------------------------------------------------------
80
81// matrix := matrix-start, (box-drawing-char | whitespace)*, matrix-row*, box-drawing-char*, matrix-end ;
82pub fn matrix(input: ParseString) -> ParseResult<Matrix> {
83  let msg = "Expects right bracket ']' to finish the matrix";
84  let (input, (_, r)) = range(matrix_start)(input)?;
85  let (input, _) = many0(alt((box_drawing_char,whitespace)))(input)?;
86  let (input, rows) = many0(matrix_row)(input)?;
87  let (input, _) = many0(alt((box_drawing_char,whitespace)))(input)?;
88  let (input, _) = whitespace0(input)?;
89  let (input, _) = match label!(matrix_end, msg, r)(input) {
90    Ok(k) => k,
91    Err(err) => {
92      return Err(err);
93    }
94  };
95  Ok((input, Matrix{rows}))
96}
97
98// matrix-column := (space | tab)*, expression, ((space | tab)*, ("," | table-separator)?, (space | tab)*) ;
99pub fn matrix_column(input: ParseString) -> ParseResult<MatrixColumn> {
100  let (input, _) = space_tab0(input)?;
101  let (input, element) = match expression(input) {
102    Ok(result) => result,
103    Err(err) => {
104      return Err(err);
105    }
106  };
107  let (input, _) = nom_tuple((space_tab0,opt(alt((comma,table_separator))), space_tab0))(input)?;
108  Ok((input, MatrixColumn{element}))
109}
110
111// matrix-row := table-separator?, (space | tab)*, matrix-column+, semicolon?, new-line?, (box-drawing-char+, new-line)? ;
112pub fn matrix_row(input: ParseString) -> ParseResult<MatrixRow> {
113  let (input, _) = space_tab0(input)?;
114  let (input, _) = opt(table_separator)(input)?;
115  let (input, _) = space_tab0(input)?;
116  let (input, columns) = match many1(matrix_column)(input) {
117    Ok(result) => result,
118    Err(error) => {
119      return Err(error);
120    }
121  };
122  let (input, _) = nom_tuple((opt(semicolon), opt(new_line)))(input)?;
123  let (input, _) = opt(nom_tuple((many1(box_drawing_char),new_line)))(input)?;
124  Ok((input, MatrixRow{columns}))
125}
126
127// matrix-start := box-tl-round | box-tl | left-bracket ;
128pub fn matrix_start(input: ParseString) -> ParseResult<Token> {
129  alt((box_tl_round, box_tl, box_tl_bold, left_bracket))(input)
130}
131
132// matrix-end := box-br-round | box-br | right-bracket ;
133pub fn matrix_end(input: ParseString) -> ParseResult<Token> {
134  let result = alt((box_br_round, box_br, box_br_bold, right_bracket))(input);
135  result
136}
137
138// Table
139// ----------------------------------------------------------------------------
140
141// table := inline-table | regular-table | fancy-table ;
142fn table(input: ParseString) -> ParseResult<Table> { 
143  alt((inline_table, regular_table, fancy_table))(input)
144}
145
146// fancy-table := table-top, fancy-header, +fancy-row, table-bottom ;
147pub fn fancy_table(input: ParseString) -> ParseResult<Table> {
148  let (input, _) = table_top(input)?;
149  let (input, _) = table_separator(input)?;
150  let (input, header) = fancy_table_header(input)?;
151  let (input, rows) = separated_list1(new_line,alt((table_row2,row_separator)))(input)?;
152  let rows: Vec<TableRow> = rows.into_iter().filter(|row| !row.columns.is_empty()).collect();
153  Ok((input, Table{header, rows}))
154}
155
156// table-header := list1(space-tab+, field), (space | tab)*, (bar| box-vert), whitespace* ;
157pub fn fancy_table_header(input: ParseString) -> ParseResult<Vec<Field>> {
158  let (input, fields) = separated_list1(table_separator,field)(input)?;
159  let (input, _) = table_separator(input)?;
160  let (input, _) = whitespace0(input)?;
161  Ok((input, fields))
162}
163
164// row-separator := *whitespace, *box-drawing-char, *(space | tab), *whitespace ;
165pub fn row_separator(input: ParseString) -> ParseResult<TableRow> {
166  let (input, _) = space_tab0(input)?;
167  let (input, _) = many1(alt((box_drawing_char,table_end,space_tab)))(input)?;
168  let (input, _) = space_tab0(input)?;
169  Ok((input, TableRow{columns: vec![]}))
170}
171
172// table-top := table-start, *box-drawing-char, new-line ;
173fn table_top(input: ParseString) -> ParseResult<()> {
174  let (input, _) = table_start(input)?;
175  let (input, _) = many0(box_drawing_char)(input)?;
176  let (input, _) = new_line(input)?;
177  Ok((input, ()))
178}
179
180// table-bottom := *box-drawing-char, table-end;
181fn table_bottom(input: ParseString) -> ParseResult<()> {
182  let (input, _) = many0(box_drawing_char)(input)?;
183  let (input, _) = table_end(input)?;
184  Ok((input, ()))
185}
186
187// inline-table := table-separator, *whitespace, table-header, *whitespace, +table-row;
188pub fn inline_table(input: ParseString) -> ParseResult<Table> {
189  let (input, _) = table_separator(input)?;
190  let (input, _) = space_tab0(input)?;
191  let (input, header) = inline_table_header(input)?;
192  let (input, _) = space_tab0(input)?;
193  let (input, rows) = many1(inline_table_row)(input)?;
194  Ok((input, Table{header, rows}))
195}
196
197// inline-table-row := *(space | tab), +(*(space | tab), expression) , *(space | tab), table-separator ;
198pub fn inline_table_row(input: ParseString) -> ParseResult<TableRow> {
199  let (input, _) = space_tab0(input)?;
200  let (input, row) = many1(nom_tuple((space_tab0, expression)))(input)?;
201  let (input, _) = space_tab0(input)?;
202  let (input, _) = table_separator(input)?;
203  let row = row.into_iter().map(|(_,tkn)| TableColumn{element:tkn}).collect();
204  Ok((input, TableRow{columns: row}))
205}
206
207// regular-table := table-separator, whitespace*, table-header, +table-row ;
208pub fn regular_table(input: ParseString) -> ParseResult<Table> {
209  let (input, _) = table_separator(input)?;
210  let (input, _) = whitespace0(input)?;
211  let (input, header) = table_header(input)?;
212  let (input, rows) = separated_list1(whitespace0, table_row)(input)?;
213  Ok((input, Table{header,rows}))
214}
215
216// table-header := list1(space-tab+, field), (space | tab)*, (bar| box-vert), whitespace* ;
217pub fn table_header(input: ParseString) -> ParseResult<Vec<Field>> {
218  let (input, fields) = separated_list1(space_tab1,header_field)(input)?;
219  let (input, _) = space_tab0(input)?;
220  let (input, _) = table_separator(input)?;
221  let (input, _) = whitespace0(input)?;
222  Ok((input, fields))
223}
224
225// table-header := list1(space-tab+, field), (space | tab)*, (bar| box-vert), whitespace* ;
226pub fn inline_table_header(input: ParseString) -> ParseResult<Vec<Field>> {
227  let (input, fields) = separated_list1(space_tab1,header_field)(input)?;
228  let (input, _) = space_tab0(input)?;
229  let (input, _) = table_separator(input)?;
230  let (input, _) = space_tab0(input)?;
231  Ok((input, fields))
232}
233
234// table-row := table-separator, *(space | tab), +table-column, *(space | tab), table-separator, ?new-line ;
235pub fn table_row2(input: ParseString) -> ParseResult<TableRow> {
236  let (input, _) = table_separator(input)?;
237  let sep = delimited(space_tab0, table_separator, space_tab0);
238  let (input, cells) = separated_list1(sep, expression)(input)?;
239  let (input, _) = space_tab0(input)?;
240  let (input, _) = table_separator(input)?;
241  let (input, _) = space_tab0(input)?;
242  let columns = cells
243      .into_iter()
244      .map(|expr| TableColumn { element: expr })
245      .collect();
246  Ok((input, TableRow { columns }))
247}
248
249// table-row := table-separator, *(space | tab), +table-column, *(space | tab), table-separator, ?new-line ;
250pub fn table_row(input: ParseString) -> ParseResult<TableRow> {
251  let (input, _) = table_separator(input)?;
252  let (input, cells) = separated_list1(space_tab1, expression)(input)?;
253  let (input, _) = space_tab0(input)?;
254  let (input, _) = table_separator(input)?;
255  let (input, _) = space_tab0(input)?;
256  let columns = cells
257      .into_iter()
258      .map(|expr| TableColumn { element: expr })
259      .collect();
260  Ok((input, TableRow { columns }))
261}
262
263// table-column := *(space | tab), expression, (*(space | tab), ("," | ?table-separator), *(space | tab)) ;
264pub fn table_column(input: ParseString) -> ParseResult<TableColumn> {
265  let (input, _) = space_tab0(input)?;
266  let (input, element) = match expression(input) {
267    Ok(result) => result,
268    Err(err) => {
269      return Err(err);
270    }
271  };
272  let (input, _) = nom_tuple((space_tab0,opt(alt((comma,table_separator))), space_tab0))(input)?;
273  Ok((input, TableColumn{element}))
274}
275
276// field := identifier, kind-annotation? ;
277pub fn header_field(input: ParseString) -> ParseResult<Field> {
278  let (input, name) = identifier(input)?;
279  let (input, kind) = kind_annotation(input)?;
280  Ok((input, Field{name, kind: Some(kind)}))
281}
282
283// field := identifier, kind-annotation? ;
284pub fn field(input: ParseString) -> ParseResult<Field> {
285  let (input, name) = identifier(input)?;
286  let (input, kind) = opt(kind_annotation)(input)?;
287  Ok((input, Field{name, kind}))
288}
289
290// 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 ;
291pub fn box_drawing_char(input: ParseString) -> ParseResult<Token> {
292  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)
293}
294
295// 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 ;
296pub fn box_drawing_emoji(input: ParseString) -> ParseResult<Token> {
297  alt((box_vert_bold, 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)
298}
299
300// table-start := box-tl-round | box-tl | left-brace ;
301pub fn table_start(input: ParseString) -> ParseResult<Token> {
302  alt((box_tl_round, box_tl, box_tl_bold, left_brace, table_separator))(input)
303}
304
305// table-end := box-br-round | box-br | right-brace ;
306pub fn table_end(input: ParseString) -> ParseResult<Token> {
307  let result = alt((box_br_round, box_br, box_br_bold, right_brace, table_separator))(input);
308  result
309}
310
311// table-separator := box_vert ;
312pub fn table_separator(input: ParseString) -> ParseResult<Token> {
313  let (input, _) = space_tab0(input)?;
314  let (input, token) = alt((box_vert,box_vert_bold,bar))(input)?;
315  let (input, _) = space_tab0(input)?;
316  Ok((input, token))
317}
318
319pub fn table_horz(input: ParseString) -> ParseResult<Token> {
320  let (input, token) = alt((dash,box_horz))(input)?;
321  Ok((input, token))
322}
323
324// Map
325// ----------------------------------------------------------------------------
326
327// empty-table := table-start, whitespace*, table-end ;
328pub fn empty_map(input: ParseString) -> ParseResult<Map> {
329  let (input, _) = left_brace(input)?;
330  let (input, _) = whitespace0(input)?;
331  let (input, _) = colon(input)?;
332  let (input, _) = whitespace0(input)?;
333  let (input, _) = right_brace(input)?;
334  Ok((input, Map{elements: vec![]}))
335}
336
337// map := "{", whitespace*, mapping*, whitespace*, "}" ;
338pub fn map(input: ParseString) -> ParseResult<Map> {
339  let msg = "Expects right bracket '}' to terminate inline table";
340  let (input, (_, r)) = range(left_brace)(input)?;
341  let (input, _) = whitespace0(input)?;
342  let (input, elements) = many1(mapping)(input)?;
343  let (input, _) = whitespace0(input)?;
344  let (input, _) = label!(right_brace, msg, r)(input)?;
345  Ok((input, Map{elements}))
346}
347
348// mapping :=  whitespace*, expression, whitespace*, ":", whitespace*, expression, comma?, whitespace* ;
349pub fn mapping(input: ParseString) -> ParseResult<Mapping> {
350  let msg1 = "Unexpected space before colon ':'";
351  let msg2 = "Expects a value";
352  let msg3 = "Expects whitespace or comma followed by whitespace";
353  let msg4 = "Expects whitespace";
354  let (input, _) = whitespace0(input)?;
355  let (input, key) = expression(input)?;
356  let (input, _) = whitespace0(input)?;
357  let (input, _) = colon(input)?;
358  let (input, _) = whitespace0(input)?;
359  let (input, value) = label!(expression, msg2)(input)?;
360  let (input, _) = whitespace0(input)?;
361  let (input, _) = opt(comma)(input)?;
362  let (input, _) = whitespace0(input)?;
363  Ok((input, Mapping{key, value}))
364}
365
366
367// Record
368// ----------------------------------------------------------------------------
369
370// record := table-start, whitespace*, binding+, whitespace*, table_end ;
371pub fn record(input: ParseString) -> ParseResult<Record> {
372  let msg = "Expects right bracket ']' to terminate inline table";
373  let (input, (_, r)) = range(table_start)(input)?;
374  let (input, _) = whitespace0(input)?;
375  let (input, bindings) = many1(binding)(input)?;
376  let (input, _) = whitespace0(input)?;
377  let (input, _) = label!(table_end, msg, r)(input)?;
378  Ok((input, Record{bindings}))
379}
380
381// binding := identifier, kind_annotation?, colon, expression, ","? ;
382pub fn binding(input: ParseString) -> ParseResult<Binding> {
383  let msg1 = "Unexpected space before colon ':'";
384  let msg2 = "Expects a value";
385  let msg3 = "Expects whitespace or comma followed by whitespace";
386  let msg4 = "Expects whitespace";
387  let (input, _) = whitespace0(input)?;
388  let (input, name) = identifier(input)?;
389  let (input, kind) = opt(kind_annotation)(input)?;
390  let (input, _) = whitespace0(input)?;
391  let (input, _) = colon(input)?;
392  let (input, _) = whitespace0(input)?;
393  let (input, value) = label!(expression, msg2)(input)?;
394  let (input, _) = whitespace0(input)?;
395  let (input, _) = opt(comma)(input)?;
396  let (input, _) = whitespace0(input)?;
397  Ok((input, Binding{name, kind, value}))
398}
399
400// Set
401// ----------------------------------------------------------------------------
402
403// empty-set := table-start, whitespace*, empty, whitespace*, table-end ;
404pub fn empty_set(input: ParseString) -> ParseResult<Set> {
405  let (input, _) = left_brace(input)?;
406  let (input, _) = whitespace0(input)?;
407  let (input, _) = opt(empty)(input)?;
408  let (input, _) = whitespace0(input)?;
409  let (input, _) = right_brace(input)?;
410  Ok((input,  Set{elements: vec![]}))
411}
412
413// set := "{", whitespace*, list0(("," | whitespace+), expression), whitespace*, "}" ;
414pub fn set(input: ParseString) -> ParseResult<Set> {
415  let msg = "Expects right bracket '}' to terminate inline table";
416  let (input, (_, r)) = range(left_brace)(input)?;
417  let (input, _) = whitespace0(input)?;
418  let (input, elements) = separated_list1(alt((list_separator,whitespace1)), expression)(input)?;
419  let (input, _) = whitespace0(input)?;
420  let (input, _) = label!(right_brace, msg, r)(input)?;
421  Ok((input, Set{elements}))
422}
423
424// Tuple
425// ----------------------------------------------------------------------------
426
427// tuple := "(", list0(",", expression), ")" ;
428pub fn tuple(input: ParseString) -> ParseResult<Tuple> {
429  let (input, _) = left_parenthesis(input)?;
430  let (input, _) = whitespace0(input)?;
431  let (input, exprs) = separated_list0(list_separator, expression)(input)?;
432  let (input, _) = whitespace0(input)?;
433  let (input, _) = right_parenthesis(input)?;
434  Ok((input, Tuple{elements: exprs}))
435}
436
437// tuple-struct = atom, "(", expression, ")" ;
438pub fn tuple_struct(input: ParseString) -> ParseResult<TupleStruct> {
439  let (input, _) = grave(input)?;
440  let (input, name) = identifier(input)?;
441  let (input, _) = left_parenthesis(input)?;
442  let (input, _) = whitespace0(input)?;
443  let (input, value) = expression(input)?;
444  let (input, _) = whitespace0(input)?;
445  let (input, _) = right_parenthesis(input)?;
446  Ok((input, TupleStruct{name, value: Box::new(value)}))
447}