private void GetNamespaceDecl(
            out string start, out string end)
        {
            start = "";
            end   = "";

            TableDescriptor.ReaderDef readerDef = null;
            if (this.descriptor.Readers.TryGetValue(
                    this.reader, out readerDef) == false)
            {
                return;
            }
            if (readerDef.Namespace == "")
            {
                return;
            }

            start = string.Format(
                "namespace {0}{1}" +
                "{{{1}",
                readerDef.Namespace, this.newLineStr);
            end = string.Format(
                "}}{0}", this.newLineStr);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        private bool AddReaderDef(XElement element)
        {
            // check name attr
            string name;

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

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

            // check namespace attr
            do
            {
                XAttribute attr = element.Attribute("namespace");
                if (attr == null)
                {
                    break;
                }

                string[] namespaceParts = attr.Value.Split('.');
                for (int i = 0; i < namespaceParts.Length; ++i)
                {
                    if (Regex.IsMatch(namespaceParts[i],
                                      @"^[a-zA-Z_]\w*$") == false)
                    {
                        PrintLineError(element,
                                       "`reader` node `namespace` attribute is invalid");
                        return(false);
                    }
                }

                def.Namespace = attr.Value;
                for (int i = 0; i < namespaceParts.Length; ++i)
                {
                    def.NamespaceParts.Add(namespaceParts[i]);
                }
            } while (false);

            this.descriptor.Readers.Add(def.Name, def);

            return(true);
        }