Skip to content

Commit 9d939dd

Browse files
feat(fgdpp): replace parser with faster library
1 parent 9c75aa1 commit 9d939dd

File tree

16 files changed

+11156
-11800
lines changed

16 files changed

+11156
-11800
lines changed

include/fgdpp/fgdpp.h

Lines changed: 93 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,116 @@
11
#pragma once
22

3+
#include <list>
34
#include <string>
45
#include <string_view>
6+
#include <unordered_map>
57
#include <vector>
68

7-
#include "structs/EntityProperties.h"
9+
#include <sourcepp/math/Vector.h>
10+
11+
class BufferStream;
12+
class BufferStreamReadOnly;
813

914
namespace fgdpp {
1015

1116
class FGD {
12-
std::string rawFGDFile;
13-
14-
//TOKENIZER
1517
public:
16-
enum TokenType {
17-
COMMENT = 0, // //
18-
DEFINITION, // @something
19-
EQUALS, // =
20-
OPEN_BRACE, // {
21-
CLOSE_BRACE, // }
22-
OPEN_BRACKET, // [
23-
CLOSE_BRACKET, // ]
24-
OPEN_PARENTHESIS, // (
25-
CLOSE_PARENTHESIS, // )
26-
COMMA, // ,
27-
STRING, // "something"
28-
PLUS, // +
29-
LITERAL, // anything that isn't any of the other tokens.
30-
COLUMN, // :
31-
NUMBER, // numbers -200000 ... 0 ... 2000000
18+
struct Entity {
19+
struct ClassProperty {
20+
std::string_view name;
21+
std::string_view arguments;
22+
};
23+
24+
struct Field {
25+
std::string_view name;
26+
std::string_view valueType;
27+
std::string_view displayName;
28+
std::string_view valueDefault;
29+
std::string_view description;
30+
};
31+
32+
struct FieldChoices {
33+
struct Choice {
34+
std::string_view value;
35+
std::string_view displayName;
36+
};
37+
38+
std::string_view name;
39+
std::string_view displayName;
40+
std::string_view valueDefault;
41+
std::string_view description;
42+
std::vector<Choice> choices;
43+
};
44+
45+
struct FieldFlags {
46+
struct Flag {
47+
std::string_view value;
48+
std::string_view displayName;
49+
std::string_view enabledByDefault;
50+
std::string_view description;
51+
};
52+
53+
std::string_view name;
54+
std::vector<Flag> flags;
55+
};
56+
57+
struct IO {
58+
std::string_view name;
59+
std::string_view valueType;
60+
std::string_view description;
61+
};
62+
63+
std::string_view classType;
64+
std::vector<ClassProperty> classProperties;
65+
66+
std::string_view description;
67+
68+
std::vector<Field> fields;
69+
std::vector<FieldChoices> fieldsWithChoices;
70+
std::vector<FieldFlags> fieldsWithFlags;
71+
std::vector<IO> inputs;
72+
std::vector<IO> outputs;
3273
};
3374

34-
struct Token {
35-
TokenType type;
36-
Range range;
37-
std::string_view string;
38-
int line;
39-
ParseError associatedError;
75+
struct AutoVisGroup {
76+
std::string_view parentName;
77+
std::string_view name;
78+
std::vector<std::string_view> entities;
4079
};
4180

42-
std::vector<Token> tokenList;
81+
FGD() = default;
4382

44-
public:
45-
FGD(std::string_view path, bool parseIncludes);
83+
explicit FGD(const std::string& fgdPath);
4684

47-
private:
48-
bool TokenizeFile();
85+
/**
86+
* Can be called multiple times in succession to load multiple FGD files.
87+
* The FGD file data will be merged with previously loaded data.
88+
* @param fgdPath The path to the FGD to load
89+
*/
90+
void load(const std::string& fgdPath);
4991

50-
//PARSER.
51-
public:
52-
FGDFile FGDFileContents;
53-
ParsingError parseError{ParseError::NO_ERROR, 0, {0, 0}};
92+
[[nodiscard]] int getVersion() const;
93+
94+
[[nodiscard]] sourcepp::math::Vec2i getMapSize() const;
95+
96+
[[nodiscard]] const std::unordered_map<std::string_view, Entity>& getEntities() const;
97+
98+
[[nodiscard]] const std::vector<std::string_view>& getMaterialExclusionDirs() const;
99+
100+
[[nodiscard]] const std::vector<AutoVisGroup>& getAutoVisGroups() const;
101+
102+
explicit operator bool() const;
103+
104+
private:
105+
void readEntities(BufferStreamReadOnly& stream, const std::string& path, std::vector<std::string>& seenPaths);
54106

55-
bool ParseFile();
107+
std::list<std::string> backingData;
56108

57-
#ifdef FGDPP_UNIFIED_FGD
58-
bool TagListDelimiter(std::vector<Token>::const_iterator& iter, TagList& tagList);
59-
#endif
109+
int version = 0;
110+
sourcepp::math::Vec2i mapSize{};
111+
std::unordered_map<std::string_view, Entity> entities;
112+
std::vector<std::string_view> materialExclusionDirs;
113+
std::vector<AutoVisGroup> autoVisGroups;
60114
};
61115

62116
} // namespace fgdpp

include/fgdpp/structs/EntityProperties.h

Lines changed: 0 additions & 159 deletions
This file was deleted.

include/sourcepp/parser/Text.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <exception>
34
#include <string_view>
45
#include <unordered_map>
56

@@ -117,4 +118,12 @@ void eatWhitespaceAndComments(BufferStream& stream, std::string_view singleLineC
117118
*/
118119
[[nodiscard]] std::string_view readUnquotedStringToBuffer(BufferStream& stream, BufferStream& backing, std::string_view end, const std::unordered_map<char, char>& escapeSequences = DEFAULT_ESCAPE_SEQUENCES);
119120

121+
class syntax_error : public std::exception {
122+
public:
123+
syntax_error() noexcept : std::exception() {}
124+
explicit syntax_error(const char* const& message) noexcept : std::exception(message) {}
125+
syntax_error(const syntax_error& other) noexcept = default;
126+
syntax_error& operator=(const syntax_error& other) noexcept = default;
127+
};
128+
120129
} // namespace sourcepp::parser::text

src/fgdpp/_fgdpp.cmake

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
add_pretty_parser(fgdpp SOURCES
2-
"${CMAKE_CURRENT_SOURCE_DIR}/include/fgdpp/structs/EntityProperties.h"
32
"${CMAKE_CURRENT_SOURCE_DIR}/include/fgdpp/fgdpp.h"
43
"${CMAKE_CURRENT_LIST_DIR}/fgdpp.cpp")
5-
6-
if(FGDPP_ENABLE_SPEN_FGD_SUPPORT)
7-
target_compile_definitions(fgdpp PUBLIC FGDPP_UNIFIED_FGD)
8-
endif()

0 commit comments

Comments
 (0)