SetError() public method

Sets an error and always returns false. The message starts with the caller's method name. Use SetSuccess to clear any existing error.
public SetError ( object expectedMessage = null, [ callerName = null ) : bool
expectedMessage object /// Optional object. Its will be used to generate an "expected '...'" message. ///
callerName [ Name of the caller (automatically injected by the compiler).
return bool
        /// <summary>
        /// Matches Int32 values that must not start with '0' ('0' is valid but '0d', where d is any digit, is not).
        /// A signed integer starts with a '-'. '-0' is valid but '-0d' (where d is any digit) is not.
        /// If the value is to big for an Int32, it fails.
        /// </summary>
        /// <param name="this">This <see cref="StringMatcher"/>.</param>
        /// <param name="i">The result integer. 0 on failure.</param>
        /// <param name="minValue">Optional minimal value.</param>
        /// <param name="maxValue">Optional maximal value.</param>
        /// <returns><c>true</c> when matched, <c>false</c> otherwise.</returns>
        public static bool MatchInt32(this StringMatcher @this, out int i, int minValue = int.MinValue, int maxValue = int.MaxValue)
        {
            i = 0;
            int  savedIndex = @this.StartIndex;
            long value      = 0;
            bool signed;

            if (@this.IsEnd)
            {
                return(@this.SetError());
            }
            if ((signed = @this.TryMatchChar('-')) && @this.IsEnd)
            {
                return(@this.BackwardAddError(savedIndex));
            }

            char c;

            if (@this.TryMatchChar('0'))
            {
                if ([email protected] && (c = @this.Head) >= '0' && c <= '9')
                {
                    return(@this.BackwardAddError(savedIndex, "0...9"));
                }
                return(@this.SetSuccess());
            }
            unchecked
            {
                long iMax = Int32.MaxValue;
                if (signed)
                {
                    iMax = iMax + 1;
                }
                while ([email protected] && (c = @this.Head) >= '0' && c <= '9')
                {
                    value = value * 10 + (c - '0');
                    if (value > iMax)
                    {
                        break;
                    }
                    @this.UncheckedMove(1);
                }
            }
            if (@this.StartIndex > savedIndex)
            {
                if (signed)
                {
                    value = -value;
                }
                if (value < (long)minValue || value > (long)maxValue)
                {
                    return(@this.BackwardAddError(savedIndex, String.Format(CultureInfo.InvariantCulture, "value between {0} and {1}", minValue, maxValue)));
                }
                i = (int)value;
                return(@this.SetSuccess());
            }
            return(@this.SetError());
        }
示例#2
0
 /// <summary>
 /// Matches a { "JSON" : "object" }, a ["JSON", "array"] or a terminal value (string, null, double, true or false)
 /// and any combination of them.
 /// Whitespaces and JS comments (//... or /* ... */) are skipped.
 /// </summary>
 /// <param name="this">This <see cref="StringMatcher"/>.</param>
 /// <param name="value">
 /// A list of objects (for array), a list of KeyValuePair&lt;string,object&gt; for object or
 /// a double, string, boolean or null (for null).</param>
 /// <returns>True on success, false on error.</returns>
 public static bool MatchJSONObject(this StringMatcher @this, out object value)
 {
     value = null;
     @this.SkipWhiteSpacesAndJSComments();
     if (@this.TryMatchChar('{'))
     {
         List <KeyValuePair <string, object> > c;
         if (!MatchJSONObjectContent(@this, out c))
         {
             return(false);
         }
         value = c;
         return(true);
     }
     if (@this.TryMatchChar('['))
     {
         List <object> t;
         if (!MatchJSONArrayContent(@this, out t))
         {
             return(false);
         }
         value = t;
         return(true);
     }
     if (TryMatchJSONTerminalValue(@this, out value))
     {
         return(true);
     }
     return(@this.SetError("JSON value."));
 }
示例#3
0
 /// <summary>
 /// Matches a JSON array content: the match ends with the first ].
 /// Whitespaces and JS comments (//... or /* ... */) are skipped.
 /// </summary>
 /// <param name="this">This <see cref="StringMatcher"/>.</param>
 /// <param name="value">The list of objects on success.</param>
 /// <returns>True on success, false otherwise.</returns>
 public static bool MatchJSONArrayContent(this StringMatcher @this, out List <object> value)
 {
     value = null;
     while ([email protected])
     {
         @this.SkipWhiteSpacesAndJSComments();
         if (@this.TryMatchChar(']'))
         {
             if (value == null)
             {
                 value = new List <object>();
             }
             return(true);
         }
         object cell;
         if (!MatchJSONObject(@this, out cell))
         {
             return(false);
         }
         if (value == null)
         {
             value = new List <object>();
         }
         value.Add(cell);
         @this.SkipWhiteSpacesAndJSComments();
         // Allow trailing comma: ,] is valid.
         @this.TryMatchChar(',');
     }
     return(@this.SetError("JSON array definition but reached end of match."));
 }
示例#4
0
 /// <summary>
 /// Matches a very simple version of a JSON object content: this match stops at the first closing }.
 /// Whitespaces and JS comments (//... or /* ... */) are skipped.
 /// </summary>
 /// <param name="this">This <see cref="StringMatcher"/>.</param>
 /// <param name="o">The read object on success as a list of KeyValuePair.</param>
 /// <returns>True on success, false on error.</returns>
 public static bool MatchJSONObjectContent(this StringMatcher @this, out List <KeyValuePair <string, object> > o)
 {
     o = null;
     while ([email protected])
     {
         @this.SkipWhiteSpacesAndJSComments();
         string propName;
         object value;
         if (@this.TryMatchChar('}'))
         {
             if (o == null)
             {
                 o = new List <KeyValuePair <string, object> >();
             }
             return(true);
         }
         if ([email protected](out propName))
         {
             o = null;
             return(@this.SetError("Quoted Property Name."));
         }
         @this.SkipWhiteSpacesAndJSComments();
         if ([email protected](':') || !MatchJSONObject(@this, out value))
         {
             o = null;
             return(false);
         }
         if (o == null)
         {
             o = new List <KeyValuePair <string, object> >();
         }
         o.Add(new KeyValuePair <string, object>(propName, value));
         @this.SkipWhiteSpacesAndJSComments();
         // This accepts e trailing comma at the end of a property list: ..."a":0,} is not an error.
         @this.TryMatchChar(',');
     }
     return(@this.SetError("JSON object definition but reached end of match."));
 }
示例#5
0
 /// <summary>
 /// Visits a terminal value. This method simply calls <see cref="StringMatcher.MatchWhiteSpaces(int)">Matcher.MatchWhiteSpaces(0)</see>
 /// to skip any whitespace and <see cref="StringMatcherTextExtension.TryMatchJSONTerminalValue(StringMatcher)">TryMatchJSONTerminalValue</see>
 /// to skip the value itself.
 /// </summary>
 /// <returns>True on success. On error a message may be retrieved from the <see cref="Matcher"/>.</returns>
 protected virtual bool VisitTerminalValue()
 {
     SkipWhiteSpaces();
     return(_m.TryMatchJSONTerminalValue() || _m.SetError());
 }