Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(484)

Side by Side Diff: chrome/tools/profile_reset/jtl_compiler_frontend.cc

Issue 24998003: Compiler for the JSON Traversal Language. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // A simple command-line compiler for JTL (JSON Traversal Language).
6 //
7 // Translates rules from a text-based, human-readable format to an easy-to-parse
8 // byte-code format, which then can be interpreted by JtlInterpreter.
9 //
10 // Example usage:
11 // jtl_compiler --input=blah.txt --hash-seed="foobar" --output=blah.dat
12
13 #include <iostream>
14 #include <string>
15
16 #include "base/command_line.h"
17 #include "base/file_util.h"
18 #include "base/files/file_path.h"
19 #include "base/logging.h"
20 #include "chrome/tools/profile_reset/jtl_compiler.h"
21
22 namespace {
23
24 // Command-line argument name: path to the input text-based JTL source code.
25 const char kInputPath[] = "input";
26
27 // Command-line argument name: path to the output byte-code.
28 const char kOutputPath[] = "output";
29
30 // Command-line argument name: the hash seed to use.
31 const char kHashSeed[] = "hash-seed";
32
33 // Error codes.
34 const char kParsingError[] = "Parsing error. Input is ill-formed.";
35 const char kArgumentCountError[] = "Wrong argument type(s) for operation.";
36 const char kArgumentTypeError[] = "Wrong number of arguments for operation.";
37 const char kUnknownOperationError[] = "No operation by this name.";
38 const char kUnknownError[] = "Unknown error.";
39
40 const char* ResolveErrorCode(JtlCompiler::CompileError::ErrorCode code) {
41 switch (code) {
42 case JtlCompiler::CompileError::PARSING_ERROR:
43 return kParsingError;
44 case JtlCompiler::CompileError::INVALID_ARGUMENT_COUNT:
45 return kArgumentCountError;
46 case JtlCompiler::CompileError::INVALID_ARGUMENT_TYPE:
47 return kArgumentTypeError;
48 case JtlCompiler::CompileError::UNKNOWN_OPERATION:
49 return kUnknownOperationError;
50 default:
51 return kUnknownError;
52 }
53 }
54
55 } // namespace
56
57 int main(int argc, char* argv[]) {
58 CommandLine::Init(argc, argv);
59 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
60 if (!cmd_line->HasSwitch(kInputPath) || !cmd_line->HasSwitch(kHashSeed) ||
61 !cmd_line->HasSwitch(kOutputPath)) {
62 std::cerr << "Usage: " << argv[0] << " <options>" << std::endl;
63 std::cerr << "\nOptions are:" << std::endl;
64 std::cerr << " --" << kInputPath << "=<file>"
65 << "\t\tPath to the input text-based JTL source code."
66 << std::endl;
67 std::cerr << " --" << kOutputPath << "=<file>"
68 << "\t\tPath to the output byte-code." << std::endl;
69 std::cerr << " --" << kHashSeed << "=<value>"
70 << "\t\tThe hash seed to use." << std::endl;
71 return -1;
72 }
73
74 base::FilePath source_code_path =
75 MakeAbsoluteFilePath(cmd_line->GetSwitchValuePath(kInputPath));
76 std::string source_code;
77 if (!base::ReadFileToString(source_code_path, &source_code)) {
78 LOG(ERROR) << "Error reading input file.";
79 return -3;
80 }
81
82 std::string bytecode;
83 JtlCompiler::CompileError error;
84 std::string hash_seed = cmd_line->GetSwitchValueASCII(kHashSeed);
85 if (!JtlCompiler::Compile(source_code, hash_seed, &bytecode, &error)) {
86 std::cerr << "Compile error. " << ResolveErrorCode(error.error_code)
87 << std::endl;
88 std::cerr << " Line number:" << error.line_number << std::endl;
89 std::cerr << " Context:" << (error.context.size() > 63
90 ? error.context.substr(0, 60) + "..."
91 : error.context) << std::endl;
92 return -2;
93 }
94
95 base::FilePath bytecode_path =
96 MakeAbsoluteFilePath(cmd_line->GetSwitchValuePath(kOutputPath));
97 int bytes_written =
98 file_util::WriteFile(cmd_line->GetSwitchValuePath(kOutputPath),
99 bytecode.data(),
100 bytecode.size());
101 if (bytes_written != static_cast<int>(bytecode.size())) {
102 LOG(ERROR) << "Error writing output file.";
103 return -3;
104 }
105
106 return 0;
107 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698