private string GenerateTableFile(
            TableDescriptor.TableDef tableDef)
        {
            string dontEditComment;
            string namespaceDeclStart;
            string namespaceDeclEnd;
            string tableDecl;
            string indent = "";

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

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

            GetTableDecl(tableDef, indent,
                         out tableDecl);

            StringBuilder sb = new StringBuilder();

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

            return(sb.ToString());
        }
        private void GetTableDeclParseFuncSingleKeyReadDataLine(
            TableDescriptor.TableDef tableDef,
            string indent, out string output)
        {
            string parseColumns;

            GetTableDeclParseFuncParseColumns(
                tableDef, indent + "    ", out parseColumns);

            output = string.Format(
                "{0}// read data lines{1}" +
                "{0}int lineNumber = 3;{1}" +
                "{0}this.rows.Clear();{1}" +
                "{0}for (;;) {{{1}" +
                "{0}    lineBuffer = r.NextLine();{1}" +
                "{0}    if (lineBuffer == null) {{{1}" +
                "{0}        break;{1}" +
                "{0}    }}{1}" +
                "{0}    if (lineBuffer.Count != columnCountReq) {{{1}" +
                "{0}        errorInfo = string.Format({1}" +
                "{0}            \"line {{0}} column count {{1}} is invalid, " +
                "should be {{2}}\",{1}" +
                "{0}            lineNumber, lineBuffer.Count, " +
                "columnCountReq);{1}" +
                "{0}        return false;{1}" +
                "{0}    }}{1}" +
                "{0}    if (lineBuffer[{2}].Length == 0) {{{1}" +
                "{0}        errorInfo = string.Format({1}" +
                "{0}            \"line {{0}} key `{3}` is empty\", " +
                "lineNumber);{1}" +
                "{0}        return false;{1}" +
                "{0}    }}{1}" +
                "{1}" +
                "{0}    Row row = new Row();{1}" +
                "{0}    int colNumber = 0;{1}" +
                "{1}" +
                "{4}" +
                "{1}" +
                "{0}    if (GetRow(row.{3}) != null) {{{1}" +
                "{0}        errorInfo = string.Format({1}" +
                "{0}            \"line {{0}} key `{3}` value {{1}} is duplicated\", " +
                "lineNumber, row.{3});{1}" +
                "{0}        return false;{1}" +
                "{0}    }}{1}" +
                "{1}" +
                "{0}    this.rows.Add(row.{3}, row);{1}" +
                "{1}" +
                "{0}    lineNumber += 1;{1}" +
                "{0}}}{1}" +
                "{1}",
                indent, this.newLineStr,
                tableDef.TableKeyColumnIndex, tableDef.TableKey.Name,
                parseColumns);
        }
        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 GetTableDeclParseFuncReadNameLine(
            TableDescriptor.TableDef tableDef,
            string indent, out string output)
        {
            StringBuilder sb = new StringBuilder();

            string start = string.Format(
                "{0}// read name line{1}" +
                "{0}lineBuffer = r.NextLine();{1}" +
                "{0}if (lineBuffer == null) {{{1}" +
                "{0}    errorInfo = \"name line is required\";{1}" +
                "{0}    return false;{1}" +
                "{0}}}{1}" +
                "{0}if (lineBuffer.Count != columnCountReq) {{{1}" +
                "{0}    errorInfo = string.Format({1}" +
                "{0}        \"name line column count {{0}} is invalid," +
                " should be {{1}}\",{1}" +
                "{0}         lineBuffer.Count, columnCountReq);{1}" +
                "{0}    return false;{1}" +
                "{0}}}{1}" +
                "{0}{{{1}" +
                "{0}    int colNumber = 0;{1}" +
                "{1}",
                indent, this.newLineStr);
            string end = string.Format(
                "{0}}}{1}" +
                "{1}",
                indent, this.newLineStr);

            sb.Append(start);

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

                sb.AppendFormat(
                    "{0}if (lineBuffer[colNumber++] != \"{1}\") {{{2}" +
                    "{0}    errorInfo = string.Format({2}" +
                    "{0}        \"column {{0}} should be named as `{1}`\"," +
                    " colNumber);{2}" +
                    "{0}    return false;{2}" +
                    "{0}}}{2}",
                    indent, columnDef.Name, this.newLineStr);
            }

            sb.Append(end);

            output = sb.ToString();
        }
        private void GetTableDeclParseFunc(
            TableDescriptor.TableDef tableDef,
            string indent, out string output)
        {
            StringBuilder sb = new StringBuilder();

            string start = string.Format(
                "{0}public bool Parse(string text, out string errorInfo){1}" +
                "{0}{{{1}" +
                "{0}    Brickred.Table.LineReader r = " +
                "new Brickred.Table.LineReader(text);{1}" +
                "{0}    System.Collections.Generic.List<string> lineBuffer = " +
                "null;{1}" +
                "{0}    int columnCountReq = {2};{1}" +
                "{1}",
                indent, this.newLineStr, tableDef.Columns.Count);
            string end = string.Format(
                "{0}    errorInfo = \"\";{1}" +
                "{0}    return true;{1}" +
                "{0}}}{1}",
                indent, this.newLineStr);

            string readCommentLine;
            string readNameLine;
            string readDataLine = "";

            indent += "    ";
            GetTableDeclParseFuncReadCommentLine(
                tableDef, indent, out readCommentLine);
            GetTableDeclParseFuncReadNameLine(
                tableDef, indent, out readNameLine);

            if (tableDef.TableKeyType == TableKeyType.SingleKey)
            {
                GetTableDeclParseFuncSingleKeyReadDataLine(
                    tableDef, indent, out readDataLine);
            }
            else if (tableDef.TableKeyType == TableKeyType.SetKey)
            {
                GetTableDeclParseFuncSetKeyReadDataLine(
                    tableDef, indent, out readDataLine);
            }

            sb.Append(start);
            sb.Append(readCommentLine);
            sb.Append(readNameLine);
            sb.Append(readDataLine);
            sb.Append(end);

            output = sb.ToString();
        }
Example #6
0
 private void CalculateTableKeyColumnIndex(
     TableDescriptor.TableDef tableDef)
 {
     for (int i = 0; i < tableDef.Columns.Count; ++i)
     {
         TableDescriptor.TableDef.ColumnDef columnDef =
             tableDef.Columns[i];
         if (columnDef == tableDef.TableKey)
         {
             tableDef.TableKeyColumnIndex = i;
             return;
         }
     }
 }
 private void GetTableDeclGetRowFunc(
     TableDescriptor.TableDef tableDef,
     string indent, out string output)
 {
     output = string.Format(
         "{0}public Row GetRow({1} key){2}" +
         "{0}{{{2}" +
         "{0}    Row row = null;{2}" +
         "{0}    if (this.rows.TryGetValue(key, out row) == false) {{{2}" +
         "{0}        return null;{2}" +
         "{0}    }}{2}" +
         "{2}" +
         "{0}    return row;{2}" +
         "{0}}}{2}",
         indent, GetCSharpType(tableDef.TableKey), this.newLineStr);
 }
 private void GetTableDeclGetRowSetFunc(
     TableDescriptor.TableDef tableDef,
     string indent, out string output)
 {
     output = string.Format(
         "{0}public System.Collections.Generic.List<Row> GetRowSet({1} key){2}" +
         "{0}{{{2}" +
         "{0}    System.Collections.Generic.List<Row> rowSet = null;{2}" +
         "{0}    if (this.rowSets.TryGetValue(key, out rowSet) == false) {{{2}" +
         "{0}        return null;{2}" +
         "{0}    }}{2}" +
         "{2}" +
         "{0}    return rowSet;{2}" +
         "{0}}}{2}",
         indent, GetCSharpType(tableDef.TableKey), this.newLineStr);
 }
        private void GetTableDeclRowClassDecl(
            TableDescriptor.TableDef tableDef,
            string indent, out string output)
        {
            StringBuilder sb = new StringBuilder();

            string start = string.Format(
                "{0}public sealed class Row{1}" +
                "{0}{{{1}",
                indent, this.newLineStr);
            string end = string.Format(
                "{0}}}{1}",
                indent, this.newLineStr);

            indent += "    ";

            sb.Append(start);

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

                string csharpType   = GetCSharpType(columnDef);
                string defaultValue = GetCSharpTypeDefaultValue(columnDef);
                if (defaultValue.Length > 10)
                {
                    sb.AppendFormat(
                        "{0}public {1} {2} ={4}" +
                        "{0}    {3};{4}",
                        indent, csharpType, columnDef.Name, defaultValue,
                        this.newLineStr);
                }
                else
                {
                    sb.AppendFormat("{0}public {1} {2} = {3};{4}",
                                    indent, csharpType, columnDef.Name, defaultValue,
                                    this.newLineStr);
                }
            }

            sb.Append(end);

            output = sb.ToString();
        }
 private void GetTableDeclParseFuncReadCommentLine(
     TableDescriptor.TableDef tableDef,
     string indent, out string output)
 {
     output = string.Format(
         "{0}// read comment line{1}" +
         "{0}lineBuffer = r.NextLine();{1}" +
         "{0}if (lineBuffer == null) {{{1}" +
         "{0}    errorInfo = \"comment line is required\";{1}" +
         "{0}    return false;{1}" +
         "{0}}}{1}" +
         "{0}if (lineBuffer.Count != columnCountReq) {{{1}" +
         "{0}    errorInfo = string.Format({1}" +
         "{0}        \"comment line column count {{0}} is invalid," +
         " should be {{1}}\",{1}" +
         "{0}         lineBuffer.Count, columnCountReq);{1}" +
         "{0}    return false;{1}" +
         "{0}}}{1}" +
         "{1}",
         indent, this.newLineStr);
 }
        private void GetTableDeclMemberDecl(
            TableDescriptor.TableDef tableDef,
            string indent, out string output)
        {
            StringBuilder sb = new StringBuilder();

            if (tableDef.TableKeyType == TableKeyType.SingleKey)
            {
                sb.AppendFormat(
                    "{0}public System.Collections.Generic.SortedDictionary" +
                    "<{1}, Row> rows ={2}" +
                    "{0}    new System.Collections.Generic.SortedDictionary" +
                    "<{1}, Row>();{2}" +
                    "{0}public System.Collections.Generic.SortedDictionary" +
                    "<{1}, Row> Rows{2}" +
                    "{0}{{{2}" +
                    "{0}    get {{ return this.rows; }}{2}" +
                    "{0}}}{2}",
                    indent, GetCSharpType(tableDef.TableKey), this.newLineStr);
            }
            else if (tableDef.TableKeyType == TableKeyType.SetKey)
            {
                sb.AppendFormat(
                    "{0}public System.Collections.Generic.SortedDictionary<{2}" +
                    "{0}    {1}, System.Collections.Generic.List<Row>> rowSets ={2}" +
                    "{0}        new System.Collections.Generic.SortedDictionary<{2}" +
                    "{0}            {1}, System.Collections.Generic.List<Row>>();{2}" +
                    "{0}public System.Collections.Generic.SortedDictionary<{2}" +
                    "{0}    {1}, System.Collections.Generic.List<Row>> RowSets{2}" +
                    "{0}{{{2}" +
                    "{0}    get {{ return this.rowSets; }}{2}" +
                    "{0}}}{2}",
                    indent, GetCSharpType(tableDef.TableKey), this.newLineStr);
            }

            output = sb.ToString();
        }
Example #12
0
        private bool AddTableDef(XElement element)
        {
            // check name attr
            string name;

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

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

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

                    if (childElement.Name == "struct")
                    {
                        // parse local struct
                        if (AddStructDef(def, childElement) == false)
                        {
                            return(false);
                        }
                    }
                    else if (childElement.Name == "col")
                    {
                        // parse column
                        if (AddTableColumnDef(def, childElement) == false)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        PrintLineError(childElement,
                                       "expect a `struct` or `col` node");
                        return(false);
                    }
                }
            }

            // check key/setkey attr
            {
                string key;

                XAttribute attr = element.Attribute("key");
                if (attr != null)
                {
                    key = attr.Value;
                    def.TableKeyType =
                        TableDescriptor.TableDef.KeyType.SingleKey;
                }
                else
                {
                    attr = element.Attribute("setkey");
                    if (attr != null)
                    {
                        key = attr.Value;
                        def.TableKeyType =
                            TableDescriptor.TableDef.KeyType.SetKey;
                    }
                    else
                    {
                        PrintLineError(element,
                                       "`table` node must contain a " +
                                       "`key` or `setkey` attribute");
                        return(false);
                    }
                }

                TableDescriptor.TableDef.ColumnDef tableKey = null;
                if (def.ColumnNameIndex.TryGetValue(
                        key, out tableKey) == false)
                {
                    PrintLineError(element,
                                   "table key `{0}` is not defined", key);
                    return(false);
                }
                if (tableKey.Type != TableDescriptor.TableDef.ColumnType.Int &&
                    tableKey.Type != TableDescriptor.TableDef.ColumnType.String)
                {
                    PrintLineError(element,
                                   "table key can only be `int` or `string` type");
                    return(false);
                }
                def.TableKey = tableKey;
            }

            // check file attr
            {
                XAttribute attr = element.Attribute("file");
                if (attr == null)
                {
                    PrintLineError(element,
                                   "`table` node must contain a `file` attribute");
                    return(false);
                }
                def.FileName = attr.Value;
            }

            // 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;
                    }
                }
            }

            CalculateTableKeyColumnIndex(def);
            this.descriptor.Tables.Add(def);
            this.descriptor.TableNameIndex.Add(def.Name, def);

            return(true);
        }
Example #13
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);
        }
        private void GetTableDeclParseFuncParseColumns(
            TableDescriptor.TableDef tableDef,
            string indent, out string output)
        {
            StringBuilder sb = new StringBuilder();

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

                ColumnType checkType;
                if (columnDef.Type == ColumnType.List)
                {
                    checkType = columnDef.ListType;
                }
                else
                {
                    checkType = columnDef.Type;
                }
                bool isList = (columnDef.Type == ColumnType.List);

                if (checkType == ColumnType.Int)
                {
                    if (isList)
                    {
                        sb.AppendFormat(
                            "{0}Brickred.Table.Util.ReadColumnIntList(" +
                            "lineBuffer[colNumber++], ref row.{1});{2}",
                            indent, columnDef.Name, this.newLineStr);
                    }
                    else
                    {
                        sb.AppendFormat(
                            "{0}row.{1} = Brickred.Table.Util.Atoi(" +
                            "lineBuffer[colNumber++]);{2}",
                            indent, columnDef.Name, this.newLineStr);
                    }
                }
                else if (checkType == ColumnType.String)
                {
                    if (isList)
                    {
                        sb.AppendFormat(
                            "{0}Brickred.Table.Util.ReadColumnStringList(" +
                            "lineBuffer[colNumber++], ref row.{1});{2}",
                            indent, columnDef.Name, this.newLineStr);
                    }
                    else
                    {
                        sb.AppendFormat(
                            "{0}row.{1} = lineBuffer[colNumber++];{2}",
                            indent, columnDef.Name, this.newLineStr);
                    }
                }
                else if (checkType == ColumnType.Struct)
                {
                    if (isList)
                    {
                        sb.AppendFormat(
                            "{0}if (Brickred.Table.Util.ReadColumnStructList({1}" +
                            "{0}        lineBuffer[colNumber++], ref row.{2}) " +
                            "== false) {{{1}" +
                            "{0}    errorInfo = string.Format({1}" +
                            "{0}        \"line {{0}} column `{2}` value is invalid\", " +
                            "lineNumber);{1}" +
                            "{0}    return false;{1}" +
                            "{0}}}{1}",
                            indent, this.newLineStr, columnDef.Name);
                    }
                    else
                    {
                        sb.AppendFormat(
                            "{0}if (row.{1}.Parse(" +
                            "lineBuffer[colNumber++]) == false) {{{2}" +
                            "{0}    errorInfo = string.Format({2}" +
                            "{0}        \"line {{0}} column `{1}` value is invalid\", " +
                            "lineNumber);{2}" +
                            "{0}    return false;{2}" +
                            "{0}}}{2}",
                            indent, columnDef.Name, this.newLineStr);
                    }
                }
            }

            output = sb.ToString();
        }
        private void GetTableDeclParseFuncSetKeyReadDataLine(
            TableDescriptor.TableDef tableDef,
            string indent, out string output)
        {
            string parseColumns;

            GetTableDeclParseFuncParseColumns(
                tableDef, indent + "    ", out parseColumns);

            string keyDefine = "";

            if (tableDef.TableKey.Type == ColumnType.Int)
            {
                keyDefine = "int key = Brickred.Table.Util.Atoi(keyStr)";
            }
            else if (tableDef.TableKey.Type == ColumnType.String)
            {
                keyDefine = "string key = keyStr";
            }

            output = string.Format(
                "{0}// read data lines{1}" +
                "{0}int lineNumber = 3;{1}" +
                "{0}string lastKey = \"\";{1}" +
                "{0}this.rowSets.Clear();{1}" +
                "{0}for (;;) {{{1}" +
                "{0}    lineBuffer = r.NextLine();{1}" +
                "{0}    if (lineBuffer == null) {{{1}" +
                "{0}        break;{1}" +
                "{0}    }}{1}" +
                "{0}    if (lineBuffer.Count != columnCountReq) {{{1}" +
                "{0}        errorInfo = string.Format({1}" +
                "{0}            \"line {{0}} column count {{1}} is invalid, " +
                "should be {{2}}\",{1}" +
                "{0}            lineNumber, lineBuffer.Count, " +
                "columnCountReq);{1}" +
                "{0}        return false;{1}" +
                "{0}    }}{1}" +
                "{1}" +
                "{0}    string keyStr = lineBuffer[{2}];{1}" +
                "{0}    if (keyStr.Length == 0) {{{1}" +
                "{0}        if (lastKey.Length > 0) {{{1}" +
                "{0}            keyStr = lastKey;{1}" +
                "{0}        }} else {{{1}" +
                "{0}            errorInfo = string.Format({1}" +
                "{0}                \"line {{0}} key `{3}` is empty\", " +
                "lineNumber);{1}" +
                "{0}            return false;{1}" +
                "{0}        }}{1}" +
                "{0}    }}{1}" +
                "{1}" +
                "{0}    Row row = new Row();{1}" +
                "{0}    int colNumber = 0;{1}" +
                "{1}" +
                "{4}" +
                "{1}" +
                "{0}    {5};{1}" +
                "{0}    if (keyStr != lastKey) {{{1}" +
                "{0}        if (GetRowSet(key) != null) {{{1}" +
                "{0}            errorInfo = string.Format({1}" +
                "{0}                \"line {{0}} key `{3}` value {{1}} is duplicated\",{1} " +
                "{0}                lineNumber, row.{3});{1}" +
                "{0}            return false;{1}" +
                "{0}        }}{1}" +
                "{0}        System.Collections.Generic.List<Row> rowSet ={1}" +
                "{0}            new System.Collections.Generic.List<Row>();{1}" +
                "{0}        rowSet.Add(row);{1}" +
                "{0}        this.rowSets.Add(key, rowSet);{1}" +
                "{0}        lastKey = keyStr;{1}" +
                "{0}    }} else {{{1}" +
                "{0}        this.rowSets[key].Add(row);{1}" +
                "{0}    }}{1}" +
                "{1}" +
                "{0}    lineNumber += 1;{1}" +
                "{0}}}{1}" +
                "{1}",
                indent, this.newLineStr,
                tableDef.TableKeyColumnIndex, tableDef.TableKey.Name,
                parseColumns, keyDefine);
        }
Example #16
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);
        }
        private void GetTableDecl(
            TableDescriptor.TableDef tableDef,
            string indent, out string output)
        {
            StringBuilder sb = new StringBuilder();

            string start = string.Format(
                "{0}public sealed class {1}{2}" +
                "{0}{{{2}",
                indent, tableDef.Name, this.newLineStr);
            string end = string.Format(
                "{0}}}{1}",
                indent, this.newLineStr);
            string rowClassDecl;
            string memberDecl;
            string parseFuncDecl;
            string getRowFuncDecl = "";

            indent += "    ";
            GetTableDeclRowClassDecl(
                tableDef, indent, out rowClassDecl);
            GetTableDeclMemberDecl(
                tableDef, indent, out memberDecl);
            GetTableDeclParseFunc(
                tableDef, indent, out parseFuncDecl);

            if (tableDef.TableKeyType == TableKeyType.SingleKey)
            {
                GetTableDeclGetRowFunc(
                    tableDef, indent, out getRowFuncDecl);
            }
            else if (tableDef.TableKeyType == TableKeyType.SetKey)
            {
                GetTableDeclGetRowSetFunc(
                    tableDef, indent, out getRowFuncDecl);
            }

            sb.Append(start);

            for (int i = 0; i < tableDef.LocalStructs.Count; ++i)
            {
                string decl;
                GetStructDecl(tableDef.LocalStructs[i],
                              indent, out decl);
                sb.Append(decl);
                sb.Append(this.newLineStr);
            }

            if (rowClassDecl != "")
            {
                sb.Append(rowClassDecl);
                sb.Append(this.newLineStr);
            }
            sb.Append(memberDecl);
            sb.Append(this.newLineStr);
            sb.Append(parseFuncDecl);
            sb.Append(this.newLineStr);
            sb.Append(getRowFuncDecl);

            sb.Append(end);

            output = sb.ToString();
        }
Example #18
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);
        }