|
1 | 1 | module; |
2 | 2 | #include "core.hpp" |
3 | | -#include "spdlog/spdlog.h" |
| 3 | +#include "runtime/SourceManager.hpp" |
4 | 4 |
|
| 5 | +#include "spdlog/spdlog.h" |
5 | 6 | #include <gmpxx.h> |
6 | 7 |
|
7 | 8 |
|
@@ -709,6 +710,7 @@ struct SingleTokenPatternV2 : PatternV2<SingleTokenPatternV2<Patterns...>> |
709 | 710 |
|
710 | 711 | static std::optional<ResultType> matches_impl(Parser &p) |
711 | 712 | { |
| 713 | + p.observe_token(p.token_position()); |
712 | 714 | if (SingleTokenPattern_<ComposedTypes<Patterns...>>::match(p)) { |
713 | 715 | const auto &t = p.lexer().peek_token(p.token_position()); |
714 | 716 | return t.has_value() ? std::make_optional(ResultType{ *t }) : std::nullopt; |
@@ -7390,33 +7392,41 @@ struct FilePattern : PatternV2<FilePattern> |
7390 | 7392 | } |
7391 | 7393 | return p.module(); |
7392 | 7394 | } |
7393 | | - size_t idx = 0; |
7394 | | - auto t = *p.lexer().peek_token(idx); |
7395 | | - auto begin = t.start().pointer_to_program; |
7396 | | - auto end = t.end().pointer_to_program; |
7397 | | - const size_t row = t.start().row; |
7398 | | - while (row == t.start().row) { |
7399 | | - end = t.end().pointer_to_program; |
7400 | | - idx++; |
7401 | | - t = *p.lexer().peek_token(idx); |
7402 | | - } |
7403 | | - std::string line{ begin, end }; |
7404 | | - spdlog::error("Syntax error on line {}: '{}'", row + 1, line); |
7405 | | - // PARSER_ERROR(); |
7406 | 7395 | return {}; |
7407 | 7396 | } |
7408 | 7397 | }; |
7409 | 7398 |
|
7410 | 7399 | namespace parser { |
7411 | | -void Parser::parse() |
| 7400 | +PyResult<std::shared_ptr<ast::Module>> Parser::parse() |
7412 | 7401 | { |
7413 | 7402 | auto result = PatternMatchV2<FilePattern>::match(*this); |
7414 | 7403 | if (result) { |
7415 | 7404 | auto [module] = *result; |
7416 | 7405 | m_module = std::move(module); |
7417 | | - m_module->print_node(""); |
| 7406 | + return Ok(m_module); |
7418 | 7407 | } |
7419 | | - DEBUG_LOG("Parser return code: {}", result.has_value()); |
| 7408 | + std::size_t index = m_furthest_token; |
| 7409 | + std::optional<Token> token = m_lexer.peek_token(index); |
| 7410 | + while (!token.has_value() && index > 0) { token = m_lexer.peek_token(--index); } |
| 7411 | + |
| 7412 | + const auto &filename = m_lexer.filename(); |
| 7413 | + const auto &program = m_lexer.program(); |
| 7414 | + auto lineno = token.has_value() ? token->start().row + 1 : 1; |
| 7415 | + auto offset = token.has_value() ? token->start().column + 1 : 1; |
| 7416 | + const auto line_count = |
| 7417 | + std::max(static_cast<std::size_t>(std::count(program.begin(), program.end(), '\n')) |
| 7418 | + + (program.empty() || program.back() == '\n' ? 0uz : 1uz), |
| 7419 | + 1uz); |
| 7420 | + if (lineno > line_count) { |
| 7421 | + lineno = line_count; |
| 7422 | + offset = SourceManager::the().line(filename, lineno).size() + 1; |
| 7423 | + } |
| 7424 | + const auto text = SourceManager::the().line(filename, lineno); |
| 7425 | + return Err(syntax_error("invalid syntax", |
| 7426 | + SyntaxErrorLocation{ .filename = filename, |
| 7427 | + .lineno = lineno, |
| 7428 | + .offset = offset, |
| 7429 | + .text = std::string{ text } })); |
7420 | 7430 | } |
7421 | 7431 |
|
7422 | 7432 | PyResult<std::shared_ptr<ast::Module>> Parser::parse_expression() |
|
0 commit comments