| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "error_printer.h" | ||
| 2 | #include "ast/nodes.h" | ||
| 3 | #include "lexer/lexer.h" | ||
| 4 | |||
| 5 | namespace lython { | ||
| 6 | |||
| 7 | String BaseErrorPrinter::indentation() { return String(indent * 2, ' '); } | ||
| 8 | std::ostream& BaseErrorPrinter::firstline() { return out; } | ||
| 9 | std::ostream& BaseErrorPrinter::newline() { return out << std::endl << indentation(); } | ||
| 10 | std::ostream& BaseErrorPrinter::errorline() { return out << std::endl; } | ||
| 11 | void BaseErrorPrinter::end() { out << std::endl; } | ||
| 12 | std::ostream& BaseErrorPrinter::codeline() { | ||
| 13 |
5/5✓ Branch 2 taken 1377 times.
✓ Branch 5 taken 1377 times.
✓ Branch 8 taken 1377 times.
✓ Branch 11 taken 1377 times.
✓ Branch 14 taken 1377 times.
|
1377 | return out << std::endl << indentation() << indentation() << "|"; |
| 14 | } | ||
| 15 | |||
| 16 | void BaseErrorPrinter::underline(CommonAttributes const& attr) { | ||
| 17 | |||
| 18 | 111 | int32 size = 1; | |
| 19 | |||
| 20 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 111 times.
|
111 | if (attr.end_col_offset.has_value()) { |
| 21 | ✗ | size = std::max(attr.end_col_offset.value() - attr.col_offset, 1); | |
| 22 | } | ||
| 23 | |||
| 24 | 111 | int32 start = std::max(1, attr.col_offset); | |
| 25 |
4/4✓ Branch 3 taken 111 times.
✓ Branch 6 taken 111 times.
✓ Branch 10 taken 111 times.
✓ Branch 13 taken 111 times.
|
111 | codeline() << String(start, ' ') << String(size, '^'); |
| 26 | 111 | } | |
| 27 | |||
| 28 | String BaseErrorPrinter::get_filename() const { | ||
| 29 |
2/2✓ Branch 0 taken 573 times.
✓ Branch 1 taken 128 times.
|
701 | if (lexer != nullptr) { |
| 30 | 573 | return lexer->file_name(); | |
| 31 | } | ||
| 32 |
1/1✓ Branch 2 taken 128 times.
|
128 | return "<input>"; |
| 33 | } | ||
| 34 | |||
| 35 | StmtNode* get_parent_stmt(Node* node) { | ||
| 36 | 305 | Array<Node const*> nodes; | |
| 37 | 305 | Node const* n = node; | |
| 38 | |||
| 39 |
1/2✓ Branch 0 taken 733 times.
✗ Branch 1 not taken.
|
733 | while (n != nullptr) { |
| 40 |
3/3✓ Branch 1 taken 733 times.
✓ Branch 3 taken 170 times.
✓ Branch 4 taken 563 times.
|
733 | if (n->family() == NodeFamily::Statement) { |
| 41 | 170 | return (StmtNode*)n; | |
| 42 | } | ||
| 43 |
3/3✓ Branch 1 taken 563 times.
✓ Branch 3 taken 135 times.
✓ Branch 4 taken 428 times.
|
563 | if (n->family() == NodeFamily::Module) { |
| 44 | 135 | return nullptr; | |
| 45 | } | ||
| 46 | 428 | n = n->get_parent(); | |
| 47 | |||
| 48 |
2/2✓ Branch 5 taken 148 times.
✓ Branch 6 taken 428 times.
|
576 | for (auto const* prev: nodes) { |
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 148 times.
|
148 | if (prev == n) { |
| 50 | kwerror("Circle found inside GC nodes"); | ||
| 51 | // circle should not happen | ||
| 52 | ✗ | return nullptr; | |
| 53 | } | ||
| 54 | } | ||
| 55 |
1/1✓ Branch 1 taken 428 times.
|
428 | nodes.push_back(n); |
| 56 | } | ||
| 57 | ✗ | return nullptr; | |
| 58 | 305 | } | |
| 59 | |||
| 60 | void BaseErrorPrinter::underline(Token const& tok) { | ||
| 61 |
1/1✓ Branch 2 taken 565 times.
|
565 | int32 tok_size = std::max(1, tok.end_col() - tok.begin_col()); |
| 62 |
1/1✓ Branch 1 taken 565 times.
|
565 | int32 tok_start = std::max(1, tok.begin_col()); |
| 63 |
4/4✓ Branch 3 taken 565 times.
✓ Branch 6 taken 565 times.
✓ Branch 10 taken 565 times.
✓ Branch 13 taken 565 times.
|
565 | codeline() << String(tok_start, ' ') << String(tok_size, '^'); |
| 64 | 565 | } | |
| 65 | |||
| 66 | } // namespace lython | ||
| 67 |