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#ifndef CHROME_TOOLS_PROFILE_RESET_JTL_COMPILER_H_
6#define CHROME_TOOLS_PROFILE_RESET_JTL_COMPILER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11
12// Compiles text-based JTL source code into JTL byte-code.
13//
14// For an overview of JTL (JSON Traversal Language), and the exhaustive list of
15// instructions, please see "chrome/browser/profile_resetter/jtl_foundation.h".
16//
17// The text-based JTL syntax itself much resembles C/C++. A program consists of
18// zero or more sentences. Each sentence is terminated by a semi-colon (;), and
19// is composed of *one* or more operations, separated by periods (.).
20//
21// Each operation resembles a C/C++ function call and consists of an instruction
22// name, and an optional argument list, which takes Boolean values and/or string
23// literals. The text-based instruction names are defined in "jtl_compiler.cc".
24//
25// Whitespace does not matter, except for inside string literals. C++-style,
26// double-slash-introduced comments are also supported.
27//
28// Example source code:
29//
30//   // Store "x"=true if path "foo.bar" is found.
31//   go("foo").go("bar").store_bool("x", true);
32//
33//   // Store "y"="1" if the above value is set.
34//   compare_stored_bool("x", true, false).store_hash("y", "1");
35//
36class JtlCompiler {
37 public:
38  struct CompileError {
39    enum ErrorCode {
40      ERROR_NONE,
41      MISMATCHED_DOUBLE_QUOTES,
42      PARSING_ERROR,
43      INVALID_OPERATION_NAME,
44      INVALID_ARGUMENT_COUNT,
45      INVALID_ARGUMENT_TYPE,
46      INVALID_ARGUMENT_VALUE
47    };
48
49    CompileError() : line_number(0), error_code(ERROR_NONE) {}
50    CompileError(size_t line_number,
51                 const std::string& context,
52                 ErrorCode error_code)
53        : line_number(line_number), context(context), error_code(error_code) {}
54
55    size_t line_number;  // 0-based.
56    std::string context;
57    ErrorCode error_code;
58  };
59
60  // Compiles text-based JTL source code contained in |source_code| into JTL
61  // byte-code to |output_bytecode|. Variable, node names, and string literals
62  // will be hashed using the seed in |hash_seed|.
63  // On success, returns true. Otherwise, returns false and fills |error| with
64  // more information (if it is non-NULL).
65  static bool Compile(const std::string& source_code,
66                      const std::string& hash_seed,
67                      std::string* output_bytecode,
68                      CompileError* error);
69
70 private:
71  DISALLOW_IMPLICIT_CONSTRUCTORS(JtlCompiler);
72};
73
74#endif  // CHROME_TOOLS_PROFILE_RESET_JTL_COMPILER_H_
75