SILICON
Loading...
Searching...
No Matches
sisl.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
20
21#include <array>
22#include <regex>
23#include <string>
24
25namespace SILICON::ui {
26namespace {
27
28 // Keep these words aligned with the reserved keywords in SISL's parser.
29 constexpr auto DECLARATION_WORDS =
30 std::to_array<std::string_view>({"arch", "enum", "instr-format", "instr", "alias"});
31 constexpr auto PROPERTY_WORDS =
32 std::to_array<std::string_view>({"endianness", "encoding", "assembly", "width"});
33 constexpr auto TYPE_WORDS = std::to_array<std::string_view>({"bits", "uint", "sint"});
34 constexpr auto VALUE_WORDS = std::to_array<std::string_view>({"Little", "Big"});
35
36 constexpr std::array KEYWORD_GROUPS{
41 };
42
43 constexpr std::array TODO_RULES{CodeSyntaxContainedRule{
44 CodeSyntaxStyle::Todo, R"((?<![A-Za-z0-9_-])(?:TODO|FIXME)(?![A-Za-z0-9_-]))"}};
45 constexpr std::array ESCAPE_RULES{
46 CodeSyntaxContainedRule{CodeSyntaxStyle::Escape, R"(\\‍(?:["\\nrt]))"}};
47
48 constexpr std::array MATCH_RULES{
51 R"((?<![A-Za-z0-9_-])(?:0[bB][01]+|0[oO][0-7]+|0[xX][0-9a-fA-F]+|[0-9]+)(?![A-Za-z0-9_-]))",
52 {}},
53 CodeSyntaxMatchRule{CodeSyntaxStyle::Operator, R"([=;:{}\‍[\‍]<>(),])", {}},
54 };
55
56 constexpr std::array REGION_RULES{
58 CodeSyntaxStyle::Comment, R"(/\*)", R"(\*/)", {}, TODO_RULES, true},
60 CodeSyntaxStyle::Comment, R"(//)", R"($)", {}, TODO_RULES, false},
61 CodeSyntaxRegionRule{CodeSyntaxStyle::String, R"(")", R"(")", R"(\\.)", ESCAPE_RULES,
62 false},
63 };
64
65 [[nodiscard]] std::string sislIndentationFor(const CodeIndentationContext& context)
66 {
67 if (context.previousNonBlankLines.empty())
68 return {};
69
70 static const std::regex strippedPortions(
71 R"(//.*|/\*.*?(?:\*/|$)|"(?:\\.|[^"\\‍])*(?:"|$))", std::regex::optimize);
72 const auto previousLine = context.previousNonBlankLines.front();
73 const auto previousCode = indentation::codePortion(previousLine, strippedPortions);
74 const auto currentCode =
75 indentation::codePortion(context.currentLine, strippedPortions);
76 auto result = indentation::leadingWhitespace(previousLine);
77
78 const auto previous = indentation::trimRight(previousCode);
79 if (!previous.empty() && std::string_view("{([").contains(previous.back()))
80 indentation::addIndentLevel(result, context.indentWidth);
81
82 const auto current = indentation::trimLeft(currentCode);
83 if (!current.empty() && std::string_view("})]").contains(current.front()))
84 indentation::removeIndentLevel(result, context.indentWidth);
85
86 return result;
87 }
88
89 constexpr auto INDENTATION_TRIGGER_PATTERNS =
90 std::to_array<std::string_view>({R"(^\s*})", R"(^\s*\‍])", R"(^\s*\))"});
91
92 constexpr CodeIndentation SISL_INDENTATION{
93 .indentationFor = sislIndentationFor,
94 .triggerPatterns = INDENTATION_TRIGGER_PATTERNS,
95 .indentWidth = 2,
96 .expandPairsOnNewline = true,
97 };
98
99} // namespace
100
102 .keywordGroups = KEYWORD_GROUPS,
103 .matchRules = MATCH_RULES,
104 .regionRules = REGION_RULES,
105 .extraWordCharacters = "-",
106 .indentation = &SISL_INDENTATION,
107};
108
109} // namespace SILICON::ui
std::string leadingWhitespace(const std::string_view line)
std::string_view trimRight(std::string_view value)
std::string_view trimLeft(std::string_view value)
void addIndentLevel(std::string &indentation, const int indentWidth)
std::string codePortion(const std::string_view line, const std::regex &strippedPortions)
void removeIndentLevel(std::string &indentation, const int indentWidth)
const CodeSyntax SISL_CODE_SYNTAX
Definition sisl.cpp:101