Skip to content

Commit

Permalink
feat(fgdpp): replace parser with faster library
Browse files Browse the repository at this point in the history
  • Loading branch information
craftablescience committed Jul 10, 2024
1 parent 9c75aa1 commit de8eace
Show file tree
Hide file tree
Showing 16 changed files with 11,156 additions and 11,800 deletions.
132 changes: 93 additions & 39 deletions include/fgdpp/fgdpp.h
Original file line number Diff line number Diff line change
@@ -1,62 +1,116 @@
#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;
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;
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;
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.

9 changes: 9 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 <exception>
#include <string_view>
#include <unordered_map>

Expand Down Expand Up @@ -117,4 +118,12 @@ 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::exception {
public:
syntax_error() noexcept : std::exception() {}
explicit syntax_error(const char* const message) noexcept : std::exception(message) {}
syntax_error(const syntax_error& other) noexcept = default;
syntax_error& operator=(const syntax_error& other) noexcept = default;
};

} // namespace sourcepp::parser::text
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

0 comments on commit de8eace

Please sign in to comment.