From 0e0e54b604f10558d3e6c4e522749c708bf3bacf Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Thu, 16 Aug 2018 19:38:04 +0100 Subject: [PATCH] added op! and vop! --- src/lib/syntax/lexer.rs | 49 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/src/lib/syntax/lexer.rs b/src/lib/syntax/lexer.rs index c1ed43d090..ea5fe15ecb 100644 --- a/src/lib/syntax/lexer.rs +++ b/src/lib/syntax/lexer.rs @@ -9,16 +9,49 @@ use syntax::ast::token::{Token, TokenData}; #[allow(unused)] macro_rules! vop { - ($this:ident, $assign_op:expr, $op:expr) => { + ($this:ident, $assign_op:expr, $op:expr) => ({ let preview = $this.preview_next()?; match preview { '=' => { $this.next()?; - $assign_op; + $assign_op } _ => $op, } - }; + }); + ($this:ident, $assign_op:expr, $op:expr, {$($case:pat => $block:expr), +}) => ({ + let preview = $this.preview_next()?; + match preview { + '=' => { + $this.next()?; + $assign_op + }, + $($case => $block)+, + _ => $op + } + }); + ($this:ident, $op:expr, {$($case:pat => $block:expr),+}) => { + let preview = $this.preview_next()?; + match preview { + $($case => $block) +, + _ => $op + } + } +} + +macro_rules! op { + ($this:ident, $assign_op:expr, $op:expr) => ({ + let punc = vop!($this, $assign_op, $op); + $this.push_punc(punc); + }); + ($this:ident, $assign_op:expr, $op:expr, {$($case:pat => $block:expr),+}) => ({ + let punc = vop!($this, $assign_op, $op, {$($case => $block),+}); + $this.push_punc(punc); + }); + ($this:ident, $op:expr, {$($case:pat => $block:expr),+}) => ({ + let punc = vop!($this, $op, {$($case => $block),+}); + $this.push_punc(); + }); } // Defining an error type @@ -287,6 +320,7 @@ impl<'a> Lexer<'a> { '[' => self.push_punc(Punctuator::OpenBracket), ']' => self.push_punc(Punctuator::CloseBracket), '?' => self.push_punc(Punctuator::Question), + // Comments '/' => { let token = match self.preview_next()? { // Matched comment @@ -315,6 +349,15 @@ impl<'a> Lexer<'a> { }; self.push_token(token) } + '*' => op!(self, Punctuator::AssignMul, Punctuator::Mul), + '+' => op!(self, Punctuator::AssignAdd, Punctuator::Add, { + '+' => Punctuator::Inc + }), + '%' => op!(self, Punctuator::AssignMod, Punctuator::Mod), + '|' => op!(self, Punctuator::AssignOr, Punctuator::Or, { + '|' => Punctuator::BoolOr + }), + ch => panic!( "{}:{}: Unexpected '{}'", self.line_number, self.column_number, ch