public VGDLNode(string content, int indent, int lineNumber, VGDLNode parent = null) { this.children = new List <VGDLNode>(); this.content = content; this.indent = indent; this.lineNumber = lineNumber; if (parent != null) { parent.insert(this); } }
/// <summary> /// Parse indented VGDL tree /// </summary> /// <param name="gameString"></param> /// <returns></returns> /// <exception cref="ArgumentException"></exception> private static VGDLNode indentTreeLexer(string gameString, int tabSize = 4) { var tabSpace = new string(' ', tabSize); //TODO: all of these are ignored by the JAVA version, consider if we need them? gameString = gameString.Replace("\t", tabSpace); gameString = gameString.Replace("(", " "); gameString = gameString.Replace(")", " "); //TODO: fix this crap, it seems they want to handle comma separated values, but forgot about this? (see hunger-games.txt) //NOTE: Figured out that the JAVA version actually doesn't change the line, because they ignore the result of the line.replace //gameString = gameString.Replace(",", " "); var lines = gameString.Split(new [] { '\n' }, StringSplitOptions.RemoveEmptyEntries); VGDLNode last = null; for (var lineNumber = 0; lineNumber < lines.Length; lineNumber++) { var line = lines[lineNumber]; //Remove comments starting with # if (line.Contains("#")) { line = line.Split('#')[0]; } //Handle whitepace and indentation var content = line.Trim(); if (content.Length > 0) { var indent = line.IndexOf(content[0]); last = new VGDLNode(content, indent, lineNumber, last); } } if (last == null) { throw new ArgumentException("Failed to parse:\n" + gameString); } //Return root return(last.getRoot()); }
public void insert(VGDLNode node) { if (indent < node.indent) { if (children.Count > 0 && children[0].indent != node.indent) { Debug.Log("child indentation: " + children[0].indent + " vs node indentation: " + node.indent); throw new Exception("[Line " + node.lineNumber.ToString() + "] child indentations must match (tabs are expanded to 4 spaces by default)"); } children.Add(node); node.parent = this; } else { if (parent == null) { throw new Exception("Root node too indented?"); } parent.insert(node); } }