private Element ParseList (IEnumerator iterator) { InstructionList list = new InstructionList (); while (iterator.MoveNext ()) { Token token = (Token) iterator.Current; if (token.Type == TokenType.CloseBracket) break; else if (token.Type == TokenType.OpenBracket) list.Add (ParseList (iterator)); else list.Add (new Element (ElementType.Literal, token.Val)); } return new Element (ElementType.List, null, list); }
private Element ParseList(IEnumerator iterator) { InstructionList list = new InstructionList(); while (iterator.MoveNext()) { Token token = (Token)iterator.Current; if (token.Type == TokenType.CloseBracket) { break; } else if (token.Type == TokenType.OpenBracket) { list.Add(ParseList(iterator)); } else { list.Add(new Element(ElementType.Literal, token.Val)); } } return(new Element(ElementType.List, null, list)); }
private InstructionList ParseBackwards (TokenList tokens, bool grouped) { Stack stack = new Stack (); for (int i = tokens.Count - 1; i >= 0; i--) { Token token = tokens[i]; switch (token.Type) { case TokenType.Number: case TokenType.String: stack.Push (new Element (ElementType.Literal, token.Val)); break; case TokenType.PlaceholderElement: stack.Push (token.Val); break; case TokenType.PlaceholderGroup: InstructionList group = ParseBackwards ((TokenList) token.Val, true); if (group.Count == 1) { stack.Push (group[0]); } else if (group.Count == 0) { } else { throw new Exception ("Unexpected grouping"); } break; case TokenType.Word: case TokenType.Infix: case TokenType.Minus: InstructionList inner = new InstructionList (); int count; ElementType etype; if (token.Type == TokenType.Word) { count = CountArgs ((string) token.Val, grouped && i == 0); if (count == -1) count = stack.Count; etype = ElementType.Statement; } else if (token.Type == TokenType.Minus) { count = 1; etype = ElementType.Statement; token.Val = "Minus"; } else { count = 2; etype = ElementType.Infix; } for (int j = 0; j < count; j++) inner.Add ((Element) stack.Pop ()); stack.Push (new Element (etype, token.Val, inner)); break; case TokenType.Newline: break; case TokenType.Variable: stack.Push (new Element (ElementType.Variable, token.Val)); break; case TokenType.QuestionMark: // Don't need to check AllowQuestionMark here as the // tokenizer checks already, stack.Push (new Element (ElementType.QuestionMark, token.Val)); break; case TokenType.PlaceholderFunction: default: throw new Exception ("Unexpected token: " + token.Type + "<" + token.Val + ">"); } } InstructionList tree = new InstructionList (); object[] toplevel = stack.ToArray (); foreach (object o in toplevel) { tree.Add ((Element) o); } return tree; }
private InstructionList ParseBackwards(TokenList tokens, bool grouped) { Stack stack = new Stack(); for (int i = tokens.Count - 1; i >= 0; i--) { Token token = tokens[i]; switch (token.Type) { case TokenType.Number: case TokenType.String: stack.Push(new Element(ElementType.Literal, token.Val)); break; case TokenType.PlaceholderElement: stack.Push(token.Val); break; case TokenType.PlaceholderGroup: InstructionList group = ParseBackwards((TokenList)token.Val, true); if (group.Count == 1) { stack.Push(group[0]); } else if (group.Count == 0) { } else { throw new Exception("Unexpected grouping"); } break; case TokenType.Word: case TokenType.Infix: case TokenType.Minus: InstructionList inner = new InstructionList(); int count; ElementType etype; if (token.Type == TokenType.Word) { count = CountArgs((string)token.Val, grouped && i == 0); if (count == -1) { count = stack.Count; } etype = ElementType.Statement; } else if (token.Type == TokenType.Minus) { count = 1; etype = ElementType.Statement; token.Val = "Minus"; } else { count = 2; etype = ElementType.Infix; } for (int j = 0; j < count; j++) { inner.Add((Element)stack.Pop()); } stack.Push(new Element(etype, token.Val, inner)); break; case TokenType.Newline: break; case TokenType.Variable: stack.Push(new Element(ElementType.Variable, token.Val)); break; case TokenType.QuestionMark: // Don't need to check AllowQuestionMark here as the // tokenizer checks already, stack.Push(new Element(ElementType.QuestionMark, token.Val)); break; case TokenType.PlaceholderFunction: default: throw new Exception("Unexpected token: " + token.Type + "<" + token.Val + ">"); } } InstructionList tree = new InstructionList(); object[] toplevel = stack.ToArray(); foreach (object o in toplevel) { tree.Add((Element)o); } return(tree); }