Skip to content

Switch to the new parser #1147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 10 additions & 25 deletions src/lpython/parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,36 +113,21 @@ std::string unique_filename(const std::string &prefix) {
}

Result<LPython::AST::ast_t*> 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<LPython::AST::Module_t*> 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<LPython::AST::Module_t*> 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;
}
Expand Down
30 changes: 15 additions & 15 deletions src/lpython/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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); }
Expand All @@ -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; }
;

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/lpython/parser/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ static inline T** vec_cast(const Vec<ast_t*> &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<type##_t, astType::type>(x)
#define STMTS(x) VEC_CAST(x, stmt)
#define EXPRS(x) VEC_CAST(x, expr)
Expand Down