Skip to content

Commit aef18ea

Browse files
authored
Merge pull request #1147 from certik/parser_switch
Switch to the new parser
2 parents e9709af + d5ce564 commit aef18ea

File tree

3 files changed

+30
-40
lines changed

3 files changed

+30
-40
lines changed

src/lpython/parser/parser.cpp

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -113,36 +113,21 @@ std::string unique_filename(const std::string &prefix) {
113113
}
114114

115115
Result<LPython::AST::ast_t*> parse_python_file(Allocator &al,
116-
const std::string &runtime_library_dir,
116+
const std::string &/*runtime_library_dir*/,
117117
const std::string &infile,
118118
diag::Diagnostics &diagnostics,
119119
bool new_parser) {
120120
LPython::AST::ast_t* ast;
121-
if (new_parser) {
122-
std::string input = read_file(infile);
123-
Result<LPython::AST::Module_t*> res = parse(al, input, diagnostics);
124-
if (res.ok) {
125-
ast = (LPython::AST::ast_t*)res.result;
126-
} else {
127-
LFORTRAN_ASSERT(diagnostics.has_error())
128-
return Error();
129-
}
121+
// We will be using the new parser from now on
122+
new_parser = true;
123+
LFORTRAN_ASSERT(new_parser)
124+
std::string input = read_file(infile);
125+
Result<LPython::AST::Module_t*> res = parse(al, input, diagnostics);
126+
if (res.ok) {
127+
ast = (LPython::AST::ast_t*)res.result;
130128
} else {
131-
std::string outfile = unique_filename(infile);
132-
std::string pycmd = "python " + runtime_library_dir
133-
+ "/lpython_parser.py " + infile + " " + outfile;
134-
int err = std::system(pycmd.c_str());
135-
if (err != 0) {
136-
std::cerr << "The command '" << pycmd << "' failed." << std::endl;
137-
return Error();
138-
}
139-
std::string input;
140-
bool status = read_file(outfile, input);
141-
if (!status) {
142-
std::cerr << "The file '" << outfile << "' cannot be read." << std::endl;
143-
return Error();
144-
}
145-
ast = LPython::deserialize_ast(al, input);
129+
LFORTRAN_ASSERT(diagnostics.has_error())
130+
return Error();
146131
}
147132
return ast;
148133
}

src/lpython/parser/parser.yy

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,12 @@ script_unit
315315
;
316316

317317
statements
318-
: TK_INDENT statements1 TK_DEDENT { $$ = $2; }
318+
: TK_INDENT statements1 TK_DEDENT { FLOC(@$, @2); LLOC(@$,@2); $$ = $2; }
319319
;
320320

321321
sep_statements
322-
: sep statements { $$ = $2; }
323-
| type_ignore_sep statements { $$ = $2; }
322+
: sep statements { FLOC(@$, @2); $$ = $2; }
323+
| type_ignore_sep statements { FLOC(@$, @2); $$ = $2; }
324324
;
325325

326326
body_stmts
@@ -334,9 +334,9 @@ statements1
334334
;
335335

336336
single_line_statements
337-
: single_line_multi_statements TK_NEWLINE { $$ = $1; }
337+
: single_line_multi_statements TK_NEWLINE { LLOC(@$,@1); $$ = $1; }
338338
| single_line_multi_statements TK_EOLCOMMENT { $$ = $1; }
339-
| single_line_statement TK_NEWLINE { $$ = A2LIST(p.m_a, $1); }
339+
| single_line_statement TK_NEWLINE { LLOC(@$,@1); $$ = A2LIST(p.m_a, $1); }
340340
| single_line_statement TK_SEMICOLON TK_NEWLINE { $$ = A2LIST(p.m_a, $1); }
341341
| single_line_statement TK_SEMICOLON TK_EOLCOMMENT { $$ = A2LIST(p.m_a, $1); }
342342
| single_line_statement TK_EOLCOMMENT { $$ = A2LIST(p.m_a, $1); }
@@ -358,10 +358,10 @@ type_ignore_sep
358358
;
359359

360360
statement
361-
: single_line_statement sep { $$ = $1; }
361+
: single_line_statement sep { LLOC(@$,@1); $$ = $1; }
362362
| single_line_statement type_ignore_sep { $$ = $1; }
363363
| multi_line_statement
364-
| multi_line_statement sep { $$ = $1; }
364+
| multi_line_statement sep { LLOC(@$,@1); $$ = $1; }
365365
| multi_line_statement type_ignore_sep { $$ = $1; }
366366
;
367367

@@ -698,28 +698,28 @@ comma_opt
698698

699699
function_def
700700
: decorators_opt KW_DEF id "(" parameter_list_opt ")" ":"
701-
body_stmts { $$ = FUNCTION_01($1, $3, $5, $8, @$); }
701+
body_stmts { FLOC(@$, @2); $$ = FUNCTION_01($1, $3, $5, $8, @$); }
702702
| decorators_opt KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
703-
body_stmts { $$ = FUNCTION_02($1, $3, $5, $8, $10, @$); }
703+
body_stmts { FLOC(@$, @2); $$ = FUNCTION_02($1, $3, $5, $8, $10, @$); }
704704
| decorators_opt KW_DEF id "(" parameter_list_opt ")" ":"
705705
TK_TYPE_COMMENT sep statements {
706-
$$ = FUNCTION_03($1, $3, $5, $10, $8, @$); }
706+
FLOC(@$, @2); $$ = FUNCTION_03($1, $3, $5, $10, $8, @$); }
707707
| decorators_opt KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
708708
TK_TYPE_COMMENT sep statements {
709-
$$ = FUNCTION_04($1, $3, $5, $8, $12, $10, @$); }
709+
FLOC(@$, @2); $$ = FUNCTION_04($1, $3, $5, $8, $12, $10, @$); }
710710
| decorators_opt KW_DEF id "(" parameter_list_opt ")" ":"
711711
sep TK_TYPE_COMMENT sep statements {
712-
$$ = FUNCTION_03($1, $3, $5, $11, $9, @$); }
712+
FLOC(@$, @2); $$ = FUNCTION_03($1, $3, $5, $11, $9, @$); }
713713
| decorators_opt KW_DEF id "(" parameter_list_opt ")" "->" expr ":"
714714
sep TK_TYPE_COMMENT sep statements {
715-
$$ = FUNCTION_04($1, $3, $5, $8, $13, $11, @$); }
715+
FLOC(@$, @2); $$ = FUNCTION_04($1, $3, $5, $8, $13, $11, @$); }
716716
;
717717

718718
class_def
719719
: decorators_opt KW_CLASS id ":" body_stmts {
720-
$$ = CLASS_01($1, $3, $5, @$); }
720+
FLOC(@$, @2); $$ = CLASS_01($1, $3, $5, @$); }
721721
| decorators_opt KW_CLASS id "(" call_arguement_list ")" ":" body_stmts {
722-
$$ = CLASS_02($1, $3, $5, $8, @$); }
722+
FLOC(@$, @2); $$ = CLASS_02($1, $3, $5, $8, @$); }
723723
;
724724

725725
async_func_def

src/lpython/parser/semantics.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ static inline T** vec_cast(const Vec<ast_t*> &x) {
5858
return s;
5959
}
6060

61+
// Assign first/last location to `a` from `b`
62+
#define FLOC(a, b) a.first = b.first;
63+
#define LLOC(a, b) a.last = b.last;
64+
65+
6166
#define VEC_CAST(x, type) vec_cast<type##_t, astType::type>(x)
6267
#define STMTS(x) VEC_CAST(x, stmt)
6368
#define EXPRS(x) VEC_CAST(x, expr)

0 commit comments

Comments
 (0)