devalang_core/core/lexer/handler/
arrow.rs

1use crate::core::lexer::token::{Token, TokenKind};
2
3pub fn handle_arrow_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    // If next char is '>', this is an arrow '->'.
13    if let Some(&c) = chars.peek() {
14        if c == '>' {
15            let mut arrow_call = ch.to_string();
16            chars.next();
17            arrow_call.push(c);
18            *column += 1;
19
20            tokens.push(Token {
21                kind: TokenKind::Arrow,
22                lexeme: arrow_call,
23                line: *line,
24                column: *column,
25                indent: *current_indent,
26            });
27            return;
28        }
29    }
30
31    // Otherwise, treat '-' as the start of a negative number if followed by digits.
32    let mut lexeme = String::from("-");
33    if let Some(&next) = chars.peek() {
34        if next.is_ascii_digit() {
35            // consume digits
36            while let Some(&d) = chars.peek() {
37                if d.is_ascii_digit() {
38                    chars.next();
39                    lexeme.push(d);
40                    *column += 1;
41                } else {
42                    break;
43                }
44            }
45            // optional decimal part
46            if let Some(&dot) = chars.peek() {
47                if dot == '.' {
48                    chars.next();
49                    lexeme.push(dot);
50                    *column += 1;
51                    while let Some(&d) = chars.peek() {
52                        if d.is_ascii_digit() {
53                            chars.next();
54                            lexeme.push(d);
55                            *column += 1;
56                        } else {
57                            break;
58                        }
59                    }
60                }
61            }
62
63            tokens.push(Token {
64                kind: TokenKind::Number,
65                lexeme,
66                line: *line,
67                column: *column,
68                indent: *current_indent,
69            });
70            return;
71        }
72    }
73
74    // Fallback: lone '-' not part of '->' or a number; emit Unknown to avoid mis-parsing as Arrow
75    tokens.push(Token {
76        kind: TokenKind::Unknown,
77        lexeme: "-".to_string(),
78        line: *line,
79        column: *column,
80        indent: *current_indent,
81    });
82}