Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2024 ChilliBits. All rights reserved. | ||
2 | |||
3 | #include "CSTVisualizer.h" | ||
4 | |||
5 | #include <util/CommonUtil.h> | ||
6 | |||
7 | namespace spice::compiler { | ||
8 | |||
9 | 952254 | std::string CSTVisualizer::buildRule(antlr4::ParserRuleContext *ctx) { | |
10 |
1/2✓ Branch 1 taken 952254 times.
✗ Branch 2 not taken.
|
952254 | std::stringstream result; |
11 | |||
12 | // Prepare strings | ||
13 |
1/2✓ Branch 1 taken 952254 times.
✗ Branch 2 not taken.
|
952254 | const std::string codeLoc = tokenToCodeLoc(*ctx->start); |
14 |
2/4✓ Branch 1 taken 952254 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 952254 times.
✗ Branch 6 not taken.
|
952254 | const std::string ruleName = ruleNames[ctx->getRuleIndex()]; |
15 |
2/4✓ Branch 1 taken 952254 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 952254 times.
✗ Branch 5 not taken.
|
952254 | const std::string nodeId = codeLoc + "_" + ruleName; |
16 | |||
17 | // Build result | ||
18 |
4/8✓ Branch 1 taken 952254 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 952254 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 952254 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 952254 times.
✗ Branch 11 not taken.
|
952254 | result << nodeId << R"( [color="lightgreen",label=")" << ruleName << "\"];\n"; |
19 | |||
20 | // Link parent node with the current one | ||
21 |
1/2✓ Branch 1 taken 952254 times.
✗ Branch 2 not taken.
|
952254 | const std::string parentNodeIdBackup = parentNodeId; |
22 |
2/2✓ Branch 1 taken 951733 times.
✓ Branch 2 taken 521 times.
|
952254 | if (!parentNodeId.empty()) |
23 |
6/12✓ Branch 1 taken 951733 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 951733 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 951733 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 951733 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 951733 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 951733 times.
✗ Branch 17 not taken.
|
951733 | result << getSpaces() << parentNodeId << " -> " << nodeId << ";\n"; |
24 |
1/2✓ Branch 1 taken 952254 times.
✗ Branch 2 not taken.
|
952254 | parentNodeId = nodeId; // Set parentNodeId for children |
25 | |||
26 | // Visit all the children | ||
27 |
2/2✓ Branch 5 taken 1304491 times.
✓ Branch 6 taken 952254 times.
|
2256745 | for (const auto &child : ctx->children) { |
28 |
2/4✓ Branch 1 taken 1304491 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1304491 times.
✗ Branch 5 not taken.
|
1304491 | result << getSpaces(); |
29 | |||
30 |
3/4✓ Branch 0 taken 1304491 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 352758 times.
✓ Branch 3 taken 951733 times.
|
1304491 | if (const auto token = dynamic_cast<antlr4::tree::TerminalNode *>(child)) { // Terminal node |
31 |
2/4✓ Branch 1 taken 352758 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 352758 times.
✗ Branch 5 not taken.
|
352758 | std::string terminalCodeLoc = tokenToCodeLoc(*token->getSymbol()); |
32 |
1/2✓ Branch 1 taken 352758 times.
✗ Branch 2 not taken.
|
352758 | std::string terminalText = token->getText(); |
33 |
3/6✓ Branch 1 taken 352758 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 352758 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 352758 times.
✗ Branch 8 not taken.
|
1411032 | CommonUtil::replaceAll(terminalText, "\\", "\\\\"); |
34 |
3/6✓ Branch 1 taken 352758 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 352758 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 352758 times.
✗ Branch 8 not taken.
|
1411032 | CommonUtil::replaceAll(terminalText, "\"", "\\\""); |
35 |
6/12✓ Branch 1 taken 352758 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 352758 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 352758 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 352758 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 352758 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 352758 times.
✗ Branch 17 not taken.
|
352758 | std::string terminalName = std::string(vocabulary.getSymbolicName(token->getSymbol()->getType())) + ": " + terminalText; |
36 | |||
37 |
4/8✓ Branch 1 taken 352758 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 352758 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 352758 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 352758 times.
✗ Branch 11 not taken.
|
352758 | result << terminalCodeLoc << R"( [color="lightblue",label=")" << terminalName << "\"];\n"; |
38 |
6/12✓ Branch 1 taken 352758 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 352758 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 352758 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 352758 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 352758 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 352758 times.
✗ Branch 17 not taken.
|
352758 | result << getSpaces() << nodeId << " -> " << terminalCodeLoc << "\n"; |
39 | 352758 | } else { // Non-terminal node | |
40 |
3/6✓ Branch 1 taken 951733 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 951733 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 951733 times.
✗ Branch 8 not taken.
|
951733 | result << std::any_cast<std::string>(visit(child)); |
41 | } | ||
42 | } | ||
43 | |||
44 | // Restore parent node id | ||
45 |
1/2✓ Branch 1 taken 952254 times.
✗ Branch 2 not taken.
|
952254 | parentNodeId = parentNodeIdBackup; |
46 | |||
47 |
1/2✓ Branch 1 taken 952254 times.
✗ Branch 2 not taken.
|
1904508 | return result.str(); |
48 | 952254 | } | |
49 | |||
50 | 2608982 | std::string CSTVisualizer::getSpaces() const { | |
51 | 2608982 | std::string spaces; | |
52 |
2/2✓ Branch 0 taken 2608982 times.
✓ Branch 1 taken 2608982 times.
|
5217964 | for (int i = 0; i < currentTabs; i++) |
53 |
1/2✓ Branch 1 taken 2608982 times.
✗ Branch 2 not taken.
|
2608982 | spaces += " "; |
54 | 2608982 | return spaces; | |
55 | ✗ | } | |
56 | |||
57 | 1305012 | std::string CSTVisualizer::tokenToCodeLoc(const antlr4::Token &token) { | |
58 |
7/14✓ Branch 1 taken 1305012 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1305012 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1305012 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1305012 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1305012 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1305012 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 1305012 times.
✗ Branch 20 not taken.
|
1305012 | return "L" + std::to_string(token.getLine()) + "C" + std::to_string(token.getCharPositionInLine()); |
59 | } | ||
60 | |||
61 | } // namespace spice::compiler | ||
62 |