public Script GetContentAsScript(string[] except, Dictionary <int, string> comments = null) { Script newScript = new Script(); foreach (ICodeStruct codeStruct in Code) { Assignation item = (Assignation)codeStruct; if (!except.Contains(item.Assignee)) { newScript.Code.Add(item); } } Assignation firstLine = newScript.Code.Where(i => i is Assignation) .OrderBy(i => ((Assignation)i).Line).FirstOrDefault() as Assignation; Assignation lastLine = newScript.Code.Where(i => i is Assignation) .OrderBy(i => ((Assignation)i).Line).LastOrDefault() as Assignation; //If no lines were found if (firstLine == null || lastLine == null) { return(newScript); } //Try to get the comments newScript.Comments = Comments.SkipWhile(c => c.Key < firstLine.Line) .TakeWhile(c => c.Key <= lastLine.Line) .ToDictionary(c => c.Key, c => c.Value); return(newScript); }
public void Analyse(string code, int line = -1) { Code.Clear(); Regex regex = new Regex(REGEX_BRACKETS); //For each block of text = brackets int currentLine = 1; foreach (Match ItemMatch in regex.Matches(code)) { try { Assignation tempo = new Assignation(1); tempo.Analyse(ItemMatch.Value, currentLine); Code.Add(tempo); currentLine += ItemMatch.Value.Count(s => s == '\n') + 1; } catch (RecursiveCodeException e) { //TODO: Add language support ErrorLogger.Instance.AddLogLine("Error during script Loading"); ErrorLogger.Instance.AddLogLine("\t" + e.Message); } catch (Exception) { //TODO: Add language support ErrorLogger.Instance.AddLogLine("Unknown error in script"); continue; } } }
public Assignation FindAssignation(string TagToFind) { if (Assignee == TagToFind) { return(this); } //If value cannot be ran through, return unfound. if (!(Value is CodeBlock)) { return(null); } Assignation found = Value.FindAssignation(TagToFind); return(found); }
public Script GetContentAsScript(string[] except, Dictionary <int, string> Comments = null) { Script newScript = new Script(); if (Value is CodeBlock) { foreach (ICodeStruct codeStruct in ((CodeBlock)Value).Code) { Assignation item = (Assignation)codeStruct; if (!except.Contains(item.Assignee)) { newScript.Code.Add(item); } } } else { //TODO: Add language support RecursiveCodeLog log = new RecursiveCodeLog(); log.AddToRecursiveChain("Impossible to obtain content, assigned value is not code", Assignee, Line.ToString()); newScript.Logger.Errors.Add(new SyntaxError(log.Message)); } //If no comments are given if (Comments == null) { return(newScript); } Assignation firstLine = newScript.Code.Where(i => i is Assignation) .OrderBy(i => ((Assignation)i).Line).FirstOrDefault() as Assignation; Assignation lastLine = newScript.Code.Where(i => i is Assignation) .OrderBy(i => ((Assignation)i).Line).LastOrDefault() as Assignation; //If no lines were found if (firstLine == null || lastLine == null) { return(newScript); } //Try to get the comments newScript.Comments = Comments.SkipWhile(c => c.Key < firstLine.Line) .TakeWhile(c => c.Key <= lastLine.Line) .ToDictionary(c => c.Key, c => c.Value); return(newScript); }
public void Analyse(string code, int line = -1) { Regex regex = new Regex(Script.REGEX_BRACKETS); //For each block of text = brackets foreach (Match ItemMatch in regex.Matches(code)) { int firstIndex = ItemMatch.Value.IndexOf('='); int lastIndex = ItemMatch.Value.LastIndexOf('='); //If it was an inline with more than one assignations if (firstIndex >= 0 && firstIndex != lastIndex && ItemMatch.Value.IndexOf('\n') <= 0 && ItemMatch.Value.IndexOf('{') <= 0) { //We got more than one equal in one line and there was //no brackets, it is a inline multi assignation Regex regex2 = new Regex(Script.REGEX_INLINE_ASSIGNATIONS); foreach (Match inlines in regex2.Matches(ItemMatch.Value)) { Assignation tempo = new Assignation(Level + 1); tempo.Analyse(inlines.Value, line); //If tempo has a value if (!String.IsNullOrEmpty(tempo.Assignee)) { Code.Add(tempo); } } } else { Assignation tempo = new Assignation(Level + 1); tempo.Analyse(ItemMatch.Value, line + 1); //If tempo has a value if (!String.IsNullOrEmpty(tempo.Assignee)) { Code.Add(tempo); } } line += ItemMatch.Value.Count(s => s == '\n'); } }
internal RecursiveCodeLog Analyse(List <SyntaxGroup> code) { if (!code.Any()) { return(null); } foreach (SyntaxGroup group in code) { //Check if there is an assignation or code value at the end of the code Assignation last = Code.LastOrDefault() as Assignation; if (last != null) { //set its end line as this line's end - 1 last.EndLine = group.Component.line - 1; } Assignation tempo = new Assignation(Level + 1); RecursiveCodeLog log = tempo.Analyse(group); if (log != null) { return(log); } //If tempo has a value if (!string.IsNullOrEmpty(tempo.Assignee)) { Code.Add(tempo); } } //Check if there is an assignation or code value at the end of the code Assignation Verylast = Code.LastOrDefault() as Assignation; if (Verylast != null) { //set its end line as this line's end - 1 Verylast.EndLine = code.Last().Component.line - 1; } return(null); }
public string TryParse(ICodeStruct block, string tag, Dictionary <int, string> comments = null, bool isMandatory = true) { Assignation found = block.FindAssignation(tag); if (found != null) { try { return(found.Value.Parse(comments)); } catch (Exception) { Logger.Errors.Add(new SyntaxError(tag, found.Line, null, new UnparsableTagException(tag))); } } if (isMandatory) { Logger.Errors.Add(new SyntaxError(tag, null, null, new MandatoryTagException(tag))); } return(null); }
public void Analyse(string code, int line = -1) { Code.Clear(); Tokenizer tokenizer = new Tokenizer(); List <SyntaxGroup> list = tokenizer.GroupTokensByBlocks(tokenizer.Tokenize(code, this)) as List <SyntaxGroup>; //Set this logger as the tokenizer logger Logger.Errors = tokenizer.Logger.Errors; //Check if there are errors. if (Logger.hasErrors()) { return; } //Return if list is null if (list == null || !list.Any()) { return; } foreach (SyntaxGroup group in list) { try { //Check if there is an assignation or code value at the end of the code Assignation last = Code.LastOrDefault() as Assignation; if (last != null) { //set its end line as this line's end - 1 last.EndLine = group.Component.line - 1; } Assignation tempo = new Assignation(1); RecursiveCodeLog log = tempo.Analyse(group); if (log != null) { Logger.Errors.Add(new SyntaxError(log.Message)); //We don't want to add broken code if possible continue; } Code.Add(tempo); } catch (Exception) { //TODO: Add language support Logger.Errors.Add(new SyntaxError("Unknown error in script")); } } //Check if there is an assignation or code value at the end of the code Assignation Verylast = Code.LastOrDefault() as Assignation; if (Verylast != null) { //Check if the last is Empty if (list.Last().Component.line == Verylast.Line) { //Empty block, set the end line as starting line + 2 Verylast.EndLine = Verylast.Line + 2; } else { //Set its end line as this line's end - 1 Verylast.EndLine = list.Last().Component.line - 1; } } }