SILICON
Toggle main menu visibility
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
18
#include <
ui/documents/code/codeSyntax.hpp
>
19
#include <
ui/documents/code/indentationUtils.hpp
>
20
21
#include <array>
22
#include <regex>
23
#include <string>
24
25
namespace
SILICON::ui
{
26
namespace
{
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{
37
CodeSyntaxKeywordGroup
{
CodeSyntaxStyle::Statement
, DECLARATION_WORDS},
38
CodeSyntaxKeywordGroup
{
CodeSyntaxStyle::Label
, PROPERTY_WORDS},
39
CodeSyntaxKeywordGroup
{
CodeSyntaxStyle::Global
, TYPE_WORDS},
40
CodeSyntaxKeywordGroup
{
CodeSyntaxStyle::Constant
, VALUE_WORDS},
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{
49
CodeSyntaxMatchRule
{
50
CodeSyntaxStyle::Number
,
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{
57
CodeSyntaxRegionRule
{
58
CodeSyntaxStyle::Comment
, R
"(/\*)", R
"(\*/)", {}, TODO_RULES,
true
},
59
CodeSyntaxRegionRule
{
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
101
const
CodeSyntax
SISL_CODE_SYNTAX
{
102
.keywordGroups = KEYWORD_GROUPS,
103
.matchRules = MATCH_RULES,
104
.regionRules = REGION_RULES,
105
.extraWordCharacters =
"-"
,
106
.indentation = &SISL_INDENTATION,
107
};
108
109
}
// namespace SILICON::ui
codeSyntax.hpp
indentationUtils.hpp
SILICON::ui::indentation::leadingWhitespace
std::string leadingWhitespace(const std::string_view line)
Definition
indentationUtils.cpp:40
SILICON::ui::indentation::trimRight
std::string_view trimRight(std::string_view value)
Definition
indentationUtils.cpp:33
SILICON::ui::indentation::trimLeft
std::string_view trimLeft(std::string_view value)
Definition
indentationUtils.cpp:25
SILICON::ui::indentation::addIndentLevel
void addIndentLevel(std::string &indentation, const int indentWidth)
Definition
indentationUtils.cpp:78
SILICON::ui::indentation::codePortion
std::string codePortion(const std::string_view line, const std::regex &strippedPortions)
Definition
indentationUtils.cpp:48
SILICON::ui::indentation::removeIndentLevel
void removeIndentLevel(std::string &indentation, const int indentWidth)
Definition
indentationUtils.cpp:83
SILICON::ui
Definition
actionGroups.hpp:14
SILICON::ui::CodeSyntaxStyle::Escape
@ Escape
Definition
codeSyntax.hpp:38
SILICON::ui::CodeSyntaxStyle::Comment
@ Comment
Definition
codeSyntax.hpp:33
SILICON::ui::CodeSyntaxStyle::String
@ String
Definition
codeSyntax.hpp:37
SILICON::ui::CodeSyntaxStyle::Statement
@ Statement
Definition
codeSyntax.hpp:27
SILICON::ui::CodeSyntaxStyle::Global
@ Global
Definition
codeSyntax.hpp:34
SILICON::ui::CodeSyntaxStyle::Label
@ Label
Definition
codeSyntax.hpp:28
SILICON::ui::CodeSyntaxStyle::Number
@ Number
Definition
codeSyntax.hpp:36
SILICON::ui::CodeSyntaxStyle::Todo
@ Todo
Definition
codeSyntax.hpp:31
SILICON::ui::CodeSyntaxStyle::Constant
@ Constant
Definition
codeSyntax.hpp:35
SILICON::ui::CodeSyntaxStyle::Operator
@ Operator
Definition
codeSyntax.hpp:32
SILICON::ui::SISL_CODE_SYNTAX
const CodeSyntax SISL_CODE_SYNTAX
Definition
sisl.cpp:101
SILICON::ui::CodeIndentationContext
Definition
codeSyntax.hpp:67
SILICON::ui::CodeIndentation
Definition
codeSyntax.hpp:76
SILICON::ui::CodeSyntaxContainedRule
Definition
codeSyntax.hpp:47
SILICON::ui::CodeSyntaxKeywordGroup
Definition
codeSyntax.hpp:42
SILICON::ui::CodeSyntaxMatchRule
Definition
codeSyntax.hpp:52
SILICON::ui::CodeSyntaxRegionRule
Definition
codeSyntax.hpp:58
SILICON::ui::CodeSyntax
Definition
codeSyntax.hpp:84
src
ui
documents
code
syntax
sisl.cpp
Generated by
1.17.0