Example #1
0
#pragma warning disable 0219
        /// <summary>
        /// Matches any input.
        /// </summary>
        /// <param name="memo">Memo.</param>
        /// <param name="index">Index.</param>
        protected MatchItem <TInput, TResult> _ParseAny(Memo <TInput, TResult> memo, ref int index)
        {
            if (!(memo.InputString != null && index >= memo.InputString.Length))
            {
                try
                {
                    var _temp  = memo.InputList != null ? memo.InputList[index] : memo.InputEnumerable.ElementAt(index);
                    var result = new MatchItem <TInput, TResult>
                    {
                        StartIndex      = index,
                        NextIndex       = index + 1,
                        InputEnumerable = memo.InputEnumerable,
                    };

                    ++index;
                    memo.Results.Push(result);
                    return(result);
                }
                catch { }
            }

            memo.Results.Push(null);
            memo.AddError(index, () => "unexpected end of file");
            return(null);
        }
Example #2
0
        /// <summary>
        /// Try to match the input.
        /// </summary>
        /// <param name="input">The input to be matched.</param>
        /// <param name="production">The top-level grammar production (rule) of the generated parser class to use.</param>
        /// <returns>The result of the match.</returns>
        public virtual MatchResult <TInput, TResult> GetMatch(IEnumerable <TInput> input, Action <Memo <TInput, TResult>, int, IEnumerable <MatchItem <TInput, TResult> > > production)
        {
            var memo = new Memo <TInput, TResult>(input);
            MatchItem <TInput, TResult> result = null;

            if (BeforeMatch != null)
            {
                BeforeMatch(memo, input, production);
            }

            try
            {
                if (Terminals == null)
                {
                    Terminals = new HashSet <string>();
                }

                result = _MemoCall(memo, production.Method.Name, 0, production, null);
            }
            catch (MatcherException me)
            {
                memo.ClearErrors();
                memo.AddError(me.Index, me.Message);
            }
            catch (Exception e)
            {
                memo.ClearErrors();
                memo.AddError(0, e.Message
#if DEBUG
                              + "\n" + e.StackTrace
#endif
                              );
            }

            memo.ClearMemoTable(); // allow memo tables to be gc'd

            var match_result = result != null
                ? new MatchResult <TInput, TResult>(this, memo, true, result.StartIndex, result.NextIndex, result.Results, memo.LastError, memo.LastErrorIndex)
                : new MatchResult <TInput, TResult>(this, memo, false, -1, -1, null, memo.LastError, memo.LastErrorIndex);

            if (AfterMatch != null)
            {
                AfterMatch(match_result);
            }

            return(match_result);
        }
Example #3
0
#pragma warning restore 0219

        /// <summary>
        /// Matches any input in an argument stream.
        /// </summary>
        /// <param name="memo">Memo.</param>
        /// <param name="item_index">Item index.</param>
        /// <param name="input_index">Input index.</param>
        /// <param name="args">Argument stream.</param>
        protected MatchItem <TInput, TResult> _ParseAnyArgs(Memo <TInput, TResult> memo, ref int item_index, ref int input_index, IEnumerable <MatchItem <TInput, TResult> > args)
        {
            if (args != null)
            {
                try
                {
                    if (input_index == 0)
                    {
                        var _temp = args.ElementAt(item_index);
                        ++item_index;
                        memo.ArgResults.Push(_temp);
                        return(_temp);
                    }
                }
                catch { }
            }

            memo.ArgResults.Push(null);
            memo.AddError(input_index, () => "not enough arguments");
            return(null);
        }
Example #4
0
        /// <summary>
        /// Call a grammar production, using memoization and handling left-recursion.
        /// </summary>
        /// <param name="memo">The memo for the current match.</param>
        /// <param name="ruleName">The name of the production.</param>
        /// <param name="index">The current index in the input stream.</param>
        /// <param name="production">The production itself.</param>
        /// <param name="args">Arguments to the production (can be null).</param>
        /// <returns>The result of the production at the given input index.</returns>
        protected MatchItem <TInput, TResult> _MemoCall
        (
            Memo <TInput, TResult> memo,
            string ruleName,
            int index,
            Action <Memo <TInput, TResult>, int, IEnumerable <MatchItem <TInput, TResult> > > production,
            IEnumerable <MatchItem <TInput, TResult> > args
        )
        {
            MatchItem <TInput, TResult> result;

            var expansion = new Expansion
            {
                Name = args == null ? ruleName : ruleName + string.Join(", ", args.Select(arg => arg.ToString()).ToArray()),
                Num  = 0
            };

            // if we have a memo record, use that
            if (memo.TryGetMemo(expansion, index, out result))
            {
                memo.Results.Push(result);
                return(result);
            }

            // if we are not handling left recursion, just call the production directly.
            if (!HandleLeftRecursion || Terminals.Contains(ruleName))
            {
                production(memo, index, args);
                result = memo.Results.Peek();

                memo.Memoize(expansion, index, result);

                if (result == null)
                {
                    memo.AddError(index, () => "expected " + ruleName);
                }

                return(result);
            }

            // check for left-recursion
            LRRecord <MatchItem <TInput, TResult> > record;

            if (memo.TryGetLRRecord(expansion, index, out record))
            {
                record.LRDetected = true;

                var involved = memo.CallStack
                               .TakeWhile(rec => rec.CurrentExpansion.Name != expansion.Name)
                               .Select(rec => rec.CurrentExpansion.Name);

                if (record.InvolvedRules != null)
                {
                    record.InvolvedRules.UnionWith(involved);
                }
                else
                {
                    record.InvolvedRules = new HashSet <string>(involved);
                }

                if (!memo.TryGetMemo(record.CurrentExpansion, index, out result))
                {
                    throw new MatcherException(index, "Problem with expansion " + record.CurrentExpansion);
                }
                memo.Results.Push(result);
            }
            // no lr information
            else
            {
                record                  = new LRRecord <MatchItem <TInput, TResult> >();
                record.LRDetected       = false;
                record.NumExpansions    = 1;
                record.CurrentExpansion = new Expansion {
                    Name = expansion.Name, Num = record.NumExpansions
                };
                record.CurrentNextIndex = -1;

                memo.Memoize(record.CurrentExpansion, index, null);
                memo.StartLRRecord(expansion, index, record);

                memo.CallStack.Push(record);

                while (true)
                {
                    production(memo, index, args);
                    result = memo.Results.Pop();

                    // do we need to keep trying the expansions?
                    if (record.LRDetected && result != null && result.NextIndex > record.CurrentNextIndex)
                    {
                        record.NumExpansions    = record.NumExpansions + 1;
                        record.CurrentExpansion = new Expansion {
                            Name = expansion.Name, Num = record.NumExpansions
                        };
                        record.CurrentNextIndex = result != null ? result.NextIndex : 0;
                        memo.Memoize(record.CurrentExpansion, index, result);

                        record.CurrentResult = result;
                    }
                    // we are done trying to expand
                    else
                    {
                        if (record.LRDetected)
                        {
                            result = record.CurrentResult;
                        }

                        memo.ForgetLRRecord(expansion, index);
                        memo.Results.Push(result);

                        // if we are not involved in any left-recursion expansions above us, memoize
                        memo.CallStack.Pop();

                        bool found_lr = memo.CallStack.Any(rec => rec.InvolvedRules != null && rec.InvolvedRules.Contains(expansion.Name));
                        if (!found_lr)
                        {
                            memo.Memoize(expansion, index, result);
                        }

                        if (result == null)
                        {
                            memo.AddError(index, () => "expected " + expansion.Name);
                        }
                        break;
                    }
                }
            }

            return(result);
        }