Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posts/pl/lexer/ #36

Open
utterances-bot opened this issue Nov 22, 2022 · 2 comments
Open

posts/pl/lexer/ #36

utterances-bot opened this issue Nov 22, 2022 · 2 comments

Comments

@utterances-bot
Copy link

Starting with a Lexer - in Rust

Building a lexer in Rust, following the book 'Writing an Interpreter'

https://mohitkarekar.com/posts/pl/lexer/

Copy link

read_identifier and read_digit have a bug. It's not while self.read_position < self.input.len(), it's while self.read_position <= self.input.len(). Testing something like 1 + 1 will show the bug.

Copy link

I think Vec<char> is an interesting approach vs str.chars however storing a Vec<char> inside of a variant is needless complexity and a waste of allocation. Instead store a slice of the source array. While you're at it you might as well go use str.char_indicies and just take &self.input[start..stop] as that would need no allocation at all and could be generalized across a token struct rather than only being part of select variants.

enum TokenKind {
  Identifier,
  Plus,
  Minus,
  ...
}

struct Token<'s> {
  kind: TokenKind,
  text: &'source str
}

struct Lexer<'s> {
  source: &'s str,
  chars: Peekable<CharIndicies<'s>>,
  current: Option<(usize, usize)>
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants