Parse into statements
(source: &str)
| 9 | |
| 10 | /// Parse into statements |
| 11 | pub(crate) fn parse_source(source: &str) -> Result<Vec<Stmt>, Vec<Error>> { |
| 12 | let tokens = crate::lexer::lex_source(source)?; |
| 13 | |
| 14 | // Filter out comments |
| 15 | let semantic_tokens: Vec<_> = tokens |
| 16 | .0 |
| 17 | .into_iter() |
| 18 | .filter(|token| { |
| 19 | !matches!( |
| 20 | token.kind, |
| 21 | crate::lexer::lr::TokenKind::Comment(_) | crate::lexer::lr::TokenKind::LineWrap(_) |
| 22 | ) |
| 23 | }) |
| 24 | .collect(); |
| 25 | |
| 26 | let input = semantic_tokens |
| 27 | .as_slice() |
| 28 | .map_span(|simple_span: SimpleSpan| { |
| 29 | let start_idx = simple_span.start(); |
| 30 | let end_idx = simple_span.end(); |
| 31 | |
| 32 | let start = semantic_tokens |
| 33 | .get(start_idx) |
| 34 | .map(|t| t.span.start) |
| 35 | .unwrap_or(0); |
| 36 | let end = semantic_tokens |
| 37 | .get(end_idx.saturating_sub(1)) |
| 38 | .map(|t| t.span.end) |
| 39 | .unwrap_or(start); |
| 40 | |
| 41 | Span { |
| 42 | start, |
| 43 | end, |
| 44 | source_id: 0, |
| 45 | } |
| 46 | }); |
| 47 | |
| 48 | let parse_result = stmt::source().parse(input); |
| 49 | let (ast, parse_errors) = parse_result.into_output_errors(); |
| 50 | |
| 51 | if !parse_errors.is_empty() { |
| 52 | log::info!("ast: {ast:?}"); |
| 53 | return Err(parse_errors.into_iter().map(|e| e.into()).collect()); |
| 54 | } |
| 55 | Ok(ast.unwrap()) |
| 56 | } |
| 57 | |
| 58 | #[test] |
| 59 | fn test_error_unicode_string() { |
no test coverage detected