Exemple #1
0
        public ParsingTreeNodeInfo(IParsingTreeNode node, ISourceTextReader textReader, Func <IParsingTreeGroup, ParsingTreeNodeInfo, IEnumerable <ParsingTreeNodeInfo> > childsAccessor, bool fullInfo, ParsingTreeNodeInfo parent = null)
        {
            this.Node   = node;
            this.Parent = parent;

            var terminal = node as IParsingTreeTerminal;

            if (terminal != null)
            {
                this.Text = "'" + textReader.GetText(terminal.From, terminal.To) + "'";

                if (fullInfo)
                {
                    this.Text = (node.Rule == null ? "<NULL>" : node.Rule.Name) + ": " +
                                (node.Expression == null ? "<NULL>" : node.Expression.ToString()) + "; " + this.Text;
                }
            }

            var group = node as IParsingTreeGroup;

            if (group != null)
            {
                this.Childs = childsAccessor(group, this);

                this.Text = node.Rule == null ? "<NULL>" : node.Rule.Name;
                if (fullInfo)
                {
                    this.Text += ": " + (node.Expression == null ? "<NULL>" : node.Expression.ToString()) + ";";
                }
            }
        }
Exemple #2
0
        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
                       ));
        }
Exemple #3
0
 public DataProvider(ICloudSettings cloudSettings, ISourceTextReader textReader, IWordsPreprocessor wordsPreprocessor,
                     IAlgorithm algorithm, ILogger logger)
 {
     this.cloudSettings     = cloudSettings;
     this.textReader        = textReader;
     this.wordsPreprocessor = wordsPreprocessor;
     this.algorithm         = algorithm;
     this.logger            = logger;
 }
Exemple #4
0
        private void SetTrees(IParsingTreeNode root, ISourceTextReader textReader)
        {
            Func <IParsingTreeGroup, ParsingTreeNodeInfo, IEnumerable <ParsingTreeNodeInfo> > childsAccessor = null;

            childsAccessor = (n, p) => n.Childs.Select(nc => new ParsingTreeNodeInfo(nc, textReader, childsAccessor, true, p)).ToArray();
            this.FullTree  = new ParsingTreeNodeInfo(root, textReader, childsAccessor, true);

            Func <IParsingTreeGroup, ParsingTreeNodeInfo, IEnumerable <ParsingTreeNodeInfo> > filteredChildsAccessor = null;

            filteredChildsAccessor = (n, p) => n.GetRuleChilds().Select(nc => new ParsingTreeNodeInfo(nc, textReader, filteredChildsAccessor, false, p)).ToArray();
            this.FilteredTree      = new ParsingTreeNodeInfo(root, textReader, filteredChildsAccessor, false);
        }
Exemple #5
0
        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;
        }
Exemple #6
0
        public static string GetContent(this IParsingTreeNode node, ISourceTextReader reader)
        {
            var from = node.GetFromLocation();

            if (from.HasValue)
            {
                var to = node.GetToLocation();
                return(reader.GetText(from.Value, to.Value));
            }
            else
            {
                return(string.Empty);
            }
        }
Exemple #7
0
        public TResult Parse(ISourceTextReader source)
        {
            var ctx = new ParserContext <TResult>(this, _analyzerInfo, source, _initialStateFabric, null, source.TextEndLocation);

            var sw = new Stopwatch();

            sw.Start();

            var result = ctx.Parse();

            sw.Stop();
            this.ParsingStatistics = sw.Elapsed;

            return(result);
        }
Exemple #8
0
        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);
        }
Exemple #9
0
            protected override bool TryRunImpl(ISourceTextReader reader, out string content)
            {
                bool result;
                var  text = reader.GetText();
                var  pos  = reader.GetPosition();

                var match = _regex.Match(text, pos);

                if (match.Success && match.Index == pos)
                {
                    content = match.Value;
                    result  = reader.Move(match.Length);
                }
                else
                {
                    content = null;
                    result  = false;
                }

                return(result);
            }
Exemple #10
0
            protected override bool TryRunImpl(ISourceTextReader reader, out string text)
            {
                int index = 0;

                for (; index < this.Characters.Length; index++)
                {
                    if (reader.Character == this.Characters[index])
                    {
                        if (reader.Move(1))
                        {
                            continue;
                        }
                    }

                    break;
                }

                bool result;

                if (index == this.Characters.Length)
                {
                    text   = this.Characters;
                    result = true;
                }
                else
                {
                    if (index > 0)
                    {
                        if (!reader.Move(-index))
                        {
                            throw new InvalidOperationException();
                        }
                    }

                    text   = null;
                    result = false;
                }

                return(result);
            }
Exemple #11
0
        public TResult ReParse(ISourceTextReader source, IParsingResult oldResult, Location limit)
        {
            var realOldResult = oldResult as TResult;

            if (realOldResult == null)
            {
                throw new ArgumentException();
            }

            var ctx = new ParserContext <TResult>(this, _analyzerInfo, source, _initialStateFabric, realOldResult, limit);

            var sw = new Stopwatch();

            sw.Start();

            var result = ctx.Parse();

            sw.Stop();
            this.ParsingStatistics = sw.Elapsed;

            return(result);
        }
Exemple #12
0
        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());
            }
        }
Exemple #13
0
 internal ParserState <TResult> CreateInitialState(ParserNode grammarRoot, ParserNode omitRoot, ISourceTextReader source, TResult oldResult, Location limit, bool enableLog)
 {
     return(this.CreateInitialStateImpl(grammarRoot, omitRoot, source, oldResult, limit, enableLog));
 }
Exemple #14
0
        //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
                           ));
            }
        }
Exemple #15
0
 protected ParserTreeState(IndentedWriter log, ParserNode prev, ParsingTreeNode.TemporaryGroup treeNode, bool?lastTerminalFailed, ISourceTextReader reader, ISourceTextReaderHolder holder = null)
     : base(log, treeNode.GrammarNode, prev, lastTerminalFailed, reader, holder)
 {
     this.TreeNode = treeNode;
 }
Exemple #16
0
 protected override bool TryRunImpl(ISourceTextReader reader, out string text)
 {
     text = reader.Character.ToString();
     return(reader.Move(1));
 }
Exemple #17
0
 protected abstract bool TryRunImpl(ISourceTextReader reader, out string text);
Exemple #18
0
 public bool TryRun(ISourceTextReader reader, out string text)
 {
     return(this.TryRunImpl(reader, out text));
 }
Exemple #19
0
 protected abstract ParserState <TResult> CreateInitialStateImpl(ParserNode grammarRoot, ParserNode omitRoot, ISourceTextReader source, TResult oldResult, Location limit, bool enableLog);
Exemple #20
0
        protected override ParserState <LinearParserState.Result> CreateInitialStateImpl(ParserNode grammarRoot, ParserNode omitRoot, ISourceTextReader source, LinearParserState.Result oldResult, Location limit, bool enableLog)
        {
            if (oldResult != null)
            {
                throw new NotImplementedException("");
            }

            return(LinearParserState.ForStart(grammarRoot, source, enableLog));
        }
Exemple #21
0
 protected override ParserState <ParserTreeState.Result> CreateInitialStateImpl(ParserNode grammarRoot, ParserNode omitRoot, ISourceTextReader source, ParserTreeState.Result oldResult, Location limit, bool enableLog)
 {
     return(oldResult == null
         ? ParserTreeState.ForStart(grammarRoot, omitRoot, source, null, limit, enableLog)
         : ParserTreeState.ForStart(grammarRoot, omitRoot, source, oldResult.Tree, limit, enableLog));
 }