protected override ParserState <Result> EnterNodeImpl(ParserNode nextNode) { var recursionCallNode = this.CurrNode as ParserNode.RecursiveParserNode; if (nextNode is ParserNode.Number) { if (this.CurrNode == nextNode.Parent) { this.PushChildIndex(); } else if (this.CurrNode.Parent == nextNode) { this.IncChildIndex(); } } return(new LinearParserState( _log, nextNode, this.CurrNode, this.LastStep.CreateEnterNode(nextNode, this.Location), recursionCallNode == null ? this.Stack : this.Stack.StartRecursionRuleCall(recursionCallNode), null, this.Reader )); }
private ParserState <Result> MayBeReconstruct(ParserNode nextNode) { ParsingTreeNode.TemporaryGroup reconstructedTree; if (this.TreeNode.TryReconstruct(nextNode, this.Location, out reconstructedTree)) { if (_log != null) { while (_log.Level > 2) { _log.Pop(); } _log.WriteLine("Tree reconstruction completed!"); } return(new ParserTreeState( _log, this.CurrNode, reconstructedTree, null, this.Reader ) { ResultReconstructed = true, InsideOmittedFragment = false }); } else { return(null); } }
protected override ParserState <Result> ExitNodeImpl(bool?terminalFailed, ParserNode nextNode) { if (this.CurrNode is ParserNode.Number && nextNode == this.CurrNode.Parent) { this.PopChildIndex(); } // fallback if (terminalFailed == true) { var cloc = this.Reader.Location; var oloc = this.LastStep.Location; if (cloc.Line != oloc.Line) { throw new NotImplementedException(); } if (cloc.Column != oloc.Column) { this.Reader.Move(oloc.Column - cloc.Column); } } return(new LinearParserState( _log, nextNode, this.CurrNode, this.LastStep is ParserStep.EnterNode && this.LastStep.Node == this.CurrNode && this.LastStep.PrevStep.Node == nextNode ? this.LastStep.PrevStep : this.LastStep.CreateExitNode(this.CurrNode, this.Location), nextNode is ParserNode.RecursiveParserNode ? this.Stack.EndRecursionRuleCall() : this.Stack, terminalFailed, this.Reader )); }
public static ParserState <Result> ForStart(ParserNode root, ISourceTextReader reader, bool enableLog) { IndentedWriter w; if (enableLog) { w = new IndentedWriter(" "); w.Push().WriteLine("Start @{0} {1}", reader.GetPosition(), root).Push(); } else { w = null; } if (root.Parent != null) { throw new ArgumentException(); } return(new LinearParserState( w, root, null, new ParserStep.EnterNode(null, root, reader.Location), StatefullStackNode.ForRoot(), null, reader )); }
public Terminal(ParserNode node, Location loc, ParsedNode next, Location from, Location to, string content) : base(node, loc, next) { this.From = from; this.To = to; this.Content = content; }
public TemporaryTerminal(ParserNode node, Location loc, TemporaryGroup parent, Location from, Location to, string content) : base(node, loc, parent) { this.From = from; this.To = to; this.Content = content; }
public TemporaryGroupChildTerminal(ParserNode grammarNode, Location loc, TemporaryChildNode prev, Location from, Location to, string content) : base(grammarNode, loc, prev) { this.From = from; this.To = to; this.Content = content; }
public Terminal(ParserStep prev, ParserNode node, Location from, Location to, string text) : base(prev, node, from) { this.From = from; this.To = to; this.Text = text; }
private ParserNode BuildGraphInternal(RuleExpression rootExpression) { _currContext = GraphContext.ForRoot(rootExpression); _recursiveNodes.Clear(); do { var ctx = _currContext; if (ctx.expression != null) { ctx.expression.Visit(_expressionsVisitor); } else { throw new NotImplementedException(); // should never happen } if (_currContext == ctx) { throw new InvalidOperationException(string.Format("Missing transition for expression [{0}] of rule [{1}] was not created!", _currContext.expression, _currContext.rule)); } if (_currContext == null) { throw new InvalidOperationException(string.Format("Invalid handler for expression [{0}] of rule [{1}]!", _currContext.expression, _currContext.rule)); } //if (_currContext.prevContext != ctx) // throw new InvalidOperationException(string.Format("Invalid previos context was set from handler for expression [{0}] of rule [{1}] was not created!", _currContext.expression, _currContext.rule)); } while (_currContext.parentContext != null); if (_currContext.childEntries == null || _currContext.childEntries.prev != null) { throw new InvalidOperationException(); } foreach (var item in _recursiveNodes) { ParserNode target = null; for (var n = item.Parent; n != null; n = n.Parent) { if (n.Rule == item.Rule && n.Parent.Rule != item.Rule && n.Parent is ParserNode.RuleCall) // && item.TargetCallExpr.ToString() && ) { target = n.Parent; break; } } if (target == null) { throw new InvalidOperationException(string.Format("Filed to find recursive rule call target for rule [{0}] from rule [{1}]!", item.Rule, item.Parent.Rule)); } item.SetTarget(target); } return(_currContext.childEntries.node); }
public GraphContext ForParentExpression(ParserNode node) { return(new GraphContext( this.parentContext.rule, this.parentContext.expression, this.parentContext.parentContext, this.parentContext.invocationCount + 1, new ChildParserNodeEntry(this.parentContext.childEntries, node) )); }
void SetParent(ParserNode parent, int index) { if (this.Parent != null) { throw new InvalidOperationException(); } this.Parent = parent; this.IndexInParentList = index; }
private LinearParserState(IndentedWriter w, ParserNode currNode, ParserNode prevNode, ParserStep lastStep, StatefullStackNode stack, bool?lastTerminalFailed, ISourceTextReader reader, ISourceTextReaderHolder holder = null) : base(w, currNode, prevNode, lastTerminalFailed, reader, holder) { this.LastStep = lastStep; this.Stack = stack; if (_log != null) { _log.WriteLine(this.Stack.ToString()); } }
internal SingleChildNode(Rule rule, RuleExpression expression, ParserNode child) : base(rule, expression) { if (child == null) { throw new ArgumentNullException("Argument 'child' cannot be null!"); } this.Child = child; child.SetParent(this, 0); }
public TemporaryReparsingGroup(IParsingTreeNode oldNode, ParserNode node, Location fromLoc, Location toLoc, TemporaryGroup parent, TemporaryChildNode child) : base(node, oldNode.Location, parent, child) { this.OldGroup = oldNode as IParsingTreeGroup; this.ReparsingFrom = fromLoc; this.ReparsingTo = toLoc; if (this.OldGroup == null) { throw new ReparsingFailException(); } }
protected override ParserState <Result> EnterNodeImpl(ParserNode nextNode) { return(this.MayBeReconstruct(nextNode) ?? new ParserTreeState( _log, this.CurrNode, this.TreeNode.CreateChildGroup(nextNode, this.Reader.Location), null, this.Reader ) { InsideOmittedFragment = this.InsideOmittedFragment }); }
public string CollectTree(ParserNode node) { _w = new IndentedWriter(); this.Push(_finishIndent); node.Visit(this); var result = _w.GetContentAsString(); _w = null; return(result); }
internal ParserState(IndentedWriter log, ParserNode curr, ParserNode prev, bool?lastTerminalFailed, ISourceTextReader reader, ISourceTextReaderHolder holder = null) { this.CurrNode = curr; this.PrevNode = prev; this.ResultReconstructed = false; _log = log; this.LastTerminalFailed = lastTerminalFailed; _reader = reader; _readerHolder = holder; }
public void SetTarget(ParserNode target) { if (target == null) { throw new ArgumentNullException("Argument 'target' cannot be null!"); } if (this.Target != null) { throw new InvalidOperationException(); } this.Target = target; }
public ParserContext(Parser <TResult> owner, AnalyzerGraphInfo analyzerInfo, ISourceTextReader source, ParserInitialStateFabric <TResult> initialStateFabric, TResult oldResult, Location limit) { _owner = owner; _grammarRoot = analyzerInfo.AnalyzerGraph; _omitRoot = analyzerInfo.OmitGraph; _source = source; _initialStateFabric = initialStateFabric; _currState = null; _oldResult = oldResult; _limit = limit; _materializeOmittedFragments = owner.MaterializeOmittedFragments; _useDelayedStates = owner.UseDelayedStates; _parserVisitor = this; // new ParserNodeLoggingVisitor(this); }
private ParserNode BuildGraphImpl() { if (!_nav.TryEnter(_rootRuleSetName)) { throw new InvalidOperationException(string.Format("Rule set [{0}] not found!", _rootRuleSetName)); } var omitExpression = _nav.GetRuleSetAttributeArgument("OmitPattern"); this.OmitGraph = omitExpression == null ? null : this.BuildGraphInternal(omitExpression); var rootExpression = _nav.GetRuleSetAttributeArgument("RootRule"); var parserGraph = this.BuildGraphInternal(rootExpression); return(parserGraph); }
protected override ParserState <Result> ExitNodeImpl(bool?terminalFailed, ParserNode nextNode) { // var nextNode = this.TreeNode.Parent.GrammarNode; // fallback if (terminalFailed == true) { var cloc = this.Reader.Location; var oloc = this.TreeNode.Location; if (cloc != oloc) { if (!this.Reader.MoveTo(oloc)) { throw new InvalidOperationException(); } } } // var removeCurr = this.TreeNode.ChildsCount == 0 || terminalFailed.Value; var removeCurr = terminalFailed.Value; if (!this.TreeNode.HasChilds && this.InsideOmittedFragment) { removeCurr = true; } //var treeNode = removeCurr ? this.TreeNode.RemoveCurrent() : ( // this.CurrNode is ParserNode.RecursiveParserNode // ? this.TreeNode.TakeOffCurrent() // : this.TreeNode.ExitChild() //); var treeNode = removeCurr ? this.TreeNode.RemoveCurrent() : this.TreeNode.ExitChild(); return(new ParserTreeState( _log, this.CurrNode, treeNode, terminalFailed, this.Reader ) { InsideOmittedFragment = this.InsideOmittedFragment }); }
public ParserState <TResult> EnterNode(ParserNode nextNode, bool offTree = false) { if (nextNode.Parent != this.CurrNode && !(this.CurrNode is ParserNode.RecursiveParserNode) && !offTree) { throw new InvalidOperationException(); } if (_log != null) { _log.Push().WriteLine("EnterNode @{2} {0} --> {1}", this.CurrNode, nextNode, this.Reader.Location).Push(); if (this.CurrNode is ParserNode.RecursiveParserNode) { _log.WriteLine("Enter to recursion"); } } return(this.EnterNodeImpl(nextNode)); }
//private ParsedNode OldRecreateChilds() //{ // ParsedNode node = null; // for (var n = this.Child; n != null; n = n.Prev) // node = n.Recreate(node); // var loc = node.Location; // var prepends = new List<ParsedNode>(); // foreach (ParsedNode item in this.OldGroup.Childs) // { // if (item.Location < loc && item.Location < this.ReparsingFrom) // prepends.Add(item); // else // break; // } // for (int i = prepends.Count - 1; i >= 0; i--) // node = prepends[i].UpdateNext(node); // return node; //} protected override bool TryReconstructImpl(ParserNode node, Location loc, out TemporaryGroup reconstructedRoot) { if (loc >= this.ReparsingTo) // reuse right { var it = this.OldGroup.Childs.GetEnumerator(); while (it.MoveNext()) { var oldChild = it.Current; if (oldChild.Location == loc && (oldChild as ParsingTreeNode).GrammarNode == node) { // reconstruct all the rest of the tree reconstructedRoot = this.ReconstructTree(); return(true); } } } reconstructedRoot = null; return(false); }
public static TemporaryGroup CreateRootGroup(IParsingTreeNode oldNode, ParserNode omitPatternRoot, Location locFrom, Location locTo, out ParserNode prevGrammarNode, out bool insideOmittedFragment) { insideOmittedFragment = false; var oldGroup = oldNode as IParsingTreeGroup; if (oldGroup == null || oldGroup.Location > locFrom) { throw new InvalidOperationException(); } prevGrammarNode = null; var newGroup = new TemporaryReparsingGroup(oldNode, (oldNode as ParsingTreeNode).GrammarNode, locFrom, locTo, null, null); var newGroupChild = CreateItemToReparse(oldGroup, newGroup, locFrom, locTo); int depth = 0; while (newGroupChild != null) { if (newGroupChild.GrammarNode == omitPatternRoot) { insideOmittedFragment = true; } prevGrammarNode = newGroup.GrammarNode; oldGroup = newGroupChild.OldGroup; newGroup = newGroupChild; newGroupChild = CreateItemToReparse(oldGroup, newGroup, locFrom, locTo); depth++; } // TODO: [Portable.Parser.Impl.ParsingTreeNode.CreateRootGroup] to think about possible parsing directions when reparsing //var lastTakenChild = newGroup.Childs.LastOrDefault(); //if (lastTakenChild != null) // prevGrammarNode = ((ParsingTreeNode)lastTakenChild).GrammarNode; prevGrammarNode = newGroup.Parent.GrammarNode; return(newGroup); }
public EnterNode(ParserStep prev, ParserNode node, Location location) : base(prev, node, location) { }
public ParserStep CreateExitNode(ParserNode node, Location location) { return(new ExitNode(this, node, location)); }
public ParserStep CreateTerminal(ParserNode node, Location from, Location to, string text) { return(new Terminal(this, node, from, to, text)); }
internal ParserStep(ParserStep prev, ParserNode Node, Location location) { this.PrevStep = prev; this.Node = Node; this.Location = location; }
protected abstract ParserState <TResult> EnterNodeImpl(ParserNode nextNode);
//public static ParserTreeState ForStart(ParserNode root, ISourceTextReader reader, bool enableLog) //{ // IndentedWriter w; // if (enableLog) // { // w = new IndentedWriter(" "); // w.Push().WriteLine("Start @{0} {1}", reader.GetPosition(), root).Push(); // } // else // { // w = null; // } // if (root.Parent != null) // throw new ArgumentException(); // return new ParserTreeState( // w, // null, // ParsingTreeNode.CreateRootGroup(root, reader.Location), // null, // reader // ); //} public static ParserState <Result> ForStart(ParserNode grammarRoot, ParserNode omitRoot, ISourceTextReader reader, IParsingTreeNode oldRoot, Location limit, bool enableLog) { IndentedWriter w; if (enableLog) { w = new IndentedWriter(" "); if (oldRoot != null) { w.WriteLine("Incrementally"); } w.Push().WriteLine("Start @{0} {1}", reader.GetPosition(), grammarRoot).Push(); } else { w = null; } if (oldRoot != null) { ParserNode prevGrammarNode = null; bool insideOmittedFragment; var treeNode = ParsingTreeNode.CreateRootGroup(oldRoot, omitRoot, reader.Location, limit, out prevGrammarNode, out insideOmittedFragment); if (!reader.MoveTo(treeNode.Location)) { throw new NotImplementedException(""); } if (w != null) { w.WriteLine("Reconstructing log indentation..."); var t = treeNode; var path = new List <ParsingTreeNode.TemporaryGroup>(); while (t != null) { path.Add(t); t = t.Parent; } path.Reverse(); for (int i = 0; i < path.Count - 1; i++) { w.Push().WriteLine("EnterNode @{2} {0} --> {1}", path[i].GrammarNode, path[i + 1].GrammarNode, path[i].Location).Push(); if (path[i].GrammarNode is ParserNode.RecursiveParserNode) { w.WriteLine("Enter to recursion"); } } w.WriteLine("...Identation reconstructed."); } ParserState <Result> state = new ParserTreeState( w, prevGrammarNode, treeNode, null, reader ); state.InsideOmittedFragment = insideOmittedFragment; if (state.LastTerminalFailed == false) { state = state.ExitNode(); } else { System.Diagnostics.Debug.Print(string.Empty); } return(state); } else { return(new ParserTreeState( w, null, ParsingTreeNode.CreateRootGroup(grammarRoot, reader.Location), null, reader )); } }