示例#1
0
 public override void Visit(StringValueSyntax stringValue)
 {
     if (_currentValue != null)
     {
         throw new InvalidOperationException("A String value must be at a leaf.");
     }
     _currentValue = new Value(stringValue.Value);
 }
示例#2
0
        public static void AddItemWithSyntax(this ValueTable table, string key, string value)
        {
            // Create a new item and matching syntax
            var newSyntaxValue = new StringValueSyntax(value);
            var newValue       = new Value(value)
            {
                // TODO: MirrorSyntax = newSyntaxTable,
            };

            // Add the model to the parent table model
            table.Add(key, newValue);

            switch (table.MirrorSyntax)
            {
            case InlineTableSyntax inlineTableSyntax:
                // Add the new syntax to the parent table syntax
                var inlineTableItemSyntax = new InlineTableItemSyntax(
                    new KeyValueSyntax()
                {
                    EqualToken = SyntaxFactory.Token(TokenKind.Equal),
                    Key        = new KeySyntax(key),
                    Value      = newSyntaxValue,
                });

                inlineTableItemSyntax.AddLeadingWhitespace();
                inlineTableItemSyntax.KeyValue.EqualToken.AddLeadingWhitespace();
                inlineTableItemSyntax.KeyValue.EqualToken.AddTrailingWhitespace();

                // A comma can not be on the last item
                // Add a comma to the previous item
                var previousItem = inlineTableSyntax.Items.LastOrDefault();
                if (previousItem != null)
                {
                    previousItem.Comma = SyntaxFactory.Token(TokenKind.Comma);
                }

                inlineTableSyntax.Items.Add(inlineTableItemSyntax);
                break;

            default:
                throw new InvalidOperationException("Unknown Syntax on ValueList");
            }
        }
示例#3
0
        public static void AddItemWithSyntax(this ValueList list, string value)
        {
            // Create a new item and matching syntax
            var newSyntaxValue = new StringValueSyntax(value);
            var newValue       = new Value(value)
            {
                // TODO: MirrorSyntax = newSyntaxTable,
            };

            // Add the model to the parent table model
            list.Add(newValue);

            newSyntaxValue.LeadingTrivia = new List <SyntaxTrivia>()
            {
                new SyntaxTrivia(TokenKind.Whitespaces, "\t"),
            };

            // Add the new syntax to the parent table syntax
            var arrayItemSyntax = new ArrayItemSyntax()
            {
                Value = newSyntaxValue,
                Comma = SyntaxFactory.Token(TokenKind.Comma),
            };

            arrayItemSyntax.Comma.TrailingTrivia = new List <SyntaxTrivia>()
            {
                SyntaxFactory.NewLineTrivia(),
            };

            switch (list.MirrorSyntax)
            {
            case ArraySyntax arraySyntax:
                arraySyntax.Items.Add(arrayItemSyntax);
                break;

            default:
                throw new InvalidOperationException("Unknown Syntax on ValueList");
            }
        }
示例#4
0
        private static void SaveClass(Type type, DocumentSyntax document, KeySyntax key = null)
        {
            key ??= new KeySyntax(type.Name);

            var fields   = type.GetFields(StaticFlags);
            var children = type.GetNestedTypes(StaticFlags);

            var table      = new TableSyntax(key);
            var tableItems = table.Items;

            foreach (var field in fields)
            {
                if (field.IsPrivate || field.IsInitOnly || !field.IsStatic || field.IsLiteral)
                {
                    continue;
                }

                if (field.GetCustomAttribute <Config.ConfigIgnoreAttribute>() != null)
                {
                    continue;
                }

                ValueSyntax value      = null;
                object      fieldValue = field.GetValue(null);

                switch (fieldValue)
                {
                case string v:
                    value = new StringValueSyntax(v);
                    break;

                case sbyte v:
                    value = new IntegerValueSyntax(v);
                    break;

                case byte v:
                    value = new IntegerValueSyntax(v);
                    break;

                case short v:
                    value = new IntegerValueSyntax(v);
                    break;

                case ushort v:
                    value = new IntegerValueSyntax(v);
                    break;

                case int v:
                    value = new IntegerValueSyntax(v);
                    break;

                case uint v:
                    value = new IntegerValueSyntax(v);
                    break;

                case ulong v:
                    value = new IntegerValueSyntax(unchecked ((long)v));
                    break;

                case float v:
                    value = new FloatValueSyntax(v);
                    break;

                case double v:
                    value = new FloatValueSyntax(v);
                    break;

                case bool v:
                    value = new BooleanValueSyntax(v);
                    break;

                default:
                    if (fieldValue is List <string> slist)
                    {
                        value = new ArraySyntax(slist.ToArray());
                    }
                    else if (fieldValue is List <int> ilist)
                    {
                        value = new ArraySyntax(ilist.ToArray());
                    }
                    else if (fieldValue.GetType().IsEnum)
                    {
                        value = new StringValueSyntax(fieldValue.GetType().GetEnumName(fieldValue));
                    }
                    break;
                }

                if (value == null)
                {
                    continue;
                }

                var keyValue = new KeyValueSyntax(
                    field.Name,
                    value
                    );

                //if (field.GetAttribute<Config.CommentAttribute>(out var attribute)) {
                //keyValue.GetChildren(Math.Max(0, keyValue.ChildrenCount - 2)).AddComment(attribute.Message);
                //}

                tableItems.Add(keyValue);
            }

            if (table.Items.ChildrenCount != 0)
            {
                document.Tables.Add(table);
            }

            foreach (var child in children)
            {
                if (child.IsNestedPrivate)
                {
                    continue;
                }
                if (child.GetCustomAttribute <Config.ConfigIgnoreAttribute>() != null)
                {
                    continue;
                }
                var childKey  = new KeySyntax(typeof(Config).Name);
                var parentKey = key.ToString().Split('.');
                if (parentKey.Length != 0)
                {
                    parentKey[0] = null;
                }
                foreach (var subKey in parentKey)
                {
                    if (subKey == null)
                    {
                        continue;
                    }
                    childKey.DotKeys.Add(new DottedKeyItemSyntax(subKey));
                }
                childKey.DotKeys.Add(new DottedKeyItemSyntax(child.Name));
                SaveClass(child, document, childKey);
            }
        }
 public override void Visit(StringValueSyntax stringValue)
 {
     _currentValue = stringValue.Value;
 }
示例#6
0
 public static string AsText(this StringValueSyntax stringKey)
 {
     return(stringKey.Token?.Text);
 }