public override void VisitLeave(OneOrMore expression) { IsMatchPredicate local = _matchStack.Pop(); _matchStack.Push( delegate(IInputIterator iterator) { Int32 cnt = 0; Int32 savePosition = iterator.Index; while (local(iterator)) { cnt++; if (savePosition == iterator.Index) { //InfiniteLoopDetectedException break; } savePosition = iterator.Index; } iterator.Index = savePosition; return(cnt > 0); } ); }
public override void VisitLeave(ZeroOrMore expression) { IsMatchPredicate local = _matchStack.Pop(); _matchStack.Push( delegate(IInputIterator iterator) { Int32 savePosition = iterator.Index; while (local(iterator)) { if (savePosition == iterator.Index) { //Exception ex = new InfiniteLoopDetectedException(); //ex.Data.Add("Iterator.Index", iterator.Index); //var rulewriter = new WriteRuleVisitor(); //expression.Accept(rulewriter); //ex.Data.Add("Expression", rulewriter.GrammarOutput); //throw ex; break; } savePosition = iterator.Index; } iterator.Index = savePosition; return(true); } ); }
public override void VisitLeave(NotPredicate expression) { IsMatchPredicate local = _matchStack.Pop(); _matchStack.Push( delegate(IInputIterator iterator) { _disableCapturingGroup.Push(true); _xmlDisableBackReferencePop.Push(true); Boolean result = true; Int32 savePosition = iterator.Index; if (!local(iterator)) { iterator.Index = savePosition; result &= true; } else { iterator.Index = savePosition; result &= false; } _xmlDisableBackReferencePop.Pop(); _disableCapturingGroup.Pop(); return(result); } ); }
protected Boolean PrioritizedChoice(IsMatchPredicate left, IsMatchPredicate right) { Int32 savePosition = iterator.Index; sandbox.Push(new Stack <AstNode>()); sandbox.Peek().Push(new AstNode()); if (left()) { Stack <AstNode> s = sandbox.Pop(); if (s.Count >= 1) { AstNode child = s.Pop(); if (sandbox.Peek().Count == 0) { sandbox.Peek().Push(child); } else { sandbox.Peek().Peek().Children.AddRange(child.Children); } } return(true); } sandbox.Pop(); iterator.Index = savePosition; sandbox.Push(new Stack <AstNode>()); sandbox.Peek().Push(new AstNode()); if (right()) { Stack <AstNode> s = sandbox.Pop(); if (s.Count >= 1) { AstNode child = s.Pop(); if (sandbox.Peek().Count == 0) { sandbox.Peek().Push(child); } else { sandbox.Peek().Peek().Children.AddRange(child.Children); } } return(true); } sandbox.Pop(); return(false); }
protected Boolean Sequence(IsMatchPredicate left, IsMatchPredicate right) { Int32 savePosition = iterator.Index; if (left() && right()) { return(true); } else { iterator.Index = savePosition; return(false); } }
protected Boolean Optional(IsMatchPredicate expression) { Int32 savePosition = iterator.Index; if (expression()) { savePosition = iterator.Index; } else { iterator.Index = savePosition; } return(true); }
public override void VisitLeave(RecursionCreate expression) { IsMatchPredicate local = _matchStack.Pop(); if (!_recursiveMethod.ContainsKey(expression.FunctionName)) { _recursiveMethod.Add( expression.FunctionName , delegate(IInputIterator iterator) { return(local(iterator)); } ); } _matchStack.Push(_recursiveMethod[expression.FunctionName]); }
protected Boolean NotPredicate(IsMatchPredicate predicate) { Boolean result = true; disableBackReferencePop.Push(true); Int32 savePosition = iterator.Index; if (!predicate()) { iterator.Index = savePosition; result &= true; } else { iterator.Index = savePosition; result &= false; } disableBackReferencePop.Pop(); return result; }
protected Boolean ZeroOrMore(IsMatchPredicate expression, String grammarExpression) { Int32 savePosition = iterator.Index; while (expression()) { if (savePosition == iterator.Index) { Exception ex = new InfiniteLoopDetectedException(); ex.Data.Add("Iterator Index", iterator.Index); ex.Data.Add("Grammar Expression", grammarExpression); throw ex; } savePosition = iterator.Index; } iterator.Index = savePosition; return(true); }
protected Boolean NotPredicate(IsMatchPredicate predicate) { Boolean result = true; disableBackReferencePop.Push(true); Int32 savePosition = iterator.Index; if (!predicate()) { iterator.Index = savePosition; result &= true; } else { iterator.Index = savePosition; result &= false; } disableBackReferencePop.Pop(); return(result); }
public override void VisitLeave(Optional expression) { IsMatchPredicate local = _matchStack.Pop(); _matchStack.Push( delegate(IInputIterator iterator) { Int32 savePosition = iterator.Index; if (local(iterator)) { savePosition = iterator.Index; } else { iterator.Index = savePosition; } return(true); } ); }
public override void VisitLeave(Sequence expression) { IsMatchPredicate localRight = _matchStack.Pop(); IsMatchPredicate localLeft = _matchStack.Pop(); _matchStack.Push( delegate(IInputIterator iterator) { Boolean result = true; Int32 savePosition = iterator.Index; if (localLeft(iterator) && localRight(iterator)) { result &= true; } else { iterator.Index = savePosition; result &= false; } return(result); } ); }
protected Boolean PrioritizedChoice(IsMatchPredicate left, IsMatchPredicate right) { Int32 savePosition = iterator.Index; sandbox.Push(new Stack<AstNode>()); sandbox.Peek().Push(new AstNode()); if (left()) { Stack<AstNode> s = sandbox.Pop(); if (s.Count >= 1) { AstNode child = s.Pop(); if (sandbox.Peek().Count == 0) { sandbox.Peek().Push(child); } else { sandbox.Peek().Peek().Children.AddRange(child.Children); } } return true; } sandbox.Pop(); iterator.Index = savePosition; sandbox.Push(new Stack<AstNode>()); sandbox.Peek().Push(new AstNode()); if (right()) { Stack<AstNode> s = sandbox.Pop(); if (s.Count >= 1) { AstNode child = s.Pop(); if (sandbox.Peek().Count == 0) { sandbox.Peek().Push(child); } else { sandbox.Peek().Peek().Children.AddRange(child.Children); } } return true; } sandbox.Pop(); return false; }
public override void VisitLeave(LimitingRepetition expression) { Int32? max = expression.Max; Int32? min = expression.Min; IsMatchPredicate local = _matchStack.Pop(); _matchStack.Push( delegate(IInputIterator iterator) { Int32 cnt = 0; Int32 savePosition = iterator.Index; Boolean result = false; if (expression.VariableLengthExpression == null) { if (min != null) { if (max == null) { // has a minimum but no max cap savePosition = iterator.Index; while (local(iterator)) { cnt++; if (savePosition == iterator.Index) { //InfiniteLoopDetectedException break; } savePosition = iterator.Index; } iterator.Index = savePosition; result = (cnt >= min); } else { // has a minimum and a max specified if (max < min) { throw new ArgumentException( "A Max property must be larger than Min when using LimitingRepetition."); } savePosition = iterator.Index; while (local(iterator)) { cnt++; savePosition = iterator.Index; if (cnt >= max) { break; } } iterator.Index = savePosition; result = (cnt <= max && cnt >= min); } } else // min == null { if (max == null) { throw new ArgumentException( "A Min and/or Max must be specified when using LimitingRepetition."); } // zero or up to a max matches of e. savePosition = iterator.Index; while (local(iterator)) { cnt++; savePosition = iterator.Index; if (cnt >= max) { break; } } iterator.Index = savePosition; result = (cnt <= max); } } else { var variableValues = _sandbox.Peek().ToArray().SelectMany(x => x.Children).ToList(); var varLengthBytes = Encoding.UTF8.GetBytes(expression.VariableLengthExpression); var varLengthIterator = new ByteInputIterator(varLengthBytes); var varLengthParser = new LimitingRepetitionVariableLengthExpressionParser(); var varLengthParseTree = varLengthParser.Parse(varLengthIterator); var varLengthSolver = new LimitingRepetitionVariableLengthExpressionSolver(varLengthIterator, iterator, variableValues); varLengthParseTree.Accept(varLengthSolver); var value = (int)varLengthSolver.Result; savePosition = iterator.Index; while (value > cnt && local(iterator)) { cnt++; savePosition = iterator.Index; if (cnt == value) { break; } } iterator.Index = savePosition; result = (cnt == value); } return(result); } ); }
protected Boolean CapturingGroup(IsMatchPredicate expression, String name, Boolean reduceBySingleChildNode, Boolean doCreateCustomNode) { if (doCreateCustomNode && pointerAstNodeFactory == null) { throw new ArgumentNullException( "Second constructor overload is required during instantiation. Function pointer to AstNodeCreate method requires to be set with this parser implementation."); } Boolean result = true; Int32 savePosition = iterator.Index; sandbox.Peek().Push(new AstNode()); if (expression()) { Byte[] matchedBytes = iterator.Text(savePosition, iterator.Index); if (backReferenceLookup.ContainsKey(name)) { backReferenceLookup[name].Push(matchedBytes); } else { backReferenceLookup.Add(name, new Stack <Byte[]>()); backReferenceLookup[name].Push(matchedBytes); } AstNode node = sandbox.Peek().Pop(); node.Token = new TokenMatch(name, savePosition, iterator.Index, iterator.Text(savePosition, iterator.Index)); if (doCreateCustomNode) { // create a custom astnode IAstNodeReplacement nodevisitor = pointerAstNodeFactory.Create(node); nodevisitor.Token = node.Token; nodevisitor.Parent = node.Parent; nodevisitor.Children = node.Children; foreach (AstNode updateparent in nodevisitor.Children) { updateparent.Parent = nodevisitor; } // since the whole tree has not finished completing this.Parent will be null on this run. // logic inside astnodereplacement is to create properties, business names, that internally check Children collection to mine data. // you still will need a top level visitor to process tree after it completely available. node.Accept(nodevisitor); node = nodevisitor; } if (reduceBySingleChildNode) { if (node.Children.Count == 1) { node = node.Children[0]; } } if (sandbox.Peek().Count > 0) { node.Parent = sandbox.Peek().Peek(); sandbox.Peek().Peek().Children.Add(node); } else { sandbox.Peek().Push(node); // don't loose the root node // each successful sandbox will have 1 item left in the stack } savePosition = iterator.Index; result &= true; } else { sandbox.Peek().Pop(); iterator.Index = savePosition; result &= false; } return(result); }
protected Boolean Optional(IsMatchPredicate expression) { Int32 savePosition = iterator.Index; if (expression()) { savePosition = iterator.Index; } else { iterator.Index = savePosition; } return true; }
protected Boolean OneOrMore(IsMatchPredicate expression, String grammarExpression) { Int32 cnt = 0; Int32 savePosition = iterator.Index; while (expression()) { if (savePosition == iterator.Index) { Exception ex = new InfiniteLoopDetectedException(); ex.Data.Add("Iterator Index", iterator.Index); ex.Data.Add("Grammar Expression", grammarExpression); throw ex; } savePosition = iterator.Index; cnt++; } iterator.Index = savePosition; return (cnt > 0); }
protected Boolean Sequence(IsMatchPredicate left, IsMatchPredicate right) { Int32 savePosition = iterator.Index; if (left() && right()) { return true; } else { iterator.Index = savePosition; return false; } }
public override void VisitLeave(CapturingGroup expression) { String name = expression.Name; Boolean reduceBySingleChildNode = expression.DoReplaceBySingleChildNode; IsMatchPredicate local = _matchStack.Pop(); _matchStack.Push( delegate(IInputIterator iterator) { if (expression.DoCreateCustomAstNode && _astNodeFactory == null) { throw new ArgumentNullException("Second constructor overload is required during instantiation. astNodeFactory requires to be set with this parser implementation."); } Int32 savePosition = iterator.Index; _sandbox.Peek().Push(new AstNode()); if (local(iterator)) { // predicates being processed should not append ast if (_disableCapturingGroup.Count > 0) { _sandbox.Peek().Pop(); iterator.Index = savePosition; return(true); } if (savePosition >= iterator.Index) { // Warn terminal does not consume and ast should not be created for it, yet it should return that it was successful match. _sandbox.Peek().Pop(); iterator.Index = savePosition; return(true); } Byte[] matchedBytes = iterator.Text(savePosition, iterator.Index - 1); if (_xmlBackReferenceLookup.ContainsKey(name)) { _xmlBackReferenceLookup[name].Push(matchedBytes); } else { _xmlBackReferenceLookup.Add(name, new Stack <Byte[]>()); _xmlBackReferenceLookup[name].Push(matchedBytes); } AstNode node = _sandbox.Peek().Pop(); node.Token = new TokenMatch(name, savePosition, iterator.Index - 1); if (expression.DoCreateCustomAstNode) { // create a custom astnode IAstNodeReplacement nodevisitor = _astNodeFactory.Create(node); nodevisitor.Token = node.Token; nodevisitor.Parent = node.Parent; nodevisitor.Children = node.Children; foreach (AstNode updateparent in nodevisitor.Children) { updateparent.Parent = nodevisitor; } // since the whole tree has not finished completing this.Parent will be null on this run. // logic inside astnodereplacement is to create properties, business names, that internally check Children collection to mine data. // you still will need a top level visitor to process tree after it completely available. node.Accept(nodevisitor); node = nodevisitor; } if (reduceBySingleChildNode) { if (node.Children.Count == 1) { node = node.Children[0]; } } if (_sandbox.Peek().Count > 0) { node.Parent = _sandbox.Peek().Peek(); _sandbox.Peek().Peek().Children.Add(node); } else { _sandbox.Peek().Push(node); // don't loose the root node // each successful sandbox will have 1 item left in the stack } return(true); } _sandbox.Peek().Pop(); iterator.Index = savePosition; return(false); }); }
protected Boolean LimitingRepetition(IsMatchPredicate expression, Int32?min, Int32?max, String grammarExpression) { Int32 cnt = 0; Int32 savePosition = iterator.Index; Boolean result = false; if (min != null) { if (max == null) { // has a minimum but no max cap savePosition = iterator.Index; while (expression()) { if (savePosition == iterator.Index) { Exception ex = new InfiniteLoopDetectedException(); ex.Data.Add("Iterator Index", iterator.Index); ex.Data.Add("Grammar Expression", grammarExpression); throw ex; } cnt++; savePosition = iterator.Index; } iterator.Index = savePosition; result = (cnt >= min); } else { // has a minimum and a max specified if (max < min) { throw new ArgumentException("A Max property must be larger than Min when using LimitingRepetition."); } savePosition = iterator.Index; while (expression()) { cnt++; savePosition = iterator.Index; if (cnt >= max) { break; } } iterator.Index = savePosition; result = (cnt <= max && cnt >= min); } } else { if (max == null) { throw new ArgumentException("A Min and/or Max must be specified when using LimitingRepetition."); } else { // zero or up to a max matches of e. savePosition = iterator.Index; while (expression()) { cnt++; savePosition = iterator.Index; if (cnt >= max) { break; } } iterator.Index = savePosition; result = (cnt <= max); } } return(result); }
public override void VisitLeave(PrioritizedChoice expression) { IsMatchPredicate localRight = _matchStack.Pop(); IsMatchPredicate localLeft = _matchStack.Pop(); _matchStack.Push( delegate(IInputIterator iterator) { Int32 savePosition = iterator.Index; _sandbox.Push(new Stack <AstNode>()); _sandbox.Peek().Push(new AstNode()); // create new sandbox which capturing group works in. // have a dummy node to act as parent container (CapturedGroup logic) if (localLeft(iterator)) { Stack <AstNode> s = _sandbox.Pop(); if (s.Count >= 1) { AstNode child = s.Pop(); if (_sandbox.Peek().Count == 0) { _sandbox.Peek().Push(child); } else { _sandbox.Peek().Peek().Children.AddRange(child.Children); // our dummy node for this sandbox is child. // move important data over to the previous sandbox. } } return(true); } _sandbox.Pop(); // destory sandbox since anything captured is no longer valid. iterator.Index = savePosition; _sandbox.Push(new Stack <AstNode>()); _sandbox.Peek().Push(new AstNode()); // create new sandbox which capturing group works in. // have a dummy node to act as parent container (CapturedGroup logic) if (localRight(iterator)) { Stack <AstNode> s = _sandbox.Pop(); if (s.Count >= 1) { AstNode child = s.Pop(); if (_sandbox.Peek().Count == 0) { _sandbox.Peek().Push(child); } else { _sandbox.Peek().Peek().Children.AddRange(child.Children); // our dummy node for this sandbox is child. // move important data over to the previous sandbox. } } return(true); } _sandbox.Pop(); // destory sandbox since anything captured is no longer valid. return(false); } ); }
protected Boolean RecursionCall(IsMatchPredicate expression) { return(expression()); }
protected Boolean CapturingGroup(IsMatchPredicate expression, String name, Boolean reduceBySingleChildNode, Boolean doCreateCustomNode) { if (doCreateCustomNode && pointerAstNodeFactory == null) { throw new ArgumentNullException( "Second constructor overload is required during instantiation. Function pointer to AstNodeCreate method requires to be set with this parser implementation."); } Boolean result = true; Int32 savePosition = iterator.Index; sandbox.Peek().Push(new AstNode()); if (expression()) { Byte[] matchedBytes = iterator.Text(savePosition, iterator.Index); if (backReferenceLookup.ContainsKey(name)) { backReferenceLookup[name].Push(matchedBytes); } else { backReferenceLookup.Add(name, new Stack<Byte[]>()); backReferenceLookup[name].Push(matchedBytes); } AstNode node = sandbox.Peek().Pop(); node.Token = new TokenMatch(name, savePosition, iterator.Index, iterator.Text(savePosition, iterator.Index)); if (doCreateCustomNode) { // create a custom astnode IAstNodeReplacement nodevisitor = pointerAstNodeFactory.Create(node); nodevisitor.Token = node.Token; nodevisitor.Parent = node.Parent; nodevisitor.Children = node.Children; foreach (AstNode updateparent in nodevisitor.Children) { updateparent.Parent = nodevisitor; } // since the whole tree has not finished completing this.Parent will be null on this run. // logic inside astnodereplacement is to create properties, business names, that internally check Children collection to mine data. // you still will need a top level visitor to process tree after it completely available. node.Accept(nodevisitor); node = nodevisitor; } if (reduceBySingleChildNode) { if (node.Children.Count == 1) { node = node.Children[0]; } } if (sandbox.Peek().Count > 0) { node.Parent = sandbox.Peek().Peek(); sandbox.Peek().Peek().Children.Add(node); } else { sandbox.Peek().Push(node); // don't loose the root node // each successful sandbox will have 1 item left in the stack } savePosition = iterator.Index; result &= true; } else { sandbox.Peek().Pop(); iterator.Index = savePosition; result &= false; } return result; }
protected Boolean LimitingRepetition(IsMatchPredicate expression, Int32? min, Int32? max, String grammarExpression) { Int32 cnt = 0; Int32 savePosition = iterator.Index; Boolean result = false; if (min != null) { if (max == null) { // has a minimum but no max cap savePosition = iterator.Index; while (expression()) { if (savePosition == iterator.Index) { Exception ex = new InfiniteLoopDetectedException(); ex.Data.Add("Iterator Index", iterator.Index); ex.Data.Add("Grammar Expression", grammarExpression); throw ex; } cnt++; savePosition = iterator.Index; } iterator.Index = savePosition; result = (cnt >= min); } else { // has a minimum and a max specified if (max < min) { throw new ArgumentException("A Max property must be larger than Min when using LimitingRepetition."); } savePosition = iterator.Index; while (expression()) { cnt++; savePosition = iterator.Index; if (cnt >= max) { break; } } iterator.Index = savePosition; result = (cnt <= max && cnt >= min); } } else { if (max == null) { throw new ArgumentException("A Min and/or Max must be specified when using LimitingRepetition."); } else { // zero or up to a max matches of e. savePosition = iterator.Index; while (expression()) { cnt++; savePosition = iterator.Index; if (cnt >= max) { break; } } iterator.Index = savePosition; result = (cnt <= max); } } return result; }
protected Boolean RecursionCall(IsMatchPredicate expression) { return expression(); }