aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/cran/cran_dep_parser.cc
blob: 401e2bc8d08f05b15abd8c7a42a71513ce502b6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <paludis/dep_spec.hh>
#include <paludis/repositories/cran/cran_dep_parser.hh>
#include <paludis/repositories/cran/cran_description.hh>
#include <paludis/util/strip.hh>
#include <paludis/util/tokeniser.hh>

#include <string>
#include <list>

using namespace paludis;

std::tr1::shared_ptr<const CompositeDepSpec>
CRANDepParser::parse(const std::string & s)
{
    Context context("When parsing CRAN 'Depends:' string: '" + s + "':");

    std::tr1::shared_ptr<CompositeDepSpec> result(new AllDepSpec);
    Tokeniser<delim_kind::AnyOfTag, delim_mode::DelimiterTag> spec_tokeniser(",");

    std::list<std::string> specs;
    spec_tokeniser.tokenise(s, std::back_inserter(specs));
    std::list<std::string>::const_iterator a(specs.begin()), a_end(specs.end());
    for ( ; a != a_end ; ++a)
    {
        Context local_context("When processing token '" + *a + "':");

        std::string aa = strip_leading(strip_trailing(*a, ")"), " \t");

        std::string name, tmp, version, range;
        std::string::size_type p(aa.find('('));
        if ((std::string::npos != p))
        {
            name = strip_leading(strip_trailing(aa.substr(0, p), " \t"), " \t");
            tmp = aa.substr(p + 1);
            p = tmp.find(')');
            aa = tmp.substr(0, p);
            version = strip_trailing(strip_leading(aa, " \t(<>=~"), " )\t\n");
            range = strip_trailing(strip_leading(aa.substr(0, aa.find(version)), " \t"), " \t\n");
        }
        else
            name = strip_leading(strip_trailing(aa, " \t"), " \t");

        CRANDescription::normalise_name(name);
        CRANDescription::normalise_version(version);

        if ("R" == name)
            name = "dev-lang/R";
        else
            name = "cran/" + name;

        std::string spec_string;
        if (version.empty() || range.empty())
            spec_string = name;
        else
            spec_string = range + name + "-" + version;
        std::tr1::shared_ptr<PackageDepSpec> spec(new PackageDepSpec(spec_string));
        result->add_child(spec);
    }

    return result;
}