2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* gd_tokenizer.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:11:42 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "gd_tokenizer.h"
|
2017-08-27 19:07:15 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "gd_functions.h"
|
2014-02-25 12:31:47 +00:00
|
|
|
#include "io/marshalls.h"
|
|
|
|
#include "map.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "print_string.h"
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const char *GDTokenizer::token_names[TK_MAX] = {
|
|
|
|
"Empty",
|
|
|
|
"Identifier",
|
|
|
|
"Constant",
|
|
|
|
"Self",
|
|
|
|
"Built-In Type",
|
|
|
|
"Built-In Func",
|
|
|
|
"In",
|
|
|
|
"'=='",
|
|
|
|
"'!='",
|
|
|
|
"'<'",
|
|
|
|
"'<='",
|
|
|
|
"'>'",
|
|
|
|
"'>='",
|
|
|
|
"'and'",
|
|
|
|
"'or'",
|
|
|
|
"'not'",
|
|
|
|
"'+'",
|
|
|
|
"'-'",
|
|
|
|
"'*'",
|
|
|
|
"'/'",
|
|
|
|
"'%'",
|
|
|
|
"'<<'",
|
|
|
|
"'>>'",
|
|
|
|
"'='",
|
|
|
|
"'+='",
|
|
|
|
"'-='",
|
|
|
|
"'*='",
|
|
|
|
"'/='",
|
|
|
|
"'%='",
|
|
|
|
"'<<='",
|
|
|
|
"'>>='",
|
|
|
|
"'&='",
|
|
|
|
"'|='",
|
|
|
|
"'^='",
|
|
|
|
"'&'",
|
|
|
|
"'|'",
|
|
|
|
"'^'",
|
|
|
|
"'~'",
|
|
|
|
//"Plus Plus",
|
|
|
|
//"Minus Minus",
|
|
|
|
"if",
|
|
|
|
"elif",
|
|
|
|
"else",
|
|
|
|
"for",
|
|
|
|
"do",
|
|
|
|
"while",
|
|
|
|
"switch (reserved)",
|
|
|
|
"case (reserved)",
|
|
|
|
"break",
|
|
|
|
"continue",
|
|
|
|
"pass",
|
|
|
|
"return",
|
|
|
|
"match",
|
|
|
|
"func",
|
|
|
|
"class",
|
|
|
|
"extends",
|
2017-05-26 17:45:39 +00:00
|
|
|
"is",
|
2017-03-05 15:44:50 +00:00
|
|
|
"onready",
|
|
|
|
"tool",
|
|
|
|
"static",
|
|
|
|
"export",
|
|
|
|
"setget",
|
|
|
|
"const",
|
|
|
|
"var",
|
|
|
|
"enum",
|
|
|
|
"preload",
|
|
|
|
"assert",
|
|
|
|
"yield",
|
|
|
|
"signal",
|
|
|
|
"breakpoint",
|
|
|
|
"rpc",
|
|
|
|
"sync",
|
|
|
|
"master",
|
|
|
|
"slave",
|
|
|
|
"'['",
|
|
|
|
"']'",
|
|
|
|
"'{'",
|
|
|
|
"'}'",
|
|
|
|
"'('",
|
|
|
|
"')'",
|
|
|
|
"','",
|
|
|
|
"';'",
|
|
|
|
"'.'",
|
|
|
|
"'?'",
|
|
|
|
"':'",
|
2017-03-31 18:53:22 +00:00
|
|
|
"'$'",
|
2017-03-05 15:44:50 +00:00
|
|
|
"'\\n'",
|
|
|
|
"PI",
|
|
|
|
"_",
|
|
|
|
"INF",
|
|
|
|
"NAN",
|
|
|
|
"Error",
|
|
|
|
"EOF",
|
|
|
|
"Cursor"
|
|
|
|
};
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-31 17:28:34 +00:00
|
|
|
struct _bit {
|
|
|
|
Variant::Type type;
|
|
|
|
const char *text;
|
|
|
|
};
|
|
|
|
//built in types
|
|
|
|
|
|
|
|
static const _bit _type_list[] = {
|
|
|
|
//types
|
|
|
|
{ Variant::BOOL, "bool" },
|
|
|
|
{ Variant::INT, "int" },
|
|
|
|
{ Variant::REAL, "float" },
|
|
|
|
{ Variant::STRING, "String" },
|
|
|
|
{ Variant::VECTOR2, "Vector2" },
|
|
|
|
{ Variant::RECT2, "Rect2" },
|
|
|
|
{ Variant::TRANSFORM2D, "Transform2D" },
|
|
|
|
{ Variant::VECTOR3, "Vector3" },
|
|
|
|
{ Variant::RECT3, "Rect3" },
|
|
|
|
{ Variant::PLANE, "Plane" },
|
|
|
|
{ Variant::QUAT, "Quat" },
|
|
|
|
{ Variant::BASIS, "Basis" },
|
|
|
|
{ Variant::TRANSFORM, "Transform" },
|
|
|
|
{ Variant::COLOR, "Color" },
|
|
|
|
{ Variant::_RID, "RID" },
|
|
|
|
{ Variant::OBJECT, "Object" },
|
|
|
|
{ Variant::NODE_PATH, "NodePath" },
|
|
|
|
{ Variant::DICTIONARY, "Dictionary" },
|
|
|
|
{ Variant::ARRAY, "Array" },
|
|
|
|
{ Variant::POOL_BYTE_ARRAY, "PoolByteArray" },
|
|
|
|
{ Variant::POOL_INT_ARRAY, "PoolIntArray" },
|
2017-07-20 09:51:15 +00:00
|
|
|
{ Variant::POOL_REAL_ARRAY, "PoolRealArray" },
|
2017-03-31 17:28:34 +00:00
|
|
|
{ Variant::POOL_STRING_ARRAY, "PoolStringArray" },
|
|
|
|
{ Variant::POOL_VECTOR2_ARRAY, "PoolVector2Array" },
|
|
|
|
{ Variant::POOL_VECTOR3_ARRAY, "PoolVector3Array" },
|
|
|
|
{ Variant::POOL_COLOR_ARRAY, "PoolColorArray" },
|
|
|
|
{ Variant::VARIANT_MAX, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _kws {
|
|
|
|
GDTokenizer::Token token;
|
|
|
|
const char *text;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const _kws _keyword_list[] = {
|
|
|
|
//ops
|
|
|
|
{ GDTokenizer::TK_OP_IN, "in" },
|
|
|
|
{ GDTokenizer::TK_OP_NOT, "not" },
|
|
|
|
{ GDTokenizer::TK_OP_OR, "or" },
|
|
|
|
{ GDTokenizer::TK_OP_AND, "and" },
|
|
|
|
//func
|
|
|
|
{ GDTokenizer::TK_PR_FUNCTION, "func" },
|
|
|
|
{ GDTokenizer::TK_PR_CLASS, "class" },
|
|
|
|
{ GDTokenizer::TK_PR_EXTENDS, "extends" },
|
|
|
|
{ GDTokenizer::TK_PR_IS, "is" },
|
|
|
|
{ GDTokenizer::TK_PR_ONREADY, "onready" },
|
|
|
|
{ GDTokenizer::TK_PR_TOOL, "tool" },
|
|
|
|
{ GDTokenizer::TK_PR_STATIC, "static" },
|
|
|
|
{ GDTokenizer::TK_PR_EXPORT, "export" },
|
|
|
|
{ GDTokenizer::TK_PR_SETGET, "setget" },
|
|
|
|
{ GDTokenizer::TK_PR_VAR, "var" },
|
|
|
|
{ GDTokenizer::TK_PR_PRELOAD, "preload" },
|
|
|
|
{ GDTokenizer::TK_PR_ASSERT, "assert" },
|
|
|
|
{ GDTokenizer::TK_PR_YIELD, "yield" },
|
|
|
|
{ GDTokenizer::TK_PR_SIGNAL, "signal" },
|
|
|
|
{ GDTokenizer::TK_PR_BREAKPOINT, "breakpoint" },
|
|
|
|
{ GDTokenizer::TK_PR_REMOTE, "remote" },
|
|
|
|
{ GDTokenizer::TK_PR_MASTER, "master" },
|
|
|
|
{ GDTokenizer::TK_PR_SLAVE, "slave" },
|
|
|
|
{ GDTokenizer::TK_PR_SYNC, "sync" },
|
|
|
|
{ GDTokenizer::TK_PR_CONST, "const" },
|
|
|
|
{ GDTokenizer::TK_PR_ENUM, "enum" },
|
|
|
|
//controlflow
|
|
|
|
{ GDTokenizer::TK_CF_IF, "if" },
|
|
|
|
{ GDTokenizer::TK_CF_ELIF, "elif" },
|
|
|
|
{ GDTokenizer::TK_CF_ELSE, "else" },
|
|
|
|
{ GDTokenizer::TK_CF_FOR, "for" },
|
|
|
|
{ GDTokenizer::TK_CF_WHILE, "while" },
|
|
|
|
{ GDTokenizer::TK_CF_DO, "do" },
|
|
|
|
{ GDTokenizer::TK_CF_SWITCH, "switch" },
|
|
|
|
{ GDTokenizer::TK_CF_CASE, "case" },
|
|
|
|
{ GDTokenizer::TK_CF_BREAK, "break" },
|
|
|
|
{ GDTokenizer::TK_CF_CONTINUE, "continue" },
|
|
|
|
{ GDTokenizer::TK_CF_RETURN, "return" },
|
|
|
|
{ GDTokenizer::TK_CF_MATCH, "match" },
|
|
|
|
{ GDTokenizer::TK_CF_PASS, "pass" },
|
|
|
|
{ GDTokenizer::TK_SELF, "self" },
|
|
|
|
{ GDTokenizer::TK_CONST_PI, "PI" },
|
|
|
|
{ GDTokenizer::TK_WILDCARD, "_" },
|
|
|
|
{ GDTokenizer::TK_CONST_INF, "INF" },
|
|
|
|
{ GDTokenizer::TK_CONST_NAN, "NAN" },
|
|
|
|
{ GDTokenizer::TK_ERROR, NULL }
|
|
|
|
};
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
const char *GDTokenizer::get_token_name(Token p_token) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_token, TK_MAX, "<error>");
|
2014-02-10 01:10:30 +00:00
|
|
|
return token_names[p_token];
|
|
|
|
}
|
|
|
|
|
2017-03-31 17:28:34 +00:00
|
|
|
bool GDTokenizer::is_token_literal(int p_offset, bool variable_safe) const {
|
|
|
|
switch (get_token(p_offset)) {
|
|
|
|
// Can always be literal:
|
|
|
|
case TK_IDENTIFIER:
|
|
|
|
|
|
|
|
case TK_PR_ONREADY:
|
|
|
|
case TK_PR_TOOL:
|
|
|
|
case TK_PR_STATIC:
|
|
|
|
case TK_PR_EXPORT:
|
|
|
|
case TK_PR_SETGET:
|
|
|
|
case TK_PR_SIGNAL:
|
|
|
|
case TK_PR_REMOTE:
|
|
|
|
case TK_PR_MASTER:
|
|
|
|
case TK_PR_SLAVE:
|
|
|
|
case TK_PR_SYNC:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Literal for non-variables only:
|
|
|
|
case TK_BUILT_IN_TYPE:
|
|
|
|
case TK_BUILT_IN_FUNC:
|
|
|
|
|
|
|
|
case TK_OP_IN:
|
2017-07-25 18:01:19 +00:00
|
|
|
//case TK_OP_NOT:
|
2017-03-31 17:28:34 +00:00
|
|
|
//case TK_OP_OR:
|
|
|
|
//case TK_OP_AND:
|
|
|
|
|
|
|
|
case TK_PR_CLASS:
|
|
|
|
case TK_PR_CONST:
|
|
|
|
case TK_PR_ENUM:
|
|
|
|
case TK_PR_PRELOAD:
|
|
|
|
case TK_PR_FUNCTION:
|
|
|
|
case TK_PR_EXTENDS:
|
|
|
|
case TK_PR_ASSERT:
|
|
|
|
case TK_PR_YIELD:
|
|
|
|
case TK_PR_VAR:
|
|
|
|
|
|
|
|
case TK_CF_IF:
|
|
|
|
case TK_CF_ELIF:
|
|
|
|
case TK_CF_ELSE:
|
|
|
|
case TK_CF_FOR:
|
|
|
|
case TK_CF_WHILE:
|
|
|
|
case TK_CF_DO:
|
|
|
|
case TK_CF_SWITCH:
|
|
|
|
case TK_CF_CASE:
|
|
|
|
case TK_CF_BREAK:
|
|
|
|
case TK_CF_CONTINUE:
|
|
|
|
case TK_CF_RETURN:
|
|
|
|
case TK_CF_MATCH:
|
|
|
|
case TK_CF_PASS:
|
|
|
|
case TK_SELF:
|
|
|
|
case TK_CONST_PI:
|
|
|
|
case TK_WILDCARD:
|
|
|
|
case TK_CONST_INF:
|
|
|
|
case TK_CONST_NAN:
|
|
|
|
case TK_ERROR:
|
|
|
|
return !variable_safe;
|
|
|
|
|
|
|
|
case TK_CONSTANT: {
|
|
|
|
switch (get_token_constant(p_offset).get_type()) {
|
|
|
|
case Variant::NIL:
|
|
|
|
case Variant::BOOL:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringName GDTokenizer::get_token_literal(int p_offset) const {
|
|
|
|
Token token = get_token(p_offset);
|
|
|
|
switch (token) {
|
|
|
|
case TK_IDENTIFIER:
|
|
|
|
return get_token_identifier(p_offset);
|
|
|
|
case TK_BUILT_IN_TYPE: {
|
|
|
|
Variant::Type type = get_token_type(p_offset);
|
|
|
|
int idx = 0;
|
|
|
|
|
|
|
|
while (_type_list[idx].text) {
|
|
|
|
if (type == _type_list[idx].type) {
|
|
|
|
return _type_list[idx].text;
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
} break; // Shouldn't get here, stuff happens
|
|
|
|
case TK_BUILT_IN_FUNC:
|
|
|
|
return GDFunctions::get_func_name(get_token_built_in_func(p_offset));
|
|
|
|
case TK_CONSTANT: {
|
|
|
|
const Variant value = get_token_constant(p_offset);
|
|
|
|
|
|
|
|
switch (value.get_type()) {
|
|
|
|
case Variant::NIL:
|
|
|
|
return "null";
|
|
|
|
case Variant::BOOL:
|
|
|
|
return value ? "true" : "false";
|
|
|
|
default: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case TK_OP_AND:
|
|
|
|
case TK_OP_OR:
|
|
|
|
break; // Don't get into default, since they can be non-literal
|
|
|
|
default: {
|
|
|
|
int idx = 0;
|
|
|
|
|
|
|
|
while (_keyword_list[idx].text) {
|
|
|
|
if (token == _keyword_list[idx].token) {
|
|
|
|
return _keyword_list[idx].text;
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ERR_EXPLAIN("Failed to get token literal");
|
|
|
|
ERR_FAIL_V("");
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
static bool _is_text_char(CharType c) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool _is_number(CharType c) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return (c >= '0' && c <= '9');
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool _is_hex(CharType c) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void GDTokenizerText::_make_token(Token p_type) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
TokenData &tk = tk_rb[tk_rb_pos];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tk.type = p_type;
|
|
|
|
tk.line = line;
|
|
|
|
tk.col = column;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tk_rb_pos = (tk_rb_pos + 1) % TK_RB_SIZE;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
void GDTokenizerText::_make_identifier(const StringName &p_identifier) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
TokenData &tk = tk_rb[tk_rb_pos];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tk.type = TK_IDENTIFIER;
|
|
|
|
tk.identifier = p_identifier;
|
|
|
|
tk.line = line;
|
|
|
|
tk.col = column;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tk_rb_pos = (tk_rb_pos + 1) % TK_RB_SIZE;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void GDTokenizerText::_make_built_in_func(GDFunctions::Function p_func) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
TokenData &tk = tk_rb[tk_rb_pos];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tk.type = TK_BUILT_IN_FUNC;
|
|
|
|
tk.func = p_func;
|
|
|
|
tk.line = line;
|
|
|
|
tk.col = column;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tk_rb_pos = (tk_rb_pos + 1) % TK_RB_SIZE;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
void GDTokenizerText::_make_constant(const Variant &p_constant) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
TokenData &tk = tk_rb[tk_rb_pos];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tk.type = TK_CONSTANT;
|
|
|
|
tk.constant = p_constant;
|
|
|
|
tk.line = line;
|
|
|
|
tk.col = column;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tk_rb_pos = (tk_rb_pos + 1) % TK_RB_SIZE;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void GDTokenizerText::_make_type(const Variant::Type &p_type) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
TokenData &tk = tk_rb[tk_rb_pos];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tk.type = TK_BUILT_IN_TYPE;
|
|
|
|
tk.vtype = p_type;
|
|
|
|
tk.line = line;
|
|
|
|
tk.col = column;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tk_rb_pos = (tk_rb_pos + 1) % TK_RB_SIZE;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void GDTokenizerText::_make_error(const String &p_error) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
error_flag = true;
|
|
|
|
last_error = p_error;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
TokenData &tk = tk_rb[tk_rb_pos];
|
|
|
|
tk.type = TK_ERROR;
|
|
|
|
tk.constant = p_error;
|
|
|
|
tk.line = line;
|
|
|
|
tk.col = column;
|
|
|
|
tk_rb_pos = (tk_rb_pos + 1) % TK_RB_SIZE;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
void GDTokenizerText::_make_newline(int p_spaces) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
TokenData &tk = tk_rb[tk_rb_pos];
|
|
|
|
tk.type = TK_NEWLINE;
|
|
|
|
tk.constant = p_spaces;
|
|
|
|
tk.line = line;
|
|
|
|
tk.col = column;
|
|
|
|
tk_rb_pos = (tk_rb_pos + 1) % TK_RB_SIZE;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
void GDTokenizerText::_advance() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (error_flag) {
|
|
|
|
//parser broke
|
|
|
|
_make_error(last_error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (code_pos >= len) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_token(TK_EOF);
|
|
|
|
return;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
#define GETCHAR(m_ofs) ((m_ofs + code_pos) >= len ? 0 : _code[m_ofs + code_pos])
|
|
|
|
#define INCPOS(m_amount) \
|
|
|
|
{ \
|
|
|
|
code_pos += m_amount; \
|
|
|
|
column += m_amount; \
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
while (true) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool is_node_path = false;
|
|
|
|
StringMode string_mode = STRING_DOUBLE_QUOTE;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (GETCHAR(0)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
case 0:
|
|
|
|
_make_token(TK_EOF);
|
|
|
|
break;
|
2014-04-05 21:50:09 +00:00
|
|
|
case '\\':
|
|
|
|
INCPOS(1);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(0) == '\r') {
|
2014-04-05 21:50:09 +00:00
|
|
|
INCPOS(1);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(0) != '\n') {
|
2014-04-05 21:50:09 +00:00
|
|
|
_make_error("Expected newline after '\\'.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
INCPOS(1);
|
2016-01-02 16:56:58 +00:00
|
|
|
line++;
|
2014-04-05 21:50:09 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (GETCHAR(0) == ' ' || GETCHAR(0) == '\t') {
|
2014-04-05 21:50:09 +00:00
|
|
|
INCPOS(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2014-02-10 01:10:30 +00:00
|
|
|
case '\t':
|
|
|
|
case '\r':
|
|
|
|
case ' ':
|
|
|
|
INCPOS(1);
|
|
|
|
continue;
|
|
|
|
case '\n': {
|
|
|
|
line++;
|
|
|
|
INCPOS(1);
|
2017-03-05 15:44:50 +00:00
|
|
|
column = 1;
|
|
|
|
int i = 0;
|
|
|
|
while (GETCHAR(i) == ' ' || GETCHAR(i) == '\t') {
|
2014-02-10 01:10:30 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
_make_newline(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case '#': { // line comment skip
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (GETCHAR(0) != '\n') {
|
2014-02-10 01:10:30 +00:00
|
|
|
code_pos++;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(0) == 0) { //end of file
|
2014-09-20 00:01:41 +00:00
|
|
|
//_make_error("Unterminated Comment");
|
|
|
|
_make_token(TK_EOF);
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
INCPOS(1);
|
2017-03-05 15:44:50 +00:00
|
|
|
column = 1;
|
2014-02-10 01:10:30 +00:00
|
|
|
line++;
|
2017-03-05 15:44:50 +00:00
|
|
|
int i = 0;
|
|
|
|
while (GETCHAR(i) == ' ' || GETCHAR(i) == '\t') {
|
2014-02-10 01:10:30 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
_make_newline(i);
|
|
|
|
return;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case '/': {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (GETCHAR(1)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
case '=': { // diveq
|
|
|
|
|
|
|
|
_make_token(TK_OP_ASSIGN_DIV);
|
|
|
|
INCPOS(1);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
_make_token(TK_OP_DIV);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case '=': {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(1) == '=') {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_token(TK_OP_EQUAL);
|
|
|
|
INCPOS(1);
|
|
|
|
|
|
|
|
} else
|
|
|
|
_make_token(TK_OP_ASSIGN);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case '<': {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(1) == '=') {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_make_token(TK_OP_LESS_EQUAL);
|
|
|
|
INCPOS(1);
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (GETCHAR(1) == '<') {
|
|
|
|
if (GETCHAR(2) == '=') {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_token(TK_OP_ASSIGN_SHIFT_LEFT);
|
|
|
|
INCPOS(1);
|
|
|
|
} else {
|
|
|
|
_make_token(TK_OP_SHIFT_LEFT);
|
|
|
|
}
|
|
|
|
INCPOS(1);
|
|
|
|
} else
|
|
|
|
_make_token(TK_OP_LESS);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case '>': {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(1) == '=') {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_token(TK_OP_GREATER_EQUAL);
|
|
|
|
INCPOS(1);
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (GETCHAR(1) == '>') {
|
|
|
|
if (GETCHAR(2) == '=') {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_token(TK_OP_ASSIGN_SHIFT_RIGHT);
|
|
|
|
INCPOS(1);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
_make_token(TK_OP_SHIFT_RIGHT);
|
|
|
|
}
|
|
|
|
INCPOS(1);
|
|
|
|
} else {
|
|
|
|
_make_token(TK_OP_GREATER);
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case '!': {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(1) == '=') {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_token(TK_OP_NOT_EQUAL);
|
|
|
|
INCPOS(1);
|
|
|
|
} else {
|
|
|
|
_make_token(TK_OP_NOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
//case '"' //string - no strings in shader
|
|
|
|
//case '\'' //string - no strings in shader
|
|
|
|
case '{':
|
|
|
|
_make_token(TK_CURLY_BRACKET_OPEN);
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
_make_token(TK_CURLY_BRACKET_CLOSE);
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
_make_token(TK_BRACKET_OPEN);
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
_make_token(TK_BRACKET_CLOSE);
|
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
_make_token(TK_PARENTHESIS_OPEN);
|
|
|
|
break;
|
|
|
|
case ')':
|
|
|
|
_make_token(TK_PARENTHESIS_CLOSE);
|
|
|
|
break;
|
|
|
|
case ',':
|
|
|
|
_make_token(TK_COMMA);
|
|
|
|
break;
|
|
|
|
case ';':
|
|
|
|
_make_token(TK_SEMICOLON);
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
_make_token(TK_QUESTION_MARK);
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
_make_token(TK_COLON); //for methods maybe but now useless.
|
|
|
|
break;
|
2017-01-08 05:04:53 +00:00
|
|
|
case '$':
|
|
|
|
_make_token(TK_DOLLAR); //for the get_node() shortener
|
|
|
|
break;
|
2014-02-10 01:10:30 +00:00
|
|
|
case '^': {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(1) == '=') {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_token(TK_OP_ASSIGN_BIT_XOR);
|
|
|
|
INCPOS(1);
|
|
|
|
} else {
|
|
|
|
_make_token(TK_OP_BIT_XOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case '~':
|
|
|
|
_make_token(TK_OP_BIT_INVERT);
|
|
|
|
break;
|
|
|
|
case '&': {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(1) == '&') {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_make_token(TK_OP_AND);
|
|
|
|
INCPOS(1);
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (GETCHAR(1) == '=') {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_token(TK_OP_ASSIGN_BIT_AND);
|
|
|
|
INCPOS(1);
|
|
|
|
} else {
|
|
|
|
_make_token(TK_OP_BIT_AND);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case '|': {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(1) == '|') {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_make_token(TK_OP_OR);
|
|
|
|
INCPOS(1);
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (GETCHAR(1) == '=') {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_token(TK_OP_ASSIGN_BIT_OR);
|
|
|
|
INCPOS(1);
|
|
|
|
} else {
|
|
|
|
_make_token(TK_OP_BIT_OR);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case '*': {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(1) == '=') {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_token(TK_OP_ASSIGN_MUL);
|
|
|
|
INCPOS(1);
|
|
|
|
} else {
|
|
|
|
_make_token(TK_OP_MUL);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case '+': {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(1) == '=') {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_token(TK_OP_ASSIGN_ADD);
|
|
|
|
INCPOS(1);
|
2017-03-05 15:44:50 +00:00
|
|
|
/*
|
2017-01-14 11:26:56 +00:00
|
|
|
} else if (GETCHAR(1)=='+') {
|
|
|
|
_make_token(TK_OP_PLUS_PLUS);
|
|
|
|
INCPOS(1);
|
|
|
|
*/
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
_make_token(TK_OP_ADD);
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case '-': {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(1) == '=') {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_token(TK_OP_ASSIGN_SUB);
|
|
|
|
INCPOS(1);
|
2017-03-05 15:44:50 +00:00
|
|
|
/*
|
2017-01-14 11:26:56 +00:00
|
|
|
} else if (GETCHAR(1)=='-') {
|
|
|
|
_make_token(TK_OP_MINUS_MINUS);
|
|
|
|
INCPOS(1);
|
|
|
|
*/
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
_make_token(TK_OP_SUB);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case '%': {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(1) == '=') {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_token(TK_OP_ASSIGN_MOD);
|
|
|
|
INCPOS(1);
|
|
|
|
} else {
|
|
|
|
_make_token(TK_OP_MOD);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case '@':
|
2017-03-05 15:44:50 +00:00
|
|
|
if (CharType(GETCHAR(1)) != '"' && CharType(GETCHAR(1)) != '\'') {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_error("Unexpected '@'");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
INCPOS(1);
|
2017-03-05 15:44:50 +00:00
|
|
|
is_node_path = true;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-10-26 09:33:22 +00:00
|
|
|
case '\'':
|
2014-02-10 01:10:30 +00:00
|
|
|
case '"': {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(0) == '\'')
|
|
|
|
string_mode = STRING_SINGLE_QUOTE;
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int i = 1;
|
|
|
|
if (string_mode == STRING_DOUBLE_QUOTE && GETCHAR(i) == '"' && GETCHAR(i + 1) == '"') {
|
|
|
|
i += 2;
|
|
|
|
string_mode = STRING_MULTILINE;
|
2014-11-02 14:31:01 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
String str;
|
2017-03-05 15:44:50 +00:00
|
|
|
while (true) {
|
|
|
|
if (CharType(GETCHAR(i)) == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_make_error("Unterminated String");
|
|
|
|
return;
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (string_mode == STRING_DOUBLE_QUOTE && CharType(GETCHAR(i)) == '"') {
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (string_mode == STRING_SINGLE_QUOTE && CharType(GETCHAR(i)) == '\'') {
|
2014-11-02 14:31:01 +00:00
|
|
|
break;
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (string_mode == STRING_MULTILINE && CharType(GETCHAR(i)) == '\"' && CharType(GETCHAR(i + 1)) == '\"' && CharType(GETCHAR(i + 2)) == '\"') {
|
|
|
|
i += 2;
|
2014-11-02 14:31:01 +00:00
|
|
|
break;
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (string_mode != STRING_MULTILINE && CharType(GETCHAR(i)) == '\n') {
|
2014-11-02 14:31:01 +00:00
|
|
|
_make_error("Unexpected EOL at String.");
|
|
|
|
return;
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (CharType(GETCHAR(i)) == 0xFFFF) {
|
2015-06-26 04:14:31 +00:00
|
|
|
//string ends here, next will be TK
|
|
|
|
i--;
|
|
|
|
break;
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (CharType(GETCHAR(i)) == '\\') {
|
2014-02-10 01:10:30 +00:00
|
|
|
//escaped characters...
|
|
|
|
i++;
|
|
|
|
CharType next = GETCHAR(i);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (next == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_error("Unterminated String");
|
|
|
|
return;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
CharType res = 0;
|
|
|
|
|
|
|
|
switch (next) {
|
|
|
|
|
|
|
|
case 'a': res = 7; break;
|
|
|
|
case 'b': res = 8; break;
|
|
|
|
case 't': res = 9; break;
|
|
|
|
case 'n': res = 10; break;
|
|
|
|
case 'v': res = 11; break;
|
|
|
|
case 'f': res = 12; break;
|
|
|
|
case 'r': res = 13; break;
|
|
|
|
case '\'': res = '\''; break;
|
|
|
|
case '\"': res = '\"'; break;
|
|
|
|
case '\\': res = '\\'; break;
|
|
|
|
case '/':
|
|
|
|
res = '/';
|
|
|
|
break; //wtf
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-09-19 21:39:50 +00:00
|
|
|
case 'u': {
|
|
|
|
//hexnumbarh - oct is deprecated
|
2017-03-05 15:44:50 +00:00
|
|
|
i += 1;
|
|
|
|
for (int j = 0; j < 4; j++) {
|
|
|
|
CharType c = GETCHAR(i + j);
|
|
|
|
if (c == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_error("Unterminated String");
|
|
|
|
return;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
|
2014-09-19 21:39:50 +00:00
|
|
|
|
|
|
|
_make_error("Malformed hex constant in string");
|
|
|
|
return;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
CharType v;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
v = c - '0';
|
|
|
|
} else if (c >= 'a' && c <= 'f') {
|
|
|
|
v = c - 'a';
|
|
|
|
v += 10;
|
|
|
|
} else if (c >= 'A' && c <= 'F') {
|
|
|
|
v = c - 'A';
|
|
|
|
v += 10;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
ERR_PRINT("BUG");
|
2017-03-05 15:44:50 +00:00
|
|
|
v = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
res <<= 4;
|
|
|
|
res |= v;
|
2014-09-19 21:39:50 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
i += 3;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
|
|
|
|
_make_error("Invalid escape sequence");
|
|
|
|
return;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
str += res;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (CharType(GETCHAR(i)) == '\n') {
|
2015-08-04 23:17:13 +00:00
|
|
|
line++;
|
2017-03-05 15:44:50 +00:00
|
|
|
column = 1;
|
2015-08-04 23:17:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
str += CharType(GETCHAR(i));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
INCPOS(i);
|
|
|
|
|
|
|
|
if (is_node_path) {
|
|
|
|
_make_constant(NodePath(str));
|
|
|
|
} else {
|
|
|
|
_make_constant(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
2014-12-17 01:31:57 +00:00
|
|
|
case 0xFFFF: {
|
|
|
|
_make_token(TK_CURSOR);
|
|
|
|
} break;
|
2014-02-10 01:10:30 +00:00
|
|
|
default: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (_is_number(GETCHAR(0)) || (GETCHAR(0) == '.' && _is_number(GETCHAR(1)))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
// parse number
|
2017-03-05 15:44:50 +00:00
|
|
|
bool period_found = false;
|
|
|
|
bool exponent_found = false;
|
|
|
|
bool hexa_found = false;
|
|
|
|
bool sign_found = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
String str;
|
2017-03-05 15:44:50 +00:00
|
|
|
int i = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (true) {
|
|
|
|
if (GETCHAR(i) == '.') {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (period_found || exponent_found) {
|
2015-06-26 04:14:31 +00:00
|
|
|
_make_error("Invalid numeric constant at '.'");
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
period_found = true;
|
|
|
|
} else if (GETCHAR(i) == 'x') {
|
|
|
|
if (hexa_found || str.length() != 1 || !((i == 1 && str[0] == '0') || (i == 2 && str[1] == '0' && str[0] == '-'))) {
|
2015-06-26 04:14:31 +00:00
|
|
|
_make_error("Invalid numeric constant at 'x'");
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
hexa_found = true;
|
|
|
|
} else if (!hexa_found && GETCHAR(i) == 'e') {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (hexa_found || exponent_found) {
|
2015-06-26 04:14:31 +00:00
|
|
|
_make_error("Invalid numeric constant at 'e'");
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
exponent_found = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else if (_is_number(GETCHAR(i))) {
|
|
|
|
//all ok
|
|
|
|
} else if (hexa_found && _is_hex(GETCHAR(i))) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if ((GETCHAR(i) == '-' || GETCHAR(i) == '+') && exponent_found) {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (sign_found) {
|
2015-06-26 04:14:31 +00:00
|
|
|
_make_error("Invalid numeric constant at '-'");
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
sign_found = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else
|
|
|
|
break;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
str += CharType(GETCHAR(i));
|
2014-02-10 01:10:30 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!(_is_number(str[str.length() - 1]) || (hexa_found && _is_hex(str[str.length() - 1])))) {
|
|
|
|
_make_error("Invalid numeric constant: " + str);
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
INCPOS(str.length());
|
2015-06-26 04:14:31 +00:00
|
|
|
if (hexa_found) {
|
2017-01-08 23:58:39 +00:00
|
|
|
int64_t val = str.hex_to_int64();
|
2015-06-26 04:14:31 +00:00
|
|
|
_make_constant(val);
|
2016-06-18 11:18:31 +00:00
|
|
|
} else if (period_found || exponent_found) {
|
2017-01-08 23:58:39 +00:00
|
|
|
double val = str.to_double();
|
2014-02-10 01:10:30 +00:00
|
|
|
//print_line("*%*%*%*% to convert: "+str+" result: "+rtos(val));
|
|
|
|
_make_constant(val);
|
2015-06-26 04:14:31 +00:00
|
|
|
} else {
|
2017-01-08 23:58:39 +00:00
|
|
|
int64_t val = str.to_int64();
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_constant(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (GETCHAR(0) == '.') {
|
2014-02-10 01:10:30 +00:00
|
|
|
//parse period
|
|
|
|
_make_token(TK_PERIOD);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_is_text_char(GETCHAR(0))) {
|
|
|
|
// parse identifier
|
|
|
|
String str;
|
2017-03-05 15:44:50 +00:00
|
|
|
str += CharType(GETCHAR(0));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int i = 1;
|
|
|
|
while (_is_text_char(GETCHAR(i))) {
|
|
|
|
str += CharType(GETCHAR(i));
|
2014-02-10 01:10:30 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool identifier = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (str == "null") {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_constant(Variant());
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (str == "true") {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_constant(true);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (str == "false") {
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_constant(false);
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool found = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int idx = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-31 17:28:34 +00:00
|
|
|
while (_type_list[idx].text) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-31 17:28:34 +00:00
|
|
|
if (str == _type_list[idx].text) {
|
|
|
|
_make_type(_type_list[idx].type);
|
2017-03-05 15:44:50 +00:00
|
|
|
found = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
|
|
|
|
//built in func?
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < GDFunctions::FUNC_MAX; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (str == GDFunctions::get_func_name(GDFunctions::Function(i))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_make_built_in_func(GDFunctions::Function(i));
|
2017-03-05 15:44:50 +00:00
|
|
|
found = true;
|
2015-06-26 04:14:31 +00:00
|
|
|
break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
2017-03-31 17:28:34 +00:00
|
|
|
//keyword
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int idx = 0;
|
|
|
|
found = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-31 17:28:34 +00:00
|
|
|
while (_keyword_list[idx].text) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-31 17:28:34 +00:00
|
|
|
if (str == _keyword_list[idx].text) {
|
|
|
|
_make_token(_keyword_list[idx].token);
|
2017-03-05 15:44:50 +00:00
|
|
|
found = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
2017-03-05 15:44:50 +00:00
|
|
|
identifier = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (identifier) {
|
|
|
|
_make_identifier(str);
|
|
|
|
}
|
|
|
|
INCPOS(str.length());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_make_error("Unknown character");
|
|
|
|
return;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
INCPOS(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void GDTokenizerText::set_code(const String &p_code) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
code = p_code;
|
2014-02-10 01:10:30 +00:00
|
|
|
len = p_code.length();
|
|
|
|
if (len) {
|
2017-03-05 15:44:50 +00:00
|
|
|
_code = &code[0];
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
_code = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
code_pos = 0;
|
|
|
|
line = 1; //it is stand-ar-ized that lines begin in 1 in code..
|
|
|
|
column = 1; //the same holds for columns
|
|
|
|
tk_rb_pos = 0;
|
|
|
|
error_flag = false;
|
|
|
|
last_error = "";
|
|
|
|
for (int i = 0; i < MAX_LOOKAHEAD + 1; i++)
|
2014-02-10 01:10:30 +00:00
|
|
|
_advance();
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
GDTokenizerText::Token GDTokenizerText::get_token(int p_offset) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, TK_ERROR);
|
|
|
|
ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, TK_ERROR);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
|
2014-02-10 01:10:30 +00:00
|
|
|
return tk_rb[ofs].type;
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
int GDTokenizerText::get_token_line(int p_offset) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, -1);
|
|
|
|
ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, -1);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
|
2014-02-10 01:10:30 +00:00
|
|
|
return tk_rb[ofs].line;
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
int GDTokenizerText::get_token_column(int p_offset) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, -1);
|
|
|
|
ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, -1);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
|
2014-02-10 01:10:30 +00:00
|
|
|
return tk_rb[ofs].col;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const Variant &GDTokenizerText::get_token_constant(int p_offset) const {
|
|
|
|
ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, tk_rb[0].constant);
|
|
|
|
ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, tk_rb[0].constant);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
|
|
|
|
ERR_FAIL_COND_V(tk_rb[ofs].type != TK_CONSTANT, tk_rb[0].constant);
|
2014-02-10 01:10:30 +00:00
|
|
|
return tk_rb[ofs].constant;
|
|
|
|
}
|
2017-03-31 17:28:34 +00:00
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
StringName GDTokenizerText::get_token_identifier(int p_offset) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, StringName());
|
|
|
|
ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, StringName());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
|
|
|
|
ERR_FAIL_COND_V(tk_rb[ofs].type != TK_IDENTIFIER, StringName());
|
2014-02-10 01:10:30 +00:00
|
|
|
return tk_rb[ofs].identifier;
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
GDFunctions::Function GDTokenizerText::get_token_built_in_func(int p_offset) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, GDFunctions::FUNC_MAX);
|
|
|
|
ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, GDFunctions::FUNC_MAX);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
|
|
|
|
ERR_FAIL_COND_V(tk_rb[ofs].type != TK_BUILT_IN_FUNC, GDFunctions::FUNC_MAX);
|
2014-02-10 01:10:30 +00:00
|
|
|
return tk_rb[ofs].func;
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
Variant::Type GDTokenizerText::get_token_type(int p_offset) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, Variant::NIL);
|
|
|
|
ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, Variant::NIL);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
|
|
|
|
ERR_FAIL_COND_V(tk_rb[ofs].type != TK_BUILT_IN_TYPE, Variant::NIL);
|
2014-02-10 01:10:30 +00:00
|
|
|
return tk_rb[ofs].vtype;
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
int GDTokenizerText::get_token_line_indent(int p_offset) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, 0);
|
|
|
|
ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
|
|
|
|
ERR_FAIL_COND_V(tk_rb[ofs].type != TK_NEWLINE, 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
return tk_rb[ofs].constant;
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
String GDTokenizerText::get_token_error(int p_offset) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(p_offset <= -MAX_LOOKAHEAD, String());
|
|
|
|
ERR_FAIL_COND_V(p_offset >= MAX_LOOKAHEAD, String());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs = (TK_RB_SIZE + tk_rb_pos + p_offset - MAX_LOOKAHEAD - 1) % TK_RB_SIZE;
|
|
|
|
ERR_FAIL_COND_V(tk_rb[ofs].type != TK_ERROR, String());
|
2014-02-10 01:10:30 +00:00
|
|
|
return tk_rb[ofs].constant;
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
void GDTokenizerText::advance(int p_amount) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(p_amount <= 0);
|
|
|
|
for (int i = 0; i < p_amount; i++)
|
2014-02-10 01:10:30 +00:00
|
|
|
_advance();
|
|
|
|
}
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-01-09 01:40:00 +00:00
|
|
|
#define BYTECODE_VERSION 12
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Error GDTokenizerBuffer::set_code_buffer(const Vector<uint8_t> &p_buffer) {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const uint8_t *buf = p_buffer.ptr();
|
|
|
|
int total_len = p_buffer.size();
|
|
|
|
ERR_FAIL_COND_V(p_buffer.size() < 24 || p_buffer[0] != 'G' || p_buffer[1] != 'D' || p_buffer[2] != 'S' || p_buffer[3] != 'C', ERR_INVALID_DATA);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
int version = decode_uint32(&buf[4]);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (version > BYTECODE_VERSION) {
|
2014-09-15 14:33:30 +00:00
|
|
|
ERR_EXPLAIN("Bytecode is too New! Please use a newer engine version.");
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(version > BYTECODE_VERSION, ERR_INVALID_DATA);
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
|
|
|
int identifier_count = decode_uint32(&buf[8]);
|
|
|
|
int constant_count = decode_uint32(&buf[12]);
|
|
|
|
int line_count = decode_uint32(&buf[16]);
|
|
|
|
int token_count = decode_uint32(&buf[20]);
|
|
|
|
|
2017-08-21 19:15:36 +00:00
|
|
|
const uint8_t *b = &buf[24];
|
2017-03-05 15:44:50 +00:00
|
|
|
total_len -= 24;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
identifiers.resize(identifier_count);
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < identifier_count; i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
int len = decode_uint32(b);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(len > total_len, ERR_INVALID_DATA);
|
|
|
|
b += 4;
|
2014-02-25 12:31:47 +00:00
|
|
|
Vector<uint8_t> cs;
|
|
|
|
cs.resize(len);
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int j = 0; j < len; j++) {
|
|
|
|
cs[j] = b[j] ^ 0xb6;
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
cs[cs.size() - 1] = 0;
|
2014-02-25 12:31:47 +00:00
|
|
|
String s;
|
2017-03-05 15:44:50 +00:00
|
|
|
s.parse_utf8((const char *)cs.ptr());
|
|
|
|
b += len;
|
|
|
|
total_len -= len + 4;
|
|
|
|
identifiers[i] = s;
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-25 12:31:47 +00:00
|
|
|
constants.resize(constant_count);
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < constant_count; i++) {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
Variant v;
|
|
|
|
int len;
|
2017-03-05 15:44:50 +00:00
|
|
|
Error err = decode_variant(v, b, total_len, &len);
|
2014-02-25 12:31:47 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
2017-03-05 15:44:50 +00:00
|
|
|
b += len;
|
|
|
|
total_len -= len;
|
|
|
|
constants[i] = v;
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(line_count * 8 > total_len, ERR_INVALID_DATA);
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < line_count; i++) {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t token = decode_uint32(b);
|
|
|
|
b += 4;
|
|
|
|
uint32_t linecol = decode_uint32(b);
|
|
|
|
b += 4;
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
lines.insert(token, linecol);
|
|
|
|
total_len -= 8;
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tokens.resize(token_count);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < token_count; i++) {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(total_len < 1, ERR_INVALID_DATA);
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if ((*b) & TOKEN_BYTE_MASK) { //little endian always
|
|
|
|
ERR_FAIL_COND_V(total_len < 4, ERR_INVALID_DATA);
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
tokens[i] = decode_uint32(b) & ~TOKEN_BYTE_MASK;
|
|
|
|
b += 4;
|
2014-02-25 12:31:47 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
tokens[i] = *b;
|
|
|
|
b += 1;
|
2014-02-25 12:31:47 +00:00
|
|
|
total_len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
token = 0;
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<uint8_t> GDTokenizerBuffer::parse_code_string(const String &p_code) {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
Vector<uint8_t> buf;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<StringName, int> identifier_map;
|
|
|
|
HashMap<Variant, int, VariantHasher, VariantComparator> constant_map;
|
|
|
|
Map<uint32_t, int> line_map;
|
2014-02-25 12:31:47 +00:00
|
|
|
Vector<uint32_t> token_array;
|
|
|
|
|
|
|
|
GDTokenizerText tt;
|
|
|
|
tt.set_code(p_code);
|
2017-03-05 15:44:50 +00:00
|
|
|
int line = -1;
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (true) {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (tt.get_token_line() != line) {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
line = tt.get_token_line();
|
|
|
|
line_map[line] = token_array.size();
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t token = tt.get_token();
|
|
|
|
switch (tt.get_token()) {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
case TK_IDENTIFIER: {
|
|
|
|
StringName id = tt.get_token_identifier();
|
|
|
|
if (!identifier_map.has(id)) {
|
|
|
|
int idx = identifier_map.size();
|
2017-03-05 15:44:50 +00:00
|
|
|
identifier_map[id] = idx;
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
token |= identifier_map[id] << TOKEN_BITS;
|
2014-02-25 12:31:47 +00:00
|
|
|
} break;
|
|
|
|
case TK_CONSTANT: {
|
|
|
|
|
|
|
|
Variant c = tt.get_token_constant();
|
|
|
|
if (!constant_map.has(c)) {
|
|
|
|
int idx = constant_map.size();
|
2017-03-05 15:44:50 +00:00
|
|
|
constant_map[c] = idx;
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
token |= constant_map[c] << TOKEN_BITS;
|
2014-02-25 12:31:47 +00:00
|
|
|
} break;
|
|
|
|
case TK_BUILT_IN_TYPE: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
token |= tt.get_token_type() << TOKEN_BITS;
|
2014-02-25 12:31:47 +00:00
|
|
|
} break;
|
|
|
|
case TK_BUILT_IN_FUNC: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
token |= tt.get_token_built_in_func() << TOKEN_BITS;
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case TK_NEWLINE: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
token |= tt.get_token_line_indent() << TOKEN_BITS;
|
2014-02-25 12:31:47 +00:00
|
|
|
} break;
|
|
|
|
case TK_ERROR: {
|
|
|
|
|
|
|
|
ERR_FAIL_V(Vector<uint8_t>());
|
|
|
|
} break;
|
|
|
|
default: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
token_array.push_back(token);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (tt.get_token() == TK_EOF)
|
2014-02-25 12:31:47 +00:00
|
|
|
break;
|
|
|
|
tt.advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
//reverse maps
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<int, StringName> rev_identifier_map;
|
|
|
|
for (Map<StringName, int>::Element *E = identifier_map.front(); E; E = E->next()) {
|
|
|
|
rev_identifier_map[E->get()] = E->key();
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<int, Variant> rev_constant_map;
|
|
|
|
const Variant *K = NULL;
|
|
|
|
while ((K = constant_map.next(K))) {
|
|
|
|
rev_constant_map[constant_map[*K]] = *K;
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<int, uint32_t> rev_line_map;
|
|
|
|
for (Map<uint32_t, int>::Element *E = line_map.front(); E; E = E->next()) {
|
|
|
|
rev_line_map[E->get()] = E->key();
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//save header
|
|
|
|
buf.resize(24);
|
2017-03-05 15:44:50 +00:00
|
|
|
buf[0] = 'G';
|
|
|
|
buf[1] = 'D';
|
|
|
|
buf[2] = 'S';
|
|
|
|
buf[3] = 'C';
|
|
|
|
encode_uint32(BYTECODE_VERSION, &buf[4]);
|
|
|
|
encode_uint32(identifier_map.size(), &buf[8]);
|
|
|
|
encode_uint32(constant_map.size(), &buf[12]);
|
|
|
|
encode_uint32(line_map.size(), &buf[16]);
|
|
|
|
encode_uint32(token_array.size(), &buf[20]);
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
//save identifiers
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Map<int, StringName>::Element *E = rev_identifier_map.front(); E; E = E->next()) {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
CharString cs = String(E->get()).utf8();
|
2017-03-05 15:44:50 +00:00
|
|
|
int len = cs.length() + 1;
|
|
|
|
int extra = 4 - (len % 4);
|
|
|
|
if (extra == 4)
|
|
|
|
extra = 0;
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
uint8_t ibuf[4];
|
2017-03-05 15:44:50 +00:00
|
|
|
encode_uint32(len + extra, ibuf);
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2014-02-25 12:31:47 +00:00
|
|
|
buf.push_back(ibuf[i]);
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
buf.push_back(cs[i] ^ 0xb6);
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < extra; i++) {
|
|
|
|
buf.push_back(0 ^ 0xb6);
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Map<int, Variant>::Element *E = rev_constant_map.front(); E; E = E->next()) {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
int len;
|
2017-03-05 15:44:50 +00:00
|
|
|
Error err = encode_variant(E->get(), NULL, len);
|
|
|
|
ERR_FAIL_COND_V(err != OK, Vector<uint8_t>());
|
|
|
|
int pos = buf.size();
|
|
|
|
buf.resize(pos + len);
|
|
|
|
encode_variant(E->get(), &buf[pos], len);
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Map<int, uint32_t>::Element *E = rev_line_map.front(); E; E = E->next()) {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
uint8_t ibuf[8];
|
2017-03-05 15:44:50 +00:00
|
|
|
encode_uint32(E->key(), &ibuf[0]);
|
|
|
|
encode_uint32(E->get(), &ibuf[4]);
|
|
|
|
for (int i = 0; i < 8; i++)
|
2014-02-25 12:31:47 +00:00
|
|
|
buf.push_back(ibuf[i]);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < token_array.size(); i++) {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
uint32_t token = token_array[i];
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (token & ~TOKEN_MASK) {
|
2014-02-25 12:31:47 +00:00
|
|
|
uint8_t buf4[4];
|
2017-03-05 15:44:50 +00:00
|
|
|
encode_uint32(token_array[i] | TOKEN_BYTE_MASK, &buf4[0]);
|
|
|
|
for (int j = 0; j < 4; j++) {
|
2014-02-25 12:31:47 +00:00
|
|
|
buf.push_back(buf4[j]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf.push_back(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
GDTokenizerBuffer::Token GDTokenizerBuffer::get_token(int p_offset) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int offset = token + p_offset;
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (offset < 0 || offset >= tokens.size())
|
2014-02-25 12:31:47 +00:00
|
|
|
return TK_EOF;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return GDTokenizerBuffer::Token(tokens[offset] & TOKEN_MASK);
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
StringName GDTokenizerBuffer::get_token_identifier(int p_offset) const {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int offset = token + p_offset;
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(offset, tokens.size(), StringName());
|
|
|
|
uint32_t identifier = tokens[offset] >> TOKEN_BITS;
|
|
|
|
ERR_FAIL_INDEX_V(identifier, (uint32_t)identifiers.size(), StringName());
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
return identifiers[identifier];
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
GDFunctions::Function GDTokenizerBuffer::get_token_built_in_func(int p_offset) const {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int offset = token + p_offset;
|
|
|
|
ERR_FAIL_INDEX_V(offset, tokens.size(), GDFunctions::FUNC_MAX);
|
|
|
|
return GDFunctions::Function(tokens[offset] >> TOKEN_BITS);
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Variant::Type GDTokenizerBuffer::get_token_type(int p_offset) const {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int offset = token + p_offset;
|
|
|
|
ERR_FAIL_INDEX_V(offset, tokens.size(), Variant::NIL);
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return Variant::Type(tokens[offset] >> TOKEN_BITS);
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int GDTokenizerBuffer::get_token_line(int p_offset) const {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int offset = token + p_offset;
|
2014-02-25 12:31:47 +00:00
|
|
|
int pos = lines.find_nearest(offset);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (pos < 0)
|
2014-02-25 12:31:47 +00:00
|
|
|
return -1;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (pos >= lines.size())
|
|
|
|
pos = lines.size() - 1;
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
uint32_t l = lines.getv(pos);
|
2017-03-05 15:44:50 +00:00
|
|
|
return l & TOKEN_LINE_MASK;
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
int GDTokenizerBuffer::get_token_column(int p_offset) const {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int offset = token + p_offset;
|
2014-02-25 12:31:47 +00:00
|
|
|
int pos = lines.find_nearest(offset);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (pos < 0)
|
2014-02-25 12:31:47 +00:00
|
|
|
return -1;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (pos >= lines.size())
|
|
|
|
pos = lines.size() - 1;
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
uint32_t l = lines.getv(pos);
|
2017-03-05 15:44:50 +00:00
|
|
|
return l >> TOKEN_LINE_BITS;
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
int GDTokenizerBuffer::get_token_line_indent(int p_offset) const {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int offset = token + p_offset;
|
|
|
|
ERR_FAIL_INDEX_V(offset, tokens.size(), 0);
|
|
|
|
return tokens[offset] >> TOKEN_BITS;
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
const Variant &GDTokenizerBuffer::get_token_constant(int p_offset) const {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int offset = token + p_offset;
|
|
|
|
ERR_FAIL_INDEX_V(offset, tokens.size(), nil);
|
|
|
|
uint32_t constant = tokens[offset] >> TOKEN_BITS;
|
|
|
|
ERR_FAIL_INDEX_V(constant, (uint32_t)constants.size(), nil);
|
2014-02-25 12:31:47 +00:00
|
|
|
return constants[constant];
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
String GDTokenizerBuffer::get_token_error(int p_offset) const {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
|
|
|
ERR_FAIL_V(String());
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void GDTokenizerBuffer::advance(int p_amount) {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_amount + token, tokens.size());
|
|
|
|
token += p_amount;
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
GDTokenizerBuffer::GDTokenizerBuffer() {
|
2014-02-25 12:31:47 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
token = 0;
|
2014-02-25 12:31:47 +00:00
|
|
|
}
|