@@ -118,7 +118,7 @@ ASTNodeUP DILParser::ParseUnaryExpression() {
118118//
119119// postfix_expression:
120120// primary_expression
121- // postfix_expression "[" expression "]"
121+ // postfix_expression "[" integer_literal "]"
122122//
123123ASTNodeUP DILParser::ParsePostfixExpression () {
124124 ASTNodeUP lhs = ParsePrimaryExpression ();
@@ -128,11 +128,17 @@ ASTNodeUP DILParser::ParsePostfixExpression() {
128128 switch (token.GetKind ()) {
129129 case Token::l_square: {
130130 m_dil_lexer.Advance ();
131- auto rhs = ParseExpression ();
131+ auto rhs = ParseIntegerConstant ();
132+ if (!rhs) {
133+ BailOut (
134+ llvm::formatv (" failed to parse integer constant: {0}" , CurToken ()),
135+ CurToken ().GetLocation (), CurToken ().GetSpelling ().length ());
136+ return std::make_unique<ErrorNode>();
137+ }
132138 Expect (Token::r_square);
133139 m_dil_lexer.Advance ();
134140 lhs = std::make_unique<ArraySubscriptNode>(loc, std::move (lhs),
135- std::move (rhs));
141+ std::move (* rhs));
136142 break ;
137143 }
138144 default :
@@ -150,8 +156,6 @@ ASTNodeUP DILParser::ParsePostfixExpression() {
150156// "(" expression ")"
151157//
152158ASTNodeUP DILParser::ParsePrimaryExpression () {
153- if (CurToken ().Is (Token::numeric_constant))
154- return ParseNumericLiteral ();
155159 if (CurToken ().IsOneOf ({Token::coloncolon, Token::identifier})) {
156160 // Save the source location for the diagnostics message.
157161 uint32_t loc = CurToken ().GetLocation ();
@@ -311,50 +315,21 @@ void DILParser::BailOut(const std::string &error, uint32_t loc,
311315 m_dil_lexer.ResetTokenIdx (m_dil_lexer.NumLexedTokens () - 1 );
312316}
313317
314- // Parse a numeric_literal .
318+ // Parse a integer_literal .
315319//
316- // numeric_literal :
317- // ? Token::numeric_constant ?
320+ // integer_literal :
321+ // ? Integer constant ?
318322//
319- ASTNodeUP DILParser::ParseNumericLiteral () {
320- Expect (Token::numeric_constant);
321- ASTNodeUP numeric_constant = ParseNumericConstant ();
322- if (numeric_constant->GetKind () == NodeKind::eErrorNode) {
323- BailOut (llvm::formatv (" Failed to parse token as numeric-constant: {0}" ,
324- CurToken ()),
325- CurToken ().GetLocation (), CurToken ().GetSpelling ().length ());
326- return std::make_unique<ErrorNode>();
327- }
328- m_dil_lexer.Advance ();
329- return numeric_constant;
330- }
331-
332- static constexpr std::pair<const char *, lldb::BasicType> type_suffixes[] = {
333- {" ull" , lldb::eBasicTypeUnsignedLongLong},
334- {" ul" , lldb::eBasicTypeUnsignedLong},
335- {" u" , lldb::eBasicTypeUnsignedInt},
336- {" ll" , lldb::eBasicTypeLongLong},
337- {" l" , lldb::eBasicTypeLong},
338- };
339-
340- ASTNodeUP DILParser::ParseNumericConstant () {
341- Token token = CurToken ();
342- auto spelling = token.GetSpelling ();
323+ std::optional<llvm::APInt> DILParser::ParseIntegerConstant () {
324+ auto spelling = CurToken ().GetSpelling ();
343325 llvm::StringRef spelling_ref = spelling;
344- lldb::BasicType type = lldb::eBasicTypeInt;
345- for (auto [suffix, t] : type_suffixes) {
346- if (spelling_ref.consume_back_insensitive (suffix)) {
347- type = t;
348- break ;
349- }
350- }
351326 llvm::APInt raw_value;
352327 if (!spelling_ref.getAsInteger (0 , raw_value)) {
353- Scalar scalar_value (raw_value);
354- return std::make_unique<ScalarLiteralNode>(token.GetLocation (), type,
355- scalar_value);
328+ m_dil_lexer.Advance ();
329+ return raw_value;
356330 }
357- return std::make_unique<ErrorNode>();
331+
332+ return std::nullopt ;
358333}
359334
360335void DILParser::Expect (Token::Kind kind) {
0 commit comments