forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
122 lines (113 loc) · 3.87 KB
/
parser.cpp
File metadata and controls
122 lines (113 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <string>
#include <sstream>
#include <lpython/parser/parser.h>
#include <lpython/parser/parser.tab.hh>
#include <libasr/diagnostics.h>
#include <libasr/string_utils.h>
#include <libasr/utils.h>
#include <lpython/parser/parser_exception.h>
#include <lpython/python_serialization.h>
namespace LFortran {
Result<LPython::AST::Module_t*> parse(Allocator &al, const std::string &s,
diag::Diagnostics &diagnostics)
{
Parser p(al, diagnostics);
try {
p.parse(s);
} catch (const parser_local::TokenizerError &e) {
Error error;
diagnostics.diagnostics.push_back(e.d);
return error;
} catch (const parser_local::ParserError &e) {
Error error;
diagnostics.diagnostics.push_back(e.d);
return error;
}
Location l;
if (p.result.size() == 0) {
l.first=0;
l.last=0;
} else {
l.first=p.result[0]->base.loc.first;
l.last=p.result[p.result.size()-1]->base.loc.last;
}
return (LPython::AST::Module_t*)LPython::AST::make_Module_t(al, l,
p.result.p, p.result.size(), nullptr, 0);
}
void Parser::parse(const std::string &input)
{
inp = input;
if (inp.size() > 0) {
if (inp[inp.size()-1] != '\n') inp.append("\n");
} else {
inp.append("\n");
}
m_tokenizer.set_string(inp);
if (yyparse(*this) == 0) {
return;
}
throw parser_local::ParserError("Parsing unsuccessful (internal compiler error)");
}
void Parser::handle_yyerror(const Location &loc, const std::string &msg)
{
std::string message;
if (msg == "syntax is ambiguous") {
message = "Internal Compiler Error: syntax is ambiguous in the parser";
} else if (msg == "syntax error") {
LFortran::YYSTYPE yylval_;
YYLTYPE yyloc_;
this->m_tokenizer.cur = this->m_tokenizer.tok;
int token = this->m_tokenizer.lex(this->m_a, yylval_, yyloc_, diag);
if (token == yytokentype::END_OF_FILE) {
message = "End of file is unexpected here";
} else if (token == yytokentype::TK_NEWLINE) {
message = "Newline is unexpected here";
} else {
std::string token_str = this->m_tokenizer.token();
std::string token_type = token2text(token);
if (token_str == token_type) {
message = "Token '" + token_str + "' is unexpected here";
} else {
message = "Token '" + token_str + "' (of type '" + token2text(token) + "') is unexpected here";
}
}
} else {
message = "Internal Compiler Error: parser returned unknown error";
}
throw parser_local::ParserError(message, loc);
}
Result<LPython::AST::ast_t*> parse_python_file(Allocator &al,
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();
}
} else {
std::string pycmd = "python " + runtime_library_dir + "/lpython_parser.py " + infile;
int err = std::system(pycmd.c_str());
if (err != 0) {
std::cerr << "The command '" << pycmd << "' failed." << std::endl;
return Error();
}
std::string infile_ser = "ser.txt";
std::string input;
bool status = read_file(infile_ser, input);
if (!status) {
std::cerr << "The file '" << infile_ser << "' cannot be read." << std::endl;
return Error();
}
ast = LPython::deserialize_ast(al, input);
}
return ast;
}
} // LFortran