public static CodeIgnoreType CheckForIgnoreType(CodeIgnoreType current, char c, int i = -1) { CodeIgnoreType IgnoreType = current; if (c == '"') { if (IgnoreType == CodeIgnoreType.None) { IgnoreType = CodeIgnoreType.String; } else if (IgnoreType == CodeIgnoreType.String) { IgnoreType = CodeIgnoreType.None; } LogIgnoreType(IgnoreType, c, i); } if (c == "'"[0] && IgnoreType == CodeIgnoreType.None) { IgnoreType = CodeIgnoreType.Char; LogIgnoreType(IgnoreType, c, i); } else if (c == "'"[0] && IgnoreType == CodeIgnoreType.Char) { IgnoreType = CodeIgnoreType.None; LogIgnoreType(IgnoreType, c, i); } return(IgnoreType); }
public static KeyValuePair <string, string> ConvertStringToArgumentsContainer(string Command, int startPosition = 0) { string TypeName = ""; string ArgumentsContainer = ""; bool ParsingArgs = false; CodeIgnoreType IgnoreType = CodeIgnoreType.None; for (int i = 0; i < Command.Length; i++) { char c = Command[i]; if (!ParsingArgs) { if (c == '(') { ParsingArgs = true; IgnoreType = CodeIgnoreType.None; continue; } if (c == ';') { break; } TypeName += c; } else { if (c == ';' && IgnoreType == CodeIgnoreType.None) { break; } IgnoreType = CheckForIgnoreType(IgnoreType, c, i); ArgumentsContainer += c; } } TypeName = TypeName.Trim(SpacesAndTabs); ArgumentsContainer = ArgumentsContainer.Trim(SpacesAndTabs); if (ArgumentsContainer[ArgumentsContainer.Length - 1] != ')') { throw new Exceptions.ParserException("Arguments container is invalid. Ending symbol is not found."); } else { ArgumentsContainer = ArgumentsContainer.Remove(ArgumentsContainer.Length - 1, 1); } return(new KeyValuePair <string, string>(TypeName, ArgumentsContainer)); }
private static void LogIgnoreType(CodeIgnoreType IgnoreType, char c = '\0', int i = -1) { if (!Variables.GetBool("parserDebug")) { return; } string idx = ""; if (c != '\0') { idx += ", char: " + c; } if (i != -1) { idx += ", at " + i.ToString(); } Debug.Log("Parser", $"({new StackTrace().GetFrame(2).GetMethod().Name}) IgnoreType = {IgnoreType.ToString()}{idx}", ConsoleColor.DarkBlue); }
/// <summary> /// Do not use namespaces. Use only for variables. /// To use namespace, type <code>using NamespaceName;</code> /// </summary> /// <param name="Command">Command</param> /// <returns></returns> public static object Resolve(string Command, object ParentVariable = null) { bool ContainsDots = false; // this will make command without strings CodeIgnoreType IgnoreType = CodeIgnoreType.None; for (int i = 0; i < Command.Length; i++) { char c = Command[i]; IgnoreType = CheckForIgnoreType(IgnoreType, c, i); if (IgnoreType == CodeIgnoreType.None) { if (c == '.') { ContainsDots = true; } } } if (!ContainsDots) { return(new VNull()); } // return normal command string Name = Command; string[] Sections = Name.Split('.'); string RealName = Sections[Sections.Length - 1].Trim(EParser.SpacesAndTabs); int AllSectionsLen = 0; for (int i = 0; i < Sections.Length - 1; i++) { AllSectionsLen += Sections[i].Length - 1; } Name = RealName; string section = ""; object prevVariable = null; var variable = ParentVariable; bool LookInside = ParentVariable != null ? true : false; for (int i = 0; i < Sections.Length; i++) { if (i >= 1) { section += '.'; } section += Sections[i]; prevVariable = variable; variable = Variables.Get(section, true); if (variable != null) { EClass c = EnvironmentManager.SearchForType(variable.GetType().Name); c.ForceInstance(variable); foreach (var field in c.PropertiesAndFields) { if (field.Name == RealName) { return(field); } } foreach (var method in c.Methods) { if (method.Name == RealName) { return(method); } } throw new TypeAccessException($"Field/property/method '{RealName}' is not found in '{section}'"); } } throw new TypeAccessException($"Type '{section}' is not found. If it's a namespace, do 'using' instead"); }
private static object[] StaticArgsToArray(string ArgumentsContainer) { // RULE: Ignore parser symbols in strings // Input: a,bc,";c:", d, Kek() // Output: [0] a // [1] bc // [2] ";c:" // ... List <object> result = new List <object>(); bool InsideInvoke = false; CodeIgnoreType IgnoreType = CodeIgnoreType.None; string CurrentArg = ""; for (int i = 0; i < ArgumentsContainer.Length; i++) { char c = ArgumentsContainer[i]; IgnoreType = CheckForIgnoreType(IgnoreType, c, i); if (c == '(' && IgnoreType == CodeIgnoreType.None) { InsideInvoke = true; } if (c == ')' && IgnoreType == CodeIgnoreType.None) { InsideInvoke = false; } CurrentArg += c; if (IgnoreType == CodeIgnoreType.None && !InsideInvoke) { if (c == ',' || i == ArgumentsContainer.Length - 1) // command end/split thing { result.Add(CurrentArg.TrimEnd(new char[] { ',' }).Trim(SpacesAndTabs).TrimEnd(new char[] { ',' })); CurrentArg = ""; // new command; } } } for (int i = 0; i < result.Count; i++) { var a = result[i]; var type = GetCommandType((string)a); if (IsCommandTypeCompatibleWith(type, CommandTypes.Method)) { var invokeResult = Cmd.Process(((string)a) + ";", false); if (invokeResult != null) { if (invokeResult.GetType() == typeof(string)) { invokeResult = "\"" + invokeResult.ToString() + "\""; } } result[i] = invokeResult; /* * string varName = GlobalVars.RandomString(15); * * Variables.SetVariableObject(varName, invokeResult, new List<string>() { "Hidden" }); * result[i] = varName; * * ParserDebug.Log("Arg-invoke variable: " + varName); * if (invokeResult != null) * ParserDebug.Log($" Value: ({invokeResult.GetType().Name}) " + invokeResult.ToString()); */ } else if (type == CommandTypes.CastValue) { result[i] = ConvertStringToVar((string)a); } } return(result.ToArray()); }
public static string[] NormalizeInvokeLines(string Command) { // Input example: // string a = "BB;;\\n;;;\n; ;;;BB";int test =KEK(a); FAK(test); if (a == b){ // kek }else{} // Output example: // [0] string a = "BB;;\n;;; // ; ;;;BB"; // [1] int test =KEK(a, ";"); // [2] FAK(test); // [3] if (a == b){kek}else{} (TODO) // TODO: // string formatting (\n,\r...) // RULES: // Only strings can contain ; char which will be ignored // Strings must be in " and always has startign " and ending " // How to avoid ; in strings? :thinking: // SPLIT IT MANUALLY List <string> result = new List <string>(); string CurrentCmd = ""; CodeCommentTypes CommentType = CodeCommentTypes.None; CodeIgnoreType IgnoreType = CodeIgnoreType.None; for (int i = 0; i < Command.Length; i++) { char c = Command[i]; IgnoreType = CheckForIgnoreType(IgnoreType, c, i); // Code comment remover START if (IgnoreType == CodeIgnoreType.None && Command.Length >= 2) { if (Command[i] == '/' && Command[i + 1] == '*' && CommentType == CodeCommentTypes.None) { CommentType = CodeCommentTypes.MultiLineComment; } if (Command[i] == '/' && Command[i + 1] == '/' && CommentType == CodeCommentTypes.None) { CommentType = CodeCommentTypes.SingleLineComment; } } // Parsed string writer if (CommentType == CodeCommentTypes.None) { CurrentCmd += c; } // Code comment remover END if (IgnoreType == CodeIgnoreType.None && Command.Length > i && i >= 1) { if (Command[i] == '/' && Command[i - 1] == '*' && CommentType == CodeCommentTypes.MultiLineComment) { CommentType = CodeCommentTypes.None; } } // Code line finalizer if (IgnoreType == CodeIgnoreType.None) { if (c == '\r' || c == '\n') { // replace this with spaces, they will be removed later c = ' '; // disable singleline comment at the end of the line if (CommentType == CodeCommentTypes.SingleLineComment) { CommentType = CodeCommentTypes.None; } } // disable single line comment at the end of the command if (i == Command.Length && CommentType == CodeCommentTypes.SingleLineComment) { CommentType = CodeCommentTypes.None; } if (c == ';' && CommentType == CodeCommentTypes.None) // command end/split thing { result.Add(CurrentCmd); CommentType = CodeCommentTypes.None; IgnoreType = CodeIgnoreType.None; CurrentCmd = ""; // new command; } } } //if (result.Count == 0) // throw new Exceptions.ParserException("Can't normalize commands"); return(result.ToArray()); }