Skip to content

Commit 2336eee

Browse files
Continue the location information for the imported files.
1 parent 465b381 commit 2336eee

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

src/lpython/parser/parser.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
namespace LFortran {
1616

1717
Result<LPython::AST::Module_t*> parse(Allocator &al, const std::string &s,
18-
diag::Diagnostics &diagnostics)
18+
uint32_t prev_loc, diag::Diagnostics &diagnostics)
1919
{
2020
Parser p(al, diagnostics);
2121
try {
22-
p.parse(s);
22+
p.parse(s, prev_loc);
2323
} catch (const parser_local::TokenizerError &e) {
2424
Error error;
2525
diagnostics.diagnostics.push_back(e.d);
@@ -42,15 +42,15 @@ Result<LPython::AST::Module_t*> parse(Allocator &al, const std::string &s,
4242
p.result.p, p.result.size(), p.type_ignore.p, p.type_ignore.size());
4343
}
4444

45-
void Parser::parse(const std::string &input)
45+
void Parser::parse(const std::string &input, uint32_t prev_loc)
4646
{
4747
inp = input;
4848
if (inp.size() > 0) {
4949
if (inp[inp.size()-1] != '\n') inp.append("\n");
5050
} else {
5151
inp.append("\n");
5252
}
53-
m_tokenizer.set_string(inp);
53+
m_tokenizer.set_string(inp, prev_loc);
5454
if (yyparse(*this) == 0) {
5555
return;
5656
}
@@ -116,13 +116,14 @@ Result<LPython::AST::ast_t*> parse_python_file(Allocator &al,
116116
const std::string &/*runtime_library_dir*/,
117117
const std::string &infile,
118118
diag::Diagnostics &diagnostics,
119+
uint32_t prev_loc,
119120
bool new_parser) {
120121
LPython::AST::ast_t* ast;
121122
// We will be using the new parser from now on
122123
new_parser = true;
123124
LFORTRAN_ASSERT(new_parser)
124125
std::string input = read_file(infile);
125-
Result<LPython::AST::Module_t*> res = parse(al, input, diagnostics);
126+
Result<LPython::AST::Module_t*> res = parse(al, input, prev_loc, diagnostics);
126127
if (res.ok) {
127128
ast = (LPython::AST::ast_t*)res.result;
128129
} else {

src/lpython/parser/parser.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ class Parser
2626
type_ignore.reserve(al, 4);
2727
}
2828

29-
void parse(const std::string &input);
29+
void parse(const std::string &input, uint32_t prev_loc);
3030
void handle_yyerror(const Location &loc, const std::string &msg);
3131
};
3232

3333

3434
// Parses Python code to AST
3535
Result<LPython::AST::Module_t*> parse(Allocator &al,
36-
const std::string &s,
36+
const std::string &s, uint32_t prev_loc,
3737
diag::Diagnostics &diagnostics);
3838

3939
Result<LPython::AST::ast_t*> parse_python_file(Allocator &al,
4040
const std::string &runtime_library_dir,
4141
const std::string &infile,
4242
diag::Diagnostics &diagnostics,
43-
bool new_parser);
43+
uint32_t prev_loc, bool new_parser);
4444

4545
} // namespace LFortran
4646

src/lpython/parser/tokenizer.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Tokenizer
1818
unsigned char *cur_line;
1919
unsigned int line_num;
2020
unsigned char *string_start;
21+
uint32_t prev_loc; // The previous file ended at this location.
2122

2223
int last_token=-1;
2324

@@ -34,7 +35,7 @@ class Tokenizer
3435
public:
3536
// Set the string to tokenize. The caller must ensure `str` will stay valid
3637
// as long as `lex` is being called.
37-
void set_string(const std::string &str);
38+
void set_string(const std::string &str, uint32_t prev_loc_);
3839

3940
// Get next token. Token ID is returned as function result, the semantic
4041
// value is put into `yylval`.
@@ -71,8 +72,8 @@ class Tokenizer
7172
// Return the current token's location
7273
void token_loc(Location &loc) const
7374
{
74-
loc.first = tok-string_start;
75-
loc.last = cur-string_start-1;
75+
loc.first = prev_loc + (tok-string_start);
76+
loc.last = prev_loc + (cur-string_start-1);
7677
}
7778

7879
void record_paren(Location &loc, char c);

src/lpython/parser/tokenizer.re

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,15 @@ void lex_imag(Allocator &al, const unsigned char *s,
122122
}
123123
}
124124

125-
void Tokenizer::set_string(const std::string &str)
125+
void Tokenizer::set_string(const std::string &str, uint32_t prev_loc_)
126126
{
127127
// The input string must be NULL terminated, otherwise the tokenizer will
128128
// not detect the end of string. After C++11, the std::string is guaranteed
129129
// to end with \0, but we check this here just in case.
130130
LFORTRAN_ASSERT(str[str.size()] == '\0');
131131
cur = (unsigned char *)(&str[0]);
132132
string_start = cur;
133+
prev_loc = prev_loc_;
133134
cur_line = cur;
134135
line_num = 1;
135136
}
@@ -777,7 +778,7 @@ Result<std::vector<int>> tokens(Allocator &al, const std::string &input,
777778
std::vector<Location> *locations)
778779
{
779780
Tokenizer t;
780-
t.set_string(input);
781+
t.set_string(input, 0);
781782
std::vector<int> tst;
782783
int token = yytokentype::END_OF_FILE + 1; // Something different from EOF
783784
while (token != yytokentype::END_OF_FILE) {

0 commit comments

Comments
 (0)