Error
An error type that consists of both AstError and TokenizerError. Used by the parse function.
pub enum Error {
/// Triggered if there's an issue creating an AST, but tokenizing must have succeeded
AstError(ast::AstError),
/// Triggered if there's an issue when tokenizing, and an AST can't be made
TokenizerError(tokenizer::TokenizerError),
}
Variants
Triggered when there’s an issue creating an AST, but tokenizing succeeded. This typically occurs when the code is syntactically invalid.
TokenizerError
tokenizer::TokenizerError
Triggered when there’s an issue during tokenization, preventing AST creation. This occurs with invalid tokens or characters.
Methods
error_message
Returns a human-readable error message.
pub fn error_message(&self) -> Cow<'_, str>
A human-readable description of the error
Example:
use full_moon::parse;
match parse("local x = ") {
Ok(_) => println!("Success!"),
Err(errors) => {
for error in errors {
eprintln!("Error: {}", error.error_message());
}
}
}
range
Returns the position range of the error in the source code.
pub fn range(&self) -> (Position, Position)
A tuple of start and end positions indicating where the error occurred
Example:
use full_moon::parse;
match parse("local x = ") {
Err(errors) => {
for error in errors {
let (start, end) = error.range();
eprintln!(
"Error from line {}, char {} to line {}, char {}",
start.line(),
start.character(),
end.line(),
end.character()
);
}
}
_ => {}
}
Display Trait
The Error type implements Display and std::error::Error:
use full_moon::parse;
if let Err(errors) = parse("local x = ") {
for error in errors {
// Display formats the error with context
println!("{}", error);
}
}
AstError
An error that occurs when creating the AST from tokens.
pub struct AstError {
/// The token that caused the error
token: Token,
/// Any additional information that could be provided for debugging
additional: Cow<'static, str>,
/// If set, this is the complete range of the error
range: Option<(Position, Position)>,
}
Methods
token
Returns the token that caused the error.
pub fn token(&self) -> &Token
A reference to the problematic token
error_message
Returns a human-readable error message.
pub fn error_message(&self) -> &str
Additional information about the error
range
Returns the range of the error.
pub fn range(&self) -> (Position, Position)
Start and end positions of the error. If no explicit range was set, returns the token’s position.
Display Output
The Display implementation provides detailed error information:
unexpected token `end`. (starting from line 1, character 10 and ending on line 1, character 13)
additional information: expected 'then'
Position
Used to represent exact positions of tokens in code.
pub struct Position {
pub(crate) bytes: usize,
pub(crate) line: usize,
pub(crate) character: usize,
}
Methods
bytes
How many bytes, ignoring lines, it would take to find this position.
pub fn bytes(self) -> usize
The byte offset from the start of the file
character
Index of the character on the line for this position.
pub fn character(self) -> usize
The character position on the current line (0-indexed)
line
Line the position lies on.
pub fn line(self) -> usize
The line number (0-indexed)
Example Usage
use full_moon::parse;
if let Err(errors) = parse("local x = ") {
for error in errors {
let (start, end) = error.range();
println!("Error at byte offset: {}", start.bytes());
println!("Line: {}, Column: {}", start.line(), start.character());
println!("Error message: {}", error.error_message());
}
}
See Also
- parse - Main parsing function that returns
Result<Ast, Vec<Error>>
- parse_fallible - Parsing with error recovery
- Ast - The Abstract Syntax Tree structure