-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Progress on sublexers and new tokens, fighting borrow rules #56
- Loading branch information
Showing
5 changed files
with
170 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ pub mod typ; | |
pub mod code_lexer; | ||
|
||
pub mod comment_lexer; | ||
|
||
pub mod string_lexer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use mango::io::typ::Reader; | ||
use mango::io::typ::ReaderResult::*; | ||
use mango::lexing::typ::Lexer; | ||
use mango::lexing::typ::MaybeToken; | ||
use mango::token::tokens::LiteralToken; | ||
use mango::token::Tokens; | ||
|
||
pub enum StringType { | ||
SingleQuotedInline, | ||
DoubleQuotedInline, | ||
MultiLine, | ||
} | ||
|
||
/// Lexes a string literal token. | ||
// Starts after the opening quote and expected to consume until closing quote. | ||
pub struct StringLexer { | ||
reader: Box<Reader>, | ||
typ: StringType, | ||
} | ||
|
||
impl StringLexer { | ||
// TODO: support other types of strings | ||
pub fn new_double_quoted(reader: Box<Reader>) -> Self { | ||
StringLexer { | ||
reader, | ||
typ: StringType::DoubleQuotedInline, | ||
} | ||
} | ||
} | ||
|
||
impl Lexer for StringLexer { | ||
fn lex(&mut self) -> MaybeToken { | ||
// TODO: doesn't handle escaping etc at all now | ||
// TODO: this is going to have a problem if `matches` automatically eats whitespace | ||
match self.reader.matches("[^\"\\n]*") { | ||
Match(value) => return MaybeToken::Token(Tokens::Literal(LiteralToken::string(value))), | ||
NoMatch() => panic!("failed to parse string"), // This can't really go wrong since empty pattern matches | ||
EOF() => return MaybeToken::Token(Tokens::Literal(LiteralToken::string("".to_owned()))), // Unclosed string literal, let code parser deal with it | ||
} | ||
} | ||
|
||
fn consume(self) -> Box<Reader> { | ||
self.reader | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
use mango::io::typ::Reader; | ||
use mango::token::Tokens; | ||
|
||
pub enum MaybeToken { | ||
Token(Tokens), | ||
End(), | ||
End, | ||
} | ||
|
||
pub trait Lexer<'r> { | ||
pub trait Lexer { | ||
// /// Create a new lexer from a reader instance. | ||
// fn new(reader: &'r mut Reader) -> Self; | ||
|
||
/// Every call to lex returns a token until the end of the input. | ||
fn lex(&mut self) -> MaybeToken; | ||
|
||
fn consume(self) -> Box<Reader>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters