Skip to content

Commit

Permalink
accept string size expressions wrapped in parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
ulmer-a committed Aug 10, 2021
1 parent 2db6aff commit 39474e0
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,18 +416,46 @@ fn parse_type_reference_type_definition(
}
}

fn parse_string_size_expression(lexer: &mut ParseSession) -> Option<Statement> {
let opening_token = lexer.token.clone();
if lexer.allow(&KeywordSquareParensOpen) || lexer.allow(&KeywordParensOpen) {
let opening_location = lexer.location().get_start();
let closing_tokens = vec![KeywordSquareParensClose, KeywordParensClose];
parse_any_in_region(lexer, closing_tokens, |lexer| {
let size_expr = parse_expression(lexer);
let error_range = SourceRange::new(opening_location..lexer.location().get_end());

if (opening_token == KeywordParensOpen && lexer.token == KeywordSquareParensClose)
|| (opening_token == KeywordSquareParensOpen && lexer.token == KeywordParensClose)
{
lexer.accept_diagnostic(Diagnostic::ImprovementSuggestion {
message: "Mismatched types of parentheses around string size expression".into(),
range: error_range,
});
} else if opening_token == KeywordParensOpen || lexer.token == KeywordParensClose {
lexer.accept_diagnostic(Diagnostic::ImprovementSuggestion {
message: "Unusual type of parentheses around string size expression, consider using square parentheses '[]'"
.into(),
range: error_range,
});
}

Some(size_expr)
})
} else {
None
}
}

fn parse_string_type_definition(
lexer: &mut ParseSession,
name: Option<String>,
) -> Option<(DataTypeDeclaration, Option<Statement>)> {
let is_wide = lexer.token == KeywordWideString;
lexer.advance();

let size = lexer.allow(&KeywordSquareParensOpen).then(|| {
parse_any_in_region(lexer, vec![KeywordSquareParensClose], |lexer| {
parse_expression(lexer)
})
});
let size = parse_string_size_expression(lexer);

Some((
DataTypeDeclaration::DataTypeDefinition {
data_type: DataType::StringType {
Expand Down

0 comments on commit 39474e0

Please sign in to comment.