From 39ac6bf53b432be896f457ce4b38f4f4a9e44139 Mon Sep 17 00:00:00 2001 From: PraneshASP Date: Fri, 8 Sep 2023 01:22:37 +0800 Subject: [PATCH] refactor: create seperate method to check duplicates --- huff_parser/src/lib.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/huff_parser/src/lib.rs b/huff_parser/src/lib.rs index 709bc691..22b122c5 100644 --- a/huff_parser/src/lib.rs +++ b/huff_parser/src/lib.rs @@ -104,13 +104,7 @@ impl Parser { TokenKind::Macro | TokenKind::Fn | TokenKind::Test => { let m = self.parse_macro()?; tracing::info!(target: "parser", "SUCCESSFULLY PARSED MACRO {}", m.name); - if contract.macros.iter().any(|existing| existing.name == m.name) { - return Err(ParserError { - kind: ParserErrorKind::DuplicateMacro(m.name), - hint: Some("MACRO names should be unique".to_string()), - spans: AstSpan(vec![self.spans[2].clone()]), - }) - } + self.check_duplicate_macro(&mut contract, m.clone())?; contract.macros.push(m); } TokenKind::JumpTable | TokenKind::JumpTablePacked | TokenKind::CodeTable => { @@ -192,6 +186,23 @@ impl Parser { std::mem::discriminant(&self.current_token.kind) == std::mem::discriminant(&kind) } + /// Checks if there is a duplicate macro name + pub fn check_duplicate_macro( + &self, + contract: &mut Contract, + m: MacroDefinition, + ) -> Result<(), ParserError> { + if contract.macros.binary_search_by(|_macro| _macro.name.cmp(&m.name)).is_ok() { + return Err(ParserError { + kind: ParserErrorKind::DuplicateMacro(m.name), + hint: Some("MACRO names should be unique".to_string()), + spans: AstSpan(vec![self.spans[2].clone()]), + }) + } else { + Ok(()) + } + } + /// Consumes the next token. pub fn consume(&mut self) { self.spans.push(self.current_token.span.clone());