Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/parser/expr/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,9 @@ impl<'a> BinaryExprParser<'a> for Parser<'a> {
return Err(error);
}
} else {
let error = ParseError::new(
let error = ParseError::from_span(
"Expected action name after 'call'".to_string(),
crate::diagnostics::Span { start: 0, end: 0 },
call_line,
call_column,
);
Expand Down
145 changes: 62 additions & 83 deletions src/parser/expr/primary.rs

Large diffs are not rendered by default.

55 changes: 29 additions & 26 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,9 @@ impl<'a> Parser<'a> {
first_token.line
);
self.bump_sync(); // Consume "end"
self.errors.push(ParseError::new(
self.errors.push(ParseError::from_token(
format!("Unexpected 'end' followed by {:?}", second_token.token),
first_token.line,
first_token.column,
first_token,
));
continue;
}
Expand Down Expand Up @@ -281,10 +280,9 @@ impl<'a> Parser<'a> {
name_parts.push(id.clone());
}
Token::IntLiteral(_) | Token::FloatLiteral(_) => {
return Err(ParseError::new(
return Err(ParseError::from_token(
format!("Cannot use a number as a variable name: {:?}", token.token),
token.line,
token.column,
&token,
));
}
Token::KeywordAs => {
Expand All @@ -294,13 +292,12 @@ impl<'a> Parser<'a> {
));
}
_ if token.token.is_structural_keyword() => {
return Err(ParseError::new(
return Err(ParseError::from_token(
format!(
"Cannot use reserved keyword '{:?}' as a variable name",
token.token
),
token.line,
token.column,
&token,
));
}
_ if token.token.is_contextual_keyword() => {
Expand All @@ -310,19 +307,19 @@ impl<'a> Parser<'a> {
name_parts.push(name);
}
_ => {
return Err(ParseError::new(
return Err(ParseError::from_token(
format!(
"Expected identifier for variable name, found {:?}",
token.token
),
token.line,
token.column,
&token,
));
}
}
} else {
return Err(ParseError::new(
return Err(ParseError::from_span(
"Expected variable name but found end of input".to_string(),
crate::diagnostics::Span { start: 0, end: 0 },
0,
0,
));
Expand All @@ -338,23 +335,21 @@ impl<'a> Parser<'a> {
break;
}
Token::IntLiteral(_) | Token::FloatLiteral(_) => {
return Err(ParseError::new(
return Err(ParseError::from_token(
format!(
"Expected 'as' after variable name, but found number: {:?}",
token.token
),
token.line,
token.column,
&token,
));
}
_ => {
return Err(ParseError::new(
return Err(ParseError::from_token(
format!(
"Expected 'as' after variable name, but found {:?}",
token.token
),
token.line,
token.column,
&token,
));
}
}
Expand Down Expand Up @@ -383,11 +378,19 @@ impl<'a> Parser<'a> {
}

if !has_identifier {
return Err(ParseError::new(
"Expected variable name".to_string(),
self.cursor.peek().map_or(0, |t| t.line),
self.cursor.peek().map_or(0, |t| t.column),
));
if let Some(token) = self.cursor.peek() {
return Err(ParseError::from_token(
"Expected variable name".to_string(),
token,
));
} else {
return Err(ParseError::from_span(
"Expected variable name".to_string(),
crate::diagnostics::Span { start: 0, end: 0 },
0,
0,
));
}
}

Ok(name)
Expand Down Expand Up @@ -483,14 +486,14 @@ impl<'a> StmtParser<'a> for Parser<'a> {
Err(ParseError::from_token(
"Unexpected 'read' - did you mean 'read output from process'?"
.to_string(),
&token_pos,
token_pos,
))
}
} else {
let token_pos = self.cursor.peek().unwrap();
Err(ParseError::from_token(
"Unexpected 'read' at end of input".to_string(),
&token_pos,
token_pos,
))
}
}
Expand Down
59 changes: 26 additions & 33 deletions src/parser/mod_complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ impl<'a> Parser<'a> {
// Standalone "end" or unexpected pattern - consume and log error
exec_trace!("Found unexpected 'end' followed by {:?} at line {}", second_token.token, first_token.line);
self.tokens.next(); // Consume "end"
self.errors.push(ParseError::new(
self.errors.push(ParseError::from_token(
format!("Unexpected 'end' followed by {:?}", second_token.token),
first_token.line,
first_token.column,
&first_token,
));
continue;
}
Expand Down Expand Up @@ -249,10 +248,9 @@ impl<'a> Parser<'a> {
self.tokens.next();
id.clone()
} else {
return Err(ParseError::new(
return Err(ParseError::from_token(
format!("Expected identifier after 'as', found {:?}", token.token),
token.line,
token.column,
token,
));
}
} else {
Expand Down Expand Up @@ -319,16 +317,16 @@ impl<'a> Parser<'a> {
name_parts.push(id.clone());
}
_ => {
return Err(ParseError::new(
return Err(ParseError::from_token(
format!("Expected identifier for variable name, found {:?}", token.token),
token.line,
token.column,
&token,
));
}
}
} else {
return Err(ParseError::new(
return Err(ParseError::from_span(
"Expected variable name but found end of input".to_string(),
crate::diagnostics::Span { start: 0, end: 0 },
0,
0,
));
Expand All @@ -354,15 +352,15 @@ impl<'a> Parser<'a> {
self.tokens.next();
Ok(())
} else {
Err(ParseError::new(
Err(ParseError::from_token(
format!("{}: expected {:?}, found {:?}", error_message, expected, token.token),
token.line,
token.column,
&token,
))
}
} else {
Err(ParseError::new(
Err(ParseError::from_span(
format!("{}: unexpected end of input", error_message),
crate::diagnostics::Span { start: 0, end: 0 },
0,
0,
))
Expand Down Expand Up @@ -543,15 +541,15 @@ impl<'a> Parser<'a> {
let token_pos = self.tokens.next().unwrap();
Ok(Expression::Variable("count".to_string(), token_pos.line, token_pos.column))
}
_ => Err(ParseError::new(
_ => Err(ParseError::from_token(
format!("Unexpected token in expression: {:?}", token.token),
token.line,
token.column,
&token,
)),
}
} else {
Err(ParseError::new(
Err(ParseError::from_span(
"Unexpected end of input while parsing expression".to_string(),
crate::diagnostics::Span { start: 0, end: 0 },
0,
0,
))
Expand Down Expand Up @@ -679,17 +677,15 @@ impl<'a> Parser<'a> {
self.tokens.next();
id.clone()
} else {
return Err(ParseError::new(
return Err(ParseError::from_token(
format!("Expected identifier after 'each', found {:?}", token.token),
token.line,
token.column,
token,
));
}
} else {
return Err(ParseError::new(
return Err(ParseError::from_token(
"Unexpected end of input after 'each'".to_string(),
0,
0,
&for_token,
));
};

Expand Down Expand Up @@ -728,17 +724,15 @@ impl<'a> Parser<'a> {
self.tokens.next();
id.clone()
} else {
return Err(ParseError::new(
return Err(ParseError::from_token(
format!("Expected identifier after 'called', found {:?}", token.token),
token.line,
token.column,
token,
));
}
} else {
return Err(ParseError::new(
return Err(ParseError::from_token(
"Unexpected end of input after 'called'".to_string(),
0,
0,
&define_token,
));
};

Expand Down Expand Up @@ -802,9 +796,8 @@ impl<'a> Parser<'a> {
self.tokens.next();
id.clone()
} else {
return Err(ParseError::new(
return Err(ParseError::from_token(
format!("Expected identifier after 'change', found {:?}", token.token),
token.line,
token.column,
token,
));
}
Loading
Loading