From d030d54978cb1ff6d5cc91c22046db5d7d1bb8ba Mon Sep 17 00:00:00 2001 From: simonkrauter Date: Sun, 17 Aug 2025 18:36:01 -0300 Subject: [PATCH] Nim: Separate built-in identifiers from keywords This change introduces a distinction between keywords and built-in identifiers in the Nim lexer. Details: - Add a second keyword list `builtin_identifiers`. - Assign new style `SCE_NIM_WORD2` to built-in identifiers. - The implementation was roughly copied from the Python lexer. --- include/SciLexer.h | 17 +++++++++-------- lexers/LexNim.cxx | 36 ++++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/include/SciLexer.h b/include/SciLexer.h index 7bdf69006..5afe7179e 100644 --- a/include/SciLexer.h +++ b/include/SciLexer.h @@ -1932,14 +1932,15 @@ #define SCE_NIM_STRING 6 #define SCE_NIM_CHARACTER 7 #define SCE_NIM_WORD 8 -#define SCE_NIM_TRIPLE 9 -#define SCE_NIM_TRIPLEDOUBLE 10 -#define SCE_NIM_BACKTICKS 11 -#define SCE_NIM_FUNCNAME 12 -#define SCE_NIM_STRINGEOL 13 -#define SCE_NIM_NUMERROR 14 -#define SCE_NIM_OPERATOR 15 -#define SCE_NIM_IDENTIFIER 16 +#define SCE_NIM_WORD2 9 +#define SCE_NIM_TRIPLE 10 +#define SCE_NIM_TRIPLEDOUBLE 11 +#define SCE_NIM_BACKTICKS 12 +#define SCE_NIM_FUNCNAME 13 +#define SCE_NIM_STRINGEOL 14 +#define SCE_NIM_NUMERROR 15 +#define SCE_NIM_OPERATOR 16 +#define SCE_NIM_IDENTIFIER 17 #define SCE_CIL_DEFAULT 0 #define SCE_CIL_COMMENT 1 #define SCE_CIL_COMMENTLINE 2 diff --git a/lexers/LexNim.cxx b/lexers/LexNim.cxx index 8686729cf..b5bb6036c 100644 --- a/lexers/LexNim.cxx +++ b/lexers/LexNim.cxx @@ -181,6 +181,7 @@ struct OptionsNim { static const char *const nimWordListDesc[] = { "Keywords", + "Built-in identifiers", nullptr }; @@ -208,14 +209,15 @@ LexicalClass lexicalClasses[] = { 6, "SCE_NIM_STRING", "literal string", "String", 7, "SCE_NIM_CHARACTER", "literal string", "Single quoted string", 8, "SCE_NIM_WORD", "keyword", "Keyword", - 9, "SCE_NIM_TRIPLE", "literal string", "Triple quotes", - 10, "SCE_NIM_TRIPLEDOUBLE", "literal string", "Triple double quotes", - 11, "SCE_NIM_BACKTICKS", "operator definition", "Identifiers", - 12, "SCE_NIM_FUNCNAME", "identifier", "Function name definition", - 13, "SCE_NIM_STRINGEOL", "error literal string", "String is not closed", - 14, "SCE_NIM_NUMERROR", "numeric error", "Numeric format error", - 15, "SCE_NIM_OPERATOR", "operator", "Operators", - 16, "SCE_NIM_IDENTIFIER", "identifier", "Identifiers", + 9, "SCE_NIM_WORD2", "keyword", "Built-in identifiers", + 10, "SCE_NIM_TRIPLE", "literal string", "Triple quotes", + 11, "SCE_NIM_TRIPLEDOUBLE", "literal string", "Triple double quotes", + 12, "SCE_NIM_BACKTICKS", "operator definition", "Identifiers", + 13, "SCE_NIM_FUNCNAME", "identifier", "Function name definition", + 14, "SCE_NIM_STRINGEOL", "error literal string", "String is not closed", + 15, "SCE_NIM_NUMERROR", "numeric error", "Numeric format error", + 16, "SCE_NIM_OPERATOR", "operator", "Operators", + 17, "SCE_NIM_IDENTIFIER", "identifier", "Identifiers", }; } @@ -223,6 +225,7 @@ LexicalClass lexicalClasses[] = { class LexerNim : public DefaultLexer { CharacterSet setWord; WordList keywords; + WordList keywords2; OptionsNim options; OptionSetNim osNim; @@ -300,6 +303,9 @@ Sci_Position SCI_METHOD LexerNim::WordListSet(int n, const char *wl) { case 0: wordListN = &keywords; break; + case 1: + wordListN = &keywords2; + break; } Sci_Position firstModification = -1; @@ -449,20 +455,26 @@ void SCI_METHOD LexerNim::Lex(Sci_PositionU startPos, Sci_Position length, sc.GetCurrent(s, sizeof(s)); int style = SCE_NIM_IDENTIFIER; - if (keywords.InList(s) && !funcNameExists) { + if (funcNameExists) { + style = SCE_NIM_FUNCNAME; + } else if (keywords.InList(s)) { // Prevent styling keywords if they are sub-identifiers const Sci_Position segStart = styler.GetStartSegment() - 1; if (segStart < 0 || styler.SafeGetCharAt(segStart, '\0') != '.') { style = SCE_NIM_WORD; } - } else if (funcNameExists) { - style = SCE_NIM_FUNCNAME; + } else if (keywords2.InList(s)) { + // Prevent styling keywords if they are sub-identifiers + const Sci_Position segStart = styler.GetStartSegment() - 1; + if (segStart < 0 || styler.SafeGetCharAt(segStart, '\0') != '.') { + style = SCE_NIM_WORD2; + } } sc.ChangeState(style); sc.SetState(SCE_NIM_DEFAULT); - if (style == SCE_NIM_WORD) { + if (style == SCE_NIM_WORD || style == SCE_NIM_WORD2) { funcNameExists = IsFuncName(s); } else { funcNameExists = false;