public static bool LocalNameExists(string name) { Dictionary <string, ParserFunction> lastLevel = GetLastLevel(); if (lastLevel == null) { return(false); } name = Constants.ConvertName(name); return(lastLevel.ContainsKey(name)); }
public static bool PopLocalVariable(string name) { if (s_locals.Count == 0) { return(false); } Dictionary <string, ParserFunction> locals = s_locals.Peek().Variables; name = Constants.ConvertName(name); return(locals.Remove(name)); }
public static bool PopLocalVariable(string name) { if (s_lastExecutionLevel == null) { return(false); } Dictionary <string, ParserFunction> locals = s_lastExecutionLevel.Variables; name = Constants.ConvertName(name); return(locals.Remove(name)); }
string GetRealName(string name) { string realName; string converted = Constants.ConvertName(name); if (!m_propertyStringMap.TryGetValue(converted, out realName)) { realName = name; } return(realName); }
// A "virtual" Constructor public ParserFunction(ParsingScript script, string item, char ch, ref string action) { if (item.Length == 0 && (ch == Constants.START_ARG || !script.StillValid())) { // There is no function, just an expression in parentheses m_impl = s_idFunction; return; } m_impl = CheckString(script, item, ch); if (m_impl != null) { return; } item = Constants.ConvertName(item); m_impl = GetRegisteredAction(item, script, ref action); if (m_impl != null) { return; } m_impl = GetArrayFunction(item, script, action); if (m_impl != null) { return; } m_impl = GetObjectFunction(item, script); if (m_impl != null) { return; } m_impl = GetVariable(item, script); if (m_impl != null) { return; } if (m_impl == s_strOrNumFunction && string.IsNullOrWhiteSpace(item)) { string problem = (!string.IsNullOrWhiteSpace(action) ? action : ch.ToString()); string restData = ch.ToString() + script.Rest; throw new ArgumentException("Couldn't parse [" + problem + "] in " + restData + "..."); } // Function not found, will try to parse this as a string in quotes or a number. s_strOrNumFunction.Item = item; m_impl = s_strOrNumFunction; }
public static ParserFunction GetFunction(string name, ParsingScript script) { name = Constants.ConvertName(name); ParserFunction impl; if (s_functions.TryGetValue(name, out impl)) { // Global function exists and is registered (e.g. pi, exp, or a variable) return(impl.NewInstance()); } return(GetFromNamespace(name, script)); }
public void SetEnumProperty(string propName, Variable value, string baseName = "") { m_propertyMap[propName] = value; string converted = Constants.ConvertName(propName); m_propertyStringMap[converted] = propName; if (m_enumMap == null) { m_enumMap = new Dictionary <int, string>(); } m_enumMap[value.AsInt()] = propName; }
public Variable SetProperty(string propName, Variable value, string baseName = "") { propName = Constants.ConvertName(propName); int ind = propName.IndexOf('.'); if (ind > 0) { // The case a.b.c = ... is dealt here recursively string varName = propName.Substring(0, ind); string actualPropName = propName.Substring(ind + 1); Variable property = GetProperty(varName); Utils.CheckNotNull(property, varName); return(property.SetProperty(actualPropName, value, baseName)); } return(FinishSetProperty(propName, value, baseName)); }
public static void AddGlobal(string name, ParserFunction function, bool isNative = true) { name = Constants.ConvertName(name); NormalizeValue(function); function.isNative = isNative; s_variables[name] = function; function.Name = Constants.GetRealName(name); #if UNITY_EDITOR == false && UNITY_STANDALONE == false && __ANDROID__ == false && __IOS__ == false if (!isNative) { Translation.AddTempKeyword(name); } #endif }
static ParserFunction GetLocalScopeVariable(string name, string scopeName) { scopeName = Path.GetFileName(scopeName); Dictionary <string, ParserFunction> localScope; if (!s_localScope.TryGetValue(scopeName, out localScope)) { return(null); } name = Constants.ConvertName(name); ParserFunction function = null; localScope.TryGetValue(name, out function); return(function); }
public static void UpdateFunction(string name, ParserFunction function) { name = Constants.ConvertName(name); // First search among local variables. if (s_locals.Count > StackLevelDelta) { Dictionary <string, ParserFunction> local = s_locals.Peek().Variables; if (local.ContainsKey(name)) { // Local function exists (a local variable) local[name] = function; return; } } // If it's not a local variable, update global. s_variables[name] = function; }
public Variable FinishSetProperty(string propName, Variable value, string baseName = "") { Variable result = Variable.EmptyInstance; m_propertyMap[propName] = value; string converted = Constants.ConvertName(propName); m_propertyStringMap[converted] = propName; Type = VarType.OBJECT; if (Object is ScriptObject) { ScriptObject obj = Object as ScriptObject; result = obj.SetProperty(propName, value).Result; } return(result); }
public static void RegisterFunction(string name, ParserFunction function, bool isNative = true) { name = Constants.ConvertName(name); function.Name = Constants.GetRealName(name); if (!string.IsNullOrWhiteSpace(s_namespace)) { StackLevel level; if (s_namespaces.TryGetValue(s_namespace, out level) && function is CustomFunction) { ((CustomFunction)function).NamespaceData = level; name = s_namespacePrefix + name; } } s_functions[name] = function; function.isNative = isNative; }
public static void AddLocalScopeVariable(string name, string scopeName, ParserFunction variable) { name = Constants.ConvertName(name); variable.isNative = false; variable.Name = Constants.GetRealName(name); if (scopeName == null) { scopeName = ""; } Dictionary <string, ParserFunction> localScope; if (!s_localScope.TryGetValue(scopeName, out localScope)) { localScope = new Dictionary <string, ParserFunction>(); } localScope[name] = variable; s_localScope[scopeName] = localScope; }
public static void AddGlobalOrLocalVariable(string name, GetVarFunction function) { name = Constants.ConvertName(name); Dictionary <string, ParserFunction> lastLevel = GetLastLevel(); if (lastLevel != null && s_locals.Peek().IsNamespace&& !string.IsNullOrWhiteSpace(s_namespace)) { name = s_namespacePrefix + name; } function.Name = Constants.GetRealName(name); if (s_locals.Count > StackLevelDelta && (LocalNameExists(name) || !GlobalNameExists(name))) { AddLocalVariable(function); } else { AddGlobal(name, function, false /* not native */); } }
public static void AddNamespace(string namespaceName) { namespaceName = Constants.ConvertName(namespaceName); if (!string.IsNullOrEmpty(s_namespace)) { throw new ArgumentException("Already inside of namespace [" + s_namespace + "]."); } StackLevel level; if (!s_namespaces.TryGetValue(namespaceName, out level)) { level = new StackLevel(namespaceName, true);; } s_locals.Push(level); s_namespaces[namespaceName] = level; s_namespace = namespaceName; s_namespacePrefix = namespaceName + "."; }
public async Task <Variable> SetPropertyAsync(string propName, Variable value, string baseName = "") { Variable result = Variable.EmptyInstance; propName = Constants.ConvertName(propName); int ind = propName.IndexOf('.'); if (ind > 0) { // The case a.b.c = ... is dealt here recursively string varName = propName.Substring(0, ind); string actualPropName = propName.Substring(ind + 1); Variable property = await GetPropertyAsync(varName); Utils.CheckNotNull(property, varName); result = await property.SetPropertyAsync(actualPropName, value, baseName); return(result); } return(FinishSetProperty(propName, value, baseName)); }
public static void AddGlobal(string name, ParserFunction function, bool isNative = true) { name = Constants.ConvertName(name); NormalizeValue(function); function.isNative = isNative; s_variables[name] = function; function.Name = Constants.GetRealName(name); #if UNITY_EDITOR == false && UNITY_STANDALONE == false && __ANDROID__ == false && __IOS__ == false if (!isNative) { Translation.AddTempKeyword(name); } #endif var handle = OnVariableChange; if (handle != null && function is GetVarFunction) { handle.Invoke(function.Name, ((GetVarFunction)function).Value, true); } }
public static void AddGlobalOrLocalVariable(string name, GetVarFunction function, ParsingScript script = null, bool localIfPossible = false) { name = Constants.ConvertName(name); if (Constants.CheckReserved(name)) { Utils.ThrowErrorMsg(name + " is a reserved name.", script, name); } bool globalOnly = !localIfPossible && !LocalNameExists(name); Dictionary <string, ParserFunction> lastLevel = GetLastLevel(); if (!globalOnly && lastLevel != null && s_lastExecutionLevel.IsNamespace && !string.IsNullOrWhiteSpace(s_namespace)) { name = s_namespacePrefix + name; } function.Name = Constants.GetRealName(name); function.Value.ParamName = function.Name; if (!globalOnly && !localIfPossible && script != null && script.StackLevel != null && !GlobalNameExists(name)) { script.StackLevel.Variables[name] = function; var handle = OnVariableChange; if (handle != null) { handle.Invoke(function.Name, function.Value, false); } } if (!globalOnly && s_locals.Count > StackLevelDelta && (localIfPossible || LocalNameExists(name) || !GlobalNameExists(name))) { AddLocalVariable(function); } else { AddGlobal(name, function, false /* not native */); } }
public Variable GetEnumProperty(string propName, ParsingScript script, string baseName = "") { propName = Constants.ConvertName(propName); if (script.Prev == Constants.START_ARG) { Variable value = Utils.GetItem(script); if (propName == Constants.TO_STRING) { return(ConvertEnumToString(value)); } else { return(new Variable(m_enumMap != null && m_enumMap.ContainsKey(value.AsInt()))); } } string[] tokens = propName.Split('.'); if (tokens.Length > 1) { propName = tokens[0]; } string match = GetActualPropertyName(propName, GetAllProperties(), baseName, this); Variable result = GetCoreProperty(match, script); if (tokens.Length > 1) { result = ConvertEnumToString(result); if (tokens.Length > 2) { string rest = string.Join(".", tokens, 2, tokens.Length - 2); result = result.GetProperty(rest, script); } } return(result); }
public static void AddLocalVariable(ParserFunction local) { NormalizeValue(local); local.m_isGlobal = false; StackLevel locals = null; if (s_locals.Count == 0) { locals = new StackLevel(); s_locals.Push(locals); } else { locals = s_locals.Peek(); } var name = Constants.ConvertName(local.Name); local.Name = Constants.GetRealName(name); locals.Variables[name] = local; #if UNITY_EDITOR == false && UNITY_STANDALONE == false && __ANDROID__ == false && __IOS__ == false Translation.AddTempKeyword(name); #endif }
public static string[] GetBaseClasses(ParsingScript script) { if (script.Current != ':') { return(new string[0]); } script.Forward(); int endArgs = script.FindFirstOf(Constants.START_GROUP.ToString()); if (endArgs < 0) { throw new ArgumentException("Couldn't extract base classes"); } string argStr = script.Substr(script.Pointer, endArgs - script.Pointer); string[] args = argStr.Split(Constants.NEXT_ARG_ARRAY, StringSplitOptions.RemoveEmptyEntries); args = args.Select(element => Constants.ConvertName(element.Trim())).ToArray(); script.Pointer = endArgs + 1; return(args); }
public static string AdjustWithNamespace(string name) { name = Constants.ConvertName(name); return(s_namespacePrefix + name); }
public static bool RemoveGlobal(string name) { name = Constants.ConvertName(name); return(s_variables.Remove(name)); }
public static void PreprocessScript(ParsingScript script) { script.Pointer = 0; int nestedLevel = 0; int functionNestedLevel = 0; int pointerOffset = 0; string currentFunction = ""; string prevToken = ""; bool inQuotes = false; int negated = 0; int arrayIndexDepth = 0; if (script.AllLabels == null) { script.AllLabels = new Dictionary <string, Dictionary <string, int> >(); } if (script.LabelToFile == null) { script.LabelToFile = new Dictionary <string, string>(); } while (script.StillValid()) { char ch = script.Current; if (ch == '{') { nestedLevel++; script.Forward(); continue; } else if (ch == '}') { nestedLevel--; if (nestedLevel <= functionNestedLevel) { currentFunction = ""; pointerOffset = 0; } script.Forward(); continue; } else if (ch == ':' && !string.IsNullOrWhiteSpace(prevToken)) { script.Forward(); Dictionary <string, int> labels; if (!script.AllLabels.TryGetValue(currentFunction, out labels)) { labels = new Dictionary <string, int>(); } labels[prevToken] = script.Pointer + 1 - pointerOffset; script.AllLabels[currentFunction] = labels; script.LabelToFile[prevToken] = script.Filename; continue; } try { string token = Parser.ExtractNextToken(script, Constants.TOKEN_SEPARATION, ref inQuotes, ref arrayIndexDepth, ref negated, out _, out _, false); if (token == Constants.FUNCTION) { script.Forward(); currentFunction = Utils.GetToken(script, Constants.TOKEN_SEPARATION); currentFunction = Constants.ConvertName(currentFunction); functionNestedLevel = nestedLevel; var sig = Utils.GetFunctionSignature(script); pointerOffset = script.Pointer + (currentFunction == "" ? 1 : 2); } prevToken = token; } catch (Exception exc) { Console.WriteLine(exc.Message); script.Forward(); } } script.Pointer = 0; }
public static bool GlobalNameExists(string name) { name = Constants.ConvertName(name); return(s_variables.ContainsKey(name) || s_functions.ContainsKey(name)); }