コード例 #1
0
ファイル: StringParser.cs プロジェクト: trimonovds/Rxx
    /// <summary>
    /// Creates a parser with a grammar that matches one or more consecutive characters until any of the specified 
    /// stop characters are parsed and joins them into a <see cref="string"/>, excluding the stop character.
    /// </summary>
    /// <param name="stops">The characters at which to stop consuming characters.</param>
    /// <returns>A parser with a grammar that consumes consecutive characters until any stop character is parsed
    /// or the sequence ends.</returns>
    protected IParser<char, string> AnyCharacterUntil(params char[] stops)
    {
      Contract.Requires(stops != null);
      Contract.Requires(stops.Length > 0);
      Contract.Ensures(Contract.Result<IParser<char, string>>() != null);

      return AnyCharacter.Not(Character(c => stops.Contains(c))).OneOrMore().Join();
    }
コード例 #2
0
        public void Terminal_Any()
        {
            var         input    = "ijk";
            var         bytes    = Encoding.UTF8.GetBytes(input);
            var         iterator = new ByteInputIterator(bytes);
            AExpression any      = new Sequence(new AnyCharacter(), new AnyCharacter());
            var         visitor  = new NpegParserVisitor(iterator);

            any.Accept(visitor);
            Assert.IsTrue(visitor.IsMatch);
            Assert.IsTrue(iterator.Index == 2,
                          "Expected two characters to be consumed and Iterator updated by 2.  0, 1 .. points to 2");

            input    = "ij";
            bytes    = Encoding.UTF8.GetBytes(input);
            iterator = new ByteInputIterator(bytes);
            visitor  = new NpegParserVisitor(iterator);
            any.Accept(visitor);
            Assert.IsTrue(visitor.IsMatch);
            Assert.IsTrue(iterator.Index == 2,
                          "Expected two characters to be consumed and Iterator updated by 2.  0, 1 .. points to 2");

            input    = "";
            bytes    = Encoding.UTF8.GetBytes(input);
            iterator = new ByteInputIterator(bytes);
            any      = new AnyCharacter();
            visitor  = new NpegParserVisitor(iterator);
            any.Accept(visitor);
            Assert.IsFalse(visitor.IsMatch);
            Assert.IsTrue(iterator.Index == 0, "Expected no characters to be consumed and index stay at zero.");

            var number = new Sequence(
                new OneOrMore(new CharacterClass {
                ClassExpression = "[0-9]"
            }),
                new NotPredicate(
                    new AnyCharacter()
                    )
                );

            input    = "012345.";
            bytes    = Encoding.UTF8.GetBytes(input);
            iterator = new ByteInputIterator(bytes);
            visitor  = new NpegParserVisitor(iterator);
            number.Accept(visitor);
            Assert.IsFalse(visitor.IsMatch);
        }
コード例 #3
0
ファイル: StringParser.cs プロジェクト: trimonovds/Rxx
    /// <summary>
    /// Creates a parser with a grammar that matches one or more consecutive characters until any of the specified 
    /// stop words are parsed and joins them into a <see cref="string"/>, excluding the stop word.
    /// </summary>
    /// <param name="stopWords">The strings at which to stop consuming characters.</param>
    /// <returns>A parser with a grammar that consumes consecutive characters until any stop word is parsed
    /// or the sequence ends.</returns>
    protected IParser<char, string> AnyCharacterUntil(params string[] stopWords)
    {
      Contract.Requires(stopWords != null);
      Contract.Requires(stopWords.Length > 0);
      Contract.Ensures(Contract.Result<IParser<char, string>>() != null);

      var stops = new List<IParser<char, string>>();

      foreach (var word in stopWords)
      {
        Contract.Assume(!string.IsNullOrEmpty(word));

        stops.Add(Word(word));
      }

      return AnyCharacter.Not(stops.Any()).OneOrMore().Join();
    }
コード例 #4
0
ファイル: TerminalNodes.cs プロジェクト: philiplaureano/NPEG
        public void Terminal_Any()
        {
            var input = "ijk";
            var bytes = Encoding.UTF8.GetBytes(input);
            var iterator = new ByteInputIterator(bytes);
            AExpression any = new Sequence(new AnyCharacter(), new AnyCharacter());
            var visitor = new NpegParserVisitor(iterator);
            any.Accept(visitor);
            Assert.IsTrue(visitor.IsMatch);
            Assert.IsTrue(iterator.Index == 2,
                          "Expected two characters to be consumed and Iterator updated by 2.  0, 1 .. points to 2");

            input = "ij";
            bytes = Encoding.UTF8.GetBytes(input);
            iterator = new ByteInputIterator(bytes);
            visitor = new NpegParserVisitor(iterator);
            any.Accept(visitor);
            Assert.IsTrue(visitor.IsMatch);
            Assert.IsTrue(iterator.Index == 2,
                          "Expected two characters to be consumed and Iterator updated by 2.  0, 1 .. points to 2");

            input = "";
            bytes = Encoding.UTF8.GetBytes(input);
            iterator = new ByteInputIterator(bytes);
            any = new AnyCharacter();
            visitor = new NpegParserVisitor(iterator);
            any.Accept(visitor);
            Assert.IsFalse(visitor.IsMatch);
            Assert.IsTrue(iterator.Index == 0, "Expected no characters to be consumed and index stay at zero.");

            var number = new Sequence(
                new OneOrMore(new CharacterClass {ClassExpression = "[0-9]"}),
                new NotPredicate(
                    new AnyCharacter()
                    )
                );
            input = "012345.";
            bytes = Encoding.UTF8.GetBytes(input);
            iterator = new ByteInputIterator(bytes);
            visitor = new NpegParserVisitor(iterator);
            number.Accept(visitor);
            Assert.IsFalse(visitor.IsMatch);
        }
コード例 #5
0
 public override void Visit(AnyCharacter expression)
 {
     _matchStack.Push(
         delegate(IInputIterator iterator)
     {
         // returns true only if this method can consume a character.
         if (iterator.Index < iterator.Length)
         {
             /*
              * Consume one character since we're neither at the end of a real string,
              * nor operating on the empty string.
              */
             iterator.Index += 1;
             return(true);
         }
         else
         {
             return(false);
         }
     }
         );
 }
コード例 #6
0
 public virtual void Visit(AnyCharacter expression, T context)
 {
 }
コード例 #7
0
 public abstract void Visit(AnyCharacter expression);
コード例 #8
0
		public override void Visit(AnyCharacter expression)
		{
			_matchStack.Push(
				delegate(IInputIterator iterator)
				{
					// returns true only if this method can consume a character.
					if (iterator.Index < iterator.Length)
					{
						/*
						 * Consume one character since we're neither at the end of a real string, 
						 * nor operating on the empty string.
						 */
						iterator.Index += 1;
						return true;
					}
					else
					{
						return false;
					}
				}
				);
		}
コード例 #9
0
 public override void Visit(AnyCharacter expression, object context)
 {
     expressions.Add(expression);
     base.Visit(expression, context);
 }
コード例 #10
0
		public override void Visit(AnyCharacter expression)
		{
			terminal.Peek().Append('.');
		}
コード例 #11
0
 public abstract void Visit(AnyCharacter expression);
コード例 #12
0
 public override void Visit(AnyCharacter expression)
 {
     terminal.Peek().Append('.');
 }
コード例 #13
0
ファイル: FirstSet.cs プロジェクト: asimpkins78/npeg
 public void Visit(AnyCharacter expression, Nonterminal context)
 {
     Add(context, expression, AnyCharacterTerminal.Instance);
 }