forked from google/flatbuffers
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpython.cc
More file actions
79 lines (71 loc) · 2.49 KB
/
Copy pathpython.cc
File metadata and controls
79 lines (71 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "codegen/python.h"
#include <set>
#include <sstream>
#include <string>
#include <utility>
namespace flatbuffers {
namespace python {
Version::Version(const std::string& version) {
std::stringstream ss(version);
char dot;
ss >> major >> dot >> minor >> dot >> micro;
}
bool Version::IsValid() const {
return (major == 0 || major == 2 || major == 3) && minor >= 0 && micro >= 0;
}
std::set<std::string> Keywords(const Version& version) {
switch (version.major) {
case 2:
// https://docs.python.org/2/reference/lexical_analysis.html#keywords
return {
"and", "as", "assert", "break", "class", "continue", "def",
"del", "elif", "else", "except", "exec", "finally", "for",
"from", "global", "if", "import", "in", "is", "lambda",
"not", "or", "pass", "print", "raise", "return", "try",
"while", "with", "yield",
};
case 0:
case 3:
// https://docs.python.org/3/reference/lexical_analysis.html#keywords
return {
"and", "as", "assert", "async", "await", "break",
"class", "continue", "def", "del", "elif", "else",
"except", "False", "finally", "for", "from", "global",
"if", "import", "in", "is", "lambda", "None",
"nonlocal", "not", "or", "pass", "raise", "return",
"True", "try", "while", "with", "yield",
};
default:
return {};
}
}
const python::Import& python::Imports::Import(const std::string& module) {
python::Import import;
import.module = module;
imports.push_back(std::move(import));
return imports.back();
}
const python::Import& python::Imports::Import(const std::string& module,
const std::string& name) {
python::Import import;
import.module = module;
import.name = name;
imports.push_back(std::move(import));
return imports.back();
}
const python::Import& python::Imports::Export(const std::string& module) {
python::Import import;
import.module = module;
exports.push_back(std::move(import));
return exports.back();
}
const python::Import& python::Imports::Export(const std::string& module,
const std::string& name) {
python::Import import;
import.module = module;
import.name = name;
exports.push_back(std::move(import));
return exports.back();
}
} // namespace python
} // namespace flatbuffers