mruby-bin-mirb: consolidate duplicate keyword lists

Move the Ruby keyword array from static definitions in both
mirb_highlight.c and mirb_completion.c to a single shared
mirb_keywords[] defined in mirb_highlight.c and declared in
mirb_highlight.h.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-01-31 09:19:28 +09:00
parent aafa05522d
commit 5394b71a60
3 changed files with 15 additions and 18 deletions
@@ -5,6 +5,7 @@
*/
#include "mirb_completion.h"
#include "mirb_highlight.h"
#include <mruby/array.h>
#include <mruby/class.h>
#include <mruby/compile.h>
@@ -58,17 +59,6 @@ strndup(const char *s, size_t n)
#define ISALNUM(c) isalnum((unsigned char)(c))
#endif
/* Ruby keywords */
static const char *ruby_keywords[] = {
"BEGIN", "END", "__ENCODING__", "__FILE__", "__LINE__",
"alias", "and", "begin", "break", "case", "class", "def",
"defined?", "do", "else", "elsif", "end", "ensure", "false",
"for", "if", "in", "module", "next", "nil", "not", "or",
"redo", "rescue", "retry", "return", "self", "super", "then",
"true", "undef", "unless", "until", "when", "while", "yield",
NULL
};
/* ============================================================
* Core Completion Engine
* ============================================================ */
@@ -377,8 +367,8 @@ void
mirb_complete_keywords(mirb_completion_ctx *ctx)
{
int i;
for (i = 0; ruby_keywords[i] != NULL; i++) {
mirb_add_completion(ctx, ruby_keywords[i]);
for (i = 0; mirb_keywords[i] != NULL; i++) {
mirb_add_completion(ctx, mirb_keywords[i]);
}
}
@@ -43,16 +43,17 @@
#define COLOR_RESET "\033[0m"
/* Keyword list - must be sorted alphabetically for bsearch */
static const char *keywords[] = {
/* Keyword list - sorted alphabetically for bsearch, NULL-terminated */
const char *mirb_keywords[] = {
"BEGIN", "END", "__ENCODING__", "__FILE__", "__LINE__",
"alias", "and", "begin", "break", "case", "class", "def",
"defined?", "do", "else", "elsif", "end", "ensure", "false",
"for", "if", "in", "module", "next", "nil", "not", "or",
"redo", "rescue", "retry", "return", "self", "super", "then",
"true", "undef", "unless", "until", "when", "while", "yield"
"true", "undef", "unless", "until", "when", "while", "yield",
NULL
};
#define NUM_KEYWORDS (sizeof(keywords) / sizeof(keywords[0]))
const size_t mirb_num_keywords = sizeof(mirb_keywords) / sizeof(mirb_keywords[0]) - 1;
static int
keyword_cmp(const void *a, const void *b)
@@ -68,7 +69,7 @@ is_keyword(const char *word, size_t len)
if (len >= sizeof(buf)) return FALSE;
memcpy(buf, word, len);
buf[len] = '\0';
return bsearch(buf, keywords, NUM_KEYWORDS, sizeof(keywords[0]), keyword_cmp) != NULL;
return bsearch(buf, mirb_keywords, mirb_num_keywords, sizeof(mirb_keywords[0]), keyword_cmp) != NULL;
}
static mrb_bool
@@ -48,6 +48,12 @@ typedef struct mirb_highlighter {
mrb_bool in_regexp;
} mirb_highlighter;
/*
* Ruby keyword list (sorted alphabetically, NULL-terminated)
*/
extern const char *mirb_keywords[];
extern const size_t mirb_num_keywords;
/*
* Initialize highlighter with auto-detected or specified theme
*/