public static LoopHeader Loop(MatchIterator iterator) { Expression array; iterator.AssertKeyword(Keywords.Loop); Token loop = (Token)iterator.Current; if (iterator.AtEnd()) { LoopHeader result = new LoopHeader(loop); return(result); } iterator.AssertExpression(out Expression firstExpression); if (iterator.AtEnd()) { // Actually checking that this expression is an array is for // a later step, when the messenger is available. array = firstExpression; LoopHeader result = new LoopHeader(loop, array); return(result); } if (iterator.NextIsKeyword(Keywords.In, out Token inToken)) { // Again, actually checking that this is indeed just a name // has to wait for a later stage. Expression name = firstExpression; iterator.AssertExpression(out array); iterator.AssertEnd(); LoopHeader result = new LoopHeader(loop, name, inToken, array); return(result); } if (iterator.NextIsKeyword(Keywords.From, out Token from)) { Expression name = firstExpression; iterator.AssertExpression(out Expression start); iterator.AssertKeyword(Keywords.To, out Token to); iterator.AssertExpression(out Expression end); iterator.AssertEnd(); LoopHeader result = new LoopHeader( loop, name, from, start, to, end); return(result); } throw iterator.UnexpectedEnd(); }
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 VariableReference CreateReference(MatchIterator iterator) { iterator.AssertTokenType(TokenType.Name, out Token token); iterator.AssertEnd(); return(new VariableReference(token)); }
public static Command Create(MatchIterator iterator) { iterator.AssertTokenType(TokenType.KeyWord, out Token keyWord); iterator.AssertEnd(); Command result = new Command(keyWord); return(result); }
public static ElseHeader Else(MatchIterator iterator) { iterator.AssertKeyword(Keywords.Else, out Token elseToken); iterator.AssertEnd(); ElseHeader result = new ElseHeader(elseToken); return(result); }
public static LiteralGet Create(MatchIterator iterator) { iterator.AssertToken(out Token token); LiteralGet result = new LiteralGet(token); iterator.AssertEnd(); return(result); }
public static IfHeader If(MatchIterator iterator) { iterator.AssertKeyword(Keywords.If, out Token ifToken); iterator.AssertExpression(out Expression condition); iterator.AssertEnd(); IfHeader result = new IfHeader(ifToken, condition); return(result); }
public static ElseIfHeader ElseIf(MatchIterator iterator) { iterator.AssertKeyword(Keywords.Else, out Token elseToken); iterator.AssertKeyword(Keywords.If, out Token ifToken); iterator.AssertExpression(out Expression condition); iterator.AssertEnd(); ElseIfHeader result = new ElseIfHeader(elseToken, ifToken, condition); return(result); }
public static Return Return(MatchIterator iterator) { iterator.AssertKeyword(Keywords.Return); Token startToken = (Token)iterator.Current; Expression expression; iterator.NextIsExpression(out expression); iterator.AssertEnd(); Return result = new Return(startToken, expression); return(result); }
public static List <Expression> Arguments(MatchIterator iterator) { List <Expression> arguments = new List <Expression>(); if (iterator.AtEnd()) { return(arguments); } do { iterator.AssertExpression(out Expression argument); arguments.Add(argument); }while (iterator.NextIsKeySymbol(Symbols.Comma)); iterator.AssertEnd(); return(arguments); }
public static CollectedParameters Parameters(MatchIterator iterator) { List <SprakType> parameterTypes = new List <SprakType>(); List <string> parameterNames = new List <string>(); CollectedParameters result = new CollectedParameters(parameterTypes, parameterNames); if (iterator.AtEnd()) { return(result); } do { iterator.AssertType(out SprakType type); iterator.AssertName(out string name); parameterTypes.Add(type); parameterNames.Add(name); }while (iterator.NextIsKeySymbol(Symbols.Comma)); iterator.AssertEnd(); return(result); }