summaryrefslogtreecommitdiffstats
path: root/edify/edify_parser.cpp
blob: 15342d3faf88d16ca535d642e4e54cabc34defee (plain) (blame)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * This is a host-side tool for validating a given edify script file.
 *
 * We used to have edify test cases here, which have been moved to
 * tests/component/edify_test.cpp.
 *
 * Caveat: It doesn't recognize functions defined through updater, which
 * makes the tool less useful. We should either extend the tool or remove it.
 */

#include <errno.h>
#include <stdio.h>

#include <string>

#include <android-base/file.h>

#include "expr.h"

static void ExprDump(int depth, const Expr* n, const std::string& script) {
    printf("%*s", depth*2, "");
    printf("%s %p (%d-%d) \"%s\"\n",
           n->name == NULL ? "(NULL)" : n->name, n->fn, n->start, n->end,
           script.substr(n->start, n->end - n->start).c_str());
    for (int i = 0; i < n->argc; ++i) {
        ExprDump(depth+1, n->argv[i], script);
    }
}

int main(int argc, char** argv) {
    RegisterBuiltins();
    FinishRegistration();

    if (argc != 2) {
        printf("Usage: %s <edify script>\n", argv[0]);
        return 1;
    }

    std::string buffer;
    if (!android::base::ReadFileToString(argv[1], &buffer)) {
        printf("%s: failed to read %s: %s\n", argv[0], argv[1], strerror(errno));
        return 1;
    }

    Expr* root;
    int error_count = 0;
    int error = parse_string(buffer.data(), &root, &error_count);
    printf("parse returned %d; %d errors encountered\n", error, error_count);
    if (error == 0 || error_count > 0) {

        ExprDump(0, root, buffer);

        State state(buffer, nullptr);
        char* result = Evaluate(&state, root);
        if (result == NULL) {
            printf("result was NULL, message is: %s\n",
                   (state.errmsg.empty() ? "(NULL)" : state.errmsg.c_str()));
        } else {
            printf("result is [%s]\n", result);
        }
    }
    return 0;
}