mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 16:21:04 +01:00
Run clang-format on parser.ypp and lexer.lpp (#738)
Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
committed by
GitHub
co-authored by
Geoff Romer
parent
973759f0ff
commit
9f67c4b9a8
@@ -50,46 +50,43 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
%code top {
|
||||
#include <algorithm>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
#include "common/check.h"
|
||||
#include "executable_semantics/syntax/syntax_helpers.h"
|
||||
#include "executable_semantics/syntax/parse_and_lex_context.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
#include "common/check.h"
|
||||
#include "executable_semantics/syntax/parse_and_lex_context.h"
|
||||
#include "executable_semantics/syntax/syntax_helpers.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
} // %code top
|
||||
|
||||
%code requires {
|
||||
#include <optional>
|
||||
#include <optional>
|
||||
|
||||
#include "executable_semantics/ast/abstract_syntax_tree.h"
|
||||
#include "executable_semantics/ast/declaration.h"
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/function_definition.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "executable_semantics/common/ptr.h"
|
||||
#include "executable_semantics/ast/paren_contents.h"
|
||||
#include "executable_semantics/syntax/bison_wrap.h"
|
||||
#include "executable_semantics/ast/abstract_syntax_tree.h"
|
||||
#include "executable_semantics/ast/declaration.h"
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/function_definition.h"
|
||||
#include "executable_semantics/ast/paren_contents.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "executable_semantics/common/ptr.h"
|
||||
#include "executable_semantics/syntax/bison_wrap.h"
|
||||
|
||||
namespace Carbon {
|
||||
class ParseAndLexContext;
|
||||
} // namespace Carbon
|
||||
|
||||
typedef void* yyscan_t;
|
||||
namespace Carbon {
|
||||
class ParseAndLexContext;
|
||||
} // namespace Carbon
|
||||
|
||||
typedef void* yyscan_t;
|
||||
} // %code requires
|
||||
|
||||
%code {
|
||||
|
||||
void Carbon::Parser::error(const location_type&, const std::string& message) {
|
||||
context.PrintDiagnostic(message);
|
||||
}
|
||||
|
||||
void Carbon::Parser::error(const location_type&, const std::string& message) {
|
||||
context.PrintDiagnostic(message);
|
||||
}
|
||||
} // %code
|
||||
|
||||
%token <int> integer_literal
|
||||
@@ -228,7 +225,10 @@ expression:
|
||||
identifier
|
||||
{ $$ = global_arena->New<IdentifierExpression>(context.SourceLoc(), $1); }
|
||||
| expression designator
|
||||
{ $$ = global_arena->New<FieldAccessExpression>(context.SourceLoc(), $1, $2); }
|
||||
{
|
||||
$$ =
|
||||
global_arena->New<FieldAccessExpression>(context.SourceLoc(), $1, $2);
|
||||
}
|
||||
| expression LEFT_SQUARE_BRACKET expression RIGHT_SQUARE_BRACKET
|
||||
{ $$ = global_arena->New<IndexExpression>(context.SourceLoc(), $1, $3); }
|
||||
| integer_literal
|
||||
@@ -243,7 +243,8 @@ expression:
|
||||
{
|
||||
int val;
|
||||
CHECK(llvm::to_integer(llvm::StringRef($1).substr(1), val));
|
||||
CHECK($1[0] == 'i' && val == 32) << "Only i32 is supported for now: " << $1;
|
||||
CHECK($1[0] == 'i' && val == 32)
|
||||
<< "Only i32 is supported for now: " << $1;
|
||||
$$ = global_arena->New<IntTypeLiteral>(context.SourceLoc());
|
||||
}
|
||||
| STRING
|
||||
@@ -256,48 +257,85 @@ expression:
|
||||
{ $$ = global_arena->New<ContinuationTypeLiteral>(context.SourceLoc()); }
|
||||
| paren_expression { $$ = $1; }
|
||||
| expression EQUAL_EQUAL expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Eq, std::vector<Ptr<const Expression>>({$1, $3})); }
|
||||
{
|
||||
$$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Eq,
|
||||
std::vector<Ptr<const Expression>>({$1, $3}));
|
||||
}
|
||||
| expression PLUS expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Add, std::vector<Ptr<const Expression>>({$1, $3})); }
|
||||
{
|
||||
$$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Add,
|
||||
std::vector<Ptr<const Expression>>({$1, $3}));
|
||||
}
|
||||
| expression MINUS expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Sub, std::vector<Ptr<const Expression>>({$1, $3})); }
|
||||
{
|
||||
$$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Sub,
|
||||
std::vector<Ptr<const Expression>>({$1, $3}));
|
||||
}
|
||||
| expression BINARY_STAR expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Mul, std::vector<Ptr<const Expression>>({$1, $3})); }
|
||||
{
|
||||
$$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Mul,
|
||||
std::vector<Ptr<const Expression>>({$1, $3}));
|
||||
}
|
||||
| expression AND expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::And, std::vector<Ptr<const Expression>>({$1, $3})); }
|
||||
{
|
||||
$$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::And,
|
||||
std::vector<Ptr<const Expression>>({$1, $3}));
|
||||
}
|
||||
| expression OR expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Or, std::vector<Ptr<const Expression>>({$1, $3})); }
|
||||
{
|
||||
$$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Or,
|
||||
std::vector<Ptr<const Expression>>({$1, $3}));
|
||||
}
|
||||
| NOT expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Not, std::vector<Ptr<const Expression>>({$2})); }
|
||||
{
|
||||
$$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Not,
|
||||
std::vector<Ptr<const Expression>>({$2}));
|
||||
}
|
||||
| MINUS expression %prec UNARY_MINUS
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Neg, std::vector<Ptr<const Expression>>({$2})); }
|
||||
{
|
||||
$$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Neg,
|
||||
std::vector<Ptr<const Expression>>({$2}));
|
||||
}
|
||||
| PREFIX_STAR expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Deref, std::vector<Ptr<const Expression>>({$2})); }
|
||||
{
|
||||
$$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Deref,
|
||||
std::vector<Ptr<const Expression>>({$2}));
|
||||
}
|
||||
| UNARY_STAR expression %prec PREFIX_STAR
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Deref, std::vector<Ptr<const Expression>>({$2})); }
|
||||
{
|
||||
$$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Deref,
|
||||
std::vector<Ptr<const Expression>>({$2}));
|
||||
}
|
||||
| expression tuple
|
||||
{ $$ = global_arena->New<CallExpression>(context.SourceLoc(), $1, $2); }
|
||||
| expression POSTFIX_STAR
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Ptr, std::vector<Ptr<const Expression>>({$1})); }
|
||||
{
|
||||
$$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Ptr,
|
||||
std::vector<Ptr<const Expression>>({$1}));
|
||||
}
|
||||
| expression UNARY_STAR
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Ptr, std::vector<Ptr<const Expression>>({$1})); }
|
||||
{
|
||||
$$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
context.SourceLoc(), Operator::Ptr,
|
||||
std::vector<Ptr<const Expression>>({$1}));
|
||||
}
|
||||
| FNTY tuple return_type
|
||||
{
|
||||
auto [return_exp, is_omitted_exp] = $3.Release();
|
||||
$$ = global_arena->New<FunctionTypeLiteral>(
|
||||
context.SourceLoc(), $2, return_exp, is_omitted_exp); }
|
||||
$$ = global_arena->New<FunctionTypeLiteral>(context.SourceLoc(), $2,
|
||||
return_exp, is_omitted_exp);
|
||||
}
|
||||
;
|
||||
designator: PERIOD identifier { $$ = $2; }
|
||||
;
|
||||
@@ -379,7 +417,7 @@ paren_pattern_base:
|
||||
// enforce that requirement.
|
||||
paren_pattern_contents:
|
||||
paren_pattern_element
|
||||
{ $$ = {.elements = {$1}, .has_trailing_comma = false }; }
|
||||
{ $$ = {.elements = {$1}, .has_trailing_comma = false}; }
|
||||
| paren_expression_contents COMMA paren_pattern_element
|
||||
{
|
||||
$$ = ParenExpressionToParenPattern($1);
|
||||
@@ -389,7 +427,9 @@ paren_pattern_contents:
|
||||
{
|
||||
$$ = $1;
|
||||
auto el = $3.Release();
|
||||
$$.elements.push_back({.name = el.name, .term = global_arena->New<ExpressionPattern>(el.term)});
|
||||
$$.elements.push_back(
|
||||
{.name = el.name,
|
||||
.term = global_arena->New<ExpressionPattern>(el.term)});
|
||||
}
|
||||
| paren_pattern_contents COMMA paren_pattern_element
|
||||
{
|
||||
@@ -411,28 +451,41 @@ tuple_pattern: paren_pattern_base
|
||||
// rules out the possibility of an `expression` at this point.
|
||||
maybe_empty_tuple_pattern:
|
||||
LEFT_PARENTHESIS RIGHT_PARENTHESIS
|
||||
{ $$ = global_arena->New<TuplePattern>(context.SourceLoc(), std::vector<TuplePattern::Field>()); }
|
||||
{
|
||||
$$ = global_arena->New<TuplePattern>(context.SourceLoc(),
|
||||
std::vector<TuplePattern::Field>());
|
||||
}
|
||||
| tuple_pattern
|
||||
{ $$ = $1; }
|
||||
;
|
||||
clause:
|
||||
CASE pattern DOUBLE_ARROW statement
|
||||
{ $$ = global_arena->RawNew<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>($2, $4); }
|
||||
{
|
||||
$$ = global_arena
|
||||
->RawNew<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>(
|
||||
$2, $4);
|
||||
}
|
||||
| DEFAULT DOUBLE_ARROW statement
|
||||
{
|
||||
auto vp = global_arena->New<BindingPattern>(
|
||||
context.SourceLoc(), std::nullopt, global_arena->New<AutoPattern>(context.SourceLoc()));
|
||||
$$ = global_arena->RawNew<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>(vp, $3);
|
||||
auto vp = global_arena -> New<BindingPattern>(
|
||||
context.SourceLoc(), std::nullopt,
|
||||
global_arena->New<AutoPattern>(context.SourceLoc()));
|
||||
$$ = global_arena
|
||||
->RawNew<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>(
|
||||
vp, $3);
|
||||
}
|
||||
;
|
||||
clause_list:
|
||||
// Empty
|
||||
{
|
||||
$$ = global_arena->RawNew<std::list<
|
||||
std::pair<Ptr<const Pattern>, Ptr<const Statement>>>>();
|
||||
$$ = global_arena->RawNew<
|
||||
std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>>();
|
||||
}
|
||||
| clause clause_list
|
||||
{ $$ = $2; $$->push_front(*$1); }
|
||||
{
|
||||
$$ = $2;
|
||||
$$->push_front(*$1);
|
||||
}
|
||||
;
|
||||
statement:
|
||||
expression EQUAL expression SEMICOLON
|
||||
@@ -452,7 +505,8 @@ statement:
|
||||
| RETURN return_expression SEMICOLON
|
||||
{
|
||||
auto [return_exp, is_omitted_exp] = $2.Release();
|
||||
$$ = global_arena->New<Return>(context.SourceLoc(), return_exp, is_omitted_exp);
|
||||
$$ = global_arena->New<Return>(context.SourceLoc(), return_exp,
|
||||
is_omitted_exp);
|
||||
}
|
||||
| block
|
||||
{ $$ = $1; }
|
||||
@@ -502,9 +556,7 @@ return_type:
|
||||
;
|
||||
generic_binding:
|
||||
identifier COLON_BANG expression
|
||||
{
|
||||
$$ = GenericBinding({.name = std::move($1), .type = $3});
|
||||
}
|
||||
{ $$ = GenericBinding({.name = std::move($1), .type = $3}); }
|
||||
;
|
||||
deduced_param_list:
|
||||
// Empty
|
||||
@@ -532,8 +584,7 @@ function_definition:
|
||||
auto [return_exp, is_omitted_exp] = $5.Release();
|
||||
$$ = global_arena->New<FunctionDefinition>(
|
||||
context.SourceLoc(), $2, $3, $4,
|
||||
global_arena->New<ExpressionPattern>(return_exp),
|
||||
is_omitted_exp, $6);
|
||||
global_arena->New<ExpressionPattern>(return_exp), is_omitted_exp, $6);
|
||||
}
|
||||
| FN identifier deduced_params maybe_empty_tuple_pattern DOUBLE_ARROW expression
|
||||
SEMICOLON
|
||||
@@ -552,8 +603,8 @@ function_declaration:
|
||||
auto [return_exp, is_omitted_exp] = $5.Release();
|
||||
$$ = global_arena->New<FunctionDefinition>(
|
||||
context.SourceLoc(), $2, $3, $4,
|
||||
global_arena->New<ExpressionPattern>(return_exp),
|
||||
is_omitted_exp, std::nullopt);
|
||||
global_arena->New<ExpressionPattern>(return_exp), is_omitted_exp,
|
||||
std::nullopt);
|
||||
}
|
||||
;
|
||||
variable_declaration: identifier COLON pattern
|
||||
@@ -566,7 +617,10 @@ member_list:
|
||||
// Empty
|
||||
{ $$ = std::list<Ptr<Member>>(); }
|
||||
| member member_list
|
||||
{ $$ = $2; $$.push_front($1); }
|
||||
{
|
||||
$$ = $2;
|
||||
$$.push_front($1);
|
||||
}
|
||||
;
|
||||
alternative:
|
||||
identifier tuple
|
||||
@@ -586,7 +640,10 @@ alternative_list:
|
||||
$$.push_front($1);
|
||||
}
|
||||
| alternative COMMA alternative_list
|
||||
{ $$ = std::move($3); $$.push_front($1); }
|
||||
{
|
||||
$$ = std::move($3);
|
||||
$$.push_front($1);
|
||||
}
|
||||
;
|
||||
declaration:
|
||||
function_definition
|
||||
@@ -594,13 +651,9 @@ declaration:
|
||||
| function_declaration
|
||||
{ $$ = global_arena->New<FunctionDeclaration>($1); }
|
||||
| CLASS identifier LEFT_CURLY_BRACE member_list RIGHT_CURLY_BRACE
|
||||
{
|
||||
$$ = global_arena->New<ClassDeclaration>(context.SourceLoc(), $2, $4);
|
||||
}
|
||||
{ $$ = global_arena->New<ClassDeclaration>(context.SourceLoc(), $2, $4); }
|
||||
| CHOICE identifier LEFT_CURLY_BRACE alternative_list RIGHT_CURLY_BRACE
|
||||
{
|
||||
$$ = global_arena->New<ChoiceDeclaration>(context.SourceLoc(), $2, $4);
|
||||
}
|
||||
{ $$ = global_arena->New<ChoiceDeclaration>(context.SourceLoc(), $2, $4); }
|
||||
| VAR variable_declaration EQUAL expression SEMICOLON
|
||||
{
|
||||
$$ = global_arena->New<VariableDeclaration>(context.SourceLoc(), $2, $4);
|
||||
|
||||
Reference in New Issue
Block a user