/// <summary>
        ///		Parses a text into an Expression string. Must start with ether " or '
        /// </summary>
        /// <param name="text"></param>
        /// <param name="offset"></param>
        /// <param name="context"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static MorestachioExpressionString ParseFrom(string text,
                                                            int offset,
                                                            TokenzierContext context,
                                                            out int index)
        {
            var result = new MorestachioExpressionString(context.CurrentLocation, text[offset]);

            var isEscapeChar = false;
            var sb           = new StringBuilder();

            //skip the string delimiter
            for (index = offset + 1; index < text.Length; index++)
            {
                var c = text[index];
                if (isEscapeChar)
                {
                    sb.Append(c);
                    if (c == result.Delimiter)
                    {
                        isEscapeChar = false;
                    }
                }
                else
                {
                    if (c == '\\')
                    {
                        isEscapeChar = true;
                    }
                    else if (c == result.Delimiter)
                    {
                        if (offset == 0 && index + 1 != text.Length)
                        {
                            context.Errors.Add(new MorestachioSyntaxError(
                                                   context
                                                   .CurrentLocation
                                                   .Offset(index)
                                                   .AddWindow(new CharacterSnippedLocation(0, index, text)),
                                                   "", c.ToString(), "did not expect " + result.Delimiter));
                            break;
                        }

                        break;
                    }
                    else
                    {
                        sb.Append(c);
                    }
                }
            }
            var currentPart = new ExpressionStringConstPart(sb.ToString(), context.CurrentLocation);

            result.StringParts.Add(currentPart);

            context.AdvanceLocation(text.Length);
            return(result);
        }
Пример #2
0
 /// <inheritdoc />
 public void ReadXml(XmlReader reader)
 {
     Location = CharacterLocation.FromFormatString(reader.GetAttribute(nameof(Location)));
     if (reader.IsEmptyElement)
     {
         return;
     }
     reader.ReadStartElement();
     while (reader.Name == nameof(ExpressionStringConstPart) && reader.NodeType != XmlNodeType.EndElement)
     {
         var constStr = new ExpressionStringConstPart();
         constStr.Location = CharacterLocation.FromFormatString(reader.GetAttribute(nameof(Location)));
         var constStrPartText = reader.ReadElementContentAsString();
         Delimiter         = constStrPartText[0];
         constStr.PartText = constStrPartText.Substring(1, constStrPartText.Length - 2);
         StringParts.Add(constStr);
         reader.ReadEndElement();
     }
 }
Пример #3
0
        /// <summary>
        ///		Parses a text into an Expression string. Must start with ether " or '
        /// </summary>
        /// <param name="text"></param>
        /// <param name="offset"></param>
        /// <param name="context"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static MorestachioExpressionString ParseFrom(string text,
                                                            int offset,
                                                            TokenzierContext context,
                                                            out int index)
        {
            var result = new MorestachioExpressionString()
            {
                Location = context.CurrentLocation
            };
            var isEscapeChar = false;
            var currentPart  = new ExpressionStringConstPart()
            {
                Location = context.CurrentLocation,
                PartText = string.Empty
            };

            //get the string delimiter thats ether " or '
            result.Delimiter = text[offset];
            result.StringParts.Add(currentPart);
            //skip the string delimiter
            for (index = offset + 1; index < text.Length; index++)
            {
                var c = text[index];
                if (isEscapeChar)
                {
                    currentPart.PartText += c;
                    if (c == result.Delimiter)
                    {
                        isEscapeChar = false;
                    }
                }
                else
                {
                    if (c == '\\')
                    {
                        isEscapeChar = true;
                    }
                    else if (c == result.Delimiter)
                    {
                        if (offset == 0 && index + 1 != text.Length)
                        {
                            context.Errors.Add(new MorestachioSyntaxError(
                                                   context
                                                   .CurrentLocation
                                                   .Offset(index)
                                                   .AddWindow(new CharacterSnippedLocation(0, index, text)),
                                                   "", c.ToString(), "did not expect " + result.Delimiter));
                            break;
                        }

                        break;
                    }
                    else
                    {
                        currentPart.PartText += c;
                    }
                }
            }
            context.AdvanceLocation(text.Length);
            return(result);
        }