diff --git a/src/lpython/parser/parser.cpp b/src/lpython/parser/parser.cpp index f5bbf0408e..a5991564d6 100644 --- a/src/lpython/parser/parser.cpp +++ b/src/lpython/parser/parser.cpp @@ -113,36 +113,21 @@ std::string unique_filename(const std::string &prefix) { } Result parse_python_file(Allocator &al, - const std::string &runtime_library_dir, + const std::string &/*runtime_library_dir*/, const std::string &infile, diag::Diagnostics &diagnostics, bool new_parser) { LPython::AST::ast_t* ast; - if (new_parser) { - std::string input = read_file(infile); - Result res = parse(al, input, diagnostics); - if (res.ok) { - ast = (LPython::AST::ast_t*)res.result; - } else { - LFORTRAN_ASSERT(diagnostics.has_error()) - return Error(); - } + // We will be using the new parser from now on + new_parser = true; + LFORTRAN_ASSERT(new_parser) + std::string input = read_file(infile); + Result res = parse(al, input, diagnostics); + if (res.ok) { + ast = (LPython::AST::ast_t*)res.result; } else { - std::string outfile = unique_filename(infile); - std::string pycmd = "python " + runtime_library_dir - + "/lpython_parser.py " + infile + " " + outfile; - int err = std::system(pycmd.c_str()); - if (err != 0) { - std::cerr << "The command '" << pycmd << "' failed." << std::endl; - return Error(); - } - std::string input; - bool status = read_file(outfile, input); - if (!status) { - std::cerr << "The file '" << outfile << "' cannot be read." << std::endl; - return Error(); - } - ast = LPython::deserialize_ast(al, input); + LFORTRAN_ASSERT(diagnostics.has_error()) + return Error(); } return ast; } diff --git a/src/lpython/parser/parser.yy b/src/lpython/parser/parser.yy index 99c9f3bd86..c61b84be4e 100644 --- a/src/lpython/parser/parser.yy +++ b/src/lpython/parser/parser.yy @@ -315,12 +315,12 @@ script_unit ; statements - : TK_INDENT statements1 TK_DEDENT { $$ = $2; } + : TK_INDENT statements1 TK_DEDENT { FLOC(@$, @2); LLOC(@$,@2); $$ = $2; } ; sep_statements - : sep statements { $$ = $2; } - | type_ignore_sep statements { $$ = $2; } + : sep statements { FLOC(@$, @2); $$ = $2; } + | type_ignore_sep statements { FLOC(@$, @2); $$ = $2; } ; body_stmts @@ -334,9 +334,9 @@ statements1 ; single_line_statements - : single_line_multi_statements TK_NEWLINE { $$ = $1; } + : single_line_multi_statements TK_NEWLINE { LLOC(@$,@1); $$ = $1; } | single_line_multi_statements TK_EOLCOMMENT { $$ = $1; } - | single_line_statement TK_NEWLINE { $$ = A2LIST(p.m_a, $1); } + | single_line_statement TK_NEWLINE { LLOC(@$,@1); $$ = A2LIST(p.m_a, $1); } | single_line_statement TK_SEMICOLON TK_NEWLINE { $$ = A2LIST(p.m_a, $1); } | single_line_statement TK_SEMICOLON TK_EOLCOMMENT { $$ = A2LIST(p.m_a, $1); } | single_line_statement TK_EOLCOMMENT { $$ = A2LIST(p.m_a, $1); } @@ -358,10 +358,10 @@ type_ignore_sep ; statement - : single_line_statement sep { $$ = $1; } + : single_line_statement sep { LLOC(@$,@1); $$ = $1; } | single_line_statement type_ignore_sep { $$ = $1; } | multi_line_statement - | multi_line_statement sep { $$ = $1; } + | multi_line_statement sep { LLOC(@$,@1); $$ = $1; } | multi_line_statement type_ignore_sep { $$ = $1; } ; @@ -698,28 +698,28 @@ comma_opt function_def : decorators_opt KW_DEF id "(" parameter_list_opt ")" ":" - body_stmts { $$ = FUNCTION_01($1, $3, $5, $8, @$); } + body_stmts { FLOC(@$, @2); $$ = FUNCTION_01($1, $3, $5, $8, @$); } | decorators_opt KW_DEF id "(" parameter_list_opt ")" "->" expr ":" - body_stmts { $$ = FUNCTION_02($1, $3, $5, $8, $10, @$); } + body_stmts { FLOC(@$, @2); $$ = FUNCTION_02($1, $3, $5, $8, $10, @$); } | decorators_opt KW_DEF id "(" parameter_list_opt ")" ":" TK_TYPE_COMMENT sep statements { - $$ = FUNCTION_03($1, $3, $5, $10, $8, @$); } + FLOC(@$, @2); $$ = FUNCTION_03($1, $3, $5, $10, $8, @$); } | decorators_opt KW_DEF id "(" parameter_list_opt ")" "->" expr ":" TK_TYPE_COMMENT sep statements { - $$ = FUNCTION_04($1, $3, $5, $8, $12, $10, @$); } + FLOC(@$, @2); $$ = FUNCTION_04($1, $3, $5, $8, $12, $10, @$); } | decorators_opt KW_DEF id "(" parameter_list_opt ")" ":" sep TK_TYPE_COMMENT sep statements { - $$ = FUNCTION_03($1, $3, $5, $11, $9, @$); } + FLOC(@$, @2); $$ = FUNCTION_03($1, $3, $5, $11, $9, @$); } | decorators_opt KW_DEF id "(" parameter_list_opt ")" "->" expr ":" sep TK_TYPE_COMMENT sep statements { - $$ = FUNCTION_04($1, $3, $5, $8, $13, $11, @$); } + FLOC(@$, @2); $$ = FUNCTION_04($1, $3, $5, $8, $13, $11, @$); } ; class_def : decorators_opt KW_CLASS id ":" body_stmts { - $$ = CLASS_01($1, $3, $5, @$); } + FLOC(@$, @2); $$ = CLASS_01($1, $3, $5, @$); } | decorators_opt KW_CLASS id "(" call_arguement_list ")" ":" body_stmts { - $$ = CLASS_02($1, $3, $5, $8, @$); } + FLOC(@$, @2); $$ = CLASS_02($1, $3, $5, $8, @$); } ; async_func_def diff --git a/src/lpython/parser/semantics.h b/src/lpython/parser/semantics.h index 5311b930f9..8fcc8e06e6 100644 --- a/src/lpython/parser/semantics.h +++ b/src/lpython/parser/semantics.h @@ -58,6 +58,11 @@ static inline T** vec_cast(const Vec &x) { return s; } +// Assign first/last location to `a` from `b` +#define FLOC(a, b) a.first = b.first; +#define LLOC(a, b) a.last = b.last; + + #define VEC_CAST(x, type) vec_cast(x) #define STMTS(x) VEC_CAST(x, stmt) #define EXPRS(x) VEC_CAST(x, expr)