38 constexpr auto STATEMENT_WORDS = std::to_array<std::string_view>({
39 std::string_view{
"always"},
102 "pulsestyle_onevent",
103 "pulsestyle_ondetect",
149 constexpr auto LABEL_WORDS =
150 std::to_array<std::string_view>({
"begin",
"end",
"fork",
"join"});
151 constexpr auto CONDITIONAL_WORDS = std::to_array<std::string_view>(
152 {
"if",
"else",
"case",
"casex",
"casez",
"default",
"endcase"});
153 constexpr auto REPEAT_WORDS =
154 std::to_array<std::string_view>({
"forever",
"repeat",
"while",
"for"});
156 constexpr std::array KEYWORD_GROUPS{
165 constexpr std::array ESCAPE_RULES{
168 constexpr std::array MATCH_RULES{
172 R"((?<![A-Za-z0-9_?])[A-Z][A-Z0-9_]+(?![A-Za-z0-9_?]))",
176 R"((?<![A-Za-z0-9_?])(?:[0-9]+)?'[sS]?[bB]\s*[0-1_xXzZ?]+(?![A-Za-z0-9_?]))",
180 R"((?<![A-Za-z0-9_?])(?:[0-9]+)?'[sS]?[oO]\s*[0-7_xXzZ?]+(?![A-Za-z0-9_?]))",
184 R"((?<![A-Za-z0-9_?])(?:[0-9]+)?'[sS]?[dD]\s*[0-9_xXzZ?]+(?![A-Za-z0-9_?]))",
188 R"((?<![A-Za-z0-9_?])(?:[0-9]+)?'[sS]?[hH]\s*[0-9a-fA-F_xXzZ?]+(?![A-Za-z0-9_?]))",
192 R"((?<![A-Za-z0-9_?])[+-]?[0-9_]+(?:\.[0-9_]*)?(?:e[0-9_]*)?(?![A-Za-z0-9_?]))",
196 R"(`(?:celldefine|default_nettype|define|else|elsif|endcelldefine|endif|ifdef|ifndef|include|line|nounconnected_drive|resetall|timescale|unconnected_drive|undef)(?![A-Za-z0-9_?]))",
203 constexpr std::array REGION_RULES{
205 R
"(//\s*synopsys dc_script_begin\b)",
206 R"(//\s*synopsys dc_script_end\b)",
211 R
"(//\s*\$s dc_script_begin\b)",
212 R"(//\s*\$s dc_script_end\b)",
232 [[nodiscard]]
bool startsWithBlockStatement(
const std::string_view code)
234 static const std::regex expression(
235 R
"(^\s*(?:(?:end)\s+)?(?:if|else)\b|^\s*(?:for|case|casex|casez|always|initial|specify|fork)\b)",
236 std::regex::optimize);
240 [[nodiscard]]
bool startsWithDeclarationBlock(
const std::string_view code)
242 static const std::regex expression(
243 R
"(^\s*(?:function|task|config|generate|primitive|table)\b)",
244 std::regex::optimize);
248 [[nodiscard]]
bool startsWithModule(
const std::string_view code)
250 static const std::regex expression(R
"(^\s*module\b)", std::regex::optimize);
254 [[nodiscard]]
bool startsWithEndModule(
const std::string_view code)
256 static const std::regex expression(R
"(^\s*endmodule\b)", std::regex::optimize);
260 [[nodiscard]]
bool startsWithPreprocessorBranch(
const std::string_view code)
262 static const std::regex expression(R
"(^\s*`(?:ifdef|ifndef|elsif|else)\b)",
263 std::regex::optimize);
267 [[nodiscard]]
bool startsWithPreprocessorCloser(
const std::string_view code)
269 static const std::regex expression(R
"(^\s*`(?:elsif|else|endif)\b)",
270 std::regex::optimize);
274 [[nodiscard]]
bool startsWithBlockCloser(
const std::string_view code)
276 static const std::regex expression(
277 R
"(^\s*(?:join|end|endcase|endfunction|endtask|endspecify|endconfig|endgenerate|endprimitive|endtable)\b)",
278 std::regex::optimize);
282 [[nodiscard]]
bool startsWithStandaloneBegin(
const std::string_view code)
284 static const std::regex expression(R
"(^\s*begin\b)", std::regex::optimize);
288 [[nodiscard]]
bool containsBegin(
const std::string_view code)
290 static const std::regex expression(R
"(\bbegin\b)", std::regex::optimize);
294 [[nodiscard]]
bool endsWithBegin(
const std::string_view code)
296 static const std::regex expression(R
"(\bbegin\b(?:\s*:\s*\w+)?\s*$)",
297 std::regex::optimize);
301 [[nodiscard]]
bool isTerminatedBlockStatement(
const std::string_view code)
304 return trimmed.ends_with(
';') ||
endsWithWord(trimmed,
"end");
307 [[nodiscard]]
bool isOpenStatement(
const std::string_view code)
314 constexpr std::string_view OPEN_CHARACTERS =
"*(,{><+-/%^&|!=?:";
315 return OPEN_CHARACTERS.contains(trimmed.back());
318 [[nodiscard]]
bool isCaseLabel(
const std::string_view code)
320 static const std::regex expression(R
"(^\s*[^=!]+\s*:\s*$)", std::regex::optimize);
324 [[nodiscard]]
bool closesMultilineStatement(
const std::string_view code)
327 if (!trimmed.ends_with(
';'))
329 const auto beforeSemicolon =
trimRight(trimmed.substr(0, trimmed.size() - 1));
330 return !beforeSemicolon.empty();
333 [[nodiscard]]
bool isOnlyClosingStatement(
const std::string_view code)
335 static const std::regex expression(R
"(^\s*\)*\s*;?\s*$)", std::regex::optimize);
336 return std::regex_match(code.begin(), code.end(), expression);
339 [[nodiscard]]
bool closesMultilineComment(
const std::string_view line)
342 if (!trimmed.ends_with(
"*/"))
344 const auto closing = trimmed.rfind(
"*/");
345 const auto opening = trimmed.rfind(
"/*", closing);
346 return opening == std::string_view::npos;
351 if (context.previousNonBlankLines.empty())
354 const auto previousLine = context.previousNonBlankLines[0];
355 const auto olderLine = context.previousNonBlankLines.size() > 1
356 ? context.previousNonBlankLines[1]
357 : std::string_view{};
358 static const std::regex strippedPortions(
359 R
"(//.*|/\*.*?(?:\*/|$)|"(?:\\.|[^"\\])*(?:"|$))", std::regex::optimize);
360 const auto previousCode =
codePortion(previousLine, strippedPortions);
361 const auto olderCode =
codePortion(olderLine, strippedPortions);
362 const auto currentCode =
codePortion(context.currentLine, strippedPortions);
367 if (closesMultilineComment(previousLine)) {
370 }
else if (startsWithBlockStatement(previousCode)) {
371 if (!isTerminatedBlockStatement(previousCode))
373 }
else if (startsWithDeclarationBlock(previousCode)) {
376 }
else if (startsWithModule(previousCode)) {
377 const auto trimmed =
trimRight(previousCode);
378 if (!trimmed.empty() && (trimmed.back() ==
'(' || trimmed.back() ==
','))
380 }
else if (endsWithBegin(previousCode)
381 && (!isOpenStatement(olderCode) || isCaseLabel(olderCode))) {
383 }
else if (!containsBegin(previousCode) && startsWithBlockStatement(olderCode)
384 && !isOpenStatement(olderCode) && !containsBegin(olderCode)) {
386 }
else if (isOpenStatement(previousCode) && !isOpenStatement(olderCode)) {
388 }
else if (closesMultilineStatement(previousCode)
389 && !isOnlyClosingStatement(previousCode) && isOpenStatement(olderCode)
392 }
else if (startsWithPreprocessorBranch(previousCode)) {
397 if (startsWithBlockCloser(currentCode)) {
399 }
else if (startsWithEndModule(currentCode)) {
401 }
else if (startsWithStandaloneBegin(currentCode)) {
402 const bool previousStartsDeclaration =
403 startsWithDeclarationBlock(previousCode) || startsWithModule(previousCode)
405 std::regex(R
"(^\s*specify\b)", std::regex::optimize));
406 if (!previousStartsDeclaration && !isOnlyClosingStatement(previousCode)
407 && (startsWithBlockStatement(previousCode)
408 ||
trimRight(previousCode).ends_with(
')')
409 || isOpenStatement(previousCode))) {
412 }
else if (
trimLeft(currentCode).starts_with(
')')
413 && (isOpenStatement(previousCode) || isOpenStatement(olderCode))) {
415 }
else if (startsWithPreprocessorCloser(currentCode)) {
422 constexpr auto INDENTATION_TRIGGER_PATTERNS = std::to_array<std::string_view>({
424 R
"((?<![A-Za-z0-9_?])(?:begin|end|join|endcase|endmodule|endfunction|endtask|endspecify|endconfig|endgenerate|endprimitive|endtable)\s*$)"},
426 R"(^\s*`(?:else|elsif|endif)\b)",
430 .indentationFor = verilogIndentationFor,
431 .triggerPatterns = INDENTATION_TRIGGER_PATTERNS,
438 .keywordGroups = KEYWORD_GROUPS,
439 .matchRules = MATCH_RULES,
440 .regionRules = REGION_RULES,
441 .extraWordCharacters =
"?",
442 .indentation = &VERILOG_INDENTATION,
std::string leadingWhitespace(const std::string_view line)
std::string_view trimRight(std::string_view value)
bool endsWithWord(const std::string_view value, const std::string_view word)
std::string_view trimLeft(std::string_view value)
bool regexSearch(const std::string_view value, const std::regex &expression)
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)
std::string leadingWhitespace(const std::string_view line)
std::string_view trimRight(std::string_view value)
bool endsWithWord(const std::string_view value, const std::string_view word)
std::string_view trimLeft(std::string_view value)
bool regexSearch(const std::string_view value, const std::regex &expression)
void addIndentLevel(std::string &indentation, const int indentWidth)
const CodeSyntax VERILOG_CODE_SYNTAX
std::string codePortion(const std::string_view line, const std::regex &strippedPortions)
void removeIndentLevel(std::string &indentation, const int indentWidth)