mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 21:31:24 +01:00
Replace int lines with file-associated SourceLocations (#779)
Most interesting changes should be ast/source_location.h and syntax/parse_and_lex_context.h
This commit is contained in:
@@ -6,14 +6,11 @@
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
// Writes a syntax error diagnostic, containing message, for the input file at
|
||||
// the given line, to standard error.
|
||||
auto ParseAndLexContext::PrintDiagnostic(const std::string& message,
|
||||
int line_num) -> void {
|
||||
auto ParseAndLexContext::PrintDiagnostic(const std::string& message) -> void {
|
||||
// TODO: Do we really want this to be fatal? It makes the comment and the
|
||||
// name a lie, and renders some of the other yyparse() result propagation code
|
||||
// moot.
|
||||
FATAL_COMPILATION_ERROR(line_num) << message;
|
||||
FATAL_COMPILATION_ERROR(SourceLoc()) << message;
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
@@ -18,11 +18,15 @@ class ParseAndLexContext {
|
||||
public:
|
||||
// Creates an instance analyzing the given input file.
|
||||
ParseAndLexContext(const std::string& input_file)
|
||||
: input_file_name(input_file) {}
|
||||
: input_file_name(global_arena->New<std::string>(input_file)) {}
|
||||
|
||||
// Writes a syntax error diagnostic, containing message, for the input file at
|
||||
// the given line, to standard error.
|
||||
auto PrintDiagnostic(const std::string& message, int line_number) -> void;
|
||||
// Writes a syntax error diagnostic containing message to standard error.
|
||||
auto PrintDiagnostic(const std::string& message) -> void;
|
||||
|
||||
auto SourceLoc() -> SourceLocation {
|
||||
return SourceLocation(input_file_name,
|
||||
static_cast<int>(current_token_position.begin.line));
|
||||
}
|
||||
|
||||
// The source range of the token being (or just) lex'd.
|
||||
location current_token_position;
|
||||
@@ -30,7 +34,7 @@ class ParseAndLexContext {
|
||||
private:
|
||||
// A path to the file processed, relative to the current working directory
|
||||
// when *this is called.
|
||||
const std::string input_file_name;
|
||||
Ptr<const std::string> input_file_name;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
@@ -81,10 +81,8 @@ class ParseAndLexContext;
|
||||
|
||||
%code {
|
||||
|
||||
extern int yylineno;
|
||||
|
||||
void Carbon::Parser::error(const location_type&, const std::string& message) {
|
||||
context.PrintDiagnostic(message, yylineno);
|
||||
context.PrintDiagnostic(message);
|
||||
}
|
||||
|
||||
} // %code
|
||||
@@ -215,84 +213,84 @@ input: declaration_list
|
||||
;
|
||||
expression:
|
||||
identifier
|
||||
{ $$ = global_arena->RawNew<IdentifierExpression>(yylineno, $1); }
|
||||
{ $$ = global_arena->RawNew<IdentifierExpression>(context.SourceLoc(), $1); }
|
||||
| expression designator
|
||||
{ $$ = global_arena->RawNew<FieldAccessExpression>(yylineno, $1, $2); }
|
||||
{ $$ = global_arena->RawNew<FieldAccessExpression>(context.SourceLoc(), $1, $2); }
|
||||
| expression "[" expression "]"
|
||||
{ $$ = global_arena->RawNew<IndexExpression>(yylineno, $1, $3); }
|
||||
{ $$ = global_arena->RawNew<IndexExpression>(context.SourceLoc(), $1, $3); }
|
||||
| integer_literal
|
||||
{ $$ = global_arena->RawNew<IntLiteral>(yylineno, $1); }
|
||||
{ $$ = global_arena->RawNew<IntLiteral>(context.SourceLoc(), $1); }
|
||||
| string_literal
|
||||
{ $$ = global_arena->RawNew<StringLiteral>(yylineno, $1); }
|
||||
{ $$ = global_arena->RawNew<StringLiteral>(context.SourceLoc(), $1); }
|
||||
| TRUE
|
||||
{ $$ = global_arena->RawNew<BoolLiteral>(yylineno, true); }
|
||||
{ $$ = global_arena->RawNew<BoolLiteral>(context.SourceLoc(), true); }
|
||||
| FALSE
|
||||
{ $$ = global_arena->RawNew<BoolLiteral>(yylineno, false); }
|
||||
{ $$ = global_arena->RawNew<BoolLiteral>(context.SourceLoc(), false); }
|
||||
| sized_type_literal
|
||||
{
|
||||
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;
|
||||
$$ = global_arena->RawNew<IntTypeLiteral>(yylineno);
|
||||
$$ = global_arena->RawNew<IntTypeLiteral>(context.SourceLoc());
|
||||
}
|
||||
| STRING
|
||||
{ $$ = global_arena->RawNew<StringTypeLiteral>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<StringTypeLiteral>(context.SourceLoc()); }
|
||||
| BOOL
|
||||
{ $$ = global_arena->RawNew<BoolTypeLiteral>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<BoolTypeLiteral>(context.SourceLoc()); }
|
||||
| TYPE
|
||||
{ $$ = global_arena->RawNew<TypeTypeLiteral>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<TypeTypeLiteral>(context.SourceLoc()); }
|
||||
| CONTINUATION_TYPE
|
||||
{ $$ = global_arena->RawNew<ContinuationTypeLiteral>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<ContinuationTypeLiteral>(context.SourceLoc()); }
|
||||
| paren_expression { $$ = $1; }
|
||||
| expression EQUAL_EQUAL expression
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Eq, std::vector<const Expression*>({$1, $3})); }
|
||||
context.SourceLoc(), Operator::Eq, std::vector<const Expression*>({$1, $3})); }
|
||||
| expression "+" expression
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Add, std::vector<const Expression*>({$1, $3})); }
|
||||
context.SourceLoc(), Operator::Add, std::vector<const Expression*>({$1, $3})); }
|
||||
| expression "-" expression
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Sub, std::vector<const Expression*>({$1, $3})); }
|
||||
context.SourceLoc(), Operator::Sub, std::vector<const Expression*>({$1, $3})); }
|
||||
| expression BINARY_STAR expression
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Mul, std::vector<const Expression*>({$1, $3})); }
|
||||
context.SourceLoc(), Operator::Mul, std::vector<const Expression*>({$1, $3})); }
|
||||
| expression AND expression
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::And, std::vector<const Expression*>({$1, $3})); }
|
||||
context.SourceLoc(), Operator::And, std::vector<const Expression*>({$1, $3})); }
|
||||
| expression OR expression
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Or, std::vector<const Expression*>({$1, $3})); }
|
||||
context.SourceLoc(), Operator::Or, std::vector<const Expression*>({$1, $3})); }
|
||||
| NOT expression
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Not, std::vector<const Expression*>({$2})); }
|
||||
context.SourceLoc(), Operator::Not, std::vector<const Expression*>({$2})); }
|
||||
| "-" expression %prec UNARY_MINUS
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Neg, std::vector<const Expression*>({$2})); }
|
||||
context.SourceLoc(), Operator::Neg, std::vector<const Expression*>({$2})); }
|
||||
| PREFIX_STAR expression
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Deref, std::vector<const Expression*>({$2})); }
|
||||
context.SourceLoc(), Operator::Deref, std::vector<const Expression*>({$2})); }
|
||||
| UNARY_STAR expression %prec PREFIX_STAR
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Deref, std::vector<const Expression*>({$2})); }
|
||||
context.SourceLoc(), Operator::Deref, std::vector<const Expression*>({$2})); }
|
||||
| expression tuple
|
||||
{ $$ = global_arena->RawNew<CallExpression>(yylineno, $1, $2); }
|
||||
{ $$ = global_arena->RawNew<CallExpression>(context.SourceLoc(), $1, $2); }
|
||||
| expression POSTFIX_STAR
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Ptr, std::vector<const Expression*>({$1})); }
|
||||
context.SourceLoc(), Operator::Ptr, std::vector<const Expression*>({$1})); }
|
||||
| expression UNARY_STAR
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Ptr, std::vector<const Expression*>({$1})); }
|
||||
context.SourceLoc(), Operator::Ptr, std::vector<const Expression*>({$1})); }
|
||||
| FNTY tuple return_type
|
||||
{ $$ = global_arena->RawNew<FunctionTypeLiteral>(
|
||||
yylineno, $2, $3.first, $3.second); }
|
||||
context.SourceLoc(), $2, $3.first, $3.second); }
|
||||
;
|
||||
designator: "." identifier { $$ = $2; }
|
||||
;
|
||||
paren_expression: paren_expression_base
|
||||
{ $$ = ExpressionFromParenContents(yylineno, $1); }
|
||||
{ $$ = ExpressionFromParenContents(context.SourceLoc(), $1); }
|
||||
;
|
||||
tuple: paren_expression_base
|
||||
{ $$ = TupleExpressionFromParenContents(yylineno, $1); }
|
||||
{ $$ = TupleExpressionFromParenContents(context.SourceLoc(), $1); }
|
||||
;
|
||||
paren_expression_element:
|
||||
expression
|
||||
@@ -335,20 +333,20 @@ pattern:
|
||||
;
|
||||
non_expression_pattern:
|
||||
AUTO
|
||||
{ $$ = global_arena->RawNew<AutoPattern>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<AutoPattern>(context.SourceLoc()); }
|
||||
| binding_lhs ":" pattern
|
||||
{ $$ = global_arena->RawNew<BindingPattern>(yylineno, $1, $3); }
|
||||
{ $$ = global_arena->RawNew<BindingPattern>(context.SourceLoc(), $1, $3); }
|
||||
| paren_pattern
|
||||
{ $$ = $1; }
|
||||
| expression tuple_pattern
|
||||
{ $$ = global_arena->RawNew<AlternativePattern>(yylineno, $1, $2); }
|
||||
{ $$ = global_arena->RawNew<AlternativePattern>(context.SourceLoc(), $1, $2); }
|
||||
;
|
||||
binding_lhs:
|
||||
identifier { $$ = $1; }
|
||||
| UNDERSCORE { $$ = std::nullopt; }
|
||||
;
|
||||
paren_pattern: paren_pattern_base
|
||||
{ $$ = PatternFromParenContents(yylineno, $1); }
|
||||
{ $$ = PatternFromParenContents(context.SourceLoc(), $1); }
|
||||
;
|
||||
paren_pattern_base:
|
||||
"(" paren_pattern_contents ")"
|
||||
@@ -390,14 +388,14 @@ paren_pattern_element:
|
||||
{ $$ = {.name = $1, .term = $3}; }
|
||||
;
|
||||
tuple_pattern: paren_pattern_base
|
||||
{ $$ = TuplePatternFromParenContents(yylineno, $1); }
|
||||
{ $$ = TuplePatternFromParenContents(context.SourceLoc(), $1); }
|
||||
;
|
||||
// Unlike most `pattern` nonterminals, this one overlaps with `expression`,
|
||||
// so it should be used only when prior context (such as an introducer)
|
||||
// rules out the possibility of an `expression` at this point.
|
||||
maybe_empty_tuple_pattern:
|
||||
"(" ")"
|
||||
{ $$ = global_arena->RawNew<TuplePattern>(yylineno, std::vector<TuplePattern::Field>()); }
|
||||
{ $$ = global_arena->RawNew<TuplePattern>(context.SourceLoc(), std::vector<TuplePattern::Field>()); }
|
||||
| tuple_pattern
|
||||
{ $$ = $1; }
|
||||
;
|
||||
@@ -407,7 +405,7 @@ clause:
|
||||
| DEFAULT DBLARROW statement
|
||||
{
|
||||
auto vp = global_arena->RawNew<BindingPattern>(
|
||||
yylineno, std::nullopt, global_arena->RawNew<AutoPattern>(yylineno));
|
||||
context.SourceLoc(), std::nullopt, global_arena->RawNew<AutoPattern>(context.SourceLoc()));
|
||||
$$ = global_arena->RawNew<std::pair<const Pattern*, const Statement*>>(vp, $3);
|
||||
}
|
||||
;
|
||||
@@ -422,35 +420,35 @@ clause_list:
|
||||
;
|
||||
statement:
|
||||
expression "=" expression ";"
|
||||
{ $$ = global_arena->RawNew<Assign>(yylineno, $1, $3); }
|
||||
{ $$ = global_arena->RawNew<Assign>(context.SourceLoc(), $1, $3); }
|
||||
| VAR pattern "=" expression ";"
|
||||
{ $$ = global_arena->RawNew<VariableDefinition>(yylineno, $2, $4); }
|
||||
{ $$ = global_arena->RawNew<VariableDefinition>(context.SourceLoc(), $2, $4); }
|
||||
| expression ";"
|
||||
{ $$ = global_arena->RawNew<ExpressionStatement>(yylineno, $1); }
|
||||
{ $$ = global_arena->RawNew<ExpressionStatement>(context.SourceLoc(), $1); }
|
||||
| if_statement
|
||||
{ $$ = $1; }
|
||||
| WHILE "(" expression ")" block
|
||||
{ $$ = global_arena->RawNew<While>(yylineno, $3, $5); }
|
||||
{ $$ = global_arena->RawNew<While>(context.SourceLoc(), $3, $5); }
|
||||
| BREAK ";"
|
||||
{ $$ = global_arena->RawNew<Break>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<Break>(context.SourceLoc()); }
|
||||
| CONTINUE ";"
|
||||
{ $$ = global_arena->RawNew<Continue>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<Continue>(context.SourceLoc()); }
|
||||
| RETURN return_expression ";"
|
||||
{ $$ = global_arena->RawNew<Return>(yylineno, $2.first, $2.second); }
|
||||
{ $$ = global_arena->RawNew<Return>(context.SourceLoc(), $2.first, $2.second); }
|
||||
| block
|
||||
{ $$ = $1; }
|
||||
| MATCH "(" expression ")" "{" clause_list "}"
|
||||
{ $$ = global_arena->RawNew<Match>(yylineno, $3, $6); }
|
||||
{ $$ = global_arena->RawNew<Match>(context.SourceLoc(), $3, $6); }
|
||||
| CONTINUATION identifier statement
|
||||
{ $$ = global_arena->RawNew<Continuation>(yylineno, $2, $3); }
|
||||
{ $$ = global_arena->RawNew<Continuation>(context.SourceLoc(), $2, $3); }
|
||||
| RUN expression ";"
|
||||
{ $$ = global_arena->RawNew<Run>(yylineno, $2); }
|
||||
{ $$ = global_arena->RawNew<Run>(context.SourceLoc(), $2); }
|
||||
| AWAIT ";"
|
||||
{ $$ = global_arena->RawNew<Await>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<Await>(context.SourceLoc()); }
|
||||
;
|
||||
if_statement:
|
||||
IF "(" expression ")" block optional_else
|
||||
{ $$ = global_arena->RawNew<If>(yylineno, $3, $5, $6); }
|
||||
{ $$ = global_arena->RawNew<If>(context.SourceLoc(), $3, $5, $6); }
|
||||
;
|
||||
optional_else:
|
||||
// Empty
|
||||
@@ -462,7 +460,7 @@ optional_else:
|
||||
;
|
||||
return_expression:
|
||||
// Empty
|
||||
{ $$ = {global_arena->RawNew<TupleLiteral>(yylineno), true}; }
|
||||
{ $$ = {global_arena->RawNew<TupleLiteral>(context.SourceLoc()), true}; }
|
||||
| expression
|
||||
{ $$ = {$1, false}; }
|
||||
;
|
||||
@@ -470,15 +468,15 @@ statement_list:
|
||||
// Empty
|
||||
{ $$ = 0; }
|
||||
| statement statement_list
|
||||
{ $$ = global_arena->RawNew<Sequence>(yylineno, $1, $2); }
|
||||
{ $$ = global_arena->RawNew<Sequence>(context.SourceLoc(), $1, $2); }
|
||||
;
|
||||
block:
|
||||
"{" statement_list "}"
|
||||
{ $$ = global_arena->RawNew<Block>(yylineno, $2); }
|
||||
{ $$ = global_arena->RawNew<Block>(context.SourceLoc(), $2); }
|
||||
;
|
||||
return_type:
|
||||
// Empty
|
||||
{ $$ = {global_arena->RawNew<TupleLiteral>(yylineno), true}; }
|
||||
{ $$ = {global_arena->RawNew<TupleLiteral>(context.SourceLoc()), true}; }
|
||||
| ARROW expression %prec FNARROW
|
||||
{ $$ = {$2, false}; }
|
||||
;
|
||||
@@ -512,7 +510,7 @@ function_definition:
|
||||
FN identifier deduced_params maybe_empty_tuple_pattern return_type block
|
||||
{
|
||||
$$ = global_arena->New<FunctionDefinition>(
|
||||
yylineno, $2, $3, $4,
|
||||
context.SourceLoc(), $2, $3, $4,
|
||||
global_arena->RawNew<ExpressionPattern>($5.first),
|
||||
$5.second, $6);
|
||||
}
|
||||
@@ -521,24 +519,24 @@ function_definition:
|
||||
// The return type is not considered "omitted" because it's automatic from
|
||||
// the expression.
|
||||
$$ = global_arena->New<FunctionDefinition>(
|
||||
yylineno, $2, $3, $4,
|
||||
global_arena->RawNew<AutoPattern>(yylineno), true,
|
||||
global_arena->RawNew<Return>(yylineno, $6, true));
|
||||
context.SourceLoc(), $2, $3, $4,
|
||||
global_arena->RawNew<AutoPattern>(context.SourceLoc()), true,
|
||||
global_arena->RawNew<Return>(context.SourceLoc(), $6, true));
|
||||
}
|
||||
;
|
||||
function_declaration:
|
||||
FN identifier deduced_params maybe_empty_tuple_pattern return_type ";"
|
||||
{
|
||||
$$ = global_arena->New<FunctionDefinition>(
|
||||
yylineno, $2, $3, $4,
|
||||
context.SourceLoc(), $2, $3, $4,
|
||||
global_arena->RawNew<ExpressionPattern>($5.first),
|
||||
$5.second, nullptr); }
|
||||
;
|
||||
variable_declaration: identifier ":" pattern
|
||||
{ $$ = global_arena->RawNew<BindingPattern>(yylineno, $1, $3); }
|
||||
{ $$ = global_arena->RawNew<BindingPattern>(context.SourceLoc(), $1, $3); }
|
||||
;
|
||||
member: VAR variable_declaration ";"
|
||||
{ $$ = global_arena->RawNew<FieldMember>(yylineno, $2); }
|
||||
{ $$ = global_arena->RawNew<FieldMember>(context.SourceLoc(), $2); }
|
||||
;
|
||||
member_list:
|
||||
// Empty
|
||||
@@ -552,7 +550,7 @@ alternative:
|
||||
| identifier
|
||||
{
|
||||
$$ = std::pair<std::string, const Expression*>(
|
||||
$1, global_arena->RawNew<TupleLiteral>(yylineno));
|
||||
$1, global_arena->RawNew<TupleLiteral>(context.SourceLoc()));
|
||||
}
|
||||
;
|
||||
alternative_list:
|
||||
@@ -573,15 +571,15 @@ declaration:
|
||||
{ $$ = global_arena->New<FunctionDeclaration>($1); }
|
||||
| CLASS identifier "{" member_list "}"
|
||||
{
|
||||
$$ = global_arena->New<ClassDeclaration>(yylineno, $2, $4);
|
||||
$$ = global_arena->New<ClassDeclaration>(context.SourceLoc(), $2, $4);
|
||||
}
|
||||
| CHOICE identifier "{" alternative_list "}"
|
||||
{
|
||||
$$ = global_arena->New<ChoiceDeclaration>(yylineno, $2, $4);
|
||||
$$ = global_arena->New<ChoiceDeclaration>(context.SourceLoc(), $2, $4);
|
||||
}
|
||||
| VAR variable_declaration "=" expression ";"
|
||||
{
|
||||
$$ = global_arena->New<VariableDeclaration>(yylineno, $2, $4);
|
||||
$$ = global_arena->New<VariableDeclaration>(context.SourceLoc(), $2, $4);
|
||||
}
|
||||
;
|
||||
declaration_list:
|
||||
|
||||
@@ -16,22 +16,23 @@ namespace Carbon {
|
||||
// Adds builtins, currently only Print(). Note Print() is experimental, not
|
||||
// standardized, but is made available for printing state in tests.
|
||||
static void AddIntrinsics(std::list<Ptr<const Declaration>>* fs) {
|
||||
SourceLocation loc("<intrinsic>", 0);
|
||||
std::vector<TuplePattern::Field> print_fields = {TuplePattern::Field(
|
||||
"0", global_arena->RawNew<BindingPattern>(
|
||||
-1, "format_str",
|
||||
loc, "format_str",
|
||||
global_arena->RawNew<ExpressionPattern>(
|
||||
global_arena->RawNew<StringTypeLiteral>(-1))))};
|
||||
global_arena->RawNew<StringTypeLiteral>(loc))))};
|
||||
auto* print_return = global_arena->RawNew<Return>(
|
||||
-1,
|
||||
loc,
|
||||
global_arena->RawNew<IntrinsicExpression>(
|
||||
IntrinsicExpression::IntrinsicKind::Print),
|
||||
false);
|
||||
auto print = global_arena->New<FunctionDeclaration>(
|
||||
global_arena->New<FunctionDefinition>(
|
||||
-1, "Print", std::vector<GenericBinding>(),
|
||||
global_arena->RawNew<TuplePattern>(-1, print_fields),
|
||||
loc, "Print", std::vector<GenericBinding>(),
|
||||
global_arena->RawNew<TuplePattern>(loc, print_fields),
|
||||
global_arena->RawNew<ExpressionPattern>(
|
||||
global_arena->RawNew<TupleLiteral>(-1)),
|
||||
global_arena->RawNew<TupleLiteral>(loc)),
|
||||
/*is_omitted_return_type=*/false, print_return));
|
||||
fs->insert(fs->begin(), print);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user