public override void Visit(Literal expression) { terminal.Peek().Append("'"); terminal.Peek().Append(Regex.Replace(expression.MatchText, @"'", @"\'")); terminal.Peek().Append("'"); if (!expression.IsCaseSensitive) { terminal.Peek().Append(@"\i"); } }
public override void Visit(Literal expression) { Boolean iscasesensitive = expression.IsCaseSensitive; String data = expression.MatchText; _matchStack.Push( delegate(IInputIterator iterator) { foreach (Char c in data) { if (iscasesensitive) { if (iterator.Current() == -1) return false; if ((Byte)iterator.Current() != c) { iterator.Next(); return false; } } else { if (iterator.Current() == -1) return false; if (Encoding.UTF8.GetString(new[] { (Byte)iterator.Current() }, 0, 1).ToUpper() != c.ToString().ToUpper()) { iterator.Next(); return false; } } iterator.Next(); } return true; } ); }
public void Terminal_Literal() { var Mixed = new Literal {MatchText = "Hello World"}; var input = "hello world"; var bytes = Encoding.UTF8.GetBytes(input); var iterator = new ByteInputIterator(bytes); var visitor = new NpegParserVisitor(iterator); Mixed.Accept(visitor); Assert.IsFalse(visitor.IsMatch); // Not case sensitve Mixed.IsCaseSensitive = false; input = "hello world"; bytes = Encoding.UTF8.GetBytes(input); iterator = new ByteInputIterator(bytes); visitor = new NpegParserVisitor(iterator); Mixed.Accept(visitor); Assert.IsTrue(visitor.IsMatch); }
public abstract void Visit(Literal expression);
public void PracticalExample_PhoneNumber() { #region terminals AExpression Digits = new CharacterClass {ClassExpression = "[0-9]"}; AExpression Hyphen = new Literal {MatchText = "-"}; #endregion #region nonterminals AExpression ThreeDigitCode = new CapturingGroup("ThreeDigitCode", new Sequence(Digits, Digits).Sequence(Digits)); AExpression FourDigitCode = new CapturingGroup("FourDigitCode", new Sequence(Digits, Digits).Sequence(Digits).Sequence(Digits)); AExpression PhoneNumber = new CapturingGroup("PhoneNumber", new Sequence(ThreeDigitCode, Hyphen) .Sequence(ThreeDigitCode) .Sequence(Hyphen) .Sequence(FourDigitCode) ); #endregion String input = "123-456-7890"; // Test Manual Composite var bytes = Encoding.UTF8.GetBytes(input); var iterator = new ByteInputIterator(bytes); var visitor = new NpegParserVisitor(iterator); PhoneNumber.Accept(visitor); Assert.IsTrue(visitor.IsMatch); AstNode node = visitor.AST; Assert.IsTrue(node.Token.Name == "PhoneNumber"); Assert.IsTrue(node.Token.ValueAsString(iterator) == "123-456-7890"); Assert.IsTrue(node.Children[0].Token.Name == "ThreeDigitCode"); Assert.IsTrue(node.Children[0].Token.ValueAsString(iterator) == "123"); Assert.IsTrue(node.Children[1].Token.Name == "ThreeDigitCode"); Assert.IsTrue(node.Children[1].Token.ValueAsString(iterator) == "456"); Assert.IsTrue(node.Children[2].Token.Name == "FourDigitCode"); Assert.IsTrue(node.Children[2].Token.ValueAsString(iterator) == "7890"); }