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

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: Addressed comments, added some unittests. 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 kMismatchedDoubleQuotes[] = "Mismatched double-quotes before EOL.";
35 const char kParsingError[] = "Parsing error. Input is ill-formed.";
36 const char kArgumentCountError[] = "Wrong argument type(s) for operation.";
37 const char kArgumentTypeError[] = "Wrong number of arguments for operation.";
38 const char kUnknownOperationError[] = "No operation by this name.";
39 const char kUnknownError[] = "Unknown error.";
40
41 const char* ResolveErrorCode(JtlCompiler::CompileError::ErrorCode code) {
42 switch (code) {
43 case JtlCompiler::CompileError::MISMATCHED_DOUBLE_QUOTES:
44 return kMismatchedDoubleQuotes;
45 case JtlCompiler::CompileError::PARSING_ERROR:
46 return kParsingError;
47 case JtlCompiler::CompileError::INVALID_ARGUMENT_COUNT:
48 return kArgumentCountError;
49 case JtlCompiler::CompileError::INVALID_ARGUMENT_TYPE:
50 return kArgumentTypeError;
51 case JtlCompiler::CompileError::INVALID_OPERATION_NAME:
52 return kUnknownOperationError;
53 default:
54 return kUnknownError;
55 }
56 }
57
58 } // namespace
59
60 int main(int argc, char* argv[]) {
61 CommandLine::Init(argc, argv);
62 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
63 if (!cmd_line->HasSwitch(kInputPath) || !cmd_line->HasSwitch(kHashSeed) ||
64 !cmd_line->HasSwitch(kOutputPath)) {
65 std::cerr << "Usage: " << argv[0] << " <options>" << std::endl;
66 std::cerr << "\nOptions are:" << std::endl;
67 std::cerr << " --" << kInputPath << "=<file>"
68 << "\t\tPath to the input text-based JTL source code."
69 << std::endl;
70 std::cerr << " --" << kOutputPath << "=<file>"
71 << "\t\tPath to the output byte-code." << std::endl;
72 std::cerr << " --" << kHashSeed << "=<value>"
73 << "\t\tThe hash seed to use." << std::endl;
74 return -1;
75 }
76
77 base::FilePath source_code_path =
78 MakeAbsoluteFilePath(cmd_line->GetSwitchValuePath(kInputPath));
79 std::string source_code;
80 if (!base::ReadFileToString(source_code_path, &source_code)) {
81 LOG(ERROR) << "Error reading input file.";
82 return -3;
83 }
84
85 std::string bytecode;
86 JtlCompiler::CompileError error;
87 std::string hash_seed = cmd_line->GetSwitchValueASCII(kHashSeed);
88 if (!JtlCompiler::Compile(source_code, hash_seed, &bytecode, &error)) {
89 std::cerr << "Compile error. " << ResolveErrorCode(error.error_code)
90 << std::endl;
91 std::cerr << " Line number:" << (error.line_number + 1) << std::endl;
92 std::cerr << " Context:" << (error.context.size() > 63
93 ? error.context.substr(0, 60) + "..."
94 : error.context) << std::endl;
95 return -2;
96 }
97
98 base::FilePath bytecode_path =
99 MakeAbsoluteFilePath(cmd_line->GetSwitchValuePath(kOutputPath));
100 int bytes_written =
101 file_util::WriteFile(cmd_line->GetSwitchValuePath(kOutputPath),
102 bytecode.data(),
103 bytecode.size());
104 if (bytes_written != static_cast<int>(bytecode.size())) {
105 LOG(ERROR) << "Error writing output file.";
106 return -3;
107 }
108
109 return 0;
110 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698