no whitepsace between types and integers/group coords

This commit is contained in:
gluax 2021-04-02 06:20:08 -04:00
parent 6b3aa7a7d0
commit dbf069d215
4 changed files with 59 additions and 27 deletions

View File

@ -16,7 +16,7 @@
use std::unimplemented;
use crate::{tokenizer::*, SyntaxError, SyntaxResult, Token, KEYWORD_TOKENS};
use crate::{tokenizer::*, unexpected_whitespace, SyntaxError, SyntaxResult, Token, KEYWORD_TOKENS};
use leo_ast::*;
use tendril::format_tendril;
@ -164,10 +164,16 @@ impl ParserContext {
/// Removes the next two tokens if they are a pair of [`GroupCoordinate`] and returns them,
/// or [None] if the next token is not a [`GroupCoordinate`].
///
pub fn eat_group_partial(&mut self) -> Option<(GroupCoordinate, GroupCoordinate, Span)> {
pub fn eat_group_partial(&mut self) -> SyntaxResult<Option<(GroupCoordinate, GroupCoordinate, Span)>> {
let mut i = self.tokens.len() - 1;
let start_span = self.tokens.get(i)?.span.clone();
let first = self.peek_group_coordinate(&mut i)?;
let start_span = match self.tokens.get(i) {
Some(span) => span.span.clone(),
None => return Ok(None),
};
let first = match self.peek_group_coordinate(&mut i) {
Some(coord) => coord,
None => return Ok(None),
};
match self.tokens.get(i) {
Some(SpannedToken {
token: Token::Comma, ..
@ -175,19 +181,24 @@ impl ParserContext {
i -= 1;
}
_ => {
return None;
return Ok(None);
}
}
let second = self.peek_group_coordinate(&mut i)?;
let second = match self.peek_group_coordinate(&mut i) {
Some(coord) => coord,
None => return Ok(None),
};
let right_paren_span;
match self.tokens.get(i) {
Some(SpannedToken {
token: Token::RightParen,
..
span,
}) => {
right_paren_span = span.clone();
i -= 1;
}
_ => {
return None;
return Ok(None);
}
}
let end_span;
@ -200,12 +211,18 @@ impl ParserContext {
i -= 1;
}
_ => {
return None;
return Ok(None);
}
}
self.tokens.drain((i + 1)..);
Some((first, second, start_span + end_span))
unexpected_whitespace(
&right_paren_span,
&end_span,
&format!("({},{})", first, second),
"group",
)?;
Ok(Some((first, second, start_span + end_span)))
}
///

View File

@ -554,7 +554,7 @@ impl ParserContext {
/// tuple initialization expression.
///
pub fn parse_tuple_expression(&mut self, span: &Span) -> SyntaxResult<Expression> {
if let Some((left, right, span)) = self.eat_group_partial() {
if let Some((left, right, span)) = self.eat_group_partial()? {
return Ok(Expression::Value(ValueExpression::Group(Box::new(GroupValue::Tuple(
GroupTuple {
span,
@ -657,19 +657,28 @@ impl ParserContext {
Some(SpannedToken {
token: Token::Field,
span: type_span,
}) => Expression::Value(ValueExpression::Field(value, span + type_span)),
}) => {
unexpected_whitespace(&span, &type_span, &value, "field")?;
Expression::Value(ValueExpression::Field(value, span + type_span))
}
Some(SpannedToken {
token: Token::Group,
span: type_span,
}) => Expression::Value(ValueExpression::Group(Box::new(GroupValue::Single(
value,
span + type_span,
)))),
Some(SpannedToken { token, span: type_span }) => Expression::Value(ValueExpression::Integer(
Self::token_to_int_type(token).expect("unknown int type token"),
value,
span + type_span,
)),
}) => {
unexpected_whitespace(&span, &type_span, &value, "group")?;
Expression::Value(ValueExpression::Group(Box::new(GroupValue::Single(
value,
span + type_span,
))))
}
Some(SpannedToken { token, span: type_span }) => {
unexpected_whitespace(&span, &type_span, &value, &token.to_string())?;
Expression::Value(ValueExpression::Integer(
Self::token_to_int_type(token).expect("unknown int type token"),
value,
span + type_span,
))
}
None => Expression::Value(ValueExpression::Implicit(value, span)),
}
}

View File

@ -91,12 +91,7 @@ impl ParserContext {
)));
}
if start.col_stop != name.span.col_start {
let mut error_span = &start + &name.span;
error_span.col_start = start.col_stop - 1;
error_span.col_stop = name.span.col_start - 1;
return Err(SyntaxError::unexpected_whitespace("@", &name.name, &error_span));
}
unexpected_whitespace(&start, &name.span, &name.name, "@")?;
let end_span;
let arguments = if self.eat(Token::LeftParen).is_some() {

View File

@ -41,3 +41,14 @@ pub fn parse(path: &str, source: &str) -> SyntaxResult<Program> {
tokens.parse_program()
}
pub fn unexpected_whitespace(left_span: &Span, right_span: &Span, left: &str, right: &str) -> SyntaxResult<()> {
if left_span.col_stop != right_span.col_start {
let mut error_span = left_span + right_span;
error_span.col_start = left_span.col_stop - 1;
error_span.col_stop = right_span.col_start - 1;
return Err(SyntaxError::unexpected_whitespace(left, right, &error_span));
}
Ok(())
}