Esempio n. 1
0
        private void TryExtractValue(List <CodeLexem> res, ref SourcePart text)
        {
            if (text[0] == '{')
            {
                res.Add(new CodeLexem(LexemType.Symbol, CutString(ref text, 1)));
            }
            else if (text[0] == '[')
            {
                res.Add(new CodeLexem(LexemType.Symbol, CutString(ref text, 1)));
            }
            else if (text[0] == '"')
            {
                res.Add(new CodeLexem(LexemType.Quotes, CutString(ref text, 1)));
                // var end = text.IndexOf('"');
                var end = FindEndQuoteForValue(text);

                res.Add(new CodeLexem(LexemType.Value, CutString(ref text, end)));
                res.Add(new CodeLexem(LexemType.Quotes, CutString(ref text, 1)));
            }
            else
            {
                var end = text.IndexOfAny(new[] { ',', '}' });
                res.Add(new CodeLexem(LexemType.Value, CutString(ref text, end)));
                res.Add(new CodeLexem(LexemType.Symbol, CutString(ref text, 1)));
            }
        }
Esempio n. 2
0
        protected override List<CodeLexem> Parse(SourcePart text)
        {
            var list = new List<CodeLexem>();

            while (text.Length > 0)
            {
                var lenght = text.Length;

                TryExtract(list, ref text, ByteOrderMark);
                TryExtract(list, ref text, "[", LexemType.Symbol);
                TryExtract(list, ref text, "{", LexemType.Symbol);

                if (TryExtract(list, ref text, "\"", LexemType.Quotes))
                {
                    ParseJsonPropertyName(list, ref text); // Extract Name
                    TryExtract(list, ref text, "\"", LexemType.Quotes);
                    TryExtract(list, ref text, ":", LexemType.Symbol);
                    TrySpace(list, ref text);
                    TryExtractValue(list, ref text); // Extract Value
                }

                ParseSymbol(list, ref text); // Parse extras
                TrySpace(list, ref text);
                TryExtract(list, ref text, "\r\n", LexemType.LineBreak);
                TryExtract(list, ref text, "\n", LexemType.LineBreak);
                TryExtract(list, ref text, "}", LexemType.Symbol);
                TryExtract(list, ref text, "]", LexemType.Symbol);

                if (lenght == text.Length)
                    break;
            }

            return list;
        }
Esempio n. 3
0
        private int FindEndQuoteForValue(SourcePart text)
        {
            if (text == null || text.Length < 2)
            {
                return(-1);
            }

            var start      = 1;
            var end        = -1;
            var length     = text.Length;
            var indexMatch = false;

            while (!indexMatch && start > 0 && start < length)
            {
                end = text.Substring(start, length - start).IndexOf("\"") + start;
                if (end == -1)
                {
                    return(-1);
                }
                indexMatch = text.Substring(end - 1, 1).IndexOf("\\") == -1;
                start      = end + 1;
            }

            return(indexMatch ? end : -1);
        }
Esempio n. 4
0
 protected virtual List<CodeLexem> Parse(SourcePart text)
 {
     var list = new List<CodeLexem>();
     var sourceString = text;
     LineBreaks(list, ref sourceString, sourceString.Length, LexemType.PlainText);
     return list;
 }
Esempio n. 5
0
        private void ParseJsonPropertyName(ICollection<CodeLexem> res, ref SourcePart text)
        {
            var index = text.IndexOf("\":");
            if (index <= 0)
                return;

            res.Add(new CodeLexem(LexemType.Property, CutString(ref text, index)));
        }
Esempio n. 6
0
        protected virtual List <CodeLexem> Parse(SourcePart text)
        {
            var list         = new List <CodeLexem>();
            var sourceString = text;

            LineBreaks(list, ref sourceString, sourceString.Length, LexemType.PlainText);
            return(list);
        }
Esempio n. 7
0
        protected bool TryExtract(List<CodeLexem> res, ref SourcePart text, string lex)
        {
            if (text.StartsWith(lex))
            {
                CutString(ref text, lex.Length);
                return true;
            }

            return false;
        }
Esempio n. 8
0
        protected string CutString(ref SourcePart text, int count)
        {
            if (count == 0)
                return string.Empty;

            previousSimbol = text[count - 1];
            var result = text.Substring(0, count);
            text = text.Substring(count);
            return result;
        }
Esempio n. 9
0
        protected bool TryExtract(List <CodeLexem> res, ref SourcePart text, string lex)
        {
            if (text.StartsWith(lex))
            {
                CutString(ref text, lex.Length);
                return(true);
            }

            return(false);
        }
Esempio n. 10
0
        protected bool TryExtract(List <CodeLexem> res, ref SourcePart text, string lex, LexemType type)
        {
            if (text.StartsWith(lex))
            {
                res.Add(new CodeLexem(type, CutString(ref text, lex.Length)));
                return(true);
            }

            return(false);
        }
Esempio n. 11
0
        private void ParseJsonPropertyName(ICollection <CodeLexem> res, ref SourcePart text)
        {
            var index = text.IndexOf("\":");

            if (index <= 0)
            {
                return;
            }

            res.Add(new CodeLexem(LexemType.Property, CutString(ref text, index)));
        }
Esempio n. 12
0
        protected void ParseValue(List <CodeLexem> res, ref SourcePart text)
        {
            const string lex   = "\"";
            var          index = text.IndexOf(lex);

            if (index < 0)
            {
                return;
            }

            LineBreaks(res, ref text, index + lex.Length - 1, LexemType.Value);
        }
Esempio n. 13
0
        private void ParseSymbol(ICollection <CodeLexem> res, ref SourcePart text)
        {
            int index = text.IndexOfAny(XmlSymbol);

            if (index != 0)
            {
                return;
            }

            res.Add(new CodeLexem(LexemType.Symbol, text.Substring(0, 1)));
            text = text.Substring(1);
        }
Esempio n. 14
0
        protected string CutString(ref SourcePart text, int count)
        {
            if (count == 0)
            {
                return(string.Empty);
            }

            previousSimbol = text[count - 1];
            var result = text.Substring(0, count);

            text = text.Substring(count);
            return(result);
        }
Esempio n. 15
0
        protected void TrySpace(List <CodeLexem> res, ref SourcePart text)
        {
            var stringBuilder = new StringBuilder();

            while (SpaceChars.Contains(text[0]))
            {
                stringBuilder.Append(CutString(ref text, 1));
            }

            if (stringBuilder.Length > 0)
            {
                res.Add(new CodeLexem(LexemType.Space, stringBuilder.ToString()));
            }
        }
Esempio n. 16
0
        protected override List <CodeLexem> Parse(SourcePart text)
        {
            var list = new List <CodeLexem>();

            while (text.Length > 0)
            {
                if (TryExtract(list, ref text, "<!--", LexemType.Comment))
                {
                    TryExtractTo(list, ref text, "-->", LexemType.Comment);
                }

                if (text.StartsWith("<", StringComparison.Ordinal))
                {
                    IsInsideBlock = false;
                }

                if (TryExtract(list, ref text, "\"{}", LexemType.Value))
                {
                    TryExtractTo(list, ref text, "\"", LexemType.Value);
                }

                if (TryExtract(list, ref text, "</", LexemType.Symbol) ||
                    TryExtract(list, ref text, "<", LexemType.Symbol) ||
                    TryExtract(list, ref text, "{", LexemType.Symbol))
                {
                    ParseXmlKeyWord(list, ref text, LexemType.Object);
                }

                if (TryExtract(list, ref text, "\"", LexemType.Quotes))
                {
                    ParseValue(list, ref text);
                }

                ParseXmlKeyWord(list, ref text, IsInsideBlock ? LexemType.PlainText : LexemType.Property);
                TryExtract(list, ref text, "\"", LexemType.Quotes);
                TryExtract(list, ref text, "}", LexemType.Symbol);

                if (text.StartsWith(">", StringComparison.Ordinal))
                {
                    IsInsideBlock = true;
                }

                ParseSymbol(list, ref text);
                TrySpace(list, ref text);
                TryExtract(list, ref text, "\n", LexemType.LineBreak);
            }
            return(list);
        }
Esempio n. 17
0
        protected void TryExtractTo(List <CodeLexem> res, ref SourcePart text, string lex, LexemType type, string except = null)
        {
            var index = text.IndexOf(lex);

            if (except != null)
            {
                while (index >= 0 && text.Substring(0, index + 1).EndsWith(except))
                {
                    index = text.IndexOf(lex, index + 1);
                }
            }

            if (index < 0)
            {
                return;
            }

            LineBreaks(res, ref text, index + lex.Length, type);
        }
Esempio n. 18
0
        protected void LineBreaks(List<CodeLexem> res, ref SourcePart text, int to, LexemType type)
        {
            while (text.Length > 0 && to > 0)
            {
                var index = text.IndexOf("\n");
                if (index >= to)
                {
                    res.Add(new CodeLexem(type, CutString(ref text, to)));
                    return;
                }

                if (index != 0)
                {
                    res.Add(new CodeLexem(type, CutString(ref text, index)));
                }

                res.Add(new CodeLexem(LexemType.LineBreak, CutString(ref text, 1)));
                to -= index + 1;
            }
        }
Esempio n. 19
0
        private void ParseXmlKeyWord(ICollection <CodeLexem> res, ref SourcePart text, LexemType type)
        {
            var index = text.IndexOfAny(XmlEndOfTerm);

            if (index <= 0)
            {
                return;
            }

            var delimiterIndex = text.IndexOf(XmlNamespaceDelimeter);

            if (delimiterIndex > 0 && delimiterIndex < index)
            {
                res.Add(new CodeLexem(type, CutString(ref text, delimiterIndex)));
                res.Add(new CodeLexem(LexemType.Symbol, CutString(ref text, 1)));
                res.Add(new CodeLexem(type, CutString(ref text, index - delimiterIndex - 1)));
                return;
            }
            res.Add(new CodeLexem(type, CutString(ref text, index)));
        }
Esempio n. 20
0
        protected void LineBreaks(List <CodeLexem> res, ref SourcePart text, int to, LexemType type)
        {
            while (text.Length > 0 && to > 0)
            {
                var index = text.IndexOf("\n");
                if (index >= to)
                {
                    res.Add(new CodeLexem(type, CutString(ref text, to)));
                    return;
                }

                if (index != 0)
                {
                    res.Add(new CodeLexem(type, CutString(ref text, index)));
                }

                res.Add(new CodeLexem(LexemType.LineBreak, CutString(ref text, 1)));
                to -= index + 1;
            }
        }
Esempio n. 21
0
        private int FindEndQuoteForValue(SourcePart text)
        {
            if (text == null || text.Length < 2)
            return -1;

              var start = 1;
              var end = -1;
              var length = text.Length;
              var indexMatch = false;

              while (!indexMatch && start > 0 && start < length)
              {
            end = text.Substring(start, length - start).IndexOf("\"") + start;
            if (end == -1)
              return -1;
            indexMatch = text.Substring(end - 1, 1).IndexOf("\\") == -1;
            start = end + 1;
              }

              return indexMatch ? end : -1;
        }
Esempio n. 22
0
        protected override List<CodeLexem> Parse(SourcePart text)
        {
            var list = new List<CodeLexem>();
            while (text.Length > 0)
            {
                if (TryExtract(list, ref text, "<!--", LexemType.Comment))
                    TryExtractTo(list, ref text, "-->", LexemType.Comment);

                if (text.StartsWith("<", StringComparison.Ordinal))
                    IsInsideBlock = false;

                if (TryExtract(list, ref text, "\"{}", LexemType.Value))
                    TryExtractTo(list, ref text, "\"", LexemType.Value);

                if (TryExtract(list, ref text, "</", LexemType.Symbol) ||
                    TryExtract(list, ref text, "<", LexemType.Symbol) ||
                    TryExtract(list, ref text, "{", LexemType.Symbol))
                {
                    ParseXmlKeyWord(list, ref text, LexemType.Object);
                }

                if (TryExtract(list, ref text, "\"", LexemType.Quotes))
                {
                    ParseValue(list, ref text);
                }

                ParseXmlKeyWord(list, ref text, IsInsideBlock ? LexemType.PlainText : LexemType.Property);
                TryExtract(list, ref text, "\"", LexemType.Quotes);
                TryExtract(list, ref text, "}", LexemType.Symbol);

                if (text.StartsWith(">", StringComparison.Ordinal))
                    IsInsideBlock = true;

                ParseSymbol(list, ref text);
                TrySpace(list, ref text);
                TryExtract(list, ref text, "\n", LexemType.LineBreak);
            }
            return list;
        }
Esempio n. 23
0
        protected override List <CodeLexem> Parse(SourcePart text)
        {
            var list = new List <CodeLexem>();

            while (text.Length > 0)
            {
                var lenght = text.Length;

                TryExtract(list, ref text, ByteOrderMark);
                TryExtract(list, ref text, "[", LexemType.Symbol);
                TryExtract(list, ref text, "{", LexemType.Symbol);

                if (TryExtract(list, ref text, "\"", LexemType.Quotes))
                {
                    ParseJsonPropertyName(list, ref text); // Extract Name
                    TryExtract(list, ref text, "\"", LexemType.Quotes);
                    TryExtract(list, ref text, ":", LexemType.Symbol);
                    TrySpace(list, ref text);
                    TryExtractValue(list, ref text); // Extract Value
                }

                ParseSymbol(list, ref text); // Parse extras
                TrySpace(list, ref text);
                TryExtract(list, ref text, "\r\n", LexemType.LineBreak);
                TryExtract(list, ref text, "\n", LexemType.LineBreak);
                TryExtract(list, ref text, "}", LexemType.Symbol);
                TryExtract(list, ref text, "]", LexemType.Symbol);

                if (lenght == text.Length)
                {
                    break;
                }
            }

            return(list);
        }
Esempio n. 24
0
        protected void TryExtractTo(List<CodeLexem> res, ref SourcePart text, string lex, LexemType type, string except = null)
        {
            var index = text.IndexOf(lex);
            if (except != null)
            {
                while (index >= 0 && text.Substring(0, index + 1).EndsWith(except))
                {
                    index = text.IndexOf(lex, index + 1);
                }
            }

            if (index < 0)
                return;

            LineBreaks(res, ref text, index + lex.Length, type);
        }
Esempio n. 25
0
        private void ParseXmlKeyWord(ICollection<CodeLexem> res, ref SourcePart text, LexemType type)
        {
            var index = text.IndexOfAny(XmlEndOfTerm);
            if (index <= 0)
                return;

            var delimiterIndex = text.IndexOf(XmlNamespaceDelimeter);
            if (delimiterIndex > 0 && delimiterIndex < index)
            {
                res.Add(new CodeLexem(type, CutString(ref text, delimiterIndex)));
                res.Add(new CodeLexem(LexemType.Symbol, CutString(ref text, 1)));
                res.Add(new CodeLexem(type, CutString(ref text, index - delimiterIndex - 1)));
                return;
            }
            res.Add(new CodeLexem(type, CutString(ref text, index)));
        }
Esempio n. 26
0
        private void TryExtractValue(List<CodeLexem> res, ref SourcePart text)
        {
            if (text[0] == '{')
            {
                res.Add(new CodeLexem(LexemType.Symbol, CutString(ref text, 1)));
            }
            else if (text[0] == '[')
            {
                res.Add(new CodeLexem(LexemType.Symbol, CutString(ref text, 1)));
            }
            else if (text[0] == '"')
            {
                res.Add(new CodeLexem(LexemType.Quotes, CutString(ref text, 1)));
               // var end = text.IndexOf('"');
                var end = FindEndQuoteForValue(text);

                res.Add(new CodeLexem(LexemType.Value, CutString(ref text, end)));
                res.Add(new CodeLexem(LexemType.Quotes, CutString(ref text, 1)));
            }
            else
            {
                var end = text.IndexOfAny(new[] { ',', '}' });
                res.Add(new CodeLexem(LexemType.Value, CutString(ref text, end)));
                res.Add(new CodeLexem(LexemType.Symbol, CutString(ref text, 1)));
            }
        }
Esempio n. 27
0
        private void ParseSymbol(ICollection<CodeLexem> res, ref SourcePart text)
        {
            var index = text.IndexOfAny(JsonSymbol);
            if (index != 0)
                return;

            res.Add(new CodeLexem(LexemType.Symbol, text.Substring(0, 1)));
            text = text.Substring(1);
        }
Esempio n. 28
0
        protected void ParseValue(List<CodeLexem> res, ref SourcePart text)
        {
            const string lex = "\"";
            var index = text.IndexOf(lex);

            if (index < 0)
                return;

            LineBreaks(res, ref text, index + lex.Length - 1, LexemType.Value);
        }
Esempio n. 29
0
        protected void TrySpace(List<CodeLexem> res, ref SourcePart text)
        {
            var stringBuilder = new StringBuilder();
            while (SpaceChars.Contains(text[0]))
            {
                stringBuilder.Append(CutString(ref text, 1));
            }

            if (stringBuilder.Length > 0)
            {
                res.Add(new CodeLexem(LexemType.Space, stringBuilder.ToString()));
            }
        }
Esempio n. 30
0
        protected bool TryExtract(List<CodeLexem> res, ref SourcePart text, string lex, LexemType type)
        {
            if (text.StartsWith(lex))
            {
                res.Add(new CodeLexem(type, CutString(ref text, lex.Length)));
                return true;
            }

            return false;
        }