Beispiel #1
0
        string GetLexeme(bool stopOnlyAtSeparatorOrQuotes, out bool intoQuotes)
        {
            intoQuotes = false;

            if (lexStringBuilderInUse)
            {
                Log.Fatal("TextBlock: GetLexeme: lexStringBuilderInUse == True.");
            }
            var lex = lexStringBuilder;

            lexStringBuilderInUse = true;
            //StringBuilder lex = new StringBuilder( 32 );
            lex.Length = 0;

            try
            {
                while (true)
                {
                    char c;
                    if (!StreamReadChar(out c))
                    {
                        if (StreamEOF)
                        {
                            return(lex.ToString().Trim());
                        }
                        Error("Unexpected end of file");
                        return("");
                    }

                    //comments
                    if (c == '/')
                    {
                        char cc;
                        if (!StreamReadChar(out cc))
                        {
                            Error("Unexpected end of file");
                            return("");
                        }

                        if (cc == '/')
                        {
                            while (true)
                            {
                                if (!StreamReadChar(out c))
                                {
                                    if (StreamEOF)
                                    {
                                        c = '\n';
                                        break;
                                    }
                                    Error("Unexpected end of file");
                                    return("");
                                }
                                if (c == '\n')
                                {
                                    break;
                                }
                            }
                        }
                        else if (cc == '*')
                        {
                            char oldChar = (char)0;

                            while (true)
                            {
                                if (!StreamReadChar(out c))
                                {
                                    if (StreamEOF)
                                    {
                                        c = ';';
                                        break;
                                    }
                                    Error("Unexpected end of file");
                                    return("");
                                }

                                if (c == '\n')
                                {
                                    linePosition++;
                                }

                                if (oldChar == '*' && c == '/')
                                {
                                    c = ';';
                                    break;
                                }

                                oldChar = c;
                            }
                        }
                        else
                        {
                            StreamSeek(streamPosition - 1);
                        }
                    }

                    if (c == '\n')
                    {
                        linePosition++;
                    }
                    else if (c == '=' || c == '{' || c == '}')
                    {
                        if (lex.Length != 0)
                        {
                            StreamSeek(streamPosition - 1);
                            return(lex.ToString().Trim());
                        }
                        return(c.ToString());
                    }

                    if ((!stopOnlyAtSeparatorOrQuotes && (c <= 32 || c == ';')) ||
                        (stopOnlyAtSeparatorOrQuotes && (c == '\n' || c == '\r' || c == ';')))
                    {
                        if (lex.Length != 0 || stopOnlyAtSeparatorOrQuotes)
                        {
                            return(lex.ToString().Trim());
                        }
                        continue;
                    }

                    if (c == '"')
                    {
                        if (lex.Length != 0)
                        {
                            StreamSeek(streamPosition - 1);
                            return(lex.ToString().Trim());
                        }

                        //quotes
                        while (true)
                        {
                            if (!StreamReadChar(out c))
                            {
                                Error("Unexpected end of file");
                                return("");
                            }
                            if (c == '\n')
                            {
                                linePosition++;
                            }
                            else if (c == '\\')
                            {
                                char c2;
                                if (!StreamReadChar(out c2))
                                {
                                    Error("Unexpected end of file");
                                    return("");
                                }

                                string ss = "\\" + c2;
                                if (c2 == 'x')
                                {
                                    for (int z = 0; z < 4; z++)
                                    {
                                        if (!StreamReadChar(out c2))
                                        {
                                            Error("Unexpected end of file");
                                            return("");
                                        }
                                        ss += c2;
                                    }
                                }
                                StringUtility._DecodeDelimiterFormatString(lex, ss);
                                //lex.Append( StringUtils.DecodeDelimiterFormatString( ss ) );
                                continue;
                            }
                            else if (c == '"')
                            {
                                intoQuotes = true;
                                return(lex.ToString());
                            }
                            lex.Append(c);
                        }
                    }

                    if (lex.Length == 0 && (c == ' ' || c == '\t'))
                    {
                        continue;
                    }

                    lex.Append(c);
                }
            }
            finally
            {
                lexStringBuilderInUse = false;
            }
        }
Beispiel #2
0
        void DumpToString(StringBuilder builder, int tabLevel)
        {
            string tabPrefix = TabLevelToString(tabLevel);

            if (!string.IsNullOrEmpty(Name))
            {
                {
                    builder.Append(tabPrefix);

                    string v;
                    if (IsNeedQuotesForLexeme(Name, false))
                    {
                        v = string.Format("\"{0}\"", StringUtility.EncodeDelimiterFormatString(Name));
                    }
                    else
                    {
                        v = Name;
                    }
                    builder.Append(v);
                }

                if (!string.IsNullOrEmpty(Data))
                {
                    builder.Append(" ");

                    string v;
                    if (IsNeedQuotesForLexeme(Data, false))
                    {
                        v = string.Format("\"{0}\"", StringUtility.EncodeDelimiterFormatString(Data));
                    }
                    else
                    {
                        v = Data;
                    }
                    builder.Append(v);
                }

                builder.Append("\r\n");
                builder.Append(tabPrefix);
                builder.Append("{\r\n");
            }

            foreach (Attribute attribute in attributes)
            {
                string name;
                string value;

                if (IsNeedQuotesForLexeme(attribute.Name, false))
                {
                    name = string.Format("\"{0}\"",
                                         StringUtility.EncodeDelimiterFormatString(attribute.Name));
                }
                else
                {
                    name = attribute.Name;
                }

                if (IsNeedQuotesForLexeme(attribute.Value, true))
                {
                    value = string.Format("\"{0}\"",
                                          StringUtility.EncodeDelimiterFormatString(attribute.Value));
                }
                else
                {
                    value = attribute.Value;
                }

                builder.Append(tabPrefix);
                builder.Append(tabLevel != -1 ? "\t" : "");
                builder.AppendFormat("{0} = {1}\r\n", name, value);
            }

            foreach (TextBlock child in children)
            {
                child.DumpToString(builder, tabLevel + 1);
            }

            if (!string.IsNullOrEmpty(Name))
            {
                builder.Append(tabPrefix);
                builder.Append("}\r\n");
            }
        }