Beispiel #1
0
            /// <summary>
            /// Analyze the list of expressions and separate them into a list of chunks. (HasValue and !HasValue together)
            /// </summary>
            /// <returns>List of chunks - expressions chunked by that, if they are evaluable during compilation. Cannot return null.</returns>
            private static List <ConcatChunk> AnalyzeChunks(Analyzer /*!*/ analyzer, IList <Expression> /*!*/ expressions)
            {
                Debug.Assert(expressions.Any());

                List <ConcatChunk> chunks = new List <ConcatChunk>();

                ConcatChunk lastChunk = null;

                // flattern concatenation expressions:
                expressions = ChainConcatenations(expressions);

                // analyze chunks
                foreach (var expr in expressions)
                {
                    var evaluation = expr.Analyze(analyzer, ExInfoFromParent.DefaultExInfo);

                    // skip empty evaluated expressions
                    if (evaluation.HasValue &&
                        (
                            evaluation.Value == null ||
                            (evaluation.Value is string && ((string)evaluation.Value) == string.Empty) ||
                            Convert.ObjectToPhpBytes(evaluation.Value).Length == 0
                        ))
                    {
                        continue;  // empty literal => skip
                    }

                    // add chunk
                    if (lastChunk == null || lastChunk.HasValue != evaluation.HasValue)
                    {
                        chunks.Add(lastChunk = new ConcatChunk(expr.Span, evaluation));
                    }
                    else if (evaluation.HasValue)
                    {
                        lastChunk.Value    = Operators.Concat(lastChunk.Value, evaluation.Value);
                        lastChunk.Position = Text.Span.Combine(lastChunk.Position, expr.Span);
                    }
                    else//if (!evaluation.HasValue)
                    {
                        lastChunk.Expressions.Add(evaluation.Expression);
                        lastChunk.Position = Text.Span.Combine(lastChunk.Position, expr.Span);
                    }
                }

                // there must be at least one expression
                if (chunks.Count == 0)
                {
                    var position = Text.Span.Invalid;
                    if (expressions.Count > 0)
                    {
                        position = expressions[0].Span;
                    }

                    chunks.Add(new ConcatChunk(position, string.Empty));
                }

                //
                return(chunks);
            }
Beispiel #2
0
        /// <summary>
        /// Analyze the list of expressions and separate them into a list of chunks. (HasValue and !HasValue together)
        /// </summary>
        /// <returns>List of chunks - expressions chunked by that, if they are evaluable during compilation. Cannot return null.</returns>
        private static List<ConcatChunk> AnalyzeChunks(Analyzer/*!*/ analyzer, List<Expression>/*!*/expressions)
        {
            Debug.Assert(expressions != null);
            Debug.Assert(expressions.Count > 0);

            List<ConcatChunk> chunks = new List<ConcatChunk>();

            ConcatChunk lastChunk = null;

            foreach (var expr in expressions)
            {
                var evaluation = expr.Analyze(analyzer, ExInfoFromParent.DefaultExInfo);

                // skip empty evaluated expressions
                if (evaluation.HasValue &&
                    (
                        evaluation.Value == null ||
                        (evaluation.Value is string && ((string)evaluation.Value) == string.Empty) ||
                        Convert.ObjectToPhpBytes(evaluation.Value).Length == 0
                    ))
                {
                    continue;  // empty literal => skip
                }

                // add chunk
                if (lastChunk == null || lastChunk.HasValue != evaluation.HasValue)
                {
                    chunks.Add(lastChunk = new ConcatChunk(evaluation));
                }
                else if (evaluation.HasValue)
                {
                    lastChunk.Value = Operators.Concat(lastChunk.Value, evaluation.Value);
                }
                else//if (!evaluation.HasValue)
                {
                    lastChunk.Expressions.Add(evaluation.Expression);
                }
            }

            // there must be at least one expression
            if (chunks.Count == 0)
                chunks.Add(new ConcatChunk(string.Empty));

            //
            return chunks;
        }