Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
feat: show cursor on parser error (#313)
Browse files Browse the repository at this point in the history
* feat: show cursor on parser error

fix #312

* fix: merge

* chore: fmt

---------

Co-authored-by: Maddiaa0 <[email protected]>
  • Loading branch information
Mouradif and Maddiaa0 committed Apr 21, 2024
1 parent c944050 commit 4c4ae27
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 26 deletions.
7 changes: 7 additions & 0 deletions huff_core/tests/parser_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn test_invalid_macro_statement() {
kind: ParserErrorKind::InvalidTokenInMacroBody(TokenKind::FreeStoragePointer),
hint: None,
spans: AstSpan(vec![Span { start: const_start, end: const_end, file: None }]),
cursor: 36,
}
)
}
Expand Down Expand Up @@ -72,6 +73,7 @@ fn test_unexpected_type() {
end: source.find("internal").unwrap_or(0) + "internal".len() - 1,
file: None
}]),
cursor: 5,
}
)
}
Expand Down Expand Up @@ -103,6 +105,7 @@ fn test_invalid_definition() {
end: source.find("invalid").unwrap_or(0) + "invalid".len() - 1,
file: None
}]),
cursor: 1,
}
)
}
Expand Down Expand Up @@ -147,6 +150,7 @@ fn test_invalid_constant_value() {
end: source.find(value).unwrap_or(0) + value.len() - 1,
file: None
}]),
cursor: 4
}
)
}
Expand Down Expand Up @@ -191,6 +195,7 @@ fn test_invalid_token_in_macro_body() {
end: source.rfind(value).unwrap_or(0) + value.len() - 1,
file: None
}]),
cursor: 15,
}
)
}
Expand Down Expand Up @@ -236,6 +241,7 @@ fn test_invalid_token_in_label_definition() {
end: source.rfind(value).unwrap_or(0) + value.len() - 1,
file: None
}]),
cursor: 17,
}
)
}
Expand Down Expand Up @@ -279,6 +285,7 @@ fn test_invalid_single_arg() {
))),
hint: Some("Expected number representing stack item count.".to_string()),
spans: AstSpan(vec![Span { start: 34, end: 34, file: None }]),
cursor: 8,
}
)
}
Expand Down
42 changes: 37 additions & 5 deletions huff_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ impl Parser {
kind: ParserErrorKind::InvalidDefinition(self.current_token.kind.clone()),
hint: Some("Definition must be one of: `function`, `event`, `constant`, `error`, `macro`, `fn`, or `test`.".to_string()),
spans: AstSpan(vec![self.current_token.span.clone()]),
cursor: self.cursor,
});
}
};
Expand All @@ -133,6 +134,7 @@ impl Parser {
TokenKind::Include
)),
spans: AstSpan(self.spans.clone()),
cursor: self.cursor,
});
}
}
Expand All @@ -158,6 +160,7 @@ impl Parser {
kind: ParserErrorKind::InvalidName(tok.clone()),
hint: Some(format!("Expected import string. Got: \"{tok}\"")),
spans: AstSpan(new_spans),
cursor: self.cursor,
});
}
};
Expand All @@ -177,6 +180,7 @@ impl Parser {
kind: ParserErrorKind::UnexpectedType(self.current_token.kind.clone()),
hint: Some(format!("Expected: \"{kind}\"")),
spans: AstSpan(self.spans.clone()),
cursor: self.cursor,
})
}
}
Expand All @@ -198,6 +202,7 @@ impl Parser {
kind: ParserErrorKind::DuplicateMacro(m.name.to_owned()),
hint: Some("MACRO names should be unique".to_string()),
spans: AstSpan(vec![m.span[2].clone()]),
cursor: self.cursor,
})
} else {
Ok(())
Expand Down Expand Up @@ -257,6 +262,7 @@ impl Parser {
kind: ParserErrorKind::InvalidName(tok.clone()),
hint: Some(format!("Expected function name, found: \"{tok}\"")),
spans: AstSpan(self.spans.clone()),
cursor: self.cursor,
});
}
};
Expand All @@ -276,6 +282,7 @@ impl Parser {
"Expected one of: `view`, `pure`, `payable`, `nonpayable`.".to_string(),
),
spans: AstSpan(vec![self.current_token.span.clone()]),
cursor: self.cursor,
})
}
};
Expand Down Expand Up @@ -319,6 +326,7 @@ impl Parser {
kind: ParserErrorKind::InvalidName(tok.clone()),
hint: Some(format!("Expected event name, found: \"{tok}\"")),
spans: AstSpan(self.spans.clone()),
cursor: self.cursor,
});
}
};
Expand Down Expand Up @@ -350,6 +358,7 @@ impl Parser {
kind: ParserErrorKind::UnexpectedType(tok),
hint: Some("Expected constant name.".to_string()),
spans: AstSpan(self.spans.clone()),
cursor: self.cursor,
});
}
};
Expand All @@ -375,6 +384,7 @@ impl Parser {
.to_string(),
),
spans: AstSpan(vec![self.current_token.span.clone()]),
cursor: self.cursor,
});
}
};
Expand Down Expand Up @@ -403,6 +413,7 @@ impl Parser {
kind: ParserErrorKind::UnexpectedType(tok),
hint: Some("Expected error name.".to_string()),
spans: AstSpan(self.spans.clone()),
cursor: self.cursor,
});
}
};
Expand Down Expand Up @@ -450,6 +461,7 @@ impl Parser {
),
hint: Some(format!("Expected string for decorator flag: {s}")),
spans: AstSpan(vec![self.current_token.span.clone()]),
cursor: self.cursor,
});
}
}
Expand All @@ -466,6 +478,7 @@ impl Parser {
),
hint: Some(format!("Expected literal for decorator flag: {s}")),
spans: AstSpan(vec![self.current_token.span.clone()]),
cursor: self.cursor,
});
}
}
Expand All @@ -475,6 +488,7 @@ impl Parser {
kind: ParserErrorKind::InvalidDecoratorFlag(s.clone()),
hint: Some(format!("Unknown decorator flag: {s}")),
spans: AstSpan(self.spans.clone()),
cursor: self.cursor,
});
}
}
Expand All @@ -493,6 +507,7 @@ impl Parser {
)),
hint: Some(String::from("Unknown decorator flag")),
spans: AstSpan(self.spans.clone()),
cursor: self.cursor,
});
}
}
Expand Down Expand Up @@ -597,6 +612,7 @@ impl Parser {
"Literal {hex_literal:?} contains too many bytes for opcode \"{o:?}\""
)),
spans: AstSpan(curr_spans),
cursor: self.cursor,
});
}

Expand All @@ -614,6 +630,7 @@ impl Parser {
o, self.current_token.kind
)),
spans: AstSpan(vec![self.current_token.span.clone()]),
cursor: self.cursor,
})
}
}
Expand Down Expand Up @@ -704,6 +721,7 @@ impl Parser {
kind: ParserErrorKind::InvalidTokenInMacroBody(kind),
hint: None,
spans: AstSpan(vec![self.current_token.span.clone()]),
cursor: self.cursor,
});
}
};
Expand Down Expand Up @@ -817,6 +835,7 @@ impl Parser {
kind: ParserErrorKind::InvalidTokenInLabelDefinition(kind),
hint: None,
spans: AstSpan(vec![self.current_token.span.clone()]),
cursor: self.cursor,
});
}
};
Expand Down Expand Up @@ -948,6 +967,7 @@ impl Parser {
"Argument names cannot be EVM types: {arg_str}"
)),
spans: AstSpan(vec![self.current_token.span.clone()]),
cursor: self.cursor,
});
}
}
Expand All @@ -958,6 +978,7 @@ impl Parser {
),
hint: Some(format!("Argument names cannot be EVM types: {ty}")),
spans: AstSpan(vec![self.current_token.span.clone()]),
cursor: self.cursor,
})
}
_ => { /* continue, valid string */ }
Expand All @@ -981,6 +1002,7 @@ impl Parser {
kind: ParserErrorKind::InvalidArgs(self.current_token.kind.clone()),
hint: None,
spans: AstSpan(vec![self.current_token.span.clone()]),
cursor: self.cursor,
});
}

Expand All @@ -1004,6 +1026,7 @@ impl Parser {
kind: ParserErrorKind::InvalidSingleArg(self.current_token.kind.clone()),
hint: Some("Expected number representing stack item count.".to_string()),
spans: AstSpan(single_arg_span),
cursor: self.cursor,
})
}
};
Expand Down Expand Up @@ -1059,6 +1082,7 @@ impl Parser {
.to_string(),
),
spans: AstSpan(new_spans),
cursor: self.cursor,
});
}
}
Expand Down Expand Up @@ -1147,7 +1171,8 @@ impl Parser {
),
hint: Some("Expected valid hex bytecode.".to_string()),
spans: AstSpan(new_spans),
});
cursor: self.cursor,
})
}
} else {
StatementType::LabelCall(ident_str.to_string())
Expand All @@ -1162,7 +1187,8 @@ impl Parser {
kind: ParserErrorKind::InvalidTableBodyToken(kind.clone()),
hint: Some("Expected an identifier string.".to_string()),
spans: AstSpan(new_spans),
});
cursor: self.cursor,
})
}
};
}
Expand All @@ -1189,6 +1215,7 @@ impl Parser {
kind: ParserErrorKind::InvalidConstant(kind),
hint: None,
spans: AstSpan(new_spans),
cursor: self.cursor,
})
}
}
Expand Down Expand Up @@ -1221,6 +1248,7 @@ impl Parser {
kind: ParserErrorKind::InvalidArgCallIdent(kind),
hint: None,
spans: AstSpan(new_spans),
cursor: self.cursor,
})
}
}
Expand Down Expand Up @@ -1250,6 +1278,7 @@ impl Parser {
kind: ParserErrorKind::InvalidArgs(kind),
hint: None,
spans: AstSpan(vec![self.current_token.span.clone()]),
cursor: self.cursor,
}),
}
}
Expand All @@ -1267,7 +1296,8 @@ impl Parser {
kind: ParserErrorKind::InvalidUint256(size),
hint: None,
spans: AstSpan(vec![self.current_token.span.clone()]),
});
cursor: self.cursor,
})
}
Ok(self.match_kind(self.current_token.kind.clone())?)
}
Expand All @@ -1277,7 +1307,8 @@ impl Parser {
kind: ParserErrorKind::InvalidBytes(size),
hint: None,
spans: AstSpan(vec![self.current_token.span.clone()]),
});
cursor: self.cursor,
})
}
Ok(self.match_kind(self.current_token.kind.clone())?)
}
Expand All @@ -1291,7 +1322,8 @@ impl Parser {
kind: ParserErrorKind::InvalidInt(size),
hint: None,
spans: AstSpan(vec![self.current_token.span.clone()]),
});
cursor: self.cursor,
})
}
let curr_token_kind = self.current_token.kind.clone();
self.consume();
Expand Down
1 change: 1 addition & 0 deletions huff_parser/tests/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,7 @@ fn test_duplicate_macro_error() {
end: occurrences[1].1,
file: None
}]),
cursor: 58,
}
)
}
Expand Down
Loading

0 comments on commit 4c4ae27

Please sign in to comment.