(
tokens: &[Token],
position: &mut usize,
)
| 5 | use crate::token::{Token, TokenKind}; |
| 6 | |
| 7 | pub(crate) fn parse_into_statement( |
| 8 | tokens: &[Token], |
| 9 | position: &mut usize, |
| 10 | ) -> Result<Statement, Box<Diagnostic>> { |
| 11 | // Consume `INTO` keyword |
| 12 | *position += 1; |
| 13 | |
| 14 | // Make sure user define explicitly the into type |
| 15 | if *position >= tokens.len() |
| 16 | || (tokens[*position].kind != TokenKind::Outfile |
| 17 | && tokens[*position].kind != TokenKind::Dumpfile) |
| 18 | { |
| 19 | return Err(Diagnostic::error( |
| 20 | "Expect Keyword `OUTFILE` or `DUMPFILE` after keyword `INTO`", |
| 21 | ) |
| 22 | .with_location(calculate_safe_location(tokens, *position)) |
| 23 | .as_boxed()); |
| 24 | } |
| 25 | |
| 26 | // Consume `OUTFILE` or `DUMPFILE` keyword |
| 27 | let file_format_kind = &tokens[*position].kind; |
| 28 | *position += 1; |
| 29 | |
| 30 | // Make sure user defined a file path as string literal |
| 31 | if *position >= tokens.len() || !matches!(tokens[*position].kind, TokenKind::String(_)) { |
| 32 | return Err(Diagnostic::error( |
| 33 | "Expect String literal as file path after OUTFILE or DUMPFILE keyword", |
| 34 | ) |
| 35 | .with_location(calculate_safe_location(tokens, *position)) |
| 36 | .as_boxed()); |
| 37 | } |
| 38 | |
| 39 | let file_path = &tokens[*position].to_string(); |
| 40 | |
| 41 | // Consume File path token |
| 42 | *position += 1; |
| 43 | |
| 44 | let is_dump_file = *file_format_kind == TokenKind::Dumpfile; |
| 45 | |
| 46 | let mut lines_terminated = if is_dump_file { "" } else { "\n" }.to_string(); |
| 47 | let mut lines_terminated_used = false; |
| 48 | |
| 49 | let mut fields_terminated = if is_dump_file { "" } else { "," }.to_string(); |
| 50 | let mut fields_terminated_used = false; |
| 51 | |
| 52 | let mut enclosed = String::new(); |
| 53 | let mut enclosed_used = false; |
| 54 | |
| 55 | while *position < tokens.len() { |
| 56 | let token = &tokens[*position]; |
| 57 | |
| 58 | if token.kind == TokenKind::Lines { |
| 59 | if is_dump_file { |
| 60 | return Err(Diagnostic::error( |
| 61 | "`LINES TERMINATED` option can't be used with INTO DUMPFILE", |
| 62 | ) |
| 63 | .add_help("To customize the format replace `DUMPFILE` with `OUTFILE` option") |
| 64 | .with_location(tokens[*position].location) |
no test coverage detected
searching dependent graphs…