Skip to content

Commit

Permalink
refactor: create seperate method to check duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
PraneshASP committed Sep 7, 2023
1 parent 5579da9 commit c8ffae5
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions huff_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit c8ffae5

Please sign in to comment.