TOP > Programming > Lua > Let's customize Lua to make comments on your own way

[Japanese]

# Let's customize Lua to make comments on your own way #
As you wise guys surely remember, we can build the Lua source code on our own.
That means.....?
Yes, we can even customize Lua.
Potentially we can make Lua behave whatever we like. That sounds fun, isn't it?

But fortunately we have the great Lua. She is as perfect as the Buddha.
Surely you never even think of customizing her at all. That's blasphemy!

Buuuuut! She has just one thing I must complain about.
#### How Come We Have To Put '--' For Comments? ####
In Lua, we are supposed to put '--' on top of any comments.
I don't want this. Definitely. Absolutely. No matter whaaat!
It doesn't look like a comment.
Above all, that's unnoticable at all.

Comments are totally important for programming making source code easier to understand.
In other words comments must be easy to notice.
To acomplish this task, we couldn't choose '--'. That's too moderate.
And to make matters worse, this looks like a decrementation. Don't decrement our comments! Please!

Our Lua is as holy as the Buddha. But even so we must protest for our right.
#### Modify the Lexical Specification ####
Lua's lexical specification is seemingly implemented in 'llex.c'. Let's see around the 341th line. Just to let you know this Lua's version is 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);
					      }
				
To be honest I don't understand what this bunch of code means but hopefully the '-' in this code is the culprit.
That looks like a face symbol and fuckin' cute but it doesn't mean innocent. Don't let it tempt you!

Just copy this entire case block and past it right below there.
Don't forget to modify '-' into '/' or something else you like.
In order to keep interchangability, let the old case block remain there.

					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);
					      }
				
Okay, let's build this modified source.
It'll tern out to recognize '//' as a comment notation.
#### Let's try ####
Describe a Lua script as it comes up...
					// print("This is a comment.")
					-- print("This is a comment, too.")
					print("This is not a comment.")
				
Let's run that.

Oh, looks cute! We worship thee, Lua the Buddha! Let's chant togather for her!


Note that I recklessly commited this blasphemous customization without knowing exactly how Lua works.
So there is a possibility that it causes a disastrous side effect.
That's a devine panishment. Repent!