public ParseResult ParseFunctionLiteral(List <Token> tokens) { var index = 0; var node = new Node(); SkipInfo skip; ParseResult result; result = _engine.GeneralParser.ParseSurrounded("(", ")", index, tokens, ParseParameters); var parameterNode = result.Node; index += result.Delta; skip = _engine.ExpectValue("{", tokens, index); index += skip.Delta; result = _engine.GeneralParser.ParseSurrounded("{", "}", index, tokens, _engine.GeneralParser.Parse); var blockNode = result.Node; index += result.Delta + 1; var returnNode = new Node { Body = "Function", Type = TokenTypes.FunctionLiteral }; returnNode.Nodes.Add(blockNode); returnNode.Nodes.Add(parameterNode); return(new ParseResult { Node = returnNode, Delta = index }); }
/// <summary> /// Parses a statement ([if,while] (expression) {block}) into a node /// </summary> public ParseResult ParseStatement(List <Token> tokens) { var index = 0; var skip = default(SkipInfo); var result = default(ParseResult); // Create main statement node var node = new Node { Body = tokens[index].Value, Type = TokenTypes.Statement }; skip = _engine.ExpectValue("(", tokens); index += skip.Delta; result = _engine.GeneralParser.ParseSurrounded("(", ")", index, tokens, _engine.ExpressionParser.ParseClean); var conditionNode = result.Node; index += result.Delta; var conditionParentNode = new Node { Body = "Condition", Type = TokenTypes.Condition }; if (conditionNode == null) { _engine.ThrowError("Condition can't be empty!", tokens[index - 1]); } conditionParentNode.Add(conditionNode); skip = _engine.ExpectValue("{", tokens, index); index += skip.Delta; result = _engine.GeneralParser.ParseSurrounded("{", "}", index, tokens, _engine.GeneralParser.Parse); var blockNode = result.Node; index += result.Delta + 1; // Add condition and block nodes to main node node.Add(conditionNode); node.Add(blockNode); return(new ParseResult { Node = node, Delta = index }); }
/// <summary> /// Parses a class definition. /// </summary> public ParseResult Parse(List <Token> tokens) { var i = 0; var skip = _engine.ExpectType(TokenTypes.Identifier, tokens, i); i += skip.Delta; skip = _engine.ExpectValue(new string[] { ":", "{" }, tokens, i); i += skip.Delta; var inheritNode = new Node { Body = "Inherit", Type = TokenTypes.Inherit }; // Class inherits from another class or classes. if (tokens[i].Equals(":", TokenTypes.Punctuator)) { var isIdentifier = true; var nextToken = default(Token); var s = _engine.ExpectType(TokenTypes.Identifier, tokens); i++; while (i < tokens.Count) { var token = tokens[i]; if (i < tokens.Count - 2) { nextToken = tokens[i + 1]; } if (token.Type == TokenTypes.EndOfExpression) { break; } if (isIdentifier) { inheritNode.Add(_engine.ExpressionParser.Parse(new List <Token> { tokens[i] }).Node); // No curly bracket means there could be another class to inherit from. // Before that a seperator ',' token has to be present. if (!(nextToken?.Equals("{", TokenTypes.Punctuator) ?? false) && i < tokens.Count - 1) { var sk = _engine.ExpectValue(",", tokens, i); isIdentifier = false; } else { break; } } else { // An object (identifier token) has to be present after a seperator ',' token. var sk = _engine.ExpectType(TokenTypes.Identifier, tokens, i); isIdentifier = true; } i++; } i++; } // Parse class body. var SurroundedTokens = _engine.GeneralParser.GetSurroundedTokens("{", "}", i, tokens); var node = ParseContents(SurroundedTokens, tokens[1].Value); var result = new ParseResult { Node = node, Delta = SurroundedTokens.Count + 1 }; var classNode = new ClassNode { InheritNode = inheritNode, BodyNode = result.Node, Name = tokens[1].Value, Token = tokens[0] }; i += result.Delta + 1; return(new ParseResult { Node = classNode, Delta = i + 1 }); }