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

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: Fixed linker errors on Win. 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 "chrome/tools/profile_reset/jtl_compiler.h"
20
21 namespace {
22
23 // Command-line argument name: path to the input text-based JTL source code.
24 const char kInputPath[] = "input";
25
26 // Command-line argument name: path to the output byte-code.
27 const char kOutputPath[] = "output";
28
29 // Command-line argument name: the hash seed to use.
30 const char kHashSeed[] = "hash-seed";
31
32 // Error codes.
33 const char kMismatchedDoubleQuotes[] = "Mismatched double-quotes before EOL.";
34 const char kParsingError[] = "Parsing error. Input is ill-formed.";
35 const char kArgumentCountError[] = "Wrong number of arguments for operation.";
36 const char kArgumentTypeError[] = "Wrong argument type(s) 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::MISMATCHED_DOUBLE_QUOTES:
43 return kMismatchedDoubleQuotes;
44 case JtlCompiler::CompileError::PARSING_ERROR:
45 return kParsingError;
46 case JtlCompiler::CompileError::INVALID_ARGUMENT_COUNT:
47 return kArgumentCountError;
48 case JtlCompiler::CompileError::INVALID_ARGUMENT_TYPE:
49 return kArgumentTypeError;
50 case JtlCompiler::CompileError::INVALID_OPERATION_NAME:
51 return kUnknownOperationError;
52 default:
53 return kUnknownError;
54 }
55 }
56
57 } // namespace
58
59 int main(int argc, char* argv[]) {
60 CommandLine::Init(argc, argv);
61 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
62 if (!cmd_line->HasSwitch(kInputPath) || !cmd_line->HasSwitch(kHashSeed) ||
63 !cmd_line->HasSwitch(kOutputPath)) {
64 std::cerr << "Usage: " << argv[0] << " <required switches>" << std::endl;
65 std::cerr << "\nRequired switches are:" << std::endl;
66 std::cerr << " --" << kInputPath << "=<file>"
67 << "\t\tPath to the input text-based JTL source code."
68 << std::endl;
69 std::cerr << " --" << kOutputPath << "=<file>"
70 << "\t\tPath to the output byte-code." << std::endl;
71 std::cerr << " --" << kHashSeed << "=<value>"
72 << "\t\tThe hash seed to use." << std::endl;
73 return -1;
74 }
75
76 base::FilePath source_code_path =
77 MakeAbsoluteFilePath(cmd_line->GetSwitchValuePath(kInputPath));
78 std::string source_code;
79 if (!base::ReadFileToString(source_code_path, &source_code)) {
80 std::cerr << "ERROR: Cannot read input file." << std::endl;
81 return -3;
82 }
83
84 std::string bytecode;
85 JtlCompiler::CompileError error;
86 std::string hash_seed = cmd_line->GetSwitchValueASCII(kHashSeed);
87 if (!JtlCompiler::Compile(source_code, hash_seed, &bytecode, &error)) {
88 std::cerr << "COMPILE ERROR: " << ResolveErrorCode(error.error_code)
89 << std::endl;
90 std::cerr << " Line number: " << (error.line_number + 1) << std::endl;
91 std::cerr << " Context: " << (error.context.size() > 63
92 ? error.context.substr(0, 60) + "..."
93 : error.context) << std::endl;
94 return -2;
95 }
96
97 base::FilePath bytecode_path =
98 MakeAbsoluteFilePath(cmd_line->GetSwitchValuePath(kOutputPath));
99 int bytes_written =
100 file_util::WriteFile(cmd_line->GetSwitchValuePath(kOutputPath),
101 bytecode.data(),
102 static_cast<int>(bytecode.size()));
103 if (bytes_written != static_cast<int>(bytecode.size())) {
104 std::cerr << "ERROR: Cannot write output file." << std::endl;
105 return -3;
106 }
107
108 return 0;
109 }
OLDNEW
« no previous file with comments | « chrome/tools/profile_reset/jtl_compiler.gyp ('k') | chrome/tools/profile_reset/jtl_compiler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698