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 | 943663 | std::string CSTVisualizer::buildRule(antlr4::ParserRuleContext *ctx) { | |
10 |
1/2✓ Branch 1 taken 943663 times.
✗ Branch 2 not taken.
|
943663 | std::stringstream result; |
11 | |||
12 | // Prepare strings | ||
13 |
1/2✓ Branch 1 taken 943663 times.
✗ Branch 2 not taken.
|
943663 | const std::string codeLoc = tokenToCodeLoc(*ctx->start); |
14 |
2/4✓ Branch 1 taken 943663 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 943663 times.
✗ Branch 6 not taken.
|
943663 | const std::string ruleName = ruleNames[ctx->getRuleIndex()]; |
15 |
2/4✓ Branch 1 taken 943663 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 943663 times.
✗ Branch 5 not taken.
|
943663 | const std::string nodeId = codeLoc + "_" + ruleName; |
16 | |||
17 | // Build result | ||
18 |
4/8✓ Branch 1 taken 943663 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 943663 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 943663 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 943663 times.
✗ Branch 11 not taken.
|
943663 | result << nodeId << R"( [color="lightgreen",label=")" << ruleName << "\"];\n"; |
19 | |||
20 | // Link parent node with the current one | ||
21 |
1/2✓ Branch 1 taken 943663 times.
✗ Branch 2 not taken.
|
943663 | const std::string parentNodeIdBackup = parentNodeId; |
22 |
2/2✓ Branch 1 taken 943142 times.
✓ Branch 2 taken 521 times.
|
943663 | if (!parentNodeId.empty()) |
23 |
6/12✓ Branch 1 taken 943142 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 943142 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 943142 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 943142 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 943142 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 943142 times.
✗ Branch 17 not taken.
|
943142 | result << getSpaces() << parentNodeId << " -> " << nodeId << ";\n"; |
24 |
1/2✓ Branch 1 taken 943663 times.
✗ Branch 2 not taken.
|
943663 | parentNodeId = nodeId; // Set parentNodeId for children |
25 | |||
26 | // Visit all the children | ||
27 |
2/2✓ Branch 5 taken 1295900 times.
✓ Branch 6 taken 943663 times.
|
2239563 | for (const auto &child : ctx->children) { |
28 |
2/4✓ Branch 1 taken 1295900 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1295900 times.
✗ Branch 5 not taken.
|
1295900 | result << getSpaces(); |
29 | |||
30 |
3/4✓ Branch 0 taken 1295900 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 352758 times.
✓ Branch 3 taken 943142 times.
|
1295900 | 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 943142 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 943142 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 943142 times.
✗ Branch 8 not taken.
|
943142 | result << std::any_cast<std::string>(visit(child)); |
41 | } | ||
42 | } | ||
43 | |||
44 | // Restore parent node id | ||
45 |
1/2✓ Branch 1 taken 943663 times.
✗ Branch 2 not taken.
|
943663 | parentNodeId = parentNodeIdBackup; |
46 | |||
47 |
1/2✓ Branch 1 taken 943663 times.
✗ Branch 2 not taken.
|
1887326 | return result.str(); |
48 | 943663 | } | |
49 | |||
50 | 2591800 | std::string CSTVisualizer::getSpaces() const { | |
51 | 2591800 | std::string spaces; | |
52 |
2/2✓ Branch 0 taken 2591800 times.
✓ Branch 1 taken 2591800 times.
|
5183600 | for (int i = 0; i < currentTabs; i++) |
53 |
1/2✓ Branch 1 taken 2591800 times.
✗ Branch 2 not taken.
|
2591800 | spaces += " "; |
54 | 2591800 | return spaces; | |
55 | ✗ | } | |
56 | |||
57 | 1296421 | std::string CSTVisualizer::tokenToCodeLoc(const antlr4::Token &token) { | |
58 |
7/14✓ Branch 1 taken 1296421 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1296421 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1296421 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1296421 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1296421 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1296421 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 1296421 times.
✗ Branch 20 not taken.
|
1296421 | return "L" + std::to_string(token.getLine()) + "C" + std::to_string(token.getCharPositionInLine()); |
59 | } | ||
60 | |||
61 | } // namespace spice::compiler | ||
62 |