Ejemplo n.º 1
0
        /// <summary>
        /// Checks whether the specified <paramref name="kind"/> of syntax
        /// error is applicable to the given <paramref name="node"/>.
        /// </summary>
        /// <param name="kind">Kind of error to check.</param>
        /// <param name="node">Candidate node for applicability.</param>
        /// <remarks>true if <paramref name="kind"/> is applicable to <paramref name="node"/>; false otherwise.</remarks>
        public static bool KindAppliesToNode(PayloadSyntaxErrorKind kind, Node node)
        {
            switch (kind)
            {
            case PayloadSyntaxErrorKind.None:
                return(true);

            case PayloadSyntaxErrorKind.ClosingBracesMissing:
            case PayloadSyntaxErrorKind.OpeningBracesMissing:
            case PayloadSyntaxErrorKind.MissingPropertyColon:
            case PayloadSyntaxErrorKind.MissingPropertyName:
            case PayloadSyntaxErrorKind.MissingPropertyValue:
                return(node is KeyedResourceInstance);

            case PayloadSyntaxErrorKind.ClosingBracketMissing:
            case PayloadSyntaxErrorKind.OpeningBracketMissing:
                return(node is ResourceInstanceCollection);

            case PayloadSyntaxErrorKind.MissingComma:
            case PayloadSyntaxErrorKind.ExtraLeadingComma:
            case PayloadSyntaxErrorKind.ExtraTailingComma:
                return(node is KeyedResourceInstance || node is ResourceInstanceCollection);

            case PayloadSyntaxErrorKind.DotAfterE:
            case PayloadSyntaxErrorKind.LeadingPlusSign:
            case PayloadSyntaxErrorKind.MultipleDots:
            case PayloadSyntaxErrorKind.MultipleE:
            case PayloadSyntaxErrorKind.MultipleEMinus:
            case PayloadSyntaxErrorKind.MultipleEPlus:
            case PayloadSyntaxErrorKind.MultipleLeadingMinus:
            case PayloadSyntaxErrorKind.MultipleLeadingPlus:
            case PayloadSyntaxErrorKind.NumberTooSmall:
            case PayloadSyntaxErrorKind.NumberTooLarge:
            case PayloadSyntaxErrorKind.NumberTooPrecise:
                // return node is NodeValue && ((NodeValue)node).Type.IsNumeric;
                return(node is ResourceInstanceSimpleProperty && TypeData.IsTypeNumeric(((ResourceInstanceSimpleProperty)node).ClrType));

            case PayloadSyntaxErrorKind.TruncatedEscapeSequence:
            case PayloadSyntaxErrorKind.TruncatedHexEscape:
            case PayloadSyntaxErrorKind.IncorrectEscapeSequence:
            case PayloadSyntaxErrorKind.IncorrectHexEscape:
            case PayloadSyntaxErrorKind.IncorrectQuotes:
            case PayloadSyntaxErrorKind.UnterminatedStringValue:
            case PayloadSyntaxErrorKind.UnquotedValue:
                // return node is NodeValue && ((NodeValue)node).Type.ClrType == typeof(string);
                return(node is ResourceInstanceSimpleProperty && ((ResourceInstanceSimpleProperty)node).ClrType == typeof(string));

            default:
                throw new NotImplementedException("KindAppliedToNode does not support " + kind);
            }
        }
Ejemplo n.º 2
0
        /// <summary>Applies this syntax error to the specified node.</summary>
        /// <param name="node">Node to apply error to.</param>
        public static string ApplyErrorsToNodeText(Node node, string text)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            string result = text;

            foreach (var f in System.Linq.Enumerable.OfType <PayloadSyntaxErrorFacet>(node.Facets))
            {
                PayloadSyntaxErrorKind kind = f.PayloadSyntaxError.Kind;
                switch (kind)
                {
                case PayloadSyntaxErrorKind.None:
                    continue;

                case PayloadSyntaxErrorKind.ClosingBracesMissing:
                    result = CachedRegex(@"}\s*$").Replace(result, "", 1);
                    continue;

                case PayloadSyntaxErrorKind.OpeningBracesMissing:
                    result = CachedRegex(@"^\s*\{").Replace(result, "", 1);
                    continue;

                case PayloadSyntaxErrorKind.MissingPropertyColon:
                    result = CachedRegex("(\\{.*):").Replace(result, "$1", 1);
                    continue;

                case PayloadSyntaxErrorKind.MissingPropertyName:
                    result = "{ : 123}";
                    continue;

                case PayloadSyntaxErrorKind.MissingPropertyValue:
                    result = CachedRegex(@"\{(.*):.*,").Replace(result, "{ $1 :,", 1);
                    continue;

                case PayloadSyntaxErrorKind.ClosingBracketMissing:
                    result = CachedRegex(@"]\s*$").Replace(result, "", 1);
                    continue;

                case PayloadSyntaxErrorKind.OpeningBracketMissing:
                    result = CachedRegex(@"^\s*\[").Replace(result, "", 1);
                    continue;

                case PayloadSyntaxErrorKind.MissingComma:
                    // A comma followed by optional whitespace and a letter.
                    result = CachedRegex(@",(\s*\p{L})").Replace(result, "$1", 1);
                    continue;

                case PayloadSyntaxErrorKind.ExtraLeadingComma:
                    result = CachedRegex(@"\{").Replace(result, "{,", 1);
                    continue;

                case PayloadSyntaxErrorKind.ExtraTailingComma:
                    result = CachedRegex(@"}\s*\z").Replace(result, ",}", 1);
                    continue;

                case PayloadSyntaxErrorKind.DotAfterE:
                    result = "1.1E+1.2";
                    continue;

                case PayloadSyntaxErrorKind.LeadingPlusSign:
                    result = "+1";
                    continue;

                case PayloadSyntaxErrorKind.MultipleDots:
                    result = "1.2.3";
                    continue;

                case PayloadSyntaxErrorKind.MultipleE:
                    result = "1.1E+1E+2";
                    continue;

                case PayloadSyntaxErrorKind.MultipleEMinus:
                    result = "1.1E--1";
                    continue;

                case PayloadSyntaxErrorKind.MultipleEPlus:
                    result = "1.1E++1";
                    continue;

                case PayloadSyntaxErrorKind.MultipleLeadingMinus:
                    result = "--1";
                    continue;

                case PayloadSyntaxErrorKind.MultipleLeadingPlus:
                    result = "++1";
                    continue;

                case PayloadSyntaxErrorKind.NumberTooSmall:
                    result = Int64.MinValue.ToString(CultureInfo.InvariantCulture) + "00";
                    continue;

                case PayloadSyntaxErrorKind.NumberTooLarge:
                    result = UInt64.MaxValue.ToString(CultureInfo.InvariantCulture) + "00";
                    continue;

                case PayloadSyntaxErrorKind.NumberTooPrecise:
                    result = "0." + new string('0', 64) + "1";
                    continue;

                case PayloadSyntaxErrorKind.TruncatedEscapeSequence:
                    result = "\"ab\\\"";
                    continue;

                case PayloadSyntaxErrorKind.TruncatedHexEscape:
                    result = "\"ab\\u123\"";
                    continue;

                case PayloadSyntaxErrorKind.IncorrectEscapeSequence:
                    result = "\"ab\\m\"";
                    continue;

                case PayloadSyntaxErrorKind.IncorrectHexEscape:
                    result = "\"ab\\u123g\"";
                    continue;

                case PayloadSyntaxErrorKind.IncorrectQuotes:
                    result = "'abc'";
                    continue;

                case PayloadSyntaxErrorKind.UnterminatedStringValue:
                    result = "\"ab";
                    continue;

                case PayloadSyntaxErrorKind.UnquotedValue:
                    result = "a";
                    continue;

                default:
                    throw new NotImplementedException("KindAppliedToNode does not support " + kind);
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Checks whether the specified <paramref name="kind"/> of syntax 
        /// error is applicable to the given <paramref name="node"/>.
        /// </summary>
        /// <param name="kind">Kind of error to check.</param>
        /// <param name="node">Candidate node for applicability.</param>
        /// <remarks>true if <paramref name="kind"/> is applicable to <paramref name="node"/>; false otherwise.</remarks>
        public static bool KindAppliesToNode(PayloadSyntaxErrorKind kind, Node node)
        {
            switch (kind)
            {
                case PayloadSyntaxErrorKind.None:
                    return true;

                case PayloadSyntaxErrorKind.ClosingBracesMissing:
                case PayloadSyntaxErrorKind.OpeningBracesMissing:
                case PayloadSyntaxErrorKind.MissingPropertyColon:
                case PayloadSyntaxErrorKind.MissingPropertyName:
                case PayloadSyntaxErrorKind.MissingPropertyValue:
                    return node is KeyedResourceInstance;

                case PayloadSyntaxErrorKind.ClosingBracketMissing:
                case PayloadSyntaxErrorKind.OpeningBracketMissing:
                    return node is ResourceInstanceCollection;

                case PayloadSyntaxErrorKind.MissingComma:
                case PayloadSyntaxErrorKind.ExtraLeadingComma:
                case PayloadSyntaxErrorKind.ExtraTailingComma:
                    return node is KeyedResourceInstance || node is ResourceInstanceCollection;

                case PayloadSyntaxErrorKind.DotAfterE:
                case PayloadSyntaxErrorKind.LeadingPlusSign:
                case PayloadSyntaxErrorKind.MultipleDots:
                case PayloadSyntaxErrorKind.MultipleE:
                case PayloadSyntaxErrorKind.MultipleEMinus:
                case PayloadSyntaxErrorKind.MultipleEPlus:
                case PayloadSyntaxErrorKind.MultipleLeadingMinus:
                case PayloadSyntaxErrorKind.MultipleLeadingPlus:
                case PayloadSyntaxErrorKind.NumberTooSmall:
                case PayloadSyntaxErrorKind.NumberTooLarge:
                case PayloadSyntaxErrorKind.NumberTooPrecise:
                    // return node is NodeValue && ((NodeValue)node).Type.IsNumeric;
                    return node is ResourceInstanceSimpleProperty && TypeData.IsTypeNumeric(((ResourceInstanceSimpleProperty)node).ClrType);

                case PayloadSyntaxErrorKind.TruncatedEscapeSequence:
                case PayloadSyntaxErrorKind.TruncatedHexEscape:
                case PayloadSyntaxErrorKind.IncorrectEscapeSequence:
                case PayloadSyntaxErrorKind.IncorrectHexEscape:
                case PayloadSyntaxErrorKind.IncorrectQuotes:
                case PayloadSyntaxErrorKind.UnterminatedStringValue:
                case PayloadSyntaxErrorKind.UnquotedValue:
                    // return node is NodeValue && ((NodeValue)node).Type.ClrType == typeof(string);
                    return node is ResourceInstanceSimpleProperty && ((ResourceInstanceSimpleProperty)node).ClrType == typeof(string);

                default:
                    throw new NotImplementedException("KindAppliedToNode does not support " + kind);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new <see cref="PayloadSyntaxError"/> instance.
 /// </summary>
 /// <param name="kind">Kind of error to inject when building payload.</param>
 public PayloadSyntaxError(PayloadSyntaxErrorKind kind)
 {
     this.kind = kind;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new <see cref="PayloadSyntaxError"/> instance.
 /// </summary>
 /// <param name="kind">Kind of error to inject when building payload.</param>
 public PayloadSyntaxError(PayloadSyntaxErrorKind kind)
 {
     this.kind = kind;
 }