make() public static method

public static make ( ) : ParseErr
return ParseErr
Example #1
0
        public static Date fromStr(string s, bool check)
        {
            try
            {
                // YYYY-MM-DD
                int year  = num(s, 0) * 1000 + num(s, 1) * 100 + num(s, 2) * 10 + num(s, 3);
                int month = num(s, 5) * 10 + num(s, 6) - 1;
                int day   = num(s, 8) * 10 + num(s, 9);

                // check separator symbols
                if (s[4] != '-' || s[7] != '-' || s.Length != 10)
                {
                    throw new System.Exception();
                }

                return(new Date(year, month, day));
            }
            catch (System.Exception)
            {
                if (!check)
                {
                    return(null);
                }
                throw ParseErr.make("Date", s).val;
            }
        }
Example #2
0
 public static Long fromStr(string s, long radix, bool check)
 {
     try
     {
         return(Long.valueOf(Convert.ToInt64(s, (int)radix)));
     }
     catch (ArgumentException)
     {
         if (!check)
         {
             return(null);
         }
         throw ParseErr.make("Int", s).val;
     }
     catch (OverflowException)
     {
         if (!check)
         {
             return(null);
         }
         throw ParseErr.make("Int", s).val;
     }
     catch (FormatException)
     {
         if (!check)
         {
             return(null);
         }
         throw ParseErr.make("Int", s).val;
     }
 }
Example #3
0
        public static Locale fromStr(string s, bool check)
        {
            int len = s.Length;

            try
            {
                if (len == 2)
                {
                    if (FanStr.isLower(s))
                    {
                        return(new Locale(s, s, null));
                    }
                }

                if (len == 5)
                {
                    string lang    = s.Substring(0, 2);
                    string country = s.Substring(3, 2);
                    if (FanStr.isLower(lang) && FanStr.isUpper(country) && s[2] == '-')
                    {
                        return(new Locale(s, lang, country));
                    }
                }
            }
            catch (Exception e)
            {
                Err.dumpStack(e);
            }
            if (!check)
            {
                return(null);
            }
            throw ParseErr.make("Locale", s).val;
        }
Example #4
0
 public static Double fromStr(string s, bool check)
 {
     try
     {
         if (s == "NaN")
         {
             return(Double.valueOf(m_nan));
         }
         if (s == "INF")
         {
             return(Double.valueOf(m_posInf));
         }
         if (s == "-INF")
         {
             return(Double.valueOf(m_negInf));
         }
         return(Double.valueOf(s));
     }
     catch (FormatException)
     {
         if (!check)
         {
             return(null);
         }
         throw ParseErr.make("Float", s).val;
     }
 }
Example #5
0
        public static Uuid fromStr(string str, bool check)
        {
            try
            {
                // sanity check
                if (str.Length != 36 || str[8] != '-' ||
                    str[13] != '-' || str[18] != '-' || str[23] != '-')
                {
                    throw new Exception();
                }

                // parse hex components
                long a = Convert.ToInt64(str.Substring(0, 8), 16);
                long b = Convert.ToInt64(str.Substring(9, 4), 16);
                long c = Convert.ToInt64(str.Substring(14, 4), 16);
                long d = Convert.ToInt64(str.Substring(19, 4), 16);
                long e = Convert.ToInt64(str.Substring(24), 16);

                return(new Uuid((a << 32) | (b << 16) | c, (d << 48) | e));
            }
            catch (Exception)
            {
                if (!check)
                {
                    return(null);
                }
                throw ParseErr.make("Uuid", str).val;
            }
        }
Example #6
0
        public static Version fromStr(string s, bool check)
        {
            List segments = new List(Sys.IntType, 4);
            int  seg      = -1;
            bool valid    = true;
            int  len      = s.Length;

            for (int i = 0; i < len; ++i)
            {
                int c = s[i];
                if (c == '.')
                {
                    if (seg < 0 || i + 1 >= len)
                    {
                        valid = false; break;
                    }
                    segments.add(Long.valueOf(seg));
                    seg = -1;
                }
                else
                {
                    if ('0' <= c && c <= '9')
                    {
                        if (seg < 0)
                        {
                            seg = c - '0';
                        }
                        else
                        {
                            seg = seg * 10 + (c - '0');
                        }
                    }
                    else
                    {
                        valid = false; break;
                    }
                }
            }
            if (seg >= 0)
            {
                segments.add(Long.valueOf(seg));
            }

            if (!valid || segments.sz() == 0)
            {
                if (check)
                {
                    throw ParseErr.make("Version", s).val;
                }
                else
                {
                    return(null);
                }
            }

            return(new Version(segments));
        }
Example #7
0
 static List checkIds(List ids)
 {
     if (ids.sz() == 0)
     {
         throw ParseErr.make("No unit ids defined").val;
     }
     for (int i = 0; i < ids.sz(); ++i)
     {
         checkId((string)ids.get(i));
     }
     return((List)ids.toImmutable());
 }
Example #8
0
        public static Time fromStr(string s, bool check)
        {
            try
            {
                // hh:mm:ss
                int hour = num(s, 0) * 10 + num(s, 1);
                int min  = num(s, 3) * 10 + num(s, 4);
                int sec  = num(s, 6) * 10 + num(s, 7);

                // check separator symbols
                if (s[2] != ':' || s[5] != ':')
                {
                    throw new System.Exception();
                }

                // optional .FFFFFFFFF
                int i     = 8;
                int ns    = 0;
                int tenth = 100000000;
                int len   = s.Length;
                if (i < len && s[i] == '.')
                {
                    ++i;
                    while (i < len)
                    {
                        int c = s[i];
                        if (c < '0' || c > '9')
                        {
                            break;
                        }
                        ns    += (c - '0') * tenth;
                        tenth /= 10;
                        ++i;
                    }
                }

                // verify everything has been parsed
                if (i < s.Length)
                {
                    throw new System.Exception();
                }

                return(new Time(hour, min, sec, ns));
            }
            catch (System.Exception)
            {
                if (!check)
                {
                    return(null);
                }
                throw ParseErr.make("Time", s).val;
            }
        }
Example #9
0
 internal Date parseDate(string s, bool check)
 {
     try
     {
         parse(s);
         return(new Date(year, (int)mon.ordinal(), day));
     }
     catch (Exception) {}
     if (check)
     {
         throw ParseErr.make("Date", s).val;
     }
     return(null);
 }
Example #10
0
 internal Time parseTime(string s, bool check)
 {
     try
     {
         parse(s);
         return(new Time(hour, min, sec, ns));
     }
     catch (Exception) {}
     if (check)
     {
         throw ParseErr.make("Time", s).val;
     }
     return(null);
 }
Example #11
0
 public static Depend fromStr(string str, bool check)
 {
     try
     {
         return(new Parser(str).parse());
     }
     catch (System.Exception)
     {
         if (!check)
         {
             return(null);
         }
         throw ParseErr.make("Depend", str).val;
     }
 }
Example #12
0
 public static BigDecimal fromStr(string s, bool check)
 {
     try
     {
         return(BigDecimal.valueOf(s));
     }
     catch (System.FormatException)
     {
         if (!check)
         {
             return(null);
         }
         throw ParseErr.make("BigDecimal", s).val;
     }
 }
Example #13
0
 public static Boolean fromStr(string s, bool check)
 {
     if (s == "true")
     {
         return(Boolean.True);
     }
     if (s == "false")
     {
         return(Boolean.False);
     }
     if (!check)
     {
         return(null);
     }
     throw ParseErr.make("Bool", s).val;
 }
Example #14
0
 static void checkId(string id)
 {
     if (id.Length == 0)
     {
         throw ParseErr.make("Invalid unit id length 0").val;
     }
     for (int i = 0; i < id.Length; ++i)
     {
         int c = id[i];
         if (FanInt.isAlpha(c) || c == '_' || c == '%' || c == '$' || c == '/' || c > 128)
         {
             continue;
         }
         throw ParseErr.make("Invalid unit id " + id + " (invalid char '" + (char)c + "')").val;
     }
 }
Example #15
0
 public static Range fromStr(string s, bool check)
 {
     try
     {
         int dot = s.IndexOf('.');
         if (s[dot + 1] != '.')
         {
             throw new Exception();
         }
         bool exclusive = s[dot + 2] == '<';
         long start     = Convert.ToInt64(s.Substring(0, dot));
         long end       = Convert.ToInt64(s.Substring(dot + (exclusive?3:2)));
         return(new Range(start, end, exclusive));
     }
     catch (Exception) {}
     if (!check)
     {
         return(null);
     }
     throw ParseErr.make("Range", s).val;
 }
Example #16
0
        protected static Enum doFromStr(Type t, string name, bool check)
        {
            // the compiler marks the value fields with the Enum flag
            Slot slot = t.slot(name, false);

            if (slot != null && (slot.m_flags & FConst.Enum) != 0)
            {
                try
                {
                    return((Enum)((Field)slot).get(null));
                }
                catch (System.Exception)
                {
                }
            }
            if (!check)
            {
                return(null);
            }
            throw ParseErr.make(t.qname(), name).val;
        }
Example #17
0
        public static DateTime fromHttpStr(string s, bool check)
        {
            for (int i = 0; i < httpFormats.Length; ++i)
            {
                try
                {
                    System.DateTime date = System.DateTime.ParseExact(s, httpFormats,
                                                                      CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite |
                                                                      DateTimeStyles.AdjustToUniversal);
                    return(dotnet(date.Ticks));
                }
                catch (System.Exception)
                {
                }
            }

            if (!check)
            {
                return(null);
            }
            throw ParseErr.make("Invalid HTTP DateTime: '" + s + "'").val;
        }
Example #18
0
        public static Charset fromStr(string name, bool check)
        {
            try
            {
                // lookup charset, to ensure we get normlized name
                Encoding en = Encoding.GetEncoding(name);

                // check normalized name for predefined charsets
                string csName = en.WebName.ToUpper();
                if (csName == "UTF-8")
                {
                    return(utf8());
                }
                if (csName == "UTF-16")
                {
                    return(utf16LE());
                }
                if (csName == "UNICODEFFFE")
                {
                    return(utf16BE());
                }
                if (csName == "ISO-8859-1")
                {
                    return(iso8859_1());
                }

                // create new wrapper
                return(new Charset(en));
            }
            catch (System.Exception e)
            {
                if (!check)
                {
                    return(null);
                }
                throw ParseErr.make("Charset", name, e.ToString()).val;
            }
        }
Example #19
0
 public static Map parseParams(string s, bool check)
 {
     try
     {
         return(doParseParams(s, 0));
     }
     catch (ParseErr.Val e)
     {
         if (!check)
         {
             return(null);
         }
         throw e;
     }
     catch (System.Exception)
     {
         if (!check)
         {
             return(null);
         }
         throw ParseErr.make("MimeType params", s).val;
     }
 }
Example #20
0
        //////////////////////////////////////////////////////////////////////////
        // Parsing
        //////////////////////////////////////////////////////////////////////////

        public static Unit define(string str)
        {
            // parse
            Unit unit = null;

            try
            {
                unit = parseUnit(str);
            }
            catch (Exception)
            {
                throw ParseErr.make("Unit", str).val;
            }

            // register
            lock (m_byId)
            {
                // lookup by its ids
                for (int i = 0; i < unit.m_ids.sz(); ++i)
                {
                    string id = (string)unit.m_ids.get(i);
                    if (m_byId[id] != null)
                    {
                        throw Err.make("Unit id is already defined: " + id).val;
                    }
                }

                // this is a new definition
                for (int i = 0; i < unit.m_ids.sz(); ++i)
                {
                    m_byId[(string)unit.m_ids.get(i)] = unit;
                }
                m_list.add(unit);
            }

            return(unit);
        }
Example #21
0
        //////////////////////////////////////////////////////////////////////////
        // Parse
        //////////////////////////////////////////////////////////////////////////

        internal DateTime parseDateTime(string s, TimeZone defTz, bool check)
        {
            try
            {
                // parse into fields
                tzOffset = System.Int32.MaxValue;
                parse(s);

                // now figure out what timezone to use
                TimeZone.Rule defRule = defTz.rule(year);
                if (tzName != null)
                {
                    // use defTz if tzName was specified and matches any variations of defTz
                    if (tzName == defTz.name() ||
                        tzName == defRule.stdAbbr ||
                        tzName == defRule.dstAbbr)
                    {
                        tz = defTz;
                    }

                    // try to map tzName to TimeZone, use defTz as fallback
                    else
                    {
                        tz = TimeZone.fromStr(tzName, false);
                        if (tz == null)
                        {
                            tz = defTz;
                        }
                    }
                }

                // if tzOffset was specified...
                else if (tzOffset != System.Int32.MaxValue)
                {
                    // figure out what expected offset was for defTz
                    int time      = hour * 3600 + min * 60 + sec;
                    int defOffset = defRule.offset + TimeZone.dstOffset(defRule, year, (int)mon.ordinal(), day, time);

                    // if specified offset matches expected offset for defTz then
                    // use defTz, otherwise use a vanilla GMT+/- timezone
                    if (tzOffset == defOffset)
                    {
                        tz = defTz;
                    }
                    else
                    {
                        tz = TimeZone.fromGmtOffset(tzOffset);
                    }
                }

                // no tzName or tzOffset specified, use defTz
                else
                {
                    tz = defTz;
                }

                // construct DateTime
                return(new DateTime(year, (int)mon.ordinal(), day, hour, min, sec, ns, tzOffset, tz));
            }
            catch (Exception) {}
            if (check)
            {
                throw ParseErr.make("DateTime", s).val;
            }
            return(null);
        }
Example #22
0
        private static DateTime fromStr(string s, bool check, bool iso)
        {
            try
            {
                // YYYY-MM-DD'T'hh:mm:ss
                int year  = num(s, 0) * 1000 + num(s, 1) * 100 + num(s, 2) * 10 + num(s, 3);
                int month = num(s, 5) * 10 + num(s, 6) - 1;
                int day   = num(s, 8) * 10 + num(s, 9);
                int hour  = num(s, 11) * 10 + num(s, 12);
                int min   = num(s, 14) * 10 + num(s, 15);
                int sec   = num(s, 17) * 10 + num(s, 18);

                // check separator symbols
                if (s[4] != '-' || s[7] != '-' ||
                    s[10] != 'T' || s[13] != ':' ||
                    s[16] != ':')
                {
                    throw new System.Exception();
                }

                // optional .FFFFFFFFF
                int i     = 19;
                int ns    = 0;
                int tenth = 100000000;
                if (s[i] == '.')
                {
                    ++i;
                    while (true)
                    {
                        int c = s[i];
                        if (c < '0' || c > '9')
                        {
                            break;
                        }
                        ns    += (c - '0') * tenth;
                        tenth /= 10;
                        ++i;
                    }
                }

                // zone offset
                int offset = 0;
                int ch     = s[i++];
                if (ch != 'Z')
                {
                    int offHour = num(s, i++) * 10 + num(s, i++);
                    if (s[i++] != ':')
                    {
                        throw new System.Exception();
                    }
                    int offMin = num(s, i++) * 10 + num(s, i++);
                    offset = offHour * 3600 + offMin * 60;
                    if (ch == '-')
                    {
                        offset = -offset;
                    }
                    else if (ch != '+')
                    {
                        throw new System.Exception();
                    }
                }

                // timezone - we share this method b/w fromStr and fromIso
                TimeZone tz;
                if (iso)
                {
                    if (i < s.Length)
                    {
                        throw new System.Exception();
                    }
                    tz = TimeZone.fromGmtOffset(offset);
                }
                else
                {
                    if (s[i++] != ' ')
                    {
                        throw new System.Exception();
                    }
                    tz = TimeZone.fromStr(s.Substring(i), true);
                }

                return(new DateTime(year, month, day, hour, min, sec, ns, offset, tz));
            }
            catch (System.Exception)
            {
                if (!check)
                {
                    return(null);
                }
                throw ParseErr.make("DateTime", s).val;
            }
        }
Example #23
0
        public static MimeType fromStr(string s, bool check)
        {
            try
            {
                // common interned mime types
                switch (s[0])
                {
                case 'i':
                    if (s == "image/png")
                    {
                        return(m_imagePng);
                    }
                    if (s == "image/jpeg")
                    {
                        return(m_imageJpeg);
                    }
                    if (s == "image/gif")
                    {
                        return(m_imageGif);
                    }
                    break;

                case 't':
                    if (s == "text/plain")
                    {
                        return(m_textPlain);
                    }
                    if (s == "text/html")
                    {
                        return(m_textHtml);
                    }
                    if (s == "text/xml")
                    {
                        return(m_textXml);
                    }
                    if (s == "text/plain; charset=utf-8")
                    {
                        return(m_textPlainUtf8);
                    }
                    if (s == "text/html; charset=utf-8")
                    {
                        return(m_textHtmlUtf8);
                    }
                    if (s == "text/xml; charset=utf-8")
                    {
                        return(m_textXmlUtf8);
                    }
                    break;

                case 'x':
                    if (s == "x-directory/normal")
                    {
                        return(m_dir);
                    }
                    break;
                }

                return(parse(s));
            }
            catch (ParseErr.Val e)
            {
                if (!check)
                {
                    return(null);
                }
                throw e;
            }
            catch (System.Exception)
            {
                if (!check)
                {
                    return(null);
                }
                throw ParseErr.make("MimeType", s).val;
            }
        }
Example #24
0
        public static TimeZone fromStr(string name, bool check)
        {
            // check cache first
            TimeZone tz;

            lock (cache)
            {
                tz = (TimeZone)cache[name];
                if (tz != null)
                {
                    return(tz);
                }
            }

            // try to load from database
            try
            {
                tz = loadTimeZone(name);
            }
            catch (Exception e)
            {
                Err.dumpStack(e);
                throw IOErr.make("Cannot load from timezone database: " + name).val;
            }

            // if not found, check aliases
            if (tz == null)
            {
                if (aliases == null)
                {
                    loadAliases();
                }
                string alias = (string)aliases[name];
                if (alias != null)
                {
                    tz = fromStr(alias); // better be found
                    lock (cache)
                    {
                        cache[tz.m_name]     = tz;
                        cache[tz.m_fullName] = tz;
                        return(tz);
                    }
                }
            }

            // if found, then cache and return
            if (tz != null)
            {
                lock (cache)
                {
                    cache[tz.m_name]     = tz;
                    cache[tz.m_fullName] = tz;
                    return(tz);
                }
            }

            // not found
            if (check)
            {
                throw ParseErr.make("TimeZone not found: " + name).val;
            }
            return(null);
        }
Example #25
0
        public static Duration fromStr(string s, bool check)
        {
            //   ns:   nanoseconds  (x 1)
            //   ms:   milliseconds (x 1,000,000)
            //   sec:  seconds      (x 1,000,000,000)
            //   min:  minutes      (x 60,000,000,000)
            //   hr:   hours        (x 3,600,000,000,000)
            //   day:  days         (x 86,400,000,000,000)
            try
            {
                int  len = s.Length;
                int  x1  = s[len - 1];
                int  x2  = s[len - 2];
                int  x3  = s[len - 3];
                bool dot = s.IndexOf('.') > 0;

                long mult      = -1;
                int  suffixLen = -1;
                switch (x1)
                {
                case 's':
                    if (x2 == 'n')
                    {
                        mult = 1L; suffixLen = 2;
                    }                                // ns
                    if (x2 == 'm')
                    {
                        mult = 1000000L; suffixLen = 2;
                    }                                      // ms
                    break;

                case 'c':
                    if (x2 == 'e' && x3 == 's')
                    {
                        mult = 1000000000L; suffixLen = 3;
                    }                                                      // sec
                    break;

                case 'n':
                    if (x2 == 'i' && x3 == 'm')
                    {
                        mult = 60000000000L; suffixLen = 3;
                    }                                                       // min
                    break;

                case 'r':
                    if (x2 == 'h')
                    {
                        mult = 3600000000000L; suffixLen = 2;
                    }                                            // hr
                    break;

                case 'y':
                    if (x2 == 'a' && x3 == 'd')
                    {
                        mult = 86400000000000L; suffixLen = 3;
                    }                                                          // day
                    break;
                }

                if (mult < 0)
                {
                    throw new Exception();
                }

                s = s.Substring(0, len - suffixLen);
                if (dot)
                {
                    return(make((long)(System.Double.Parse(s) * (double)mult)));
                }
                else
                {
                    return(make(Int64.Parse(s) * mult));
                }
            }
            catch (Exception)
            {
                if (!check)
                {
                    return(null);
                }
                throw ParseErr.make("Duration", s).val;
            }
        }
Example #26
0
        public static Duration fromIso(string s, bool check)
        {
            try
            {
                long      ticks = 0;
                bool      neg   = false;
                IsoParser p     = new IsoParser(s);

                // check for negative
                if (p.cur == '-')
                {
                    neg = true; p.consume();
                }
                else if (p.cur == '+')
                {
                    p.consume();
                }

                // next char must be P
                p.consume('P');
                if (p.cur == -1)
                {
                    throw new System.Exception();
                }

                // D
                int num = 0;
                if (p.cur != 'T')
                {
                    num = p.num();
                    p.consume('D');
                    ticks += num * nsPerDay;
                    if (p.cur == -1)
                    {
                        return(new Duration(ticks));
                    }
                }

                // next char must be T
                p.consume('T');
                if (p.cur == -1)
                {
                    throw new System.Exception();
                }
                num = p.num();

                // H
                if (num >= 0 && p.cur == 'H')
                {
                    p.consume();
                    ticks += num * nsPerHr;
                    num    = p.num();
                }

                // M
                if (num >= 0 && p.cur == 'M')
                {
                    p.consume();
                    ticks += num * nsPerMin;
                    num    = p.num();
                }

                // S
                if (num >= 0 && p.cur == 'S' || p.cur == '.')
                {
                    ticks += num * nsPerSec;
                    if (p.cur == '.')
                    {
                        p.consume(); ticks += p.frac();
                    }
                    p.consume('S');
                }

                // verify we parsed everything
                if (p.cur != -1)
                {
                    throw new System.Exception();
                }

                // negate if necessary and return result
                if (neg)
                {
                    ticks = -ticks;
                }
                return(new Duration(ticks));
            }
            catch (System.Exception)
            {
                if (!check)
                {
                    return(null);
                }
                throw ParseErr.make("ISO 8601 Duration", s).val;
            }
        }