1 #include <torch/csrc/jit/script/lexer.h> 3 #include <c10/util/Exception.h> 7 #include <unordered_map> 13 static const std::unordered_map<int, int> binary_prec = {
39 static const std::unordered_map<int, int> unary_prec = {
45 bool SharedParserData::isUnary(
int kind,
int* prec) {
46 auto it = unary_prec.find(kind);
47 if (it != unary_prec.end()) {
53 bool SharedParserData::isBinary(
int kind,
int* prec) {
54 auto it = binary_prec.find(kind);
55 if (it != binary_prec.end()) {
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)
75 return str_to_kind.at(str);
76 }
catch (std::out_of_range& err) {
77 throw std::out_of_range(
"unknown token in stringToKind");
81 std::string kindToString(
int kind) {
83 return std::string(1, kind);
85 #define DEFINE_CASE(tok, str, _) \ 88 TC_FORALL_TOKEN_KINDS(DEFINE_CASE)
91 throw std::runtime_error(
"Unknown kind: " + std::to_string(kind));
95 SharedParserData& sharedParserData() {
96 static SharedParserData data;