Caffe2 - C++ API
A deep learning, cross platform ML framework
lexer.cpp
1 #include <torch/csrc/jit/script/lexer.h>
2 
3 #include <c10/util/Exception.h>
4 
5 #include <mutex>
6 #include <string>
7 #include <unordered_map>
8 
9 namespace torch {
10 namespace jit {
11 namespace script {
12 
13 static const std::unordered_map<int, int> binary_prec = {
14  {TK_IF, 1},
15  {TK_AND, 2},
16  {TK_OR, 2},
17  // reserve a level for unary not
18  {'<', 4},
19  {'>', 4},
20  {TK_IS, 4},
21  {TK_ISNOT, 4},
22  {TK_EQ, 4},
23  {TK_LE, 4},
24  {TK_GE, 4},
25  {TK_NE, 4},
26  {'|', 5},
27  {'^', 6},
28  {'&', 7},
29  {'+', 8},
30  {'-', 8},
31  {'*', 9},
32  {'/', 9},
33  {TK_FLOOR_DIV, 9},
34  {'%', 9},
35  {'@', 9},
36  {TK_POW, 10},
37 };
38 
39 static const std::unordered_map<int, int> unary_prec = {
40  {TK_NOT, 3},
41  {'-', 9},
42  {'*', 9},
43 };
44 
45 bool SharedParserData::isUnary(int kind, int* prec) {
46  auto it = unary_prec.find(kind);
47  if (it != unary_prec.end()) {
48  *prec = it->second;
49  return true;
50  }
51  return false;
52 }
53 bool SharedParserData::isBinary(int kind, int* prec) {
54  auto it = binary_prec.find(kind);
55  if (it != binary_prec.end()) {
56  *prec = it->second;
57  return true;
58  }
59  return false;
60 }
61 
62 int stringToKind(const std::string& str) {
63  static std::once_flag init_flag;
64  static std::unordered_map<std::string, int> str_to_kind;
65  std::call_once(init_flag, []() {
66  for (char tok : std::string(valid_single_char_tokens))
67  str_to_kind[std::string(1, tok)] = tok;
68 #define DEFINE_CASE(tok, _, str) \
69  if (std::string(str) != "") \
70  str_to_kind[str] = tok;
71  TC_FORALL_TOKEN_KINDS(DEFINE_CASE)
72 #undef DEFINE_CASE
73  });
74  try {
75  return str_to_kind.at(str);
76  } catch (std::out_of_range& err) {
77  throw std::out_of_range("unknown token in stringToKind");
78  }
79 }
80 
81 std::string kindToString(int kind) {
82  if (kind < 256)
83  return std::string(1, kind);
84  switch (kind) {
85 #define DEFINE_CASE(tok, str, _) \
86  case tok: \
87  return str;
88  TC_FORALL_TOKEN_KINDS(DEFINE_CASE)
89 #undef DEFINE_CASE
90  default:
91  throw std::runtime_error("Unknown kind: " + std::to_string(kind));
92  }
93 }
94 
95 SharedParserData& sharedParserData() {
96  static SharedParserData data; // safely handles multi-threaded init
97  return data;
98 }
99 } // namespace script
100 } // namespace jit
101 } // namespace torch
Definition: jit_type.h:17