public static FunctionHeader Header(MatchIterator iterator) { iterator.AssertTokenType(TokenType.Type, out Token typeToken); iterator.AssertTokenType(TokenType.Name, out Token nameToken); iterator.AssertKeySymbol(Symbols.OpenBracket, out _); Token open = (Token)iterator.Current; CollectedParameters parameters; if (iterator.Next(out parameters)) { iterator.MoveNext(); } else { parameters = new CollectedParameters(); } iterator.AssertKeySymbol(Symbols.CloseBracket, out _); Token end = (Token)iterator.Current; iterator.AssertEnd(); FunctionTypeSignature typeSignature = new FunctionTypeSignature(parameters.Types); FunctionSignature signature = new FunctionSignature( FunctionSignature.Local, nameToken.Content, typeSignature); FunctionHeader result = new FunctionHeader( typeToken, nameToken, open, end, signature, parameters.Names); return(result); }
public static FunctionCall Call(MatchIterator iterator) { // It would be cool in patterner v2 if the pattern was constructed in the same method as the finalizer. // For example, a "dummy" matchIterator could be passed through here and pick up the requirements, perhaps? // This would, of course, require the big feature of pattern simplification // It would also require defining a special "Expression" subpattern? At this point it would be better if everything were an expression, I guess iterator.AssertTokenType(TokenType.Name, out Token name); iterator.AssertKeySymbol(Symbols.OpenBracket, out _); Token open = (Token)iterator.Current; if (iterator.Next(out List <Expression> arguments)) { iterator.MoveNext(); } else { arguments = new List <Expression>(); } iterator.AssertKeySymbol(Symbols.CloseBracket, out _); Token end = (Token)iterator.Current; iterator.AssertEnd(); FunctionCall result = new FunctionCall(name, open, end, arguments); return(result); }
public static VariableAssignment Assignment(MatchIterator iterator) { if (!iterator.NextIsToken(TokenType.Type, out Token type)) { type = null; } iterator.AssertTokenType(TokenType.Name, out Token name); if (iterator.Next(out List <CollectedIndex> indices)) { iterator.MoveNext(); } else { indices = new List <CollectedIndex>(0); } iterator.AssertTokenType(TokenType.Operator, out Token op); if (!iterator.NextIsExpression(out Expression value)) { value = null; } iterator.AssertEnd(); return(new VariableAssignment(type, name, indices, op, value)); }
public static Expression CreateOld(MatchIterator iterator) { Token opToken; if (iterator.NextIsToken(TokenType.Operator, out opToken)) { // assume a left operator for now Expression righExpr = Create(iterator); return(new OperatorCall(opToken, null, righExpr)); } Expression nextExpr; if (iterator.NextIsKeySymbol(Symbols.OpenBracket)) { iterator.AssertExpression(out nextExpr); iterator.AssertKeySymbol(Symbols.CloseBracket, out _); } else { iterator.AssertExpression(out nextExpr); } if (iterator.AtEnd()) { return(nextExpr); } if (iterator.Next(out List <CollectedIndex> indices)) { foreach (CollectedIndex index in indices) { nextExpr = new Indexer(nextExpr, index.Open, index.Index, index.Close); } iterator.MoveNext(); } if (iterator.AtEnd()) { return(nextExpr); } iterator.AssertTokenType(TokenType.Operator, out opToken); if (iterator.AtEnd()) { // Assume a right operator for now return(new OperatorCall(opToken, nextExpr, null)); } // Assume a binary operator for now Expression remainder = Create(iterator); return(new OperatorCall(opToken, nextExpr, remainder)); }
private static Expression GetValue(MatchIterator iterator) { if (iterator.NextIsToken(TokenType.Operator, out Token opToken)) { // Early out: if we are starting with an operator, than it is // a right unary operator, and unary ops have the highest // precedence at the moment. return(new OperatorCall(opToken, null, GetValue(iterator))); } Expression expression; // We don't have to recursively descend through the brackets - // that is handled at the pattern matching level. As far as we are // concerned, there are either expressions or expressions wrapped // in brackets. if (iterator.NextIsKeySymbol(Symbols.OpenBracket, out Token open)) { iterator.AssertExpression(out Expression value); iterator.AssertKeySymbol(Symbols.CloseBracket, out Token close); expression = new Group(open, value, close); } else { iterator.AssertExpression(out expression); } // Finally, consider any indexing that follow the expression. // Sprak only allows one level of this, but that will be enforced // later where a more friendly error can be displayed. if (iterator.Next(out List <CollectedIndex> indices)) { foreach (CollectedIndex index in indices) { expression = new Indexer(expression, index.Open, index.Index, index.Close); } iterator.MoveNext(); } return(expression); }
public static LiteralArrayGet CreateArray(MatchIterator iterator) { Token start, end; iterator.AssertKeySymbol(Symbols.OpenSquareBracket, out _); start = (Token)iterator.Current; if (iterator.Next(out List <Expression> elements)) { iterator.MoveNext(); } else { elements = new List <Expression>(); } iterator.AssertKeySymbol(Symbols.CloseSquareBracket, out _); end = (Token)iterator.Current; return(new LiteralArrayGet(start, end, elements)); }