mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Separate parsing from type-checking and evaluation. (#345)
This change caused the syntax error in undef2.6c ("fun" instead of "fn") to be
detected as one would expect, thus the changed golden file.
This commit is contained in:
@@ -6,12 +6,12 @@
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
#include "executable_semantics/syntax/parser.h"
|
||||
#include "executable_semantics/syntax/syntax_helpers.h"
|
||||
#include "executable_semantics/tracing_flag.h"
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
|
||||
extern FILE* yyin;
|
||||
extern auto yyparse() -> int; // NOLINT(readability-identifier-naming)
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
// yydebug = 1;
|
||||
@@ -34,5 +34,22 @@ int main(int argc, char* argv[]) {
|
||||
if (quiet_option) {
|
||||
Carbon::tracing_output = false;
|
||||
}
|
||||
return yyparse();
|
||||
|
||||
// No AST yet
|
||||
std::optional<Carbon::AST> parsedInput = std::nullopt;
|
||||
|
||||
// Parse and handle syntax errors
|
||||
auto syntaxErrorCode = yyparse(parsedInput);
|
||||
if (syntaxErrorCode != 0) {
|
||||
return syntaxErrorCode;
|
||||
}
|
||||
|
||||
if (parsedInput == std::nullopt) {
|
||||
std::cerr << "Internal error: parser validated syntax yet didn't produce "
|
||||
"an AST.\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Typecheck and run the parsed program.
|
||||
Carbon::ExecProgram(*parsedInput);
|
||||
}
|
||||
|
||||
@@ -14,21 +14,30 @@
|
||||
}
|
||||
|
||||
%code requires {
|
||||
#include <optional>
|
||||
|
||||
#include "executable_semantics/ast/declaration.h"
|
||||
#include "executable_semantics/ast/field_list.h"
|
||||
#include "executable_semantics/ast/function_definition.h"
|
||||
|
||||
namespace Carbon {
|
||||
// A representation of the parsed program.
|
||||
using AST = std::list<Carbon::Declaration>*;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
%code {
|
||||
extern int yylineno;
|
||||
extern int yylex();
|
||||
|
||||
void yyerror(char* error) {
|
||||
void yyerror(std::optional<Carbon::AST> const& parsedProgram, char* error) {
|
||||
Carbon::PrintSyntaxError(error, yylineno);
|
||||
}
|
||||
// void yyerror(char* error, ...);
|
||||
}
|
||||
|
||||
%parse-param {std::optional<Carbon::AST>& parsedProgram}
|
||||
|
||||
%union {
|
||||
char* str;
|
||||
int num;
|
||||
@@ -109,7 +118,7 @@ void yyerror(char* error) {
|
||||
%locations
|
||||
%%
|
||||
input: declaration_list
|
||||
{ Carbon::ExecProgram($1); }
|
||||
{ parsedProgram = $1; }
|
||||
;
|
||||
pattern:
|
||||
expression
|
||||
|
||||
+1
-3
@@ -1,4 +1,2 @@
|
||||
********** source program **********
|
||||
********** type checking **********
|
||||
error, program must contain a function named `main`
|
||||
executable_semantics/testdata/undef2.6c:7: syntax error
|
||||
EXIT CODE: 255
|
||||
|
||||
Reference in New Issue
Block a user