public static EdnNode handleTagged(EdnToken token, EdnNode value) { EdnNode node = new EdnNode(); node.line = token.line; //String tagName = token.value.substr(1, token.value.length() - 1); String tagName = token.value.Substring(1, token.value.Length - 1); if (tagName == "_") { node.type = NodeType.EdnDiscard; } else if (tagName == "") { //special case where we return early as # { is a set - thus tagname is empty node.type = NodeType.EdnSet; if (value.type != NodeType.EdnMap) { throw new Exception("Was expection a { } after hash to build set"); } node.values = value.values; return(node); } else { node.type = NodeType.EdnTagged; } if (!validSymbol(tagName)) { throw new Exception("Invalid tag name"); } EdnToken symToken = new EdnToken(); symToken.type = TokenType.TokenAtom; symToken.line = token.line; symToken.value = tagName; List <EdnNode> values = new List <EdnNode>(); //values.push_back(handleAtom(symToken)); //values.push_back(value); values.Add(handleAtom(symToken)); node.values = values; return(node); }
public static EdnNode handleAtom(EdnToken token) { EdnNode node = new EdnNode(); node.line = token.line; node.value = token.value; if (validNil(token.value)) { node.type = NodeType.EdnNil; } else if (token.type == TokenType.TokenString) { node.type = NodeType.EdnString; } else if (validChar(token.value)) { node.type = NodeType.EdnChar; } else if (validBool(token.value)) { node.type = NodeType.EdnBool; } else if (validInt(token.value)) { node.type = NodeType.EdnInt; } else if (validFloat(token.value)) { node.type = NodeType.EdnFloat; } else if (validKeyword(token.value)) { node.type = NodeType.EdnKeyword; } else if (validSymbol(token.value)) { node.type = NodeType.EdnSymbol; } else { throw new Exception("Could not parse atom"); } return(node); }
public static EdnNode handleCollection(EdnToken token, List <EdnNode> values) { EdnNode node = new EdnNode(); node.line = token.line; node.values = values; if (token.value == "(") { node.type = NodeType.EdnList; } else if (token.value == "[") { node.type = NodeType.EdnVector; } if (token.value == "{") { node.type = NodeType.EdnMap; } return(node); }
public static string pprint(ref EdnNode node, int indent = 1) { string prefix = ""; if (indent != 0) { //prefix.insert(0, indent, ' '); prefix.Insert(0, " "); } string output = ""; if (node.type == NodeType.EdnList || node.type == NodeType.EdnSet || node.type == NodeType.EdnVector || node.type == NodeType.EdnMap) { string vals = ""; //for (list<EdnNode>::iterator it=node.values.begin(); it != node.values.end(); ++it) //foreach(EdnNode it in node.values) for (int i = 0; i < node.values.Count; i++) { if (vals.Length > 0) { vals += prefix; } EdnNode it = node.values[i]; vals += pprint(ref it, indent + 1); if (node.type == NodeType.EdnMap) { ++i; it = node.values[i]; vals += " " + pprint(ref it, 1); //++it; //vals += " " + pprint(*it, 1); } //Console.WriteLine("현재요소카운트:{0}, 마지막요소카운트:{1}, 거리:{2}", i, node.values.Count, node.values.Count-i); //if (std::distance(it, node.values.end()) != 1) vals += "\n"; if ((node.values.Count - i) != 1) { vals += "\n"; } } if (node.type == NodeType.EdnList) { output = "(" + vals + ")"; } else if (node.type == NodeType.EdnVector) { output = "[" + vals + "]"; } else if (node.type == NodeType.EdnMap) { output = "{" + vals + "}"; } else if (node.type == NodeType.EdnSet) { output = "#{" + vals + "}"; } //#ifdef DEBUG // return "<" + typeToString(node.type) + " " + output + ">"; //#endif } else if (node.type == NodeType.EdnTagged) { //output = "#" + pprint(node.values.front()) + " " + pprint(node.values.back()); EdnNode first = node.values.First(); EdnNode last = node.values.Last(); output = "#" + pprint(ref first) + " " + pprint(ref last); //#ifdef DEBUG // return "<EdnTagged " + output + ">"; //#endif } else if (node.type == NodeType.EdnString) { output = "\"" + escapeQuotes(node.value) + "\""; //#ifdef DEBUG // return "<EdnString " + output + ">"; //#endif } else { //#ifdef DEBUG // return "<" + typeToString(node.type) + " " + node.value + ">"; //#endif output = node.value; } return(output); }