Exemplo n.º 1
0
Arquivo: Atom.cs Projeto: rcarz/fusion
        /// <summary>
        /// Converts the given strings into package atoms.
        /// </summary>
        /// <param name="atoms">an array of strings to parse</param>
        /// <param name="opts">parsing options</param>
        /// <returns>an array of atoms</returns>
        public static Atom[] ParseAll(string[] atoms, AtomParseOptions opts)
        {
            List<Atom> results = new List<Atom>();

            foreach (string a in atoms)
                results.Add(Atom.Parse(a, opts));

            return results.ToArray();
        }
Exemplo n.º 2
0
Arquivo: Atom.cs Projeto: rcarz/fusion
        /// <summary>
        /// Converts a string into a package atom.
        /// </summary>
        /// <param name="atom">the string to parse</param>
        /// <param name="opts">parsing options</param>
        /// <returns>an atom</returns>
        public static Atom Parse(string atom, AtomParseOptions opts)
        {
            Match m;
            uint slot = 0;
            string atomstr;

            /* full package atom with implicit equals: =(category/package)-(version):(slot) */
            atomstr = String.Format(
                "^(?:=?)({0}/{1})-({2})(?:[:]({3}))?$",
                CATEGORY_NAME_FMT,
                PACKAGE_NAME_FMT,
                PackageVersion.VERSION_FMT,
                SLOT_FMT);
            m = Regex.Match(atom.ToLower(), atomstr);
            if (opts != AtomParseOptions.WithoutVersion && m.Success) {
                UInt32.TryParse(m.Groups[3].Value, out slot);
                return new Atom("=", m.Groups[1].Value, m.Groups[2].Value, slot);
            }

            /* full package name with slot and implicit equals: =(category/package):(slot) */
            atomstr = String.Format(
                "^(?:=?)({0}/{1})[:]({2})$",
                CATEGORY_NAME_FMT,
                PACKAGE_NAME_FMT,
                SLOT_FMT);
            m = Regex.Match(atom.ToLower(), atomstr);
            if (opts != AtomParseOptions.VersionRequired && m.Success) {
                UInt32.TryParse(m.Groups[2].Value, out slot);
                return new Atom("=", m.Groups[1].Value, "", slot);
            }

            /* package short name with version and implicit equals: =(package)-(version):(slot) */
            atomstr = String.Format(
                "^(?:=?)({0})-({1})(?:[:]({2}))?$",
                PACKAGE_NAME_FMT,
                PackageVersion.VERSION_FMT,
                SLOT_FMT);
            m = Regex.Match(atom.ToLower(), atomstr);
            if (opts != AtomParseOptions.WithoutVersion && m.Success) {
                UInt32.TryParse(m.Groups[3].Value, out slot);
                return new Atom("=", m.Groups[1].Value, m.Groups[2].Value, slot);
            }

            /* package short name with slot and implicit equals: =(package):(slot) */
            atomstr = String.Format(
                "^(?:=?)({0})[:]({1})$",
                PACKAGE_NAME_FMT,
                SLOT_FMT);
            m = Regex.Match(atom.ToLower(), atomstr);
            if (opts != AtomParseOptions.VersionRequired && m.Success) {
                UInt32.TryParse(m.Groups[2].Value, out slot);
                return new Atom("=", m.Groups[1].Value, "", slot);
            }

            /* full package atom: (operator)(category/package)-(version):(slot) */
            atomstr = String.Format(
                "^({0})({1}/{2})-({3})(?:[:]({4}))?$",
                CMP_OPERATOR_FMT,
                CATEGORY_NAME_FMT,
                PACKAGE_NAME_FMT,
                PackageVersion.VERSION_FMT,
                SLOT_FMT);
            m = Regex.Match(atom.ToLower(), atomstr);
            if ((opts == AtomParseOptions.VersionOptional || opts == AtomParseOptions.VersionRequired) && m.Success) {
                UInt32.TryParse(m.Groups[5].Value, out slot);
                return new Atom(m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value, slot);
            }

            /* fully-qualified package name: (category/package) */
            m = Regex.Match(atom.ToLower(), "^(" + CATEGORY_NAME_FMT + "/" + PACKAGE_NAME_FMT + ")$");
            if ((opts == AtomParseOptions.WithoutVersion || opts == AtomParseOptions.VersionOptional) && m.Success)
                return new Atom(m.Groups[1].Value);

            /* package short name: (package) */
            m = Regex.Match(atom.ToLower(), "^(" + PACKAGE_NAME_FMT + ")$");
            if ((opts == AtomParseOptions.WithoutVersion || opts == AtomParseOptions.VersionOptional) && m.Success)
                return new Atom(m.Groups[1].Value);

            throw new BadAtomException(atom);
        }