devalang_core/core/lexer/handler/
string.rs

1use crate::core::lexer::token::{Token, TokenKind};
2
3pub fn handle_string_lexer(
4    ch: char,
5    chars: &mut std::iter::Peekable<std::str::Chars>,
6    current_indent: &mut usize,
7    _indent_stack: &mut [usize],
8    tokens: &mut Vec<Token>,
9    line: &mut usize,
10    column: &mut usize,
11) {
12    let quote_char = ch;
13
14    let start_column = *column;
15    let start_line = *line;
16
17    *column += 1;
18
19    let mut string_content = String::new();
20
21    while let Some(&next_ch) = chars.peek() {
22        if next_ch == quote_char {
23            chars.next();
24            *column += 1;
25            break;
26        } else if next_ch == '\\' {
27            chars.next();
28            *column += 1;
29
30            if let Some(escaped) = chars.next() {
31                match escaped {
32                    'n' => string_content.push('\n'),
33                    't' => string_content.push('\t'),
34                    '\\' => string_content.push('\\'),
35                    '"' => string_content.push('"'),
36                    '\'' => string_content.push('\''),
37                    other => {
38                        string_content.push('\\');
39                        string_content.push(other);
40                    }
41                }
42                *column += 1;
43            }
44        } else {
45            chars.next();
46            if next_ch == '\n' {
47                *line += 1;
48                *column = 1;
49            } else {
50                *column += 1;
51            }
52            string_content.push(next_ch);
53        }
54    }
55
56    tokens.push(Token {
57        kind: TokenKind::String,
58        lexeme: string_content,
59        indent: *current_indent,
60        line: start_line,
61        column: start_column,
62    });
63}