/*
Monster - an advanced game scripting language
Copyright (C) 2007-2009 Nicolay Korslund
Email: <korslund@gmail.com>
WWW: http://monster.snaptoad.com/
This file (codetml.d) is part of the Monster script language
package.
Monster is distributed as free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 3, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
version 3 along with this program. If not, see
http://www.gnu.org/licenses/ .
*/
module codetml;
import monster.minibos.string;
import monster.minibos.stream;
import monster.minibos.stdio;
import monster.util.string : begins;
//import monster.minibos.stdio;
// Check if a character is alpha-numerical or an underscore
bool validIdentChar(char c)
{
if(validFirstIdentChar(c) || numericalChar(c))
return true;
return false;
}
// Same as above, except numbers are not allowed as the first
// character. Will extend to support UTF8 later.
bool validFirstIdentChar(char c)
{
if((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c == '_') ) return true;
return false;
}
bool numericalChar(char c)
{
return c >= '0' && c <= '9';
}
int keywordLookup[char[]];
int typeLookup[char[]];
static this()
{
foreach(int i, tok; keywordList)
keywordLookup[tok] = i;
foreach(int i, tok; typeList)
typeLookup[tok] = i;
}
// Index table of all basic types
const char[][] typeList =
[ "int", "float", "bool", "char", "double", "long", "uint", "ulong" ];
// Index table of all keywords
const char[][] keywordList =
[
"class", "return", "for", "this",
"new", "if", "else", "foreach",
"foreach_reverse", "do", "while",
"until", "continue", "break", "switch",
"select", "state", "typeof", "singleton",
"clone", "static", "const", "abstract",
"idle", "out", "ref", "public", "private",
"protected", "true", "false", "native",
"null", "goto", "halt", "auto", "var", "in", "import"
];
int lineNum;
void fail(char[] msg)
{
throw new Exception(format("%s: %s", lineNum, msg));
}
void main(char[][] args)
{
if(args.length != 2)
{
writefln("no input file");
return;
}
scope Stream inf = new BufferedFile(args[1]);
// Various parsing modes
enum
{
Normal, // Normal mode
Block, // Block comment
Nest // Nested block comment
}
int mode = Normal;
int nests = 0; // Nest level
writefln("<pre class=\"mcode\">");
foreach(ulong lnum, char[] line; inf)
{
lineNum = lnum;
void remWord(char[] str)
{
assert(line.begins(str));
line = line[str.length..$];
}
// Removes 'str' from the beginning of 'line', or from
// line[leadIn..$] if leadIn != 0.
void remWordWrite(char[] str, int leadIn)
{
assert(line.length >= leadIn+str.length);
writef("%s", line[0..leadIn+str.length]);
line = line[leadIn..$];
remWord(str);
}
void remRest()
{
writef("%s", line);
line = null;
}
line = stripr(line);
restart:
if(line.length == 0)
{
writefln();
continue;
}
if(mode == Block)
{
int index = line.find("*/");
// If we find a '*/', the comment is done
if(index != -1)
{
mode = Normal;
// Cut it the comment from the input
remWordWrite("*/", index);
writef("</span>");
}
else
{
// Comment not ended on this line, try the next
remRest();
}
// Start over
goto restart;
}
if(mode == Nest)
{
// Check for nested /+ and +/ in here, but go to restart if
// none is found (meaning the comment continues on the next
// line), or reset mode and go to restart if nest level ever
// gets to 0.
do
{
int incInd = -1;
int decInd = -1;
// Find the first matching '/+' or '+/
foreach(int i, char c; line[0..$-1])
{
if(c == '/' && line[i+1] == '+')
{
incInd = i;
break;
}
else if(c == '+' && line[i+1] == '/')
{
decInd = i;
break;
}
}
// Add a nest level when '/+' is found
if(incInd != -1)
{
remWordWrite("/+", incInd);
nests++;
continue; // Search more in this line
}
// Remove a nest level when '+/' is found
if(decInd != -1)
{
// Remove the +/ from input
remWordWrite("+/", decInd);
nests--; // Remove a level
assert(nests >= 0);
// Are we done? If so, return to normal mode.
if(nests == 0)
{
mode = Normal;
writef("</span>");
break;
}
continue;
}
// Nothing found on this line, try the next
remRest();
break;
}
while(line.length >= 2);
goto restart;
}
// Comment - start next line
if(line.begins("//"))
{
writef("<span class=\"m_comment\">%s</span>", line);
line = null;
goto restart;
}
// Block comment
if(line.begins("/*"))
{
mode = Block;
writef("<span class=\"m_comment\">/*");
line = line[2..$];
goto restart;
}
// Nested comment
if(line.begins("/+"))
{
mode = Nest;
writef("<span class=\"m_comment\">/+");
line = line[2..$];
nests++;
goto restart;
}
if(line.begins("*/")) fail("Unexpected end of block comment");
if(line.begins("+/")) fail("Unexpected end of nested comment");
// String literals (multi-line literals not implemented yet)
if(line.begins("\""))
{
int len = 1;
bool found = false;
foreach(char ch; line[1..$])
{
len++;
// No support for escape sequences as of now
if(ch == '"')
{
found = true;
break;
}
}
if(!found) fail("Unterminated string literal '" ~line~"'");
writef("<span class=\"m_string\">%s</span>", line[0..len]);
remWord(line[0..len]);
goto restart;
}
// Character literals (not parsed yet, so escape sequences like
// '\n', '\'', or unicode stuff won't work.)
if(line[0] == '\'')
{
if(line.length < 2 || line[2] != '\'')
fail("Malformed character literal " ~line);
writef("<span class=\"m_char\">%s</span>", line[0..3]);
remWord(line[0..3]);
goto restart;
}
// Numerical literals - if it starts with a number, we accept
// it, until it is interupted by an unacceptible character. We
// also accept numbers on the form .NUM. We do not try to parse
// the number here.
if(numericalChar(line[0]) ||
// Cover the .num case
( line.length >= 2 && line[0] == '.' &&
numericalChar(line[1]) ))
{
// Treat the rest as we would an identifier - the actual
// interpretation will be done later. We allow non-numerical
// tokens in the literal, such as 0x0a or 1_000_000. We must
// also explicitly allow '.' dots
int len = 1;
bool lastDot = false; // Was the last char a '.'?
foreach(char ch; line[1..$])
{
if(ch == '.')
{
// We accept "." but not "..", as this might be an
// operator.
if(lastDot)
{
len--; // Remove the last dot and exit.
break;
}
lastDot = true;
}
else
{
if(!validIdentChar(ch)) break;
lastDot = false;
}
len++;
}
writef("<span class=\"m_number\">%s</span>", line[0..len]);
remWord(line[0..len]);
goto restart;
}
// Check for identifiers
if(validFirstIdentChar(line[0]))
{
// It's an identifier or name, find the length
int len = 1;
foreach(char ch; line[1..$])
{
if(!validIdentChar(ch)) break;
len++;
}
char[] id = line[0..len];
// We only allow certain identifiers to begin with __, as
// these are reserved for internal use.
if(id.begins("__"))
if(id != "__STACK__")
fail("Identifier " ~ id ~ " is not allowed to begin with __");
// Check if this is a keyword
if(id in keywordLookup)
writef("<span class=\"m_keyword\">%s</span>", id);
// Type?
else if(id in typeLookup)
writef("<span class=\"m_type\">%s</span>", id);
else
// It's an identifier
writef("%s", id);
remWord(id);
goto restart;
}
// We don't know what the hell it is, so just print one char and
// hope it works out.
writef("%s", line[0]);
line = line[1..$];
goto restart;
}
if(mode == Block) fail("Unterminated block comment");
if(mode == Nest) fail("Unterminated nested comment");
writefln("</pre>");
}