Ejemplo n.º 1
0
 public OptionSyntax(SyntaxToken nameMarker, SyntaxToken name,
                     SyntaxToken valueMarker, ValueSyntax value)
     : base(SyntaxKind.OptionSyntax, new SyntaxNode[] { nameMarker, name, valueMarker, value })
 {
     NameMarker  = nameMarker;
     Name        = name;
     ValueMarker = valueMarker;
     Value       = value;
 }
Ejemplo n.º 2
0
        private string GetValue(ValueSyntax valueSyntax)
        {
            if (valueSyntax == null || valueSyntax.Tokens.Count == 0)
            {
                return(String.Empty);
            }

            if (valueSyntax.Tokens.Count == 1)
            {
                return(valueSyntax.Tokens[0].StringValue);
            }

            return(String.Join("", valueSyntax.Tokens.Select(_ => _.StringValue)));
        }
Ejemplo n.º 3
0
        public static object AsObject(this ValueSyntax value)
        {
            switch (value)
            {
            case BooleanValueSyntax booleanValueSyntax:
                return(booleanValueSyntax.Value);

            case DateTimeValueSyntax dateTimeValueSyntax:
                return(dateTimeValueSyntax.Value);

            case FloatValueSyntax floatValueSyntax:
                return(floatValueSyntax.Value);

            case IntegerValueSyntax integerValueSyntax:
                return(integerValueSyntax.Value);

            case StringValueSyntax stringValueSyntax:
                return(stringValueSyntax.Value);
            }
            return(null);
        }
        private void AddSpans(
            List <StyledSpan <TStyle> > spans,
            ValueSyntax valueSyntax,
            CommandLineStyleName @class,
            HighlightingPalette <CommandLineStyleName, TStyle> palette)
        {
            var valueTokens = valueSyntax.Tokens;
            var first       = valueTokens[0];
            var last        = valueTokens[valueTokens.Count - 1];

            if (first.HasLeadingTrivia)
            {
                spans.Add(GetDefaultSpan(first.LeadingTrivia.Span, palette));
            }

            spans.Add(GetStyledSpan(@class, valueSyntax.Span, palette));

            if (last.HasTrailingTrivia)
            {
                spans.Add(GetDefaultSpan(last.TrailingTrivia.Span, palette));
            }
        }
Ejemplo n.º 5
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 VisitValueSyntax(ValueSyntax value)
 {
     _OnTypeSyntax(value);
     base.VisitValueSyntax(value);
 }
Ejemplo n.º 7
0
 /// <summary>
 /// $hack(jefflill): This extension returns the actual string value for a <see cref="ValueSyntax"/>.
 /// </summary>
 /// <param name="value">The source value.</param>
 /// <returns>The values's string name.</returns>
 public static string GetValue(this ValueSyntax value) => Normalize(value.ToString());
Ejemplo n.º 8
0
        private OptionSyntax ScanOption(ElementsCursor <SyntaxToken> cursor, List <SyntaxError> errors,
                                        SyntaxToken nameMarker, SyntaxToken name, IReadOnlyList <string> commandName)
        {
            SyntaxToken valueMarker = null;
            ValueSyntax value       = null;

            if (!HasMoreTokens(cursor))
            {
                return(new OptionSyntax(nameMarker, name, valueMarker, value));
            }

            var token = cursor.Peek();

            if (IsOptionValueMarker(token))
            {
                valueMarker = token;
                cursor.MoveNext();
                token = cursor.Peek();
            }
            else
            {
                if (!HasSpaceAfterOrEnd(name, token))
                {
                    var builder = new SyntaxTokenBuilder(name);
                    builder.TrailingTrivia = _syntaxFactory.WhitespaceTriviaAfter(name, length: 0, missing: true);
                    name = builder.Build();

                    errors.Add(_syntaxFactory.MissingWhitespaceError(name.TrailingTrivia.Span));
                }

                _semanticModel.TryGetOptionType(commandName, name.StringValue, out Type valueType);

                if (IsFlag(valueType))
                {
                    // Flag can have a value only if a value marker is specified
                    // because it have value 'true' by default.
                    return(new OptionSyntax(nameMarker, name, valueMarker, value));
                }
            }

            bool missingValue = valueMarker != null &&
                                (!HasMoreTokens(cursor) || (valueMarker.HasTrailingTrivia && IsOptionNameOrEndOfOptions(token, cursor.Peek(1))));

            if (missingValue)
            {
                SyntaxToken valueToken = null;
                var         builder    = new SyntaxTokenBuilder();
                var         span       = valueMarker.Span;
                _syntaxFactory.LiteralToken(builder, span.Source, span.End, 0, null, missing: true);

                if (valueMarker.HasTrailingTrivia)
                {
                    // Place trailing trivia after missing option value
                    builder.TrailingTrivia = valueMarker.TrailingTrivia;
                    valueToken             = builder.Build();

                    // and remove it from value marker.
                    builder.InitializeWith(valueMarker);
                    builder.TrailingTrivia = null;
                    valueMarker            = builder.Build();
                }
                else
                {
                    valueToken = builder.Build();
                }

                errors.Add(_syntaxFactory.MissingOptionValueError(valueToken.Span));
                value = new ValueSyntax(valueToken);
                return(new OptionSyntax(nameMarker, name, valueMarker, value));
            }

            bool isValue = (valueMarker != null && !valueMarker.HasTrailingTrivia) ||
                           !IsOptionNameOrEndOfOptions(token, cursor.Peek(1));

            if (isValue)
            {
                value = ScanValue(cursor, errors);
            }

            return(new OptionSyntax(nameMarker, name, valueMarker, value));
        }
 public virtual void VisitValueSyntax(ValueSyntax value)
 {
     Visit(value.Properties);
 }
 public bool VisitValue(ValueSyntax value, CommandLineHighlightingVisitorParams <TStyle> param)
 {
     return(false);
 }
Ejemplo n.º 11
0
 public OperandSyntax(int index, ValueSyntax value)
     : base(SyntaxKind.OperandSyntax, new[] { value })
 {
     Index = index;
     Value = value;
 }