From 15e275513d762fd590d718e6e1b3e6bd3ee78034 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 6 Sep 2018 17:21:30 -0700 Subject: [PATCH] [lexer] Pass Builders around by reference instead of implicitly Copying --- lexer/src/states/begin.rs | 2 +- lexer/src/states/hash.rs | 2 +- lexer/src/states/number/digit.rs | 2 +- lexer/src/states/number/mod.rs | 2 +- lexer/src/states/number/prefix.rs | 16 +++++++++++----- lexer/src/states/number/sign.rs | 2 +- 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lexer/src/states/begin.rs b/lexer/src/states/begin.rs index 4231cc6..c1bdec4 100644 --- a/lexer/src/states/begin.rs +++ b/lexer/src/states/begin.rs @@ -35,7 +35,7 @@ impl State for Begin { StateResult::advance(Box::new(IdSub{})) } else if c.is_hash() { StateResult::advance(Box::new(Hash::new())) - } else if let Some(st) = Digit::with_char(Builder::new(), c) { + } else if let Some(st) = Digit::with_char(&Builder::new(), c) { StateResult::advance(Box::new(st)) } else { StateResult::fail(Error::invalid_char(c)) diff --git a/lexer/src/states/hash.rs b/lexer/src/states/hash.rs index 80f7103..cd96cd3 100644 --- a/lexer/src/states/hash.rs +++ b/lexer/src/states/hash.rs @@ -28,7 +28,7 @@ impl State for Hash { StateResult::advance(Box::new(Bool::new(buf.as_str()))) }, c if c.is_radix() || c.is_exactness() => { - if let Some(st) = Prefix::with_char(Builder::new(), c) { + if let Some(st) = Prefix::with_char(&Builder::new(), c) { StateResult::advance(Box::new(st)) } else { StateResult::fail(Error::new(format!("invalid numeric prefix character: {}", c))) diff --git a/lexer/src/states/number/digit.rs b/lexer/src/states/number/digit.rs index 26e9493..d2823c0 100644 --- a/lexer/src/states/number/digit.rs +++ b/lexer/src/states/number/digit.rs @@ -15,7 +15,7 @@ impl Digit { Digit(b) } - pub fn with_char(b: Builder, c: char) -> Option { + pub fn with_char(b: &Builder, c: char) -> Option { let mut b = b.clone(); if !b.seen_radix() { b.push_radix(Radix::Dec); diff --git a/lexer/src/states/number/mod.rs b/lexer/src/states/number/mod.rs index 933fca4..9852460 100644 --- a/lexer/src/states/number/mod.rs +++ b/lexer/src/states/number/mod.rs @@ -20,7 +20,7 @@ pub enum Sign { Neg = -1, Pos = 1 } #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum Exact { Yes, No } -#[derive(Copy, Clone, Debug)] +#[derive(Clone, Debug)] pub struct Builder { radix: Option, sign: Option, diff --git a/lexer/src/states/number/prefix.rs b/lexer/src/states/number/prefix.rs index df6fe87..001abd2 100644 --- a/lexer/src/states/number/prefix.rs +++ b/lexer/src/states/number/prefix.rs @@ -18,7 +18,7 @@ impl Prefix { Prefix(b) } - pub fn with_char(b: Builder, c: char) -> Option { + pub fn with_char(b: &Builder, c: char) -> Option { if let Some(ex) = Exact::from(c) { if b.seen_exact() { return None; @@ -42,10 +42,10 @@ impl Prefix { impl State for Prefix { fn lex(&mut self, c: char) -> StateResult { if c.is_hash() { - StateResult::advance(Box::new(Hash(self.0))) - } else if let Some(st) = Sign::with_char(self.0, c) { + StateResult::advance(Box::new(Hash::new(&self.0))) + } else if let Some(st) = Sign::with_char(&self.0, c) { StateResult::advance(Box::new(st)) - } else if let Some(st) = Digit::with_char(self.0, c) { + } else if let Some(st) = Digit::with_char(&self.0, c) { StateResult::advance(Box::new(st)) } else { StateResult::fail(Error::invalid_char(c)) @@ -57,9 +57,15 @@ impl State for Prefix { } } +impl Hash { + fn new(b: &Builder) -> Hash { + Hash(b.clone()) + } +} + impl State for Hash { fn lex(&mut self, c: char) -> StateResult { - if let Some(st) = Prefix::with_char(self.0, c) { + if let Some(st) = Prefix::with_char(&self.0, c) { StateResult::advance(Box::new(st)) } else { StateResult::fail(Error::invalid_char(c)) diff --git a/lexer/src/states/number/sign.rs b/lexer/src/states/number/sign.rs index 11310aa..6e5d1a4 100644 --- a/lexer/src/states/number/sign.rs +++ b/lexer/src/states/number/sign.rs @@ -15,7 +15,7 @@ impl Sign { Sign(b) } - pub fn with_char(b: Builder, c: char) -> Option { + pub fn with_char(b: &Builder, c: char) -> Option { if !b.seen_sign() { match c { '+' => {