Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fgdpp): replace parser with faster library #10

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 101 additions & 39 deletions include/fgdpp/fgdpp.h
Original file line number Diff line number Diff line change
@@ -1,62 +1,124 @@
#pragma once

#include <list>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

#include "structs/EntityProperties.h"
#include <sourcepp/math/Vector.h>

class BufferStream;
class BufferStreamReadOnly;

namespace fgdpp {

class FGD {
std::string rawFGDFile;

//TOKENIZER
public:
enum TokenType {
COMMENT = 0, // //
DEFINITION, // @something
EQUALS, // =
OPEN_BRACE, // {
CLOSE_BRACE, // }
OPEN_BRACKET, // [
CLOSE_BRACKET, // ]
OPEN_PARENTHESIS, // (
CLOSE_PARENTHESIS, // )
COMMA, // ,
STRING, // "something"
PLUS, // +
LITERAL, // anything that isn't any of the other tokens.
COLUMN, // :
NUMBER, // numbers -200000 ... 0 ... 2000000
struct Entity {
struct ClassProperty {
std::string_view name;
std::string_view arguments;
};

struct Field {
std::string_view name;
std::string_view valueType;
bool readonly;
bool reportable;
std::string_view displayName;
std::string_view valueDefault;
std::string_view description;
};

struct FieldChoices {
struct Choice {
std::string_view value;
std::string_view displayName;
};

std::string_view name;
bool readonly;
bool reportable;
std::string_view displayName;
std::string_view valueDefault;
std::string_view description;
std::vector<Choice> choices;
};

struct FieldFlags {
struct Flag {
std::string_view value;
std::string_view displayName;
std::string_view enabledByDefault;
std::string_view description;
};

std::string_view name;
bool readonly;
bool reportable;
std::string_view displayName;
std::string_view description;
std::vector<Flag> flags;
};

struct IO {
std::string_view name;
std::string_view valueType;
std::string_view description;
};

std::string_view classType;
std::vector<ClassProperty> classProperties;

std::string_view description;

std::vector<Field> fields;
std::vector<FieldChoices> fieldsWithChoices;
std::vector<FieldFlags> fieldsWithFlags;
std::vector<IO> inputs;
std::vector<IO> outputs;
};

struct Token {
TokenType type;
Range range;
std::string_view string;
int line;
ParseError associatedError;
struct AutoVisGroup {
std::string_view parentName;
std::string_view name;
std::vector<std::string_view> entities;
};

std::vector<Token> tokenList;
FGD() = default;

public:
FGD(std::string_view path, bool parseIncludes);
explicit FGD(const std::string& fgdPath);

private:
bool TokenizeFile();
/**
* Can be called multiple times in succession to load multiple FGD files.
* The FGD file data will be merged with previously loaded data.
* @param fgdPath The path to the FGD to load
*/
void load(const std::string& fgdPath);

//PARSER.
public:
FGDFile FGDFileContents;
ParsingError parseError{ParseError::NO_ERROR, 0, {0, 0}};
[[nodiscard]] int getVersion() const;

[[nodiscard]] sourcepp::math::Vec2i getMapSize() const;

[[nodiscard]] const std::unordered_map<std::string_view, Entity>& getEntities() const;

[[nodiscard]] const std::vector<std::string_view>& getMaterialExclusionDirs() const;

[[nodiscard]] const std::vector<AutoVisGroup>& getAutoVisGroups() const;

explicit operator bool() const;

private:
void readEntities(BufferStreamReadOnly& stream, const std::string& path, std::vector<std::string>& seenPaths);

bool ParseFile();
std::list<std::string> backingData;

#ifdef FGDPP_UNIFIED_FGD
bool TagListDelimiter(std::vector<Token>::const_iterator& iter, TagList& tagList);
#endif
int version = 0;
sourcepp::math::Vec2i mapSize{};
std::unordered_map<std::string_view, Entity> entities;
std::vector<std::string_view> materialExclusionDirs;
std::vector<AutoVisGroup> autoVisGroups;
};

} // namespace fgdpp
159 changes: 0 additions & 159 deletions include/fgdpp/structs/EntityProperties.h

This file was deleted.

8 changes: 8 additions & 0 deletions include/sourcepp/parser/Text.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <stdexcept>
#include <string_view>
#include <unordered_map>

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

class syntax_error : public std::runtime_error {
public:
explicit syntax_error(const char* message) noexcept : std::runtime_error(message) {}
syntax_error(const syntax_error& other) noexcept = default;
syntax_error& operator=(const syntax_error& other) noexcept = default;
};

} // namespace sourcepp::parser::text
4 changes: 2 additions & 2 deletions include/sourcepp/string/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ void toUpper(std::string& input);

[[nodiscard]] std::string padNumber(int number, int width, char pad = '0');

void normalizeSlashes(std::string& path, bool stripTerminalSlashes = true);
void normalizeSlashes(std::string& path, bool stripSlashPrefix = false, bool stripSlashSuffix = true);

void denormalizeSlashes(std::string& path, bool stripTerminalSlashes = true);
void denormalizeSlashes(std::string& path, bool stripSlashPrefix = false, bool stripSlashSuffix = true);

} // namespace sourcepp::string
5 changes: 0 additions & 5 deletions src/fgdpp/_fgdpp.cmake
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
add_pretty_parser(fgdpp SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/include/fgdpp/structs/EntityProperties.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/fgdpp/fgdpp.h"
"${CMAKE_CURRENT_LIST_DIR}/fgdpp.cpp")

if(FGDPP_ENABLE_SPEN_FGD_SUPPORT)
target_compile_definitions(fgdpp PUBLIC FGDPP_UNIFIED_FGD)
endif()
Loading