FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
lpython--OpenSrc/src/lpython/parser/parser.cpp at main · Shashikumar-ezhilarasu/lpython--OpenSrc · GitHub
Shashikumar-ezhilarasu
/
lpython--OpenSrc
Public
forked from
lcompilers/lpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
lpython--OpenSrc
/
src
/
lpython
/
parser
/
parser.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
137 lines (126 loc) · 4.28 KB
Breadcrumbs
lpython--OpenSrc
/
src
/
lpython
/
parser
/
parser.cpp
Copy path
File metadata and controls
137 lines (126 loc) · 4.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#
include
<
iostream
>
#
include
<
string
>
#
include
<
sstream
>
#
include
<
fstream
>
#
include
<
chrono
>
#
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
LCompilers
::LPython {
Result<LPython::
AST
::Module_t*>
parse
(Allocator &al,
const
std::string &s,
uint32_t
prev_loc, diag::Diagnostics &diagnostics)
{
Parser
p
(al, diagnostics);
try
{
p.
parse
(s, prev_loc);
}
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
(), p.
type_ignore
.
p
, p.
type_ignore
.
size
());
}
void
Parser::parse
(
const
std::string &input,
uint32_t
prev_loc)
{
inp = input;
if
(inp.
size
() >
0
) {
if
(inp[inp.
size
()-
1
] !=
'
\n
'
) inp.
append
(
"
\n
"
);
}
else
{
inp.
append
(
"
\n
"
);
}
m_tokenizer.
set_string
(inp, prev_loc);
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
"
) {
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);
}
bool
file_exists
(
const
std::string &name) {
std::ifstream
file
(name);
if
(!file.
is_open
()) {
return
false
;
}
return
true
;
}
std::string
unique_filename
(
const
std::string &prefix) {
uint64_t
ms = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now
().
time_since_epoch
()).
count
();
ms = ms %
1000000000
;
srand
((
unsigned
) ms);
std::string hex =
"
0123456789ABCDEF
"
;
std::string random_hash;
for
(
int
i=
0
; i <
6
; i++) {
random_hash += hex[
rand
() %
16
];
}
int
counter =
1
;
std::string filename = prefix + random_hash +
std::to_string
(counter);
while
(
file_exists
(filename)) {
counter++;
filename = prefix + random_hash +
std::to_string
(counter);
}
return
filename;
}
Result<LPython::
AST
::
ast_t
*>
parse_python_file
(Allocator &al,
const
std::string &
/*
runtime_library_dir
*/
,
const
std::string &infile,
diag::Diagnostics &diagnostics,
uint32_t
prev_loc,
[[maybe_unused]]
bool
new_parser) {
LPython::
AST
::
ast_t
* ast;
//
We will be using the new parser from now on
new_parser =
true
;
LCOMPILERS_ASSERT
(new_parser)
std::string input =
read_file
(infile);
Result<LPython::
AST
::Module_t*> res =
parse
(al, input, prev_loc, diagnostics);
if
(res.
ok
) {
ast = (LPython::
AST
::
ast_t
*)res.
result
;
}
else
{
LCOMPILERS_ASSERT
(diagnostics.
has_error
())
return
Error
();
}
return
ast;
}
}
//
namespace LCompilers::LPython
Back
|
FazBrowse Home
|
New Git URL