2017-01-15 22:03:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.ZDoom
|
|
|
|
|
{
|
|
|
|
|
internal class ZScriptTokenString : System.Attribute
|
|
|
|
|
{
|
|
|
|
|
public string Value { get; private set; }
|
|
|
|
|
|
|
|
|
|
public ZScriptTokenString(string value)
|
|
|
|
|
{
|
|
|
|
|
Value = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal enum ZScriptTokenType
|
|
|
|
|
{
|
|
|
|
|
// generic tokens
|
|
|
|
|
Identifier, // meow
|
|
|
|
|
Integer, // -666
|
|
|
|
|
Double, // 1.3
|
|
|
|
|
String, // "..."
|
|
|
|
|
Name, // '...'
|
|
|
|
|
|
|
|
|
|
// comments
|
|
|
|
|
LineComment, // // blablabla
|
|
|
|
|
BlockComment, // /* blablabla */
|
|
|
|
|
Whitespace, // whitespace is a legit token.
|
|
|
|
|
|
|
|
|
|
// invalid token
|
|
|
|
|
Invalid,
|
|
|
|
|
|
|
|
|
|
[ZScriptTokenString("#")] Preprocessor,
|
|
|
|
|
[ZScriptTokenString("\n")] Newline,
|
|
|
|
|
|
|
|
|
|
[ZScriptTokenString("{")] OpenCurly,
|
|
|
|
|
[ZScriptTokenString("}")] CloseCurly,
|
|
|
|
|
|
|
|
|
|
[ZScriptTokenString("(")] OpenParen,
|
|
|
|
|
[ZScriptTokenString(")")] CloseParen,
|
|
|
|
|
|
|
|
|
|
[ZScriptTokenString("[")] OpenSquare,
|
|
|
|
|
[ZScriptTokenString("]")] CloseSquare,
|
|
|
|
|
|
|
|
|
|
[ZScriptTokenString(".")] Dot,
|
|
|
|
|
[ZScriptTokenString(",")] Comma,
|
|
|
|
|
|
|
|
|
|
// == != < > <= >=
|
|
|
|
|
[ZScriptTokenString("==")] OpEquals,
|
2017-01-17 18:19:45 +00:00
|
|
|
|
[ZScriptTokenString("~==")] OpEqualsCaseInsensitive,
|
2017-01-15 22:03:44 +00:00
|
|
|
|
[ZScriptTokenString("!=")] OpNotEquals,
|
|
|
|
|
[ZScriptTokenString("<")] OpLessThan,
|
|
|
|
|
[ZScriptTokenString(">")] OpGreaterThan,
|
|
|
|
|
[ZScriptTokenString("<=")] OpLessOrEqual,
|
|
|
|
|
[ZScriptTokenString(">=")] OpGreaterOrEqual,
|
|
|
|
|
|
|
|
|
|
// ternary operator (x ? y : z), also the colon after state labels
|
|
|
|
|
[ZScriptTokenString("?")] Questionmark,
|
|
|
|
|
[ZScriptTokenString(":")] Colon,
|
2017-01-16 11:18:46 +00:00
|
|
|
|
[ZScriptTokenString("::")] DoubleColon,
|
2017-01-15 22:03:44 +00:00
|
|
|
|
|
|
|
|
|
// + - * / << >> ~ ^ & |
|
|
|
|
|
[ZScriptTokenString("+")] OpAdd,
|
|
|
|
|
[ZScriptTokenString("-")] OpSubtract,
|
|
|
|
|
[ZScriptTokenString("*")] OpMultiply,
|
|
|
|
|
[ZScriptTokenString("/")] OpDivide,
|
|
|
|
|
[ZScriptTokenString("<<")] OpLeftShift,
|
|
|
|
|
[ZScriptTokenString(">>")] OpRightShift,
|
|
|
|
|
[ZScriptTokenString("~")] OpNegate,
|
|
|
|
|
[ZScriptTokenString("^")] OpXor,
|
|
|
|
|
[ZScriptTokenString("&")] OpAnd,
|
|
|
|
|
[ZScriptTokenString("|")] OpOr,
|
2017-01-17 18:19:45 +00:00
|
|
|
|
[ZScriptTokenString("..")] OpStringConcat,
|
2017-01-15 22:03:44 +00:00
|
|
|
|
|
|
|
|
|
// = += -= *= /= <<= >>= ~= ^= &= |=
|
|
|
|
|
[ZScriptTokenString("=")] OpAssign,
|
|
|
|
|
[ZScriptTokenString("+=")] OpAssignAdd,
|
|
|
|
|
[ZScriptTokenString("-=")] OpAssignSubtract,
|
|
|
|
|
[ZScriptTokenString("*=")] OpAssignMultiply,
|
|
|
|
|
[ZScriptTokenString("/=")] OpAssignDivide,
|
|
|
|
|
[ZScriptTokenString("<<=")] OpAssignLeftShift,
|
|
|
|
|
[ZScriptTokenString(">>=")] OpAssignRightShift,
|
|
|
|
|
[ZScriptTokenString("~=")] OpAssignNegate,
|
|
|
|
|
[ZScriptTokenString("^=")] OpAssignXor,
|
|
|
|
|
[ZScriptTokenString("&=")] OpAssignAnd,
|
|
|
|
|
[ZScriptTokenString("|=")] OpAssignOr,
|
|
|
|
|
|
|
|
|
|
// unary: !
|
|
|
|
|
[ZScriptTokenString("!")] OpUnaryNot,
|
|
|
|
|
|
|
|
|
|
// semicolon
|
|
|
|
|
[ZScriptTokenString(";")] Semicolon
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class ZScriptToken
|
|
|
|
|
{
|
|
|
|
|
public ZScriptToken()
|
|
|
|
|
{
|
|
|
|
|
IsValid = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ZScriptTokenType Type { get; internal set; }
|
|
|
|
|
public string Value { get; internal set; }
|
|
|
|
|
public int ValueInt { get; internal set; }
|
|
|
|
|
public double ValueDouble { get; internal set; }
|
|
|
|
|
public bool IsValid { get; internal set; }
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return string.Format("<Token.{0} ({1})>", Type.ToString(), Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class ZScriptTokenizer
|
|
|
|
|
{
|
|
|
|
|
private BinaryReader reader;
|
2017-02-04 15:25:48 +00:00
|
|
|
|
private static Dictionary<string, ZScriptTokenType> namedtokentypes; // these are tokens that have precise equivalent in the enum (like operators)
|
|
|
|
|
private static Dictionary<ZScriptTokenType, string> namedtokentypesreverse; // these are tokens that have precise equivalent in the enum (like operators)
|
|
|
|
|
private static List<string> namedtokentypesorder; // this is the list of said tokens ordered by length.
|
2017-01-15 22:03:44 +00:00
|
|
|
|
|
2017-01-17 09:20:46 +00:00
|
|
|
|
public BinaryReader Reader { get { return reader; } }
|
2017-01-15 22:21:47 +00:00
|
|
|
|
public long LastPosition { get; private set; }
|
|
|
|
|
|
2017-01-15 22:03:44 +00:00
|
|
|
|
public ZScriptTokenizer(BinaryReader br)
|
|
|
|
|
{
|
|
|
|
|
reader = br;
|
|
|
|
|
|
2017-02-04 15:25:48 +00:00
|
|
|
|
if (namedtokentypes == null || namedtokentypesreverse == null || namedtokentypesorder == null)
|
2017-01-15 22:03:44 +00:00
|
|
|
|
{
|
2017-02-04 15:25:48 +00:00
|
|
|
|
namedtokentypes = new Dictionary<string, ZScriptTokenType>();
|
|
|
|
|
namedtokentypesreverse = new Dictionary<ZScriptTokenType, string>();
|
|
|
|
|
namedtokentypesorder = new List<string>();
|
|
|
|
|
// initialize the token type list.
|
|
|
|
|
IEnumerable<ZScriptTokenType> tokentypes = Enum.GetValues(typeof(ZScriptTokenType)).Cast<ZScriptTokenType>();
|
|
|
|
|
foreach (ZScriptTokenType tokentype in tokentypes)
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
FieldInfo fi = typeof(ZScriptTokenType).GetField(tokentype.ToString());
|
|
|
|
|
ZScriptTokenString[] attrs = (ZScriptTokenString[])fi.GetCustomAttributes(typeof(ZScriptTokenString), false);
|
|
|
|
|
if (attrs.Length == 0) continue;
|
|
|
|
|
//
|
|
|
|
|
namedtokentypes.Add(attrs[0].Value, tokentype);
|
|
|
|
|
namedtokentypesreverse.Add(tokentype, attrs[0].Value);
|
|
|
|
|
namedtokentypesorder.Add(attrs[0].Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namedtokentypesorder.Sort(delegate (string a, string b)
|
|
|
|
|
{
|
|
|
|
|
if (a.Length > b.Length)
|
|
|
|
|
return -1;
|
|
|
|
|
if (a.Length < b.Length)
|
|
|
|
|
return 1;
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-01-15 22:03:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SkipWhitespace() // note that this skips both whitespace, newlines AND comments
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
ZScriptToken tok = ExpectToken(ZScriptTokenType.Newline, ZScriptTokenType.BlockComment, ZScriptTokenType.LineComment, ZScriptTokenType.Whitespace);
|
|
|
|
|
if (tok == null || !tok.IsValid) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
private ZScriptToken TryReadWhitespace()
|
2017-01-15 22:03:44 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
long cpos = LastPosition = reader.BaseStream.Position;
|
|
|
|
|
char c = reader.ReadChar();
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
string whitespace = " \r\t\u00A0";
|
|
|
|
|
|
|
|
|
|
// check whitespace
|
|
|
|
|
if (whitespace.Contains(c))
|
|
|
|
|
{
|
|
|
|
|
string ws_content = "";
|
|
|
|
|
ws_content += c;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
char cnext = reader.ReadChar();
|
|
|
|
|
if (whitespace.Contains(cnext))
|
|
|
|
|
{
|
|
|
|
|
ws_content += cnext;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reader.BaseStream.Position--;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-01-15 22:03:44 +00:00
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
ZScriptToken tok = new ZScriptToken();
|
|
|
|
|
tok.Type = ZScriptTokenType.Whitespace;
|
|
|
|
|
tok.Value = ws_content;
|
|
|
|
|
return tok;
|
|
|
|
|
}
|
2017-01-15 22:03:44 +00:00
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
reader.BaseStream.Position = cpos;
|
|
|
|
|
return null;
|
2017-01-15 22:03:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
private ZScriptToken TryReadIdentifier()
|
2017-01-15 22:03:44 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
long cpos = LastPosition = reader.BaseStream.Position;
|
|
|
|
|
char c = reader.ReadChar();
|
|
|
|
|
|
|
|
|
|
// check identifier
|
|
|
|
|
if ((c >= 'a' && c <= 'z') ||
|
|
|
|
|
(c >= 'A' && c <= 'Z') ||
|
|
|
|
|
(c == '_'))
|
2017-01-15 22:03:44 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
string id_content = "";
|
|
|
|
|
id_content += c;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
char cnext = reader.ReadChar();
|
|
|
|
|
if ((cnext >= 'a' && cnext <= 'z') ||
|
|
|
|
|
(cnext >= 'A' && cnext <= 'Z') ||
|
|
|
|
|
(cnext == '_') ||
|
|
|
|
|
(cnext >= '0' && cnext <= '9'))
|
|
|
|
|
{
|
|
|
|
|
id_content += cnext;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-01-15 22:03:44 +00:00
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
reader.BaseStream.Position--;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-01-15 22:03:44 +00:00
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
ZScriptToken tok = new ZScriptToken();
|
|
|
|
|
tok.Type = ZScriptTokenType.Identifier;
|
|
|
|
|
tok.Value = id_content;
|
|
|
|
|
return tok;
|
|
|
|
|
}
|
2017-01-15 22:03:44 +00:00
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
reader.BaseStream.Position = cpos;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ZScriptToken TryReadNumber()
|
|
|
|
|
{
|
|
|
|
|
long cpos = LastPosition = reader.BaseStream.Position;
|
|
|
|
|
char c = reader.ReadChar();
|
|
|
|
|
|
|
|
|
|
// check integer
|
|
|
|
|
if ((c >= '0' && c <= '9') || c == '.')
|
|
|
|
|
{
|
|
|
|
|
bool isint = true;
|
|
|
|
|
bool isdouble = (c == '.');
|
|
|
|
|
bool isexponent = false;
|
|
|
|
|
if (isdouble) // make sure next character is an integer, otherwise its probably a member access
|
2017-01-15 22:03:44 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
char cnext = reader.ReadChar();
|
|
|
|
|
if (!(cnext >= '0' && cnext <= '9'))
|
2017-01-15 22:03:44 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
isint = false;
|
2017-01-16 11:18:46 +00:00
|
|
|
|
reader.BaseStream.Position--;
|
2017-01-15 22:03:44 +00:00
|
|
|
|
}
|
2017-01-16 11:18:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
if (isint)
|
2017-01-16 11:18:46 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
bool isoctal = (c == '0');
|
|
|
|
|
bool ishex = false;
|
|
|
|
|
string i_content = "";
|
|
|
|
|
i_content += c;
|
|
|
|
|
while (true)
|
2017-01-16 05:20:49 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
char cnext = reader.ReadChar();
|
|
|
|
|
if (!isdouble && (cnext == 'x') && i_content.Length == 1)
|
|
|
|
|
{
|
|
|
|
|
isoctal = false;
|
|
|
|
|
ishex = true;
|
|
|
|
|
}
|
|
|
|
|
else if ((cnext >= '0' && cnext <= '7') ||
|
|
|
|
|
(!isoctal && cnext >= '8' && cnext <= '9') ||
|
|
|
|
|
(ishex && ((cnext >= 'a' && cnext <= 'f') || (cnext >= 'A' && cnext <= 'F'))))
|
|
|
|
|
{
|
|
|
|
|
i_content += cnext;
|
|
|
|
|
}
|
|
|
|
|
else if (!ishex && !isdouble && !isexponent && cnext == '.')
|
|
|
|
|
{
|
|
|
|
|
isdouble = true;
|
|
|
|
|
isoctal = false;
|
|
|
|
|
i_content += '.';
|
|
|
|
|
}
|
|
|
|
|
else if (!isoctal && !ishex && !isexponent && (cnext == 'e' || cnext == 'E'))
|
|
|
|
|
{
|
|
|
|
|
isexponent = true;
|
|
|
|
|
isdouble = true;
|
|
|
|
|
i_content += 'e';
|
|
|
|
|
cnext = reader.ReadChar();
|
|
|
|
|
if (cnext == '-') i_content += '-';
|
|
|
|
|
else reader.BaseStream.Position--;
|
|
|
|
|
}
|
|
|
|
|
else
|
2017-01-16 05:20:49 +00:00
|
|
|
|
{
|
|
|
|
|
reader.BaseStream.Position--;
|
2017-01-16 08:20:47 +00:00
|
|
|
|
break;
|
2017-01-16 05:20:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
ZScriptToken tok = new ZScriptToken();
|
|
|
|
|
tok.Type = (isdouble ? ZScriptTokenType.Double : ZScriptTokenType.Integer);
|
|
|
|
|
tok.Value = i_content;
|
|
|
|
|
try
|
2017-01-16 05:20:49 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
if (ishex || isoctal || !isdouble)
|
2017-01-16 05:20:49 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
int numbase = 10;
|
|
|
|
|
if (ishex) numbase = 16;
|
|
|
|
|
else if (isoctal) numbase = 8;
|
|
|
|
|
tok.ValueInt = Convert.ToInt32(tok.Value, numbase);
|
|
|
|
|
tok.ValueDouble = tok.ValueInt;
|
|
|
|
|
}
|
|
|
|
|
else if (isdouble)
|
|
|
|
|
{
|
|
|
|
|
string dval = (tok.Value[0] == '.') ? "0" + tok.Value : tok.Value;
|
|
|
|
|
tok.ValueDouble = Convert.ToDouble(dval);
|
|
|
|
|
tok.ValueInt = (int)tok.ValueDouble;
|
2017-01-16 05:20:49 +00:00
|
|
|
|
}
|
2017-01-17 08:19:48 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
//throw new Exception(tok.ToString());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tok;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reader.BaseStream.Position = cpos;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ZScriptToken TryReadStringOrComment(bool allowstring, bool allowname, bool allowblock, bool allowline)
|
|
|
|
|
{
|
|
|
|
|
long cpos = LastPosition = reader.BaseStream.Position;
|
|
|
|
|
char c = reader.ReadChar();
|
2017-01-16 05:20:49 +00:00
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
switch (c)
|
|
|
|
|
{
|
|
|
|
|
case '/': // comment
|
|
|
|
|
{
|
|
|
|
|
if (!allowblock && !allowline) break;
|
|
|
|
|
char cnext = reader.ReadChar();
|
|
|
|
|
if (cnext == '/')
|
2017-01-16 05:20:49 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
if (!allowline) break;
|
|
|
|
|
// line comment: read until newline but not including it
|
|
|
|
|
string cmt = "";
|
2017-01-16 08:20:47 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
cnext = reader.ReadChar();
|
|
|
|
|
if (cnext == '\n')
|
2017-01-16 08:20:47 +00:00
|
|
|
|
{
|
|
|
|
|
reader.BaseStream.Position--;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
cmt += cnext;
|
2017-01-16 05:20:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
ZScriptToken tok = new ZScriptToken();
|
|
|
|
|
tok.Type = ZScriptTokenType.LineComment;
|
|
|
|
|
tok.Value = cmt;
|
2017-01-16 08:20:47 +00:00
|
|
|
|
return tok;
|
|
|
|
|
}
|
2017-01-17 08:19:48 +00:00
|
|
|
|
else if (cnext == '*')
|
2017-01-15 22:03:44 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
if (!allowblock) break;
|
|
|
|
|
// block comment: read until closing sequence
|
|
|
|
|
string cmt = "";
|
|
|
|
|
while (true)
|
2017-01-15 22:03:44 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
cnext = reader.ReadChar();
|
|
|
|
|
if (cnext == '*')
|
2017-01-15 22:03:44 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
char cnext2 = reader.ReadChar();
|
|
|
|
|
if (cnext2 == '/')
|
2017-01-15 22:03:44 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
reader.BaseStream.Position--;
|
2017-01-15 22:03:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
cmt += cnext;
|
2017-01-15 22:03:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
ZScriptToken tok = new ZScriptToken();
|
|
|
|
|
tok.Type = ZScriptTokenType.BlockComment;
|
|
|
|
|
tok.Value = cmt;
|
|
|
|
|
return tok;
|
2017-01-15 22:03:44 +00:00
|
|
|
|
}
|
2017-01-17 08:19:48 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2017-01-15 22:03:44 +00:00
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
case '"':
|
|
|
|
|
case '\'':
|
|
|
|
|
{
|
|
|
|
|
if ((c == '"' && !allowstring) || (c == '\'' && !allowname)) break;
|
|
|
|
|
ZScriptTokenType type = (c == '"' ? ZScriptTokenType.String : ZScriptTokenType.Name);
|
|
|
|
|
string s = "";
|
|
|
|
|
while (true)
|
2017-01-15 22:03:44 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
// todo: parse escape sequences properly
|
|
|
|
|
char cnext = reader.ReadChar();
|
|
|
|
|
if (cnext == '\\') // escape sequence. right now, do nothing
|
2017-01-15 22:03:44 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
cnext = reader.ReadChar();
|
|
|
|
|
s += cnext;
|
|
|
|
|
}
|
|
|
|
|
else if (cnext == c)
|
|
|
|
|
{
|
|
|
|
|
ZScriptToken tok = new ZScriptToken();
|
|
|
|
|
tok.Type = type;
|
|
|
|
|
tok.Value = s;
|
|
|
|
|
return tok;
|
2017-01-15 22:03:44 +00:00
|
|
|
|
}
|
2017-01-17 08:19:48 +00:00
|
|
|
|
else s += cnext;
|
2017-01-15 22:03:44 +00:00
|
|
|
|
}
|
2017-01-17 08:19:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reader.BaseStream.Position = cpos;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ZScriptToken TryReadNamedToken()
|
|
|
|
|
{
|
|
|
|
|
long cpos = LastPosition = reader.BaseStream.Position;
|
2017-01-15 22:03:44 +00:00
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
// named tokens
|
|
|
|
|
char[] namedtoken_buf = reader.ReadChars(namedtokentypesorder[0].Length);
|
|
|
|
|
string namedtoken = new string(namedtoken_buf);
|
|
|
|
|
foreach (string namedtokentype in namedtokentypesorder)
|
|
|
|
|
{
|
|
|
|
|
if (namedtoken.StartsWith(namedtokentype))
|
|
|
|
|
{
|
|
|
|
|
// found the token.
|
|
|
|
|
reader.BaseStream.Position = cpos + namedtokentype.Length;
|
|
|
|
|
ZScriptToken tok = new ZScriptToken();
|
|
|
|
|
tok.Type = namedtokentypes[namedtokentype];
|
|
|
|
|
tok.Value = namedtokentype;
|
|
|
|
|
return tok;
|
2017-01-15 22:03:44 +00:00
|
|
|
|
}
|
2017-01-17 08:19:48 +00:00
|
|
|
|
}
|
2017-01-15 22:03:44 +00:00
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
reader.BaseStream.Position = cpos;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-01-15 22:03:44 +00:00
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
public ZScriptToken ExpectToken(params ZScriptTokenType[] oneof)
|
|
|
|
|
{
|
|
|
|
|
long cpos = reader.BaseStream.Position;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// try to expect whitespace
|
|
|
|
|
if (oneof.Contains(ZScriptTokenType.Whitespace))
|
|
|
|
|
{
|
|
|
|
|
ZScriptToken tok = TryReadWhitespace();
|
|
|
|
|
if (tok != null) return tok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// try to expect an identifier
|
|
|
|
|
if (oneof.Contains(ZScriptTokenType.Identifier))
|
|
|
|
|
{
|
|
|
|
|
ZScriptToken tok = TryReadIdentifier();
|
|
|
|
|
if (tok != null) return tok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool blinecomment = oneof.Contains(ZScriptTokenType.LineComment);
|
|
|
|
|
bool bblockcomment = oneof.Contains(ZScriptTokenType.BlockComment);
|
|
|
|
|
bool bstring = oneof.Contains(ZScriptTokenType.String);
|
|
|
|
|
bool bname = oneof.Contains(ZScriptTokenType.Name);
|
|
|
|
|
|
|
|
|
|
if (bstring || bname || bblockcomment || blinecomment)
|
|
|
|
|
{
|
|
|
|
|
// try to expect a string
|
|
|
|
|
ZScriptToken tok = TryReadStringOrComment(bstring, bname, bblockcomment, blinecomment);
|
|
|
|
|
if (tok != null) return tok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if we are expecting a number (double or int), try to read it
|
|
|
|
|
if (oneof.Contains(ZScriptTokenType.Integer) || oneof.Contains(ZScriptTokenType.Double))
|
2017-01-15 22:03:44 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
ZScriptToken tok = TryReadNumber();
|
|
|
|
|
if (tok != null && oneof.Contains(tok.Type)) return tok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// try to expect special tokens
|
|
|
|
|
// read max
|
|
|
|
|
char[] namedtoken_buf = reader.ReadChars(namedtokentypesorder[0].Length);
|
|
|
|
|
string namedtoken = new string(namedtoken_buf);
|
|
|
|
|
foreach (ZScriptTokenType expected in oneof)
|
|
|
|
|
{
|
|
|
|
|
// check if there is a value for this one
|
|
|
|
|
if (!namedtokentypesreverse.ContainsKey(expected))
|
|
|
|
|
continue;
|
|
|
|
|
string namedtokentype = namedtokentypesreverse[expected];
|
|
|
|
|
if (namedtoken.StartsWith(namedtokentype))
|
2017-01-15 22:03:44 +00:00
|
|
|
|
{
|
2017-01-17 08:19:48 +00:00
|
|
|
|
// found the token.
|
|
|
|
|
reader.BaseStream.Position = cpos + namedtokentype.Length;
|
|
|
|
|
ZScriptToken tok = new ZScriptToken();
|
|
|
|
|
tok.Type = namedtokentypes[namedtokentype];
|
|
|
|
|
tok.Value = namedtokentype;
|
|
|
|
|
return tok;
|
2017-01-15 22:03:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-17 08:19:48 +00:00
|
|
|
|
}
|
2017-02-04 15:25:48 +00:00
|
|
|
|
catch (Exception)
|
2017-01-17 08:19:48 +00:00
|
|
|
|
{
|
2017-02-03 07:15:54 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
reader.BaseStream.Position = cpos;
|
|
|
|
|
}
|
2017-02-04 15:25:48 +00:00
|
|
|
|
catch (Exception)
|
2017-02-03 07:15:54 +00:00
|
|
|
|
{
|
|
|
|
|
/* ... */
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 08:19:48 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// token was not found, try to read the token that was actually found and return that
|
|
|
|
|
reader.BaseStream.Position = cpos;
|
|
|
|
|
ZScriptToken invalid = ReadToken();
|
2017-01-17 09:20:46 +00:00
|
|
|
|
if (invalid != null) invalid.IsValid = false;
|
2017-01-17 08:19:48 +00:00
|
|
|
|
reader.BaseStream.Position = cpos;
|
|
|
|
|
return invalid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// short_circuit only checks for string literals and "everything else", reporting "everything else" as invalid tokens
|
|
|
|
|
public ZScriptToken ReadToken(bool short_circuit = false)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ZScriptToken tok;
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
tok = TryReadWhitespace();
|
|
|
|
|
if (tok != null) return tok;
|
|
|
|
|
|
|
|
|
|
if (!short_circuit)
|
|
|
|
|
{
|
|
|
|
|
tok = TryReadIdentifier();
|
|
|
|
|
if (tok != null) return tok;
|
|
|
|
|
|
|
|
|
|
tok = TryReadNumber();
|
|
|
|
|
if (tok != null) return tok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tok = TryReadStringOrComment(true, true, true, true);
|
|
|
|
|
if (tok != null) return tok;
|
|
|
|
|
|
|
|
|
|
if (!short_circuit)
|
|
|
|
|
{
|
|
|
|
|
tok = TryReadNamedToken();
|
|
|
|
|
if (tok != null) return tok;
|
|
|
|
|
}
|
2017-01-15 22:03:44 +00:00
|
|
|
|
|
|
|
|
|
// token not found.
|
|
|
|
|
tok = new ZScriptToken();
|
|
|
|
|
tok.Type = ZScriptTokenType.Invalid;
|
2017-01-17 08:19:48 +00:00
|
|
|
|
tok.Value = "" + reader.ReadChar();
|
2017-01-15 22:03:44 +00:00
|
|
|
|
tok.IsValid = false;
|
|
|
|
|
return tok;
|
|
|
|
|
}
|
2017-02-04 15:25:48 +00:00
|
|
|
|
catch (Exception)
|
2017-01-15 22:03:44 +00:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-16 01:49:58 +00:00
|
|
|
|
|
|
|
|
|
public static string TokensToString(IEnumerable<ZScriptToken> tokens)
|
|
|
|
|
{
|
|
|
|
|
string outs = "";
|
|
|
|
|
foreach (ZScriptToken tok in tokens)
|
|
|
|
|
outs += tok.Value;
|
|
|
|
|
return outs;
|
|
|
|
|
}
|
2017-01-15 22:03:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|