@@ -93,43 +93,44 @@ ASTNodeUP DILParser::ParseExpression() { return ParseCastExpression(); }
9393// "(" type_id ")" cast_expression
9494
9595ASTNodeUP DILParser::ParseCastExpression () {
96- // This can be a C-style cast, try parsing the contents as a type declaration.
97- if (CurToken ().Is (Token::l_paren)) {
98- Token token = CurToken ();
99- uint32_t loc = token.GetLocation ();
96+ if (!CurToken ().Is (Token::l_paren))
97+ return ParseUnaryExpression ();
10098
101- // Enable lexer backtracking, so that we can rollback in case it's not
102- // actually a type declaration.
99+ // This could be a type cast, try parsing the contents as a type declaration.
100+ Token token = CurToken ();
101+ uint32_t loc = token.GetLocation ();
103102
104- // Start tentative parsing (save token location/idx, for possible rollback).
105- uint32_t save_token_idx = m_dil_lexer. GetCurrentTokenIdx ();
103+ // Enable lexer backtracking, so that we can rollback in case it's not
104+ // actually a type declaration.
106105
107- // Consume the token only after enabling the backtracking .
108- m_dil_lexer.Advance ();
106+ // Start tentative parsing (save token location/idx, for possible rollback) .
107+ uint32_t save_token_idx = m_dil_lexer.GetCurrentTokenIdx ();
109108
110- // Try parsing the type declaration. If the returned value is not valid,
111- // then we should rollback and try parsing the expression.
112- auto type_id = ParseTypeId ();
113- if (type_id) {
114- // Successfully parsed the type declaration. Commit the backtracked
115- // tokens and parse the cast_expression.
109+ // Consume the token only after enabling the backtracking.
110+ m_dil_lexer.Advance ();
116111
117- if (!type_id.value ().IsValid ())
118- return std::make_unique<ErrorNode>();
112+ // Try parsing the type declaration. If the returned value is not valid,
113+ // then we should rollback and try parsing the expression.
114+ auto type_id = ParseTypeId ();
115+ if (type_id) {
116+ // Successfully parsed the type declaration. Commit the backtracked
117+ // tokens and parse the cast_expression.
119118
120- Expect (Token::r_paren);
121- m_dil_lexer.Advance ();
122- auto rhs = ParseCastExpression ();
119+ if (!type_id.value ().IsValid ())
120+ return std::make_unique<ErrorNode>();
123121
124- return std::make_unique<CStyleCastNode>(
125- loc, type_id. value (), std::move (rhs), CStyleCastKind::eNone );
126- }
122+ Expect (Token::r_paren);
123+ m_dil_lexer. Advance ( );
124+ auto rhs = ParseCastExpression ();
127125
128- // Failed to parse the contents of the parentheses as a type declaration.
129- // Rollback the lexer and try parsing it as unary_expression.
130- TentativeParsingRollback (save_token_idx);
126+ return std::make_unique<CastNode>(
127+ loc, type_id.value (), std::move (rhs), CastKind::eNone);
131128 }
132129
130+ // Failed to parse the contents of the parentheses as a type declaration.
131+ // Rollback the lexer and try parsing it as unary_expression.
132+ TentativeParsingRollback (save_token_idx);
133+
133134 return ParseUnaryExpression ();
134135}
135136
0 commit comments