Beispiel #1
0
 public ValueCell(StructField def, IConvertible value, string displayValue, int offset)
     : base(def)
 {
     _value = value;
     _displayValue = displayValue;
     _offset = offset;
 }
Beispiel #2
0
 protected ReferenceBase(StructField baseField, string attrName, string targetName, TextPosition position)
 {
     _baseField  = baseField;
     _attrName   = attrName;
     _targetName = targetName;
     _position   = position;
 }
Beispiel #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);
            }
        }
Beispiel #4
0
 protected ReferenceBase(StructField baseField, string attrName, string targetName, TextPosition position)
 {
     _baseField = baseField;
     _attrName = attrName;
     _targetName = targetName;
     _position = position;
 }
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #7
0
 internal void LinkField(StructField field)
 {
     if (_linkedFields == null)
     {
         _linkedFields = new List <StructField>();
     }
     _linkedFields.Add(field);
     field._linkedToField = this;
 }
Beispiel #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);
        }
Beispiel #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);
            }
        }
Beispiel #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;
            }
        }
Beispiel #11
0
 public void AddField(StructField field)
 {
     _fields.Add(field);
 }
Beispiel #12
0
 public ExprCell(StructField def, StructInstance instance, Expression expression) : base(def)
 {
     _instance   = instance;
     _expression = expression;
 }
Beispiel #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);
        }
Beispiel #14
0
 protected internal virtual bool CanLinkField(StructField nextField)
 {
     return false;
 }
Beispiel #15
0
 public EnumReference(StructField baseField, string attrName, string targetName, TextPosition position)
     : base(baseField, attrName, targetName, position)
 {
 }
Beispiel #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);
        }
Beispiel #17
0
 internal void LinkField(StructField field)
 {
     if (_linkedFields == null)
         _linkedFields = new List<StructField>();
     _linkedFields.Add(field);
     field._linkedToField = this;
 }
Beispiel #18
0
 public ValueCell(StructField def, IConvertible value, string displayValue, int offset) : base(def)
 {
     _value        = value;
     _displayValue = displayValue;
     _offset       = offset;
 }
Beispiel #19
0
 public void AddField(StructField field)
 {
     _fields.Add(field);
 }
Beispiel #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;
     }
 }
Beispiel #21
0
 protected internal virtual bool CanLinkField(StructField nextField)
 {
     return(false);
 }
Beispiel #22
0
 public static StructCell CreateErrorCell(StructField def, string errorMessage)
 {
     return(new ValueCell(def, new ErrValue(errorMessage), -1));
 }
Beispiel #23
0
 protected StructCell(StructField def)
 {
     _def = def;
 }
Beispiel #24
0
 public EnumReference(StructField baseField, string attrName, string targetName, TextPosition position)
     : base(baseField, attrName, targetName, position)
 {
 }
Beispiel #25
0
 protected StructCell(StructField def)
 {
     _def = def;
 }
Beispiel #26
0
 public static StructCell CreateErrorCell(StructField def, string errorMessage)
 {
     return new ValueCell(def, new ErrValue(errorMessage), -1);
 }
Beispiel #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;
        }