private static bool DoSubDefinition(Token <TokenType> first, Interpreter interpreter, SourceReader reader, State state) { bool meta = reader.Take(TokenType.Question); reader.Read(TokenType.LeftSquare); var parameters = new List <Tuple <string, ParamFlags> >(); var tName = reader.Read(TokenType.Text, "subroutine name"); if (!Util.ValidateName(tName.Value)) { throw new RantException(reader.Source, tName, "Invalid subroutine name: '" + tName.Value + "'"); } if (!reader.Take(TokenType.Colon)) { reader.Read(TokenType.RightSquare); } else { while (true) { bool isTokens = reader.Take(TokenType.At); parameters.Add(Tuple.Create(reader.Read(TokenType.Text, "parameter name").Value, isTokens ? ParamFlags.Code : ParamFlags.None)); if (reader.Take(TokenType.RightSquare, false)) { break; } reader.Read(TokenType.Semicolon); } } reader.SkipSpace(); reader.Read(TokenType.Colon); var body = reader.ReadToScopeClose(TokenType.LeftSquare, TokenType.RightSquare, BracketPairs.All).ToArray(); if (meta) { interpreter.PushState(State.CreateDerivedDistinct(reader.Source, body, interpreter)); state.AddPreBlueprint(new FunctionBlueprint(interpreter, _ => { _.Engine.Subroutines.Define(tName.Value, Subroutine.FromString(tName.Value, _.PopResultString(), parameters.ToArray())); return(false); })); } else { interpreter.Engine.Subroutines.Define(tName.Value, Subroutine.FromTokens(tName.Value, reader.Source, body, parameters.ToArray())); } return(meta); }
private static bool DoQuery(Interpreter interpreter, SourceReader reader, State state) { var first = reader.ReadToken(); reader.SkipSpace(); var namesub = reader.Read(TokenType.Text, "list name").Split(new[] { '.' }, 2).ToArray(); var q = new Query(namesub[0].Value.Trim(), namesub.Length == 2 ? namesub[1].Value : "", "", reader.Take(TokenType.Dollar), null, null); Token <TokenType> token = null; // Class filter list. Not initialized unless class filters actually exist. List <Tuple <bool, string> > cfList = null; while (true) { if (reader.Take(TokenType.Hyphen)) { // Initialize the filter list. (cfList ?? (cfList = new List <Tuple <bool, string> >())).Clear(); do { bool notin = reader.Take(TokenType.Exclamation); if (notin && q.Exclusive) { throw new RantException(reader.Source, reader.PrevToken, "Cannot use the '!' modifier on exclusive class filters."); } cfList.Add(Tuple.Create(!notin, reader.Read(TokenType.Text, "class identifier").Value.Trim())); } while (reader.Take(TokenType.Pipe)); q.ClassFilters.Add(cfList.ToArray()); } else if (reader.Take(TokenType.Question)) { token = reader.Read(TokenType.Regex, "regex"); q.RegexFilters.Add(Tuple.Create(true, Util.ParseRegex(token.Value))); } else if (reader.Take(TokenType.Without)) { token = reader.Read(TokenType.Regex, "regex"); q.RegexFilters.Add(Tuple.Create(false, Util.ParseRegex(token.Value))); } else if (reader.Take(TokenType.DoubleColon)) { token = reader.Read(TokenType.Text, "carrier name"); q.Carrier = token.Value.Trim(); if (!reader.Take(TokenType.RightAngle)) { throw new RantException(reader.Source, token, "Expected '>' after carrier. (The carrier should be your last query argument!)"); } break; } else if (reader.Take(TokenType.RightAngle)) { break; } else if (!reader.SkipSpace()) { var t = !reader.End ? reader.ReadToken() : null; throw new RantException(reader.Source, t, t == null ? "Unexpected end-of-file in query." : "Unexpected token '" + t.Value + "' in query."); } } interpreter.Print(interpreter.Engine.Vocabulary.Query(interpreter.RNG, q)); return(false); }