Skip to content

Commit

Permalink
Started preparing for generics by adding ability to parse polymorphs
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Oct 23, 2024
1 parent c777582 commit e56432a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod compound_identifier_state;
mod hex_number_state;
mod identifier_state;
mod number_state;
mod polymorph_state;
mod state;
mod string_state;

Expand All @@ -17,6 +18,7 @@ use crate::{
token::{StringLiteral, StringModifier, Token, TokenKind},
};
use compound_identifier_state::CompoundIdentifierState;
use polymorph_state::PolymorphState;

pub struct Lexer<T: Text + Send> {
characters: T,
Expand All @@ -36,6 +38,7 @@ impl<T: Text + Send> Lexer<T> {
State::Idle => self.feed_idle(),
State::Identifier(_) => self.feed_identifier(),
State::CompoundIdentifier(_) => self.feed_compound_identifier(),
State::Polymorph(_) => self.feed_polymorph(),
State::String(_) => self.feed_string(),
State::Number(_) => self.feed_number(),
State::HexNumber(_) => self.feed_hex_number(),
Expand Down Expand Up @@ -326,6 +329,13 @@ impl<T: Text + Send> Lexer<T> {
});
Waiting
}
'$' => {
self.state = State::Polymorph(PolymorphState {
identifier: String::new(),
start_source: source,
});
Waiting
}
_ if c.is_alphabetic() || c == '_' => {
self.state = State::Identifier(IdentifierState {
identifier: String::from(c),
Expand Down Expand Up @@ -385,6 +395,24 @@ impl<T: Text + Send> Lexer<T> {
}
}

fn feed_polymorph(&mut self) -> FeedResult<Token> {
use FeedResult::*;

let state = self.state.as_mut_polymorph();

match self.characters.peek() {
Character::At(c, _) if c.is_alphabetic() || c.is_ascii_digit() || c == '_' => {
state.identifier.push(self.characters.next().unwrap().0);
Waiting
}
_ => {
let token = state.finalize();
self.state = State::Idle;
Has(token)
}
}
}

fn feed_string(&mut self) -> FeedResult<Token> {
use FeedResult::*;

Expand Down
16 changes: 16 additions & 0 deletions src/lexer/polymorph_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::{
source_files::Source,
token::{Token, TokenKind},
};

pub struct PolymorphState {
pub identifier: String,
pub start_source: Source,
}

impl PolymorphState {
pub fn finalize(&mut self) -> Token {
let identifier = std::mem::take(&mut self.identifier);
TokenKind::Polymorph(identifier).at(self.start_source)
}
}
11 changes: 10 additions & 1 deletion src/lexer/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{
compound_identifier_state::CompoundIdentifierState, hex_number_state::HexNumberState,
identifier_state::IdentifierState, number_state::NumberState, string_state::StringState,
identifier_state::IdentifierState, number_state::NumberState, polymorph_state::PolymorphState,
string_state::StringState,
};
use derive_more::Unwrap;

Expand All @@ -9,6 +10,7 @@ pub enum State {
Idle,
Identifier(IdentifierState),
CompoundIdentifier(CompoundIdentifierState),
Polymorph(PolymorphState),
String(StringState),
Number(NumberState),
HexNumber(HexNumberState),
Expand All @@ -29,6 +31,13 @@ impl State {
}
}

pub fn as_mut_polymorph(&mut self) -> &mut PolymorphState {
match self {
State::Polymorph(polymorph) => polymorph,
_ => panic!(),
}
}

pub fn as_mut_string(&mut self) -> &mut StringState {
match self {
State::String(state) => state,
Expand Down
3 changes: 3 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub enum TokenKind {
Error(String),
Newline,
Identifier(String),
Polymorph(String),
NamespacedIdentifier(Name),
OpenCurly,
CloseCurly,
Expand Down Expand Up @@ -139,6 +140,7 @@ impl Display for TokenKind {
TokenKind::Error(message) => write!(f, "'lex error - {}'", message),
TokenKind::Newline => f.write_str("'newline'"),
TokenKind::Identifier(name) => write!(f, "(identifier) '{}'", name),
TokenKind::Polymorph(name) => write!(f, "'${}'", name),
TokenKind::NamespacedIdentifier(name) => write!(
f,
"(namespaced identifier) '{}{}'",
Expand Down Expand Up @@ -272,6 +274,7 @@ impl TokenKind {
| TokenKind::Error(_)
| TokenKind::Newline
| TokenKind::Identifier(_)
| TokenKind::Polymorph(_)
| TokenKind::NamespacedIdentifier(_)
| TokenKind::CloseCurly
| TokenKind::OpenParen
Expand Down

0 comments on commit e56432a

Please sign in to comment.