private void ReadCarriers(TokenReader reader, Carrier carrier, RantCompiler compiler) { while (!reader.End) { var token = reader.ReadLooseToken(); switch (token.Type) { // match carrier case R.Equal: { var name = reader.Read(R.Text, "acc-carrier-name"); if (name.Value != null) { carrier.AddComponent(CarrierComponentType.Match, name.Value); } } break; // associative and match associative, // disassociative and match disassociative // divergent and match-divergent // relational and match-relational case R.At: { var carrierType = CarrierComponentType.Associative; // disassociative if (reader.PeekToken().Type == R.Exclamation) { carrierType = CarrierComponentType.Dissociative; reader.ReadToken(); } // divergent else if (reader.PeekToken().Type == R.Plus) { carrierType = CarrierComponentType.Divergent; reader.ReadToken(); } else if (reader.PeekToken().Type == R.Question) { carrierType = CarrierComponentType.Relational; reader.ReadToken(); } // match if (reader.PeekToken().Type == R.Equal) { switch (carrierType) { case CarrierComponentType.Associative: carrierType = CarrierComponentType.MatchAssociative; break; case CarrierComponentType.Dissociative: carrierType = CarrierComponentType.MatchDissociative; break; case CarrierComponentType.Divergent: carrierType = CarrierComponentType.MatchDivergent; break; case CarrierComponentType.Relational: carrierType = CarrierComponentType.MatchRelational; break; } reader.ReadToken(); } var name = reader.Read(R.Text, "acc-carrier-name"); if (name.Value != null) { carrier.AddComponent(carrierType, name.Value); } } break; // unique and match unique case R.Exclamation: { var carrierType = CarrierComponentType.Unique; // match unique if (reader.PeekToken().Type == R.Equal) { carrierType = CarrierComponentType.MatchUnique; reader.ReadToken(); } var name = reader.Read(R.Text, "acc-carrier-name"); if (name.Value != null) { carrier.AddComponent(carrierType, name.Value); } } break; // rhyming case R.Ampersand: { var name = reader.Read(R.Text, "acc-carrier-name"); if (name.Value != null) { carrier.AddComponent(CarrierComponentType.Rhyme, name.Value); } } break; // we're done, go away case R.RightAngle: return; default: compiler.SyntaxError(token, false, "err-compiler-unexpected-token"); break; } } }
private static bool DoQuery(VM interpreter, Token <R> firstToken, PatternReader reader, State state) { reader.SkipSpace(); bool storeMacro = false; bool macroIsGlobal = false; string macroName = null; // Check if this is a macro if (reader.Take(R.At)) { reader.SkipSpace(); var macroNameToken = reader.Read(R.Text, "query macro name"); macroName = macroNameToken.Value; reader.SkipSpace(); // Check if the macro is a definition or a call. // A definition will start with a colon ':' or equals '=' after the name. A call will only consist of the name. switch (reader.ReadToken().ID) { case R.Colon: // Local definition { if (!ValidateName(macroNameToken.Value)) { throw Error(reader.Source, macroNameToken, "Invalid macro name: '\{macroNameToken.Value}'"); } storeMacro = true; } break; case R.Equal: // Global definition { if (!ValidateName(macroNameToken.Value)) { throw Error(reader.Source, macroNameToken, "Invalid macro name: '\{macroNameToken.Value}'"); } storeMacro = true; macroIsGlobal = true; } break; case R.RightAngle: // Call { Query q; var mNameSub = macroNameToken.Value.Split(new[] { '.' }, StringSplitOptions.None); if (!interpreter.LocalQueryMacros.TryGetValue(mNameSub[0], out q) && !interpreter.Engine.GlobalQueryMacros.TryGetValue(mNameSub[0], out q)) { throw new RantException(reader.Source, macroNameToken, "Nonexistent query macro '\{macroName}'"); } if (mNameSub.Length > 2) { throw Error(reader.Source, firstToken, "Invald subtype accessor on macro call."); } var oldSub = q.Subtype; if (mNameSub.Length == 2) { q.Subtype = mNameSub[1]; } interpreter.Print(interpreter.Engine.Dictionary?.Query(interpreter.RNG, q, interpreter.QueryState)); q.Subtype = oldSub; return(false); } } } else if (reader.Take(R.DoubleColon)) // Carrier reset { Token <R> token; while ((token = reader.ReadToken()).ID != R.RightAngle) { switch (token.ID) { case R.At: interpreter.QueryState.DeleteAssociation(reader.ReadLoose(R.Text, "associative carrier name").Value); break; case R.Exclamation: interpreter.QueryState.DeleteUnique(reader.ReadLoose(R.Text, "unique carrier name").Value); break; case R.Equal: interpreter.QueryState.DeleteMatch(reader.ReadLoose(R.Text, "match carrier name").Value); break; case R.Ampersand: interpreter.QueryState.DeleteRhyme(reader.ReadLoose(R.Text, "rhyme carrier name").Value); break; default: throw Error(reader.Source, token, "Unrecognized token in carrier reset: '\{token.Value}'"); } reader.SkipSpace(); } return(false); } reader.SkipSpace(); var namesub = reader.Read(R.Text, "dictionary name").Split(new[] { '.' }, 2).ToArray(); reader.SkipSpace(); bool exclusive = reader.Take(R.Dollar); List <Tuple <bool, string> > cfList = null; List <Tuple <bool, string>[]> classFilterList = null; List <Tuple <bool, Regex> > regList = null; Carrier carrier = null; SyllablePredicateFunc syllableRange = null; Token <R> queryToken = null; if (reader.IsNext(R.RangeLiteral)) { syllableRange = SyllablePredicate.Create(reader.ReadToken()); } // Read query arguments while (true) { reader.SkipSpace(); if (reader.Take(R.Hyphen)) { reader.SkipSpace(); // Initialize the filter list. (cfList ?? (cfList = new List <Tuple <bool, string> >())).Clear(); do { bool notin = reader.Take(R.Exclamation); reader.SkipSpace(); if (notin && exclusive) { throw new RantException(reader.Source, reader.PrevToken, "Cannot use the '!' modifier on exclusive class filters."); } cfList.Add(Tuple.Create(!notin, reader.Read(R.Text, "class identifier").Value.Trim())); reader.SkipSpace(); } while (reader.Take(R.Pipe)); (classFilterList ?? (classFilterList = new List <Tuple <bool, string>[]>())).Add(cfList.ToArray()); } else if (reader.Take(R.Question)) { reader.SkipSpace(); queryToken = reader.Read(R.Regex, "regex"); (regList ?? (regList = new List <Tuple <bool, Regex> >())).Add(Tuple.Create(true, ParseRegex(queryToken.Value))); } else if (reader.Take(R.Without)) { reader.SkipSpace(); queryToken = reader.Read(R.Regex, "regex"); (regList ?? (regList = new List <Tuple <bool, Regex> >())).Add(Tuple.Create(false, ParseRegex(queryToken.Value))); } else if (reader.Take(R.DoubleColon)) // Start of carrier { reader.SkipSpace(); carrier = new Carrier(); Token <R> typeToken; CarrierComponent comp = CarrierComponent.Match; while ((typeToken = reader.ReadToken()).ID != R.RightAngle) { switch (typeToken.ID) { case R.Exclamation: // Unique comp = reader.Take(R.Equal) ? CarrierComponent.MatchUnique : CarrierComponent.Unique; break; case R.Equal: // Match comp = CarrierComponent.Match; break; case R.Ampersand: // Rhyme comp = CarrierComponent.Rhyme; break; case R.At: // Associative/Relational/Dissociative/Divergent { if (reader.Take(R.Question)) { comp = reader.Take(R.Equal) ? CarrierComponent.MatchRelational : CarrierComponent.Relational; } else if (reader.Take(R.Exclamation)) { comp = reader.Take(R.Equal) ? CarrierComponent.MatchDissociative : CarrierComponent.Dissociative; } else if (reader.Take(R.Plus)) { comp = reader.Take(R.Equal) ? CarrierComponent.MatchDivergent : CarrierComponent.Divergent; } else { comp = reader.Take(R.Equal) ? CarrierComponent.MatchAssociative : CarrierComponent.Associative; } } break; default: throw new RantException(reader.Source, typeToken, "Unrecognized token '\{typeToken.Value}' in carrier."); } carrier.AddComponent(comp, reader.ReadLoose(R.Text, "carrier component name").Value); reader.SkipSpace(); } break; }