/// <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(); }
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); }
/// <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(); }
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); }
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); } } ); }
public virtual void Visit(AnyCharacter expression, T context) { }
public abstract void Visit(AnyCharacter expression);
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; } } ); }
public override void Visit(AnyCharacter expression, object context) { expressions.Add(expression); base.Visit(expression, context); }
public override void Visit(AnyCharacter expression) { terminal.Peek().Append('.'); }
public void Visit(AnyCharacter expression, Nonterminal context) { Add(context, expression, AnyCharacterTerminal.Instance); }