Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parentheses with string size arguments #229

Merged
merged 2 commits into from
Aug 11, 2021
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
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
78 changes: 77 additions & 1 deletion src/parser/tests/parse_errors/parse_error_literals_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
ast::SourceRange,
ast::Statement::LiteralInteger,
ast::*,
parser::{parse, tests::lex},
Diagnostic,
};
Expand Down Expand Up @@ -134,3 +135,78 @@ fn literal_oct_number_with_double_underscores() {
}
);
}

#[test]
fn string_with_round_parens_can_be_parsed() {
let (result, diagnostics) = parse(lex(r#"
TYPE MyString1 : STRING(253); END_TYPE
TYPE MyString2 : STRING[254) := 'abc'; END_TYPE
TYPE MyString3 : STRING(255]; END_TYPE
"#));

assert_eq!(
diagnostics,
vec! [
Diagnostic::ImprovementSuggestion {
message: "Unusual type of parentheses around string size expression, consider using square parentheses '[]'"
.into(),
range: SourceRange::new(37..41),
},
Diagnostic::ImprovementSuggestion {
message: "Mismatched types of parentheses around string size expression".into(),
range: SourceRange::new(88..92),
},
Diagnostic::ImprovementSuggestion {
message: "Mismatched types of parentheses around string size expression".into(),
range: SourceRange::new(148..152),
}
]
);

let ast_string = format!("{:#?}", &result.types);

let expected_ast = format!(
"{:#?}",
vec![
UserTypeDeclaration {
data_type: DataType::StringType {
name: Some("MyString1".to_string()),
size: Some(LiteralInteger {
value: 253,
location: (10..11).into(),
}),
is_wide: false,
},
initializer: None,
},
UserTypeDeclaration {
data_type: DataType::StringType {
name: Some("MyString2".to_string()),
size: Some(LiteralInteger {
value: 254,
location: (10..11).into(),
}),
is_wide: false,
},
initializer: Some(Statement::LiteralString {
is_wide: false,
location: SourceRange::undefined(),
value: "abc".into(),
}),
},
UserTypeDeclaration {
data_type: DataType::StringType {
name: Some("MyString3".to_string()),
size: Some(LiteralInteger {
value: 255,
location: (10..11).into(),
}),
is_wide: false,
},
initializer: None,
}
]
);

assert_eq!(ast_string, expected_ast);
}