private void GetStructDecl(
            TableDescriptor.StructDef structDef,
            string indent, out string output)
        {
            StringBuilder sb = new StringBuilder();

            string start = string.Format(
                "{0}public sealed class {1} : Brickred.Table.BaseStruct{2}" +
                "{0}{{{2}",
                indent, structDef.Name, this.newLineStr);
            string end = string.Format(
                "{0}}}{1}",
                indent, this.newLineStr);
            string fieldDecl;
            string parseFuncDecl;

            indent += "    ";
            GetStructDeclFieldDecl(
                structDef, indent, out fieldDecl);
            GetStructDeclParseFunc(
                structDef, indent, out parseFuncDecl);

            sb.Append(start);
            if (fieldDecl != "")
            {
                sb.Append(fieldDecl);
                sb.Append(this.newLineStr);
            }
            sb.Append(parseFuncDecl);
            sb.Append(end);

            output = sb.ToString();
        }
        private string GenerateGlobalStructFile(
            TableDescriptor.StructDef structDef)
        {
            string dontEditComment;
            string namespaceDeclStart;
            string namespaceDeclEnd;
            string structDecl;
            string indent = "";

            GetDontEditComment(
                out dontEditComment);
            GetNamespaceDecl(
                out namespaceDeclStart, out namespaceDeclEnd);

            if (namespaceDeclStart != "")
            {
                indent += "    ";
            }

            GetStructDecl(structDef, indent,
                          out structDecl);

            StringBuilder sb = new StringBuilder();

            sb.Append(dontEditComment);
            sb.Append(this.newLineStr);
            sb.Append(namespaceDeclStart);
            sb.Append(structDecl);
            sb.Append(namespaceDeclEnd);

            return(sb.ToString());
        }
        public override bool Generate(
            TableDescriptor descriptor, string reader,
            string outputDir, NewLineType newLineType)
        {
            this.descriptor = descriptor;
            this.reader     = reader;

            if (newLineType == NewLineType.Dos)
            {
                this.newLineStr = "\r\n";
            }
            else
            {
                this.newLineStr = "\n";
            }

            for (int i = 0; i < this.descriptor.GlobalStructs.Count; ++i)
            {
                TableDescriptor.StructDef def =
                    this.descriptor.GlobalStructs[i];

                string filePath = Path.Combine(
                    outputDir, def.Name + ".cs");
                string fileContent = GenerateGlobalStructFile(def);
                try {
                    File.WriteAllText(filePath, fileContent);
                } catch (Exception e) {
                    Console.Error.WriteLine(string.Format(
                                                "error: write file {0} failed: {1}",
                                                filePath, e.Message));
                    return(false);
                }
            }

            for (int i = 0; i < this.descriptor.Tables.Count; ++i)
            {
                TableDescriptor.TableDef def = this.descriptor.Tables[i];

                string filePath = Path.Combine(
                    outputDir, def.Name + ".cs");
                string fileContent = GenerateTableFile(def);
                try {
                    File.WriteAllText(filePath, fileContent);
                } catch (Exception e) {
                    Console.Error.WriteLine(string.Format(
                                                "error: write file {0} failed: {1}",
                                                filePath, e.Message));
                    return(false);
                }
            }

            return(true);
        }
        private void GetStructDeclFieldDecl(
            TableDescriptor.StructDef structDef,
            string indent, out string output)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < structDef.Fields.Count; ++i)
            {
                TableDescriptor.StructDef.FieldDef fieldDef =
                    structDef.Fields[i];

                string csharpType   = GetCSharpType(fieldDef);
                string defaultValue = GetCSharpTypeDefaultValue(fieldDef);
                sb.AppendFormat("{0}public {1} {2} = {3};{4}",
                                indent, csharpType, fieldDef.Name, defaultValue,
                                this.newLineStr);
            }

            output = sb.ToString();
        }
        private void GetStructDeclParseFunc(
            TableDescriptor.StructDef structDef,
            string indent, out string output)
        {
            StringBuilder sb = new StringBuilder();

            string start = string.Format(
                "{0}public override bool Parse(string text){1}" +
                "{0}{{{1}",
                indent, this.newLineStr);
            string end = string.Format(
                "{0}}}{1}",
                indent, this.newLineStr);

            sb.Append(start);

            indent += "    ";

            if (structDef.Fields.Count == 0)
            {
                sb.AppendFormat(
                    "{0}return true;{1}",
                    indent, this.newLineStr);
            }
            else
            {
                sb.AppendFormat(
                    "{0}Brickred.Table.ColumnSpliter s ={1}" +
                    "{0}    new Brickred.Table.ColumnSpliter(text, ';');{1}" +
                    "{1}",
                    indent, this.newLineStr);

                for (int i = 0; i < structDef.Fields.Count; ++i)
                {
                    TableDescriptor.StructDef.FieldDef fieldDef =
                        structDef.Fields[i];

                    if (fieldDef.Type == FieldType.Int)
                    {
                        sb.AppendFormat(
                            "{0}if (s.NextInt(ref this.{1}) == false) {{{2}" +
                            "{0}    return false;{2}" +
                            "{0}}}{2}",
                            indent, fieldDef.Name, this.newLineStr);
                    }
                    else if (fieldDef.Type == FieldType.String)
                    {
                        sb.AppendFormat(
                            "{0}if (s.NextString(ref this.{1}) == false) {{{2}" +
                            "{0}    return false;{2}" +
                            "{0}}}{2}",
                            indent, fieldDef.Name, this.newLineStr);
                    }
                }

                sb.AppendFormat(
                    "{0}if (s.NextString()) {{{1}" +
                    "{0}    return false;{1}" +
                    "{0}}}{1}" +
                    "{1}" +
                    "{0}return true;{1}",
                    indent, this.newLineStr);
            }

            sb.Append(end);

            output = sb.ToString();
        }
Пример #6
0
        private bool AddTableColumnDef(
            TableDescriptor.TableDef tableDef, XElement element)
        {
            // check name attr
            string name;

            {
                XAttribute attr = element.Attribute("name");
                if (attr == null)
                {
                    PrintLineError(element,
                                   "`col` node must contain a `name` attribute");
                    return(false);
                }
                name = attr.Value;
            }
            if (Regex.IsMatch(name, @"^[a-zA-Z_]\w*$") == false)
            {
                PrintLineError(element,
                               "`col` node `name` attribute is invalid");
                return(false);
            }
            if (tableDef.ColumnNameIndex.ContainsKey(name))
            {
                PrintLineError(element,
                               "`col` node `name` attribute duplicated");
                return(false);
            }

            // check type attr
            string type;

            {
                XAttribute attr = element.Attribute("type");
                if (attr == null)
                {
                    PrintLineError(element,
                                   "`col` node must contain a `type` attribute");
                    return(false);
                }
                type = attr.Value;
            }

            TableDescriptor.TableDef.ColumnDef def =
                new TableDescriptor.TableDef.ColumnDef();
            def.ParentRef  = tableDef;
            def.Name       = name;
            def.LineNumber = GetLineNumber(element);

            // get type info
            string columnTypeStr = type;

            {
                Match m = Regex.Match(type, @"^list{(.+)}$");
                if (m.Success)
                {
                    columnTypeStr = m.Groups[1].Value;
                    def.Type      = TableDescriptor.TableDef.ColumnType.List;
                }
            }

            TableDescriptor.TableDef.ColumnType columnType =
                TableDescriptor.TableDef.ColumnType.None;
            if (columnTypeStr == "int")
            {
                columnType = TableDescriptor.TableDef.ColumnType.Int;
            }
            else if (columnTypeStr == "string")
            {
                columnType = TableDescriptor.TableDef.ColumnType.String;
            }
            else
            {
                for (;;)
                {
                    TableDescriptor.StructDef refStructDef = null;

                    // check is local struct
                    if (tableDef.LocalStructNameIndex.TryGetValue(
                            columnTypeStr, out refStructDef))
                    {
                        columnType       = TableDescriptor.TableDef.ColumnType.Struct;
                        def.RefStructDef = refStructDef;
                        break;
                    }

                    // check is global struct
                    if (this.descriptor.GlobalStructNameIndex.TryGetValue(
                            columnTypeStr, out refStructDef))
                    {
                        columnType       = TableDescriptor.TableDef.ColumnType.Struct;
                        def.RefStructDef = refStructDef;
                        break;
                    }

                    PrintLineError(element,
                                   "type `{0}` is undefined", columnTypeStr);
                    return(false);
                }
            }

            if (def.Type == TableDescriptor.TableDef.ColumnType.List)
            {
                def.ListType = columnType;
            }
            else
            {
                def.Type = columnType;
            }

            // check readby attr
            {
                XAttribute attr = element.Attribute("readby");
                if (attr != null)
                {
                    string[] readers = attr.Value.Split('|');
                    for (int i = 0; i < readers.Length; ++i)
                    {
                        string reader = readers[i];

                        TableDescriptor.ReaderDef readerDef = null;
                        if (this.descriptor.Readers.TryGetValue(
                                reader, out readerDef) == false)
                        {
                            PrintLineError(element,
                                           "reader `{0}` is not defined", reader);
                            return(false);
                        }
                        def.Readers[reader] = readerDef;
                    }
                }
            }

            tableDef.Columns.Add(def);
            tableDef.ColumnNameIndex.Add(def.Name, def);

            return(true);
        }
Пример #7
0
        private bool AddStructFieldDef(
            TableDescriptor.StructDef structDef, XElement element)
        {
            // check name attr
            string name;

            {
                XAttribute attr = element.Attribute("name");
                if (attr == null)
                {
                    PrintLineError(element,
                                   "`field` node must contain a `name` attribute");
                    return(false);
                }
                name = attr.Value;
            }
            if (Regex.IsMatch(name, @"^[a-zA-Z_]\w*$") == false)
            {
                PrintLineError(element,
                               "`field` node `name` attribute is invalid");
                return(false);
            }
            if (structDef.FieldNameIndex.ContainsKey(name))
            {
                PrintLineError(element,
                               "`field` node `name` attribute duplicated");
                return(false);
            }

            // check type attr
            string type;

            {
                XAttribute attr = element.Attribute("type");
                if (attr == null)
                {
                    PrintLineError(element,
                                   "`field` node must contain a `type` attribute");
                    return(false);
                }
                type = attr.Value;
            }

            TableDescriptor.StructDef.FieldDef def =
                new TableDescriptor.StructDef.FieldDef();
            def.ParentRef  = structDef;
            def.Name       = name;
            def.LineNumber = GetLineNumber(element);

            if (type == "int")
            {
                def.Type = TableDescriptor.StructDef.FieldType.Int;
            }
            else if (type == "string")
            {
                def.Type = TableDescriptor.StructDef.FieldType.String;
            }
            else
            {
                PrintLineError(element,
                               "type `{0}` is invalid", type);
                return(false);
            }

            structDef.Fields.Add(def);
            structDef.FieldNameIndex.Add(def.Name, def);

            return(true);
        }
Пример #8
0
        private bool AddStructDef(
            TableDescriptor.TableDef tableDef, XElement element)
        {
            // check name attr
            string name;

            {
                XAttribute attr = element.Attribute("name");
                if (attr == null)
                {
                    PrintLineError(element,
                                   "`struct` node must contain a `name` attribute");
                    return(false);
                }
                name = attr.Value;
            }
            if (Regex.IsMatch(name, @"^[a-zA-Z_]\w*$") == false)
            {
                PrintLineError(element,
                               "`struct` node `name` attribute is invalid");
                return(false);
            }
            if (tableDef == null)
            {
                if (this.descriptor.GlobalStructNameIndex.ContainsKey(name) ||
                    this.descriptor.TableNameIndex.ContainsKey(name))
                {
                    PrintLineError(element,
                                   "`struct` node `name` attribute duplicated");
                    return(false);
                }
            }
            else
            {
                if (tableDef.LocalStructNameIndex.ContainsKey(name))
                {
                    PrintLineError(element,
                                   "`struct` node `name` attribute duplicated");
                    return(false);
                }
                if (name == "Row" ||
                    name == "Rows" ||
                    name == "RowSet" ||
                    name == "RowSets")
                {
                    PrintLineError(element,
                                   "local struct can not be named as " +
                                   "`Row`, `Rows`, `RowSet` or `RowSets`");
                    return(false);
                }
            }

            TableDescriptor.StructDef def =
                new TableDescriptor.StructDef();
            def.ParentRef  = tableDef;
            def.Name       = name;
            def.LineNumber = GetLineNumber(element);

            // parse fields
            {
                IEnumerator <XElement> iter =
                    element.Elements().GetEnumerator();
                while (iter.MoveNext())
                {
                    XElement childElement = iter.Current;

                    if (childElement.Name != "field")
                    {
                        PrintLineError(childElement,
                                       "expect a `field` node");
                        return(false);
                    }

                    if (AddStructFieldDef(def, childElement) == false)
                    {
                        return(false);
                    }
                }
            }

            if (tableDef == null)
            {
                this.descriptor.GlobalStructs.Add(def);
                this.descriptor.GlobalStructNameIndex.Add(def.Name, def);
            }
            else
            {
                tableDef.LocalStructs.Add(def);
                tableDef.LocalStructNameIndex.Add(def.Name, def);
            }

            return(true);
        }
Пример #9
0
        public bool FilterByReader(string reader)
        {
            if (this.descriptor == null)
            {
                return(false);
            }

            if (this.descriptor.Readers.ContainsKey(reader) == false)
            {
                Console.Error.WriteLine(string.Format(
                                            "error: reader `{0}` is not defined", reader));
                return(false);
            }

            // remove unread tables
            List <TableDescriptor.TableDef> filteredTables =
                new List <TableDescriptor.TableDef>();

            for (int i = 0; i < this.descriptor.Tables.Count; ++i)
            {
                TableDescriptor.TableDef tableDef =
                    this.descriptor.Tables[i];

                if (tableDef.Readers.Count == 0 ||
                    tableDef.Readers.ContainsKey(reader))
                {
                    filteredTables.Add(tableDef);
                }
                else
                {
                    this.descriptor.TableNameIndex.Remove(tableDef.Name);
                }
            }
            this.descriptor.Tables = filteredTables;

            // remove unread columns
            for (int i = 0; i < this.descriptor.Tables.Count; ++i)
            {
                TableDescriptor.TableDef tableDef =
                    this.descriptor.Tables[i];

                List <TableDescriptor.TableDef.ColumnDef> filteredColumns =
                    new List <TableDescriptor.TableDef.ColumnDef>();
                for (int j = 0; j < tableDef.Columns.Count; ++j)
                {
                    TableDescriptor.TableDef.ColumnDef columnDef =
                        tableDef.Columns[j];
                    if (columnDef == tableDef.TableKey ||
                        columnDef.Readers.Count == 0 ||
                        columnDef.Readers.ContainsKey(reader))
                    {
                        filteredColumns.Add(columnDef);
                    }
                    else
                    {
                        tableDef.ColumnNameIndex.Remove(columnDef.Name);
                    }
                }
                tableDef.Columns = filteredColumns;
                CalculateTableKeyColumnIndex(tableDef);
            }

            // collect used structs
            Dictionary <TableDescriptor.StructDef, bool> usedStructs =
                new Dictionary <TableDescriptor.StructDef, bool>();

            for (int i = 0; i < this.descriptor.Tables.Count; ++i)
            {
                TableDescriptor.TableDef tableDef =
                    this.descriptor.Tables[i];

                for (int j = 0; j < tableDef.Columns.Count; ++j)
                {
                    TableDescriptor.TableDef.ColumnDef columnDef =
                        tableDef.Columns[j];

                    if (columnDef.RefStructDef != null)
                    {
                        usedStructs[columnDef.RefStructDef] = true;
                    }
                }
            }

            // remove unused global structs
            List <TableDescriptor.StructDef> filteredGlobalStructs =
                new List <TableDescriptor.StructDef>();

            for (int i = 0; i < this.descriptor.GlobalStructs.Count; ++i)
            {
                TableDescriptor.StructDef structDef =
                    this.descriptor.GlobalStructs[i];

                if (usedStructs.ContainsKey(structDef))
                {
                    filteredGlobalStructs.Add(structDef);
                }
                else
                {
                    this.descriptor.GlobalStructNameIndex.Remove(
                        structDef.Name);
                }
            }
            this.descriptor.GlobalStructs = filteredGlobalStructs;

            // remove unused local structs
            for (int i = 0; i < this.descriptor.Tables.Count; ++i)
            {
                TableDescriptor.TableDef tableDef =
                    this.descriptor.Tables[i];

                List <TableDescriptor.StructDef> filteredLocalStructs =
                    new List <TableDescriptor.StructDef>();
                for (int j = 0; j < tableDef.LocalStructs.Count; ++j)
                {
                    TableDescriptor.StructDef structDef =
                        tableDef.LocalStructs[j];

                    if (usedStructs.ContainsKey(structDef))
                    {
                        filteredLocalStructs.Add(structDef);
                    }
                    else
                    {
                        tableDef.LocalStructNameIndex.Remove(structDef.Name);
                    }
                }
                tableDef.LocalStructs = filteredLocalStructs;
            }

            return(true);
        }