summaryrefslogtreecommitdiffstats
path: root/edify/edify_parser.cpp
diff options
context:
space:
mode:
authorTao Bao <tbao@google.com>2016-10-04 19:06:09 +0200
committerandroid-build-merger <android-build-merger@google.com>2016-10-04 19:06:09 +0200
commit254c4bfbd42d2c78dd1c26119affc5813e6d4fa2 (patch)
treed44fa9da1aea4aafb4d544d66426426539371e3e /edify/edify_parser.cpp
parentMerge "Turn on -Werror for recovery" into nyc-mr1-dev-plus-aosp (diff)
parentMerge "edify: Move the testcases to gtest." am: 0bc2df1696 am: 6fbed10fa5 (diff)
downloadandroid_bootable_recovery-254c4bfbd42d2c78dd1c26119affc5813e6d4fa2.tar
android_bootable_recovery-254c4bfbd42d2c78dd1c26119affc5813e6d4fa2.tar.gz
android_bootable_recovery-254c4bfbd42d2c78dd1c26119affc5813e6d4fa2.tar.bz2
android_bootable_recovery-254c4bfbd42d2c78dd1c26119affc5813e6d4fa2.tar.lz
android_bootable_recovery-254c4bfbd42d2c78dd1c26119affc5813e6d4fa2.tar.xz
android_bootable_recovery-254c4bfbd42d2c78dd1c26119affc5813e6d4fa2.tar.zst
android_bootable_recovery-254c4bfbd42d2c78dd1c26119affc5813e6d4fa2.zip
Diffstat (limited to 'edify/edify_parser.cpp')
-rw-r--r--edify/edify_parser.cpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/edify/edify_parser.cpp b/edify/edify_parser.cpp
new file mode 100644
index 000000000..16ad2ab71
--- /dev/null
+++ b/edify/edify_parser.cpp
@@ -0,0 +1,89 @@
+/*
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "expr.h"
+#include "parser.h"
+
+void ExprDump(int depth, Expr* n, char* script) {
+ printf("%*s", depth*2, "");
+ char temp = script[n->end];
+ script[n->end] = '\0';
+ printf("%s %p (%d-%d) \"%s\"\n",
+ n->name == NULL ? "(NULL)" : n->name, n->fn, n->start, n->end,
+ script+n->start);
+ script[n->end] = temp;
+ 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;
+ }
+
+ FILE* f = fopen(argv[1], "r");
+ if (f == NULL) {
+ printf("%s: %s: No such file or directory\n", argv[0], argv[1]);
+ return 1;
+ }
+ char buffer[8192];
+ int size = fread(buffer, 1, 8191, f);
+ fclose(f);
+ buffer[size] = '\0';
+
+ Expr* root;
+ int error_count = 0;
+ int error = parse_string(buffer, &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;
+ state.cookie = NULL;
+ state.script = buffer;
+ state.errmsg = NULL;
+
+ char* result = Evaluate(&state, root);
+ if (result == NULL) {
+ printf("result was NULL, message is: %s\n",
+ (state.errmsg == NULL ? "(NULL)" : state.errmsg));
+ free(state.errmsg);
+ } else {
+ printf("result is [%s]\n", result);
+ }
+ }
+ return 0;
+}