当前位置: 首页>>代码示例>>C#>>正文


C# Lexer.Restore方法代码示例

本文整理汇总了C#中Lexer.Restore方法的典型用法代码示例。如果您正苦于以下问题:C# Lexer.Restore方法的具体用法?C# Lexer.Restore怎么用?C# Lexer.Restore使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Lexer的用法示例。


在下文中一共展示了Lexer.Restore方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Integer

 public Integer(string module, string name, Lexer lexer)
 {
     Symbol temp = lexer.NextNonEOLSymbol;
     if (temp == Symbol.OpenBracket)
     {
         while ((temp = lexer.NextSymbol) != Symbol.CloseBracket)
         {
         }     
         
         return;
     }
     
     if (temp == Symbol.OpenParentheses) 
     {
         while ((temp = lexer.NextSymbol) != Symbol.CloseParentheses)
         {
         }  
         
         return;
     }
     
     lexer.Restore(temp); 
 }
开发者ID:xxjeng,项目名称:nuxleus,代码行数:23,代码来源:Integer.cs

示例2: matchInContext

   /// <summary>
   /// Finds the object that best matches the phrase
   /// </summary>
   /// <param name="lexer">The text being matched</param>
   /// <param name="score">The score for the best match: 0 if no match[out]</param>
   /// <param name="numWordsMatch">The number of words that match [out]</param>
   /// <param name="bestMatch">The object that best matches [out]</param>
   /// <param name="seen">Set of objects already examined</param>
   void matchInContext(ZObject addressedTo, Lexer lexer, 
               ref double score, ref int numWordsMatch,
               ref object bestMatch,
               ref LexState lexerState, Dictionary<object,object> seen)
   {
      // Make a note of where we are in the text
      var state = lexer.Save();
      // Search for the best item within the addressedTo object and it's children
      addressedTo.match(lexer, ref score, ref numWordsMatch, ref bestMatch,
         ref lexerState, seen);

      // Check to see if it a reference to the other party of a relationship
      //  a. Make a list of objects; eg connected to this via relationship/2
      //   i. If the link has a gatekeeper, see if the gate is open or closed
      // b. See which matches best the noun phrases

      //TODO: Bump the score to keep things closer to ourselves ?
      // Scan in each of the directions
      // We can't traverse the edge though
      foreach (var pair in relations)
      {
         // Go back to the saved point in the text
         lexer.Restore(state);
         // Match the relation/direction
         pair.Key.match(lexer, ref score, ref numWordsMatch, ref bestMatch,
            ref lexerState, seen);

         if (!(pair.Value is SparseGraph<ZObject>))
            continue;

         // Get the edge.. if none, then nothing can match
         List<Edge<ZObject>> edges;
         if (!((SparseGraph<ZObject>)pair.Value).successors(addressedTo, out edges) || edges.Count < 1)
         {
            // There is no specific destination here for this location;
            // the player could have given an abstract direction without a
            // concrete direciton.  We'll keep the direction as a match,
            // if it did match.
            continue;
         }

         // If the direction matched, we will keep the edge as it's referrant
         if (bestMatch == pair.Key) 
         {
            // We match the direction, so we are referring to the edge
            bestMatch     = edges[0];
         }

         // Go back to the saved point in the text
         lexer.Restore(state);

         // Match the room or gating door
         // Search for the best item within the addressedTo object and it's
         // children
         match(edges[0], lexer, ref score, ref numWordsMatch, ref bestMatch,
            ref lexerState, seen);
      }
      // Go back to the saved point in the text
      lexer.Restore(state);

      // bump the search to parents and their children
      if (null != addressedTo.Parent)
      {
         //TODO: Bump the score to keep things closer to ourselves ?
         matchInContext(addressedTo.Parent, lexer, ref score, ref numWordsMatch,
                                 ref bestMatch, ref lexerState, seen);
      }
   }
开发者ID:randym32,项目名称:InteractiveText-CS,代码行数:76,代码来源:InStory-interp.cs

示例3: getNounPhrases

   /// <summary>
   /// Gets the objects referred to by each of the noun phrases
   /// </summary>
   /// <param name="lexer">The lexer that provides the input</param>
   /// <param name="addressedTo"></param>
   /// <param name="err"></param>
   /// <returns></returns>
   List<object> getNounPhrases(Lexer lexer, ZObject addressedTo, Err err)
   {
      // The list of referred to objects
      var ret = new List<object>();
      lexer.Preprocess();

      // Map the rest of the words to nouns
      while (!lexer.EOF)
      {
         int matchLength  = 0;
         LexState lexerState = null;
         // Match the next noun phrase
         var t = matchInContext(addressedTo, lexer, out matchLength, ref lexerState);
         // 5. if no noun was mapped
         if (null == t)
         {
            // Try the main relations (isa)

            // error could not understand at index
            err . SB . AppendFormat("The phrase \"{0}\" isn't understood.", 
                                    lexer.ToEndOfLine().Trim());
            return null;
         }

         // Save the noun phrase
         ret.Add(t);

         // Move past the words in that subphrase
         if (null != lexerState)
            lexer.Restore(lexerState);
         lexer.Preprocess();
      }

      return ret;
      // couldNotUnderstand(string, startingAt);
      // isAmbiguous(words);
   }
开发者ID:randym32,项目名称:InteractiveText-CS,代码行数:44,代码来源:InStory-interp.cs

示例4: interp

   /// <summary>
   /// This is used to invoked the handler for the verb
   /// </summary>
   /// <param name="stmt">The statment to interpret</param>
   /// <param name="line">The line number it was invoked</param>
   /// <param name="err">Where to place any descriptive error messages</param>
   /// <returns>True on success, false on error</returns>
   bool interp(propositionContext ctxt,Lexer lexer, Err err)
   {
      // A reference to where the statement was made
      var src = new SrcInFile(null, lexer.Line);
      // See if there is a first noun, to whom the message is addressed
      var addressedTo = player;
      int matchLength  = 0;
      LexState lexerState = null;
      var tmp = matchInContext(((ZObject) player), lexer, out matchLength,
         ref lexerState);
      if (null != tmp && tmp is ZObject)
      {
         // Use the new object as the item to be commanded
         addressedTo = (ZObject) tmp;
         // Move past the words in that subphrase
         if (null != lexerState)
            lexer.Restore(lexerState);
      }

      // The handler for the different number of arguments
      dStatement[] handlers = null;

      // Find a matching verb phrase
      var phrase = findVerbPhrase(lexer);

      // Get the noun phrases
      var args = getNounPhrases(lexer, (ZObject) player, err);
      var n = null == args? 0: args.Count;

      // See if we need to guess the handler for the noun
      if (null == phrase)
      {
         // If there was nothing resolved about the nouns
         if (null == args)
            args = new List<object>();
         if (addressedTo != player)
         {
            // Move the "addressed to" back to the front
            addressedTo = player;
            args.Insert(0, tmp);
            n = args.Count;
         }

         // See if it said nothing
         if (0 == args.Count)
            return false;

         // See if the first item is a 
         tmp = args[0];

         // If it is a direction roll it around
         if (tmp is ZObject && !isKind((ZObject) tmp, dirKind))
         {
            // Format an error message
            err . linkTo(src);
            err . SB . AppendFormat("What do you mean?");
            return false;
         }

         // It is a direction 
         // Ok, lets just make it go?
         phrase = findVerbPhrase("go");
      }

      // If can't find the verb at all, complain
      if (null == phrase || !verbs.TryGetValue(phrase, out handlers))
      {
         // Format an error message
         err . linkTo(src);
         err . SB . AppendFormat("The verb isn't understood.");
         return false;
      }

      // If there isn't a form for that number of verbs, complain
      var h = n >= handlers . Length ? null : handlers[n];
      if (null == h)
      {
         err . linkTo(src);
         err . SB . AppendFormat("Verb '{0}' isn't known for that arity.", phrase);
         return false;
      }

      // Call the delegate
      return h(ctxt, args, src, err);
   }
开发者ID:randym32,项目名称:InteractiveText-CS,代码行数:92,代码来源:InStory-interp.cs

示例5: findVerbPhrase

   /// <summary>
   /// Finds a verb phrase that matches
   /// </summary>
   /// <param name="lex"></param>
   /// <param name="stateEndVerbPhrase"></param>
   /// <returns></returns>
   WordList findVerbPhrase(Lexer lex)
   {
      // First, make a list of all verb phrases
      var candidates = new List<WordList>();
      foreach (var item in verbs)
      {
         candidates.Add(item.Key);
      }

      // The words of the verb phrase
      var verbWords = new List<string>();
      var prevState = lex.Save();
      LexState stateEndVerbPhrase=prevState;

      // Next, get words from the lexer as long they match a phrase
      lex.Preprocess();
      while (!lex.EOF)
      {
         var word = lex.Symbol();
         lex.Preprocess();
         
         // See if the word matches any candidate
         var newCandidates = new List<WordList>();
         var numVerbWords = verbWords . Count;
         foreach (var candidate in candidates)
         {
            // Skip "shorter" phrases
            if (numVerbWords >= candidate.words.Length)
                continue;
            // Get the word for this far in
            var item = candidate.words[numVerbWords];

            // Is there a match?
            // Check for exact, but caseless match; or a partial
            if (item.Equals(word, StringComparison.CurrentCultureIgnoreCase)
               || ((word. Length < item.Length)
                     && item.Substring(0, word.Length).Equals(word, StringComparison.CurrentCultureIgnoreCase)))
            {
               // keep it
               newCandidates.Add(candidate);
            }
         }
         // Did anyone match?
         if (newCandidates . Count < 1)
            break;
         // Save the word and the matches
         candidates = newCandidates;
         verbWords.Add(word);
         stateEndVerbPhrase = lex.Save();
      }


      // Check to see if any matched
      if (candidates.Count < 1 || verbWords.Count < 1)
      {
         lex.Restore(prevState);
         return null;
      }

      // Jump back tot he end of the verb phrase
      lex.Restore(stateEndVerbPhrase);

      /// The words for the verb phrase
      WordList verbWordList = new WordList(verbWords.ToArray());

      // Rank the matched phrases, finding the best one
      var bestScore = 0.0;
      WordList bestCandidate = null;
      foreach (var candidate in candidates)
      {
         // Assign a score to the candidate.
         var score = candidate.score(verbWordList);
         // Is it a better match?
         if (score > bestScore)
         {
            // Keep it
            bestScore = score;
            bestCandidate=candidate;
         }
      }

      // Return the best matched phrase
      return bestCandidate;
   }
开发者ID:randym32,项目名称:InteractiveText-CS,代码行数:90,代码来源:InStory-verb.cs


注:本文中的Lexer.Restore方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。