Пример #1
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);
        }
Пример #2
0
 private static void LoadAttributes(StructLexer lexer, List <Attribute> attrs)
 {
     if (lexer.PeekNextToken() == StructTokenType.OpenSquare)
     {
         lexer.GetNextToken(StructTokenType.OpenSquare);
         while (true)
         {
             TextPosition pos      = lexer.CurrentPosition;
             string       attrName = lexer.GetNextToken(StructTokenType.String);
             string       attrValue;
             if (lexer.CheckNextToken(StructTokenType.Equals))
             {
                 attrValue = lexer.GetAttributeValue(out pos);
             }
             else
             {
                 attrValue = "1";
             }
             attrs.Add(new Attribute(attrName, attrValue, pos));
             if (lexer.CheckNextToken(StructTokenType.CloseSquare))
             {
                 break;
             }
             if (!lexer.CheckNextToken(StructTokenType.Comma))
             {
                 throw new Exception("Unexpected token");
             }
         }
     }
 }
Пример #3
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);
        }
Пример #4
0
        private void LoadEnum(StructLexer lexer, List <Attribute> attrs)
        {
            string  name    = lexer.GetNextToken(StructTokenType.String);
            EnumDef enumDef = new EnumDef(_curStructFile, name);

            LoadAttributes(lexer, attrs);

            foreach (Attribute attr in attrs)
            {
                enumDef.SetAttribute(attr.Key, attr.Value);
            }

            lexer.GetNextToken(StructTokenType.OpenCurly);
            uint lastValue = UInt32.MaxValue;

            while (!lexer.CheckNextToken(StructTokenType.CloseCurly))
            {
                string constName = lexer.GetNextToken(StructTokenType.String);
                if (lexer.CheckNextToken(StructTokenType.Equals))
                {
                    string constValue = lexer.GetNextToken(StructTokenType.String);
                    lastValue = (uint)ExpressionParser.Parse(constValue).EvaluateInt(null);
                }
                else
                {
                    lastValue++;
                }
                enumDef.AddValue(constName, lastValue);

                if (!lexer.CheckNextToken(StructTokenType.Comma))
                {
                    if (lexer.PeekNextToken() != StructTokenType.CloseCurly)
                    {
                        throw new Exception("Unexpected token in enum: " + lexer.PeekNextToken());
                    }
                }
            }
            _curStructFile.Add(enumDef);
        }