Beispiel #1
0
        private IEnumerable <IParseResult <TResult> > ParseIterator()
        {
            Contract.Requires(Cursor != null);
            Contract.Ensures(Contract.Result <IEnumerable <IParseResult <TResult> > >() != null);

            try
            {
#if !SILVERLIGHT && !PORT_45 && !PORT_40
                using (var traceContext = ParserTraceSources.TraceExecution(Cursor, TraceName))
                {
                    foreach (var result in Parse(traceContext))
                    {
                        yield return(result);
                    }
                }
#else
                foreach (var result in Parse())
                {
                    yield return(result);
                }
#endif
            }
            finally
            {
                Interlocked.Exchange(ref parsing, 0);
            }

            cursor.Reset();
        }
Beispiel #2
0
        public IEnumerable <IParseResult <TResult> > Parse(ICursor <TSource> source)
        {
#if !SILVERLIGHT && !PORT_45 && !PORT_40
            using (ParserTraceSources.TraceQueryCompilation(name))
#endif
            {
                IEnumerable <IParseResult <TResult> > results;

                if (parserFactory == null)
                {
                    results = parse(source);

                    Contract.Assume(results != null);
                }
                else
                {
                    var parser = parserFactory.Value;

                    Contract.Assume(parser != null);

                    results = parser.Parse(source);
                }

                return(results);
            }
        }
        protected override IEnumerable <IParseResult <TResult> > Parse()
#endif
        {
            var cursor = Cursor;

            IParser <TSource, TResult> compiledGrammar;

#if !SILVERLIGHT && !PORT_45 && !PORT_40
            using (ParserTraceSources.TraceGrammarCompilation())
            {
                compiledGrammar = grammar(cursor);
            }
#else
            compiledGrammar = grammar(cursor);
#endif

            do
            {
#if !SILVERLIGHT && !PORT_45 && !PORT_40
                traceContext.TraceBeginIteration();
#endif

                bool hasResult = false;

                foreach (var result in compiledGrammar.Parse(cursor))
                {
                    var lookAhead = result as ILookAheadParseResult <TResult>;

                    if (lookAhead != null)
                    {
                        lookAhead.OnCompleted(success: true);
                        continue;
                    }

                    hasResult = true;

#if !SILVERLIGHT && !PORT_45 && !PORT_40
                    traceContext.TraceResult(result);
#endif

                    yield return(result);

                    if (!cursor.AtEndOfSequence)
                    {
                        cursor.Move(result.Length);
                    }
                }

                if (!hasResult)
                {
                    cursor.MoveToEnd();
                }
            }while (!cursor.AtEndOfSequence);
        }
        public IEnumerable <IParseResult <T> > Parse(ICursor <T> source)
        {
            foreach (var value in source)
            {
#if !SILVERLIGHT && !PORT_45 && !PORT_40
                ParserTraceSources.TraceInput(value);
#endif

                yield return(ParseResult.Create(value, length: 1));

                yield break;
            }
        }
        /// <summary>
        /// Constructs a new instance of the <see cref="ParserTraceExecutionContext" /> class.
        /// </summary>
        /// <param name="name">Name of the parser to be traced.</param>
        /// <param name="cursor">The parser's cursor.</param>
        public ParserTraceExecutionContext(string name, IParserCursorState cursor)
        {
            Contract.Requires(name == null || name.Length > 0);
            Contract.Requires(cursor != null);

            if (ParserTraceSources.Execution.Switch.ShouldTrace(TraceEventType.Verbose) ||
                ParserTraceSources.Execution.Switch.ShouldTrace(TraceEventType.Start))
            {
                this.id     = Interlocked.Increment(ref lastTracedExecutionId);
                this.name   = ParserTraceSources.FormatName(name);
                this.cursor = cursor;

                stopwatch = new Stopwatch();
            }
        }