| ◆◆ Luaをカスタマイズ。コメント文字を追加 ◆◆ |
|
賢いみなさんは覚えてると思うけど、Luaってソースからビルドできるんでしたよね。 ということは? カスタマイズできるということです。 気に入らない挙動を矯正したり? 機能を追加したり? なんだか楽しそうーーー。 でも Lua に不満なんてないのですよね。まるで仏様のように完璧です。 改造なんて畏れ多いことをしようとなどとは露ほども思いません。 と思ったけど1つだけあった。 |
| ◆◆ コメント文字を追加したい ◆◆ |
|
Luaではソースコード中にコメントを書くとき、文の冒頭に「--」と書きますよね。 これ、やだ。 やだったらやだ。 なんか全然コメントって感じがしないんだもん。 なんと言っても、目立たない。 ソースをわかりやすくするための機能としては、不足。不足ったら不足。 ていうかこれ、デクリメントじゃん。減らさないでー。 たとえ相手が仏でも、これだけは絶対に譲れない。 |
| ◆◆ コメント文字を定義しているソースをいじる ◆◆ |
|
該当のソースは、たぶん「llex.c」の 341行目付近です。 ちなみに Luaのバージョンは 5.1.4です。
static int llex (LexState *ls, SemInfo *seminfo) {
luaZ_resetbuffer(ls->buff);
for (;;) {
switch (ls->current) {
case '\n':
case '\r': {
inclinenumber(ls);
continue;
}
case '-': {
next(ls);
if (ls->current != '-') return '-';
/* else is a comment */
next(ls);
if (ls->current == '[') {
int sep = skip_sep(ls);
/* `skip_sep' may dirty the buffer */
luaZ_resetbuffer(ls->buff);
if (sep >= 0) {
read_long_string(ls, NULL, sep); /* long comment */
luaZ_resetbuffer(ls->buff);
continue;
}
}
/* else short comment */
while (!currIsNewline(ls) && ls->current != EOZ)
next(ls);
continue;
}
case '[': {
int sep = skip_sep(ls);
if (sep >= 0) {
read_long_string(ls, seminfo, sep);
return TK_STRING;
}
else if (sep == -1) return '[';
else luaX_lexerror
(ls, "invalid long string delimiter", TK_STRING);
}
正直よくわかんないんですけど、たぶん、この '-' という顔文字みたいなのが犯人です。
かわいい顔に騙されるな!この case文を丸ごとコピーして、すぐ下に貼り付け。 '-' を '/' にでも変えておきます。 もちろん、他の文字がよかったら、好きなように書くとよいですね。 一応、互換性のために、元の case文も消さずに残しておきます。
static int llex (LexState *ls, SemInfo *seminfo) {
luaZ_resetbuffer(ls->buff);
for (;;) {
switch (ls->current) {
case '\n':
case '\r': {
inclinenumber(ls);
continue;
}
case '-': {
next(ls);
if (ls->current != '-') return '-';
/* else is a comment */
next(ls);
if (ls->current == '[') {
int sep = skip_sep(ls);
/* `skip_sep' may dirty the buffer */
luaZ_resetbuffer(ls->buff);
if (sep >= 0) {
read_long_string(ls, NULL, sep); /* long comment */
luaZ_resetbuffer(ls->buff);
continue;
}
}
/* else short comment */
while (!currIsNewline(ls) && ls->current != EOZ)
next(ls);
continue;
}
case '/': {
next(ls);
if (ls->current != '/') return '/';
/* else is a comment */
next(ls);
if (ls->current == '[') {
int sep = skip_sep(ls);
/* `skip_sep' may dirty the buffer */
luaZ_resetbuffer(ls->buff);
if (sep >= 0) {
read_long_string(ls, NULL, sep); /* long comment */
luaZ_resetbuffer(ls->buff);
continue;
}
}
/* else short comment */
while (!currIsNewline(ls) && ls->current != EOZ)
next(ls);
continue;
}
case '[': {
int sep = skip_sep(ls);
if (sep >= 0) {
read_long_string(ls, seminfo, sep);
return TK_STRING;
}
else if (sep == -1) return '[';
else luaX_lexerror
(ls, "invalid long string delimiter", TK_STRING);
}
これでビルドすると、「//」もコメントを表す文字として認識してくれるようになります。 |
| ◆◆ ためしてみよう ◆◆ |
適当に Luaスクリプトを書きまして……、
// print("ここはコメント")
-- print("ここもコメント")
print("ここはコメントじゃない")
これを実行してみます。
おお、よさそうですね。南無Lua仏! ご唱和下さい!ちなみにこの改造ですが、ちゃんと理解してやってるわけではないので、 もしかしたら、思わぬ副作用があるかも。天罰! |