37#define DEBUG_PRINTF(debug_level, ...) \
39 if (debugSDNA > debug_level) { \
40 printf(__VA_ARGS__); \
51 static const char *files[] = {
52#include "dna_includes_as_strings.h"
59 std::ifstream file(filepath, std::ios::binary | std::ios::ate);
63 const std::streamsize size = file.tellg();
67 file.seekg(0, std::ios::beg);
69 return file.read(
data.data(), size) ?
true :
false;
96 tokens_.append(std::move(token));
101 return pos_ >= tokens_.size();
106 return (pos_ + offset < tokens_.size()) ? tokens_[pos_ + offset].kind :
TOKEN_END;
111 return tokens_[pos_ + offset];
116 return tokens_[pos_++];
126 if (this->
kind() == k) {
151 if (expr[0] ==
' ') {
177 return (c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z') || c ==
'_';
192 const char *cur = source.
begin();
193 const char *end = source.
end();
199 if (
ELEM(c,
' ',
'\t',
'\n',
'\r')) {
205 if (c ==
'/' && cur + 1 < end && cur[1] ==
'/') {
207 while (cur < end && *cur !=
'\n') {
214 if (c ==
'/' && cur + 1 < end && cur[1] ==
'*') {
216 while (cur + 1 < end && !(cur[0] ==
'*' && cur[1] ==
'/')) {
228 while (cur < end && *cur !=
'"') {
229 if (*cur ==
'\\' && cur + 1 < end) {
242 const char *
begin = cur;
246 while (cur + 1 < end && cur[0] ==
':' && cur[1] ==
':') {
258 if (c >=
'0' && c <=
'9') {
259 const char *
begin = cur;
260 while (cur < end && *cur >=
'0' && *cur <=
'9') {
279 while (!stream.
at_end()) {
294 while (!stream.
at_end() && stream.
kind() !=
')') {
304 return stripped_stream;
318 "File '%s' contains struct we can't parse \"%s\"\n",
323 if (
ELEM(type_name,
"long",
"ulong")) {
325 "File '%s' contains use of \"%s\" in DNA struct which is not allowed\n",
341 if (!name_strip.
is_empty() && name_strip[0] ==
'_' && !name_strip.
startswith(
"_pad")) {
343 "File '%s': only '_pad' variables can start with an underscore, found '%s'\n",
351 if (!rest.
is_empty() && rest[0] >=
'a' && rest[0] <=
'z') {
355 bool has_only_digit_or_none =
true;
356 for (
char c : rest) {
357 if (!((c >=
'0' && c <=
'9') || c ==
'_')) {
358 has_only_digit_or_none =
false;
362 if (has_only_digit_or_none) {
364 "File '%s': padding variables must be formatted '_pad[number]', found '%s'\n",
385 while (!stream.
at_end() && depth > 0) {
390 else if (k == close) {
400 while (!stream.
at_end()) {
402 if (depth == 0 &&
ELEM(k,
',',
';')) {
405 if (k ==
'{' || k ==
'(') {
408 else if (k ==
'}' || k ==
')') {
418 while (!stream.
at_end() && stream.
kind() ==
'[') {
422 while (!stream.
at_end() && depth > 0) {
428 else if (t.
kind ==
']') {
443 while (!stream.
at_end() && depth > 0) {
449 else if (t.
kind ==
'>') {
476 if (!stream.
at_end() && stream.
kind() ==
'(') {
480 while (!stream.
at_end() && stream.
kind() ==
'[') {
499 if (!stream.
at_end() && stream.
kind() ==
'(') {
517 while (!stream.
at_end() && stream.
kind() !=
'}') {
526 "File '%s' contains a member declaration that can't be parsed\n",
540 DEBUG_PRINTF(1,
"\t|\t|\tfound type %s (", type_name.c_str());
544 std::string member_name;
547 "File '%s' contains a member declaration that can't be parsed\n",
557 r_struct.
members.append({.type_name = type_name, .member_name = member_name});
567 "File '%s' contains a member declaration that can't be parsed\n",
576 fprintf(stderr,
"File '%s' contains a struct without a closing brace\n", filepath.
c_str());
586 while (!stream.
at_end() && depth > 0) {
610 std::string underlying_type;
614 "File '%s' contains enum '%s' with unparseable underlying type\n",
623 if (stream.
at_end() || stream.
kind() !=
'{') {
630 r_enums.
append({.type_name = std::move(name), .underlying_type = std::move(underlying_type)});
642 fprintf(stderr,
"Can't read file %s\n", filepath.
c_str());
649 bool skip_next_struct =
false;
651 while (!stream.
at_end()) {
653 if (stream.
kind() ==
'#' && stream.
kind(1) ==
'#') {
656 skip_next_struct =
true;
679 if (skip_next_struct) {
680 skip_next_struct =
false;
685 const std::string struct_name(name);
698 r_structs.
append(std::move(parsed_struct));
709 for (
const char *
filename : include_files) {
710 const std::string path = std::string(base_directory) +
filename;
771 if (mapping ==
nullptr || member.
member_name.empty()) {
798 if (found ==
nullptr) {
804 "Struct '%s' member '%s' uses enum '%s' without an explicit underlying type\n",
817 static const std::pair<const char *, const char *> integer_type_aliases[] = {
818 {
"uint8_t",
"uchar"},
819 {
"int16_t",
"short"},
820 {
"uint16_t",
"ushort"},
825 for (
const auto &[alias, sdna_name] : integer_type_aliases) {
840 enum_map.
add(parsed_enum.type_name, &parsed_enum);
const Value * lookup_ptr(const Key &key) const
bool add(const Key &key, const Value &value)
constexpr const char * c_str() const
constexpr StringRef drop_prefix(int64_t n) const
void append(const T &value)
constexpr const char * end() const
constexpr bool startswith(StringRef prefix) const
constexpr const char * begin() const
constexpr bool is_empty() const
constexpr int64_t size() const
const Token & peek(int64_t offset=0) const
void append(Token &&token)
bool consume_expression(StringRef expr)
TokenKind kind(int64_t offset=0) const
bool consume(TokenKind k)
bool consume_keyword(StringRef kw)
#define DEBUG_PRINTF(debug_level,...)
VecBase< int32_t, 3 > int3
VecBase< float, 2 > float2
VecBase< int32_t, 2 > int2
VecBase< float, 3 > float3
VecBase< int32_t, 4 > int4
VecBase< float, 4 > float4
bool substitute_cpp_types(Vector< ParsedStruct > &structs, const Span< ParsedEnum > enums, bool)
static bool parse_struct_body(TokenStream &stream, const StringRefNull filepath, ParsedStruct &r_struct)
constexpr TokenKind TOKEN_NUMBER
static bool parse_member_declaration(TokenStream &stream, std::string &out)
static void substitute_vector_or_matrix(ParsedMember &member)
static void skip_initializer(TokenStream &stream)
static bool parse_paren_declaration(TokenStream &stream, std::string &out)
static TokenStream strip_ignored_tokens(TokenStream stream)
static bool substitute_enum(const ParsedStruct &parsed_struct, ParsedMember &member, const Map< StringRef, const ParsedEnum * > &enum_map)
static TokenStream tokenize_dna_header(StringRef source)
static void append_template_suffix(TokenStream &stream, std::string &out)
static void substitute_sdna_integer_type(ParsedMember &member)
static bool is_valid_type_name(const StringRefNull type_name, const StringRefNull filepath)
static bool read_file_data(const StringRefNull filepath, Vector< char > &data)
static void skip_struct_body(TokenStream &stream)
static bool is_identifier_continuation(char c)
constexpr TokenKind TOKEN_END
static bool parse_enum_declaration(TokenStream &stream, const StringRefNull filepath, Vector< ParsedEnum > &r_enums)
static bool is_identifier_start(char c)
bool parse_dna_headers(const StringRefNull base_directory, Vector< ParsedStruct > &r_structs, Vector< ParsedEnum > &r_enums, Span< const char * > include_files)
static bool is_valid_member_name(const StringRefNull name, const StringRefNull filepath)
static void substitute_listbase_t(ParsedMember &member)
static void skip_balanced(TokenStream &stream, TokenKind open, TokenKind close)
static void append_array_suffix(TokenStream &stream, std::string &out)
Span< const char * > default_dna_header_filenames()
constexpr TokenKind TOKEN_IDENTIFIER
static const CppVectorMatrixTypeMap cpp_array_type_mappings[]
static bool parse_dna_header(const StringRefNull filepath, Vector< ParsedStruct > &r_structs, Vector< ParsedEnum > &r_enums)
std::string filename(const std::string_view &str, size_t pos)
MatBase< float, 2, 2 > float2x2
MatBase< float, 4, 4 > float4x4
BMesh const char void * data
StringRef DNA_member_id_string_ref(const StringRef member_full)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
MatBase< float, 3, 3 > float3x3
std::string underlying_type
Vector< ParsedMember > members