From d3a555b14627f4d886c3fe99b9afdb4213e57a72 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 1 Jun 2018 21:51:27 +0200 Subject: [PATCH] Add operator lexing #52 --- src/mango/lexing/code_lexer.rs | 11 +++++++++-- src/mango/token/tokens/operator.rs | 4 ++++ src/mango/util/codeparts/operator.rs | 5 +++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/mango/lexing/code_lexer.rs b/src/mango/lexing/code_lexer.rs index 41af68c9..5574f253 100644 --- a/src/mango/lexing/code_lexer.rs +++ b/src/mango/lexing/code_lexer.rs @@ -8,6 +8,7 @@ use mango::token::tokens::EndBlockToken; use mango::token::tokens::EndStatementToken; use mango::token::tokens::IdentifierToken; use mango::token::tokens::KeywordToken; +use mango::token::tokens::OperatorToken; use mango::token::tokens::ParenthesisCloseToken; use mango::token::tokens::ParenthesisOpenToken; use mango::token::tokens::StartBlockToken; @@ -134,7 +135,7 @@ impl Lexer for CodeLexer { // // Indentation done; do the rest of lexing. // - // Parse identifers and keywords. This assumes that keywords are a subset of identifiers. + // Parse identifiers and keywords. This assumes that keywords are a subset of identifiers. if let Match(word) = self .reader .borrow_mut() @@ -155,7 +156,13 @@ impl Lexer for CodeLexer { return self.lex(); } // Operator - // todo + let operator_match_res = self + .reader + .borrow_mut() + .matches(OperatorToken::subpattern()); + if let Match(token) = operator_match_res { + return Token(Tokens::Operator(OperatorToken::from_str(&token).unwrap())); + } // Association // todo // Grouping symbols diff --git a/src/mango/token/tokens/operator.rs b/src/mango/token/tokens/operator.rs index eb19db24..4515887e 100644 --- a/src/mango/token/tokens/operator.rs +++ b/src/mango/token/tokens/operator.rs @@ -34,6 +34,10 @@ impl OperatorToken { pub fn is_mult_div(&self) -> bool { self.symbol == Symbol::Asterisk || self.symbol == Symbol::Slash } + + pub fn subpattern() -> &'static str { + Symbol::subpattern() + } } impl ToText for OperatorToken { diff --git a/src/mango/util/codeparts/operator.rs b/src/mango/util/codeparts/operator.rs index 00dae4f0..71d37b33 100644 --- a/src/mango/util/codeparts/operator.rs +++ b/src/mango/util/codeparts/operator.rs @@ -27,6 +27,11 @@ impl Symbol { ))), } } + + /// Generate an eager subpattern to match tokens, that can be composed in a regular expression. + pub fn subpattern() -> &'static str { + r"(\+|\-|\*|\/)" + } } impl Display for Symbol {