示例#1
0
 public ValueCell(StructField def, IConvertible value, string displayValue, int offset)
     : base(def)
 {
     _value = value;
     _displayValue = displayValue;
     _offset = offset;
 }
示例#2
0
 protected ReferenceBase(StructField baseField, string attrName, string targetName, TextPosition position)
 {
     _baseField  = baseField;
     _attrName   = attrName;
     _targetName = targetName;
     _position   = position;
 }
示例#3
0
        internal void SetFieldAttribute(StructField field, string key, string value, TextPosition pos)
        {
            if (field == null) return;

            Type fieldType = field.GetType();

            while(true)
            {
                Dictionary<string, AttributeType> fieldsForType;
                if (_registry.TryGetValue(fieldType, out fieldsForType))
                {
                    AttributeType attrType;
                    if (fieldsForType.TryGetValue(key, out attrType))
                    {
                        SetFieldAttributeValue(field, key, attrType, value, pos);
                        return;
                    }
                }
                if (fieldType.Equals(typeof(StructField))) break;
                fieldType = fieldType.BaseType;
            }

            try
            {
                field.SetAttribute(key, value);
            }
            catch(Exception)
            {
                throw new ParseException("Unknown attribute " + key, pos);
            }
        }
示例#4
0
 protected ReferenceBase(StructField baseField, string attrName, string targetName, TextPosition position)
 {
     _baseField = baseField;
     _attrName = attrName;
     _targetName = targetName;
     _position = position;
 }
示例#5
0
        private void LoadFieldGroup(StructLexer lexer, StructDef structDef, StructField parentField)
        {
            lexer.GetNextToken(StructTokenType.OpenCurly);
            StructField linkToField = null;

            while (lexer.PeekNextToken() != StructTokenType.CloseCurly)
            {
                StructField field = LoadField(lexer, structDef, parentField);

                if (field != null)
                {
                    bool isLinked = false;
                    if (linkToField != null)
                    {
                        isLinked = linkToField.CanLinkField(field);
                    }

                    if (isLinked)
                    {
                        linkToField.LinkField(field);
                    }
                    else
                    {
                        linkToField = field;
                    }

                    field.Validate();
                }
            }
            lexer.GetNextToken(StructTokenType.CloseCurly);
        }
示例#6
0
        private StructField LoadField(StructLexer lexer, StructDef structDef, StructField parentField)
        {
            List <Attribute> attrs = new List <Attribute>();

            LoadAttributes(lexer, attrs);
            TextPosition fieldPosition = lexer.CurrentPosition;
            string       fieldType     = lexer.GetNextToken(StructTokenType.String);
            StructField  field         = null;

            try
            {
                field          = _fieldFactory.CreateField(structDef, fieldType, _attributeRegistry);
                field.Name     = fieldType;
                field.Position = fieldPosition;
            }
            catch (Exception ex)
            {
                _errors.Add(new ParseException(ex.Message, fieldPosition));
            }
            LoadAttributes(lexer, attrs);
            if (lexer.PeekNextToken() != StructTokenType.Semicolon && lexer.PeekNextToken() != StructTokenType.OpenCurly)
            {
                TextPosition pos = lexer.CurrentPosition;
                string       tag = lexer.GetNextToken(StructTokenType.String);
                LoadAttributes(lexer, attrs);
                if (field != null)
                {
                    _attributeRegistry.SetFieldAttribute(field, field.DefaultAttribute, tag, pos);
                }
            }

            foreach (Attribute attr in attrs)
            {
                _attributeRegistry.SetFieldAttribute(field, attr.Key, attr.Value, attr.Position);
            }

            if (lexer.PeekNextToken() == StructTokenType.OpenCurly)
            {
                LoadFieldGroup(lexer, structDef, field);
            }
            else
            {
                lexer.GetNextToken(StructTokenType.Semicolon);
            }

            if (field != null)
            {
                field.EndPosition = lexer.LastTokenEndPosition;
                if (parentField == null)
                {
                    structDef.AddField(field);
                }
                else
                {
                    parentField.AddChildField(field);
                }
            }
            return(field);
        }
示例#7
0
 internal void LinkField(StructField field)
 {
     if (_linkedFields == null)
     {
         _linkedFields = new List <StructField>();
     }
     _linkedFields.Add(field);
     field._linkedToField = this;
 }
示例#8
0
        public void AddChildField(StructField field)
        {
            if (!_acceptsChildren)
            {
                throw new Exception("Field does not accept children");
            }

            if (_childFields == null)
            {
                _childFields = new List <StructField>();
            }
            _childFields.Add(field);
        }
示例#9
0
        internal void SetFieldAttribute(StructField field, string key, string value, TextPosition pos)
        {
            if (field == null)
            {
                return;
            }

            Type fieldType = field.GetType();

            while (true)
            {
                Dictionary <string, AttributeType> fieldsForType;
                if (_registry.TryGetValue(fieldType, out fieldsForType))
                {
                    AttributeType attrType;
                    if (fieldsForType.TryGetValue(key, out attrType))
                    {
                        SetFieldAttributeValue(field, key, attrType, value, pos);
                        return;
                    }
                }
                if (fieldType.Equals(typeof(StructField)))
                {
                    break;
                }
                fieldType = fieldType.BaseType;
            }

            try
            {
                field.SetAttribute(key, value);
            }
            catch (Exception)
            {
                throw new ParseException("Unknown attribute " + key, pos);
            }
        }
示例#10
0
        private static void SetFieldAttributeValue(StructField field, string key, AttributeType type, string value,
                                                   TextPosition pos)
        {
            switch (type)
            {
            case AttributeType.Expression:
                try
                {
                    field.SetAttributeValue(key, ExpressionParser.Parse(value));
                }
                catch (ParseException ex)
                {
                    throw new ParseException(ex.Message, pos.OffsetBy(ex.Position));
                }
                break;

            case AttributeType.String:
                field.SetAttributeValue(key, value);
                break;

            case AttributeType.Int:
                field.SetAttributeValue(key, Int32.Parse(value));
                break;

            case AttributeType.Bool:
                field.SetAttributeValue(key, (Int32.Parse(value) != 0));
                break;

            case AttributeType.StructRef:
                field.StructDef.StructFile.AddReference(new StructReference(field, key, value, pos));
                break;

            case AttributeType.EnumRef:
                field.StructDef.StructFile.AddReference(new EnumReference(field, key, value, pos));
                break;
            }
        }
示例#11
0
 public void AddField(StructField field)
 {
     _fields.Add(field);
 }
示例#12
0
 public ExprCell(StructField def, StructInstance instance, Expression expression) : base(def)
 {
     _instance   = instance;
     _expression = expression;
 }
示例#13
0
        public void AddChildField(StructField field)
        {
            if (!_acceptsChildren)
                throw new Exception("Field does not accept children");

            if (_childFields == null)
                _childFields = new List<StructField>();
            _childFields.Add(field);
        }
示例#14
0
 protected internal virtual bool CanLinkField(StructField nextField)
 {
     return false;
 }
示例#15
0
 public EnumReference(StructField baseField, string attrName, string targetName, TextPosition position)
     : base(baseField, attrName, targetName, position)
 {
 }
示例#16
0
        private void LoadFieldGroup(StructLexer lexer, StructDef structDef, StructField parentField)
        {
            lexer.GetNextToken(StructTokenType.OpenCurly);
            StructField linkToField = null;
            while(lexer.PeekNextToken() != StructTokenType.CloseCurly)
            {
                StructField field = LoadField(lexer, structDef, parentField);

                if (field != null)
                {
                    bool isLinked = false;
                    if (linkToField != null)
                        isLinked = linkToField.CanLinkField(field);

                    if (isLinked)
                        linkToField.LinkField(field);
                    else
                        linkToField = field;

                    field.Validate();
                }
            }
            lexer.GetNextToken(StructTokenType.CloseCurly);
        }
示例#17
0
 internal void LinkField(StructField field)
 {
     if (_linkedFields == null)
         _linkedFields = new List<StructField>();
     _linkedFields.Add(field);
     field._linkedToField = this;
 }
示例#18
0
 public ValueCell(StructField def, IConvertible value, string displayValue, int offset) : base(def)
 {
     _value        = value;
     _displayValue = displayValue;
     _offset       = offset;
 }
示例#19
0
 public void AddField(StructField field)
 {
     _fields.Add(field);
 }
示例#20
0
 private static void SetFieldAttributeValue(StructField field, string key, AttributeType type, string value,
     TextPosition pos)
 {
     switch(type)
     {
         case AttributeType.Expression:
             try
             {
                 field.SetAttributeValue(key, ExpressionParser.Parse(value));
             }
             catch(ParseException ex)
             {
                 throw new ParseException(ex.Message, pos.OffsetBy(ex.Position));
             }
             break;
         case AttributeType.String:
             field.SetAttributeValue(key, value);
             break;
         case AttributeType.Int:
             field.SetAttributeValue(key, Int32.Parse(value));
             break;
         case AttributeType.Bool:
             field.SetAttributeValue(key, (Int32.Parse(value) != 0));
             break;
         case AttributeType.StructRef:
             field.StructDef.StructFile.AddReference(new StructReference(field, key, value, pos));
             break;
         case AttributeType.EnumRef:
             field.StructDef.StructFile.AddReference(new EnumReference(field, key, value, pos));
             break;
     }
 }
示例#21
0
 protected internal virtual bool CanLinkField(StructField nextField)
 {
     return(false);
 }
示例#22
0
 public static StructCell CreateErrorCell(StructField def, string errorMessage)
 {
     return(new ValueCell(def, new ErrValue(errorMessage), -1));
 }
示例#23
0
 protected StructCell(StructField def)
 {
     _def = def;
 }
示例#24
0
 public EnumReference(StructField baseField, string attrName, string targetName, TextPosition position)
     : base(baseField, attrName, targetName, position)
 {
 }
示例#25
0
 protected StructCell(StructField def)
 {
     _def = def;
 }
示例#26
0
 public static StructCell CreateErrorCell(StructField def, string errorMessage)
 {
     return new ValueCell(def, new ErrValue(errorMessage), -1);
 }
示例#27
0
        private StructField LoadField(StructLexer lexer, StructDef structDef, StructField parentField)
        {
            List<Attribute> attrs = new List<Attribute>();
            LoadAttributes(lexer, attrs);
            TextPosition fieldPosition = lexer.CurrentPosition;
            string fieldType = lexer.GetNextToken(StructTokenType.String);
            StructField field = null;
            try
            {
                field = _fieldFactory.CreateField(structDef, fieldType, _attributeRegistry);
                field.Name = fieldType;
                field.Position = fieldPosition;
            }
            catch(Exception ex)
            {
                _errors.Add(new ParseException(ex.Message, fieldPosition));
            }
            LoadAttributes(lexer, attrs);
            if (lexer.PeekNextToken() != StructTokenType.Semicolon && lexer.PeekNextToken() != StructTokenType.OpenCurly)
            {
                TextPosition pos = lexer.CurrentPosition;
                string tag = lexer.GetNextToken(StructTokenType.String);
                LoadAttributes(lexer, attrs);
                if (field != null)
                    _attributeRegistry.SetFieldAttribute(field, field.DefaultAttribute, tag, pos);
            }

            foreach (Attribute attr in attrs)
                _attributeRegistry.SetFieldAttribute(field, attr.Key, attr.Value, attr.Position);

            if (lexer.PeekNextToken() == StructTokenType.OpenCurly)
                LoadFieldGroup(lexer, structDef, field);
            else
                lexer.GetNextToken(StructTokenType.Semicolon);

            if (field != null)
            {
                field.EndPosition = lexer.LastTokenEndPosition;
                if (parentField == null)
                    structDef.AddField(field);
                else
                    parentField.AddChildField(field);
            }
            return field;
        }