diff options
| author | Mike Buland <mike@xagasoft.com> | 2026-06-05 11:28:07 -0700 |
|---|---|---|
| committer | Mike Buland <mike@xagasoft.com> | 2026-06-05 11:28:07 -0700 |
| commit | 11f5bc0c5a8bcfb6ac8fef8abc33297c4addc81b (patch) | |
| tree | c5d08d320c1cd280ce9ba1f59c0385808a036e67 /src/lexer.rs | |
| parent | 414681bc8b8ccbff81c0878fbf3ff193f7f55e0f (diff) | |
| download | crimtag-11f5bc0c5a8bcfb6ac8fef8abc33297c4addc81b.tar.gz crimtag-11f5bc0c5a8bcfb6ac8fef8abc33297c4addc81b.tar.bz2 crimtag-11f5bc0c5a8bcfb6ac8fef8abc33297c4addc81b.tar.xz crimtag-11f5bc0c5a8bcfb6ac8fef8abc33297c4addc81b.zip | |
Started on the parser. Thinking about some tweaks.
- Creating a LookaheadIterator as a general class that could be used.
- Wrap symbols in a symbol struct that tracks symbol start and end
position for error reporting.
- Make a struct for position in a file as well.
Diffstat (limited to 'src/lexer.rs')
| -rw-r--r-- | src/lexer.rs | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/lexer.rs b/src/lexer.rs index e80e461..94825b2 100644 --- a/src/lexer.rs +++ b/src/lexer.rs | |||
| @@ -2,7 +2,12 @@ use std::iter::Iterator; | |||
| 2 | //use core::error::Error; | 2 | //use core::error::Error; |
| 3 | use std::str::CharIndices; | 3 | use std::str::CharIndices; |
| 4 | 4 | ||
| 5 | #[derive(Debug)] | 5 | #[derive(Debug,Copy,Clone)] |
| 6 | pub enum ErrorType { | ||
| 7 | UnexpectedChar(char), | ||
| 8 | } | ||
| 9 | |||
| 10 | #[derive(Debug,Copy,Clone)] | ||
| 6 | pub enum Symbol<'a> { | 11 | pub enum Symbol<'a> { |
| 7 | StartFlat, | 12 | StartFlat, |
| 8 | StartPoint, | 13 | StartPoint, |
| @@ -15,7 +20,7 @@ pub enum Symbol<'a> { | |||
| 15 | Token(&'a str), | 20 | Token(&'a str), |
| 16 | Literal(&'a str), | 21 | Literal(&'a str), |
| 17 | Text(&'a str), | 22 | Text(&'a str), |
| 18 | Error{ line: u32, row: u32, what: String }, | 23 | Error{ line: u32, row: u32, what: ErrorType }, |
| 19 | EOS, | 24 | EOS, |
| 20 | } | 25 | } |
| 21 | 26 | ||
| @@ -104,7 +109,7 @@ impl<'a> Lexer<'a> { | |||
| 104 | } | 109 | } |
| 105 | } | 110 | } |
| 106 | 111 | ||
| 107 | fn error( &self, what: String ) -> Option<Symbol<'a>> { | 112 | fn error( &self, what: ErrorType ) -> Option<Symbol<'a>> { |
| 108 | Some(Symbol::Error { | 113 | Some(Symbol::Error { |
| 109 | line: self.line, | 114 | line: self.line, |
| 110 | row: self.row, | 115 | row: self.row, |
| @@ -162,6 +167,7 @@ impl<'a> Lexer<'a> { | |||
| 162 | return self.parse_literal_str(); | 167 | return self.parse_literal_str(); |
| 163 | } | 168 | } |
| 164 | Some('=') => { | 169 | Some('=') => { |
| 170 | self.next(); | ||
| 165 | return Some(Symbol::Equals); | 171 | return Some(Symbol::Equals); |
| 166 | } | 172 | } |
| 167 | _ => {} | 173 | _ => {} |
| @@ -186,7 +192,7 @@ impl<'a> Lexer<'a> { | |||
| 186 | 192 | ||
| 187 | fn parse_literal_str(&mut self) -> Option<Symbol<'a>> { | 193 | fn parse_literal_str(&mut self) -> Option<Symbol<'a>> { |
| 188 | if let Some(chr) = self.cur() && chr != '"' { | 194 | if let Some(chr) = self.cur() && chr != '"' { |
| 189 | return self.error(format!("Expected '\"' but found '{}'", chr)); | 195 | return self.error( ErrorType::UnexpectedChar(chr) ); |
| 190 | } | 196 | } |
| 191 | let start = self.peek_index(); | 197 | let start = self.peek_index(); |
| 192 | while self.next().is_some_and(|chr| chr != '"') { } | 198 | while self.next().is_some_and(|chr| chr != '"') { } |
