Skip to content

Commit

Permalink
Refactorings (#104)
Browse files Browse the repository at this point in the history
* Added access modifier node for API and output.

* Rewrote typescript visitor to explicitly pick child nodes.

* Added optional fallback to default children parsing.

* Added different error handling behaviors for not implemented nodes.

* Added placeholder ecmascript language.
  • Loading branch information
mcmikecreations committed Nov 26, 2023
1 parent f5e34f5 commit 9ea1059
Show file tree
Hide file tree
Showing 59 changed files with 1,968 additions and 207 deletions.
8 changes: 6 additions & 2 deletions docs/notes/2023_09_20_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ String containing a pointer to a function in the format `void* (size_t byteCount

String containing a pointer to a function in the format `void (void* pointer)`.

### Parsing/ProcessUnsupported
### Parsing/UnsupportedBehavior

String containing `true` or `false`. Whether unsupported nodes and (optionally) their children should be parsed without a payload.
String containing a value to specify the processing behavior for unsupported nodes.

- `0` or `throw` to generate a runtime-dependent error and terminate the parsing process.
- `1` or `pass` to pass unsupported nodes, but parse everything else, including their children. May break the syntax.
- `2` or `skip` to pass unsupported nodes and children. Should keep the syntax correct unless it's an important node.
13 changes: 10 additions & 3 deletions examples/c/parse/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ add_executable(
)

target_include_directories(${BINARY_NAME} PRIVATE "${APP_BASE_PATH}/include")
target_link_libraries(${BINARY_NAME} PRIVATE cl_core)
target_link_libraries(${BINARY_NAME} PRIVATE cl_core cl_lang)

add_dependencies(${BINARY_NAME} cl_lang cl_lang_base_antlr cl_lang_typescript cl_lang_csharp_ref)
add_dependencies(${BINARY_NAME} cl_lang_base_antlr cl_lang_ecmascript cl_lang_csharp_ref)

get_target_property(ANTLR_RUNTIME_LIBRARIES cl_lang_base_antlr target_antlr_libraries)

Expand All @@ -33,7 +33,7 @@ set_target_properties(
set(
dependencies
$<TARGET_FILE:cl_core> ${ANTLR_RUNTIME_LIBRARIES} $<TARGET_FILE:cl_lang> $<TARGET_FILE:cl_lang_base_antlr>
#$<TARGET_FILE:cl_lang_typescript>
#$<TARGET_FILE:cl_lang_ecmascript>
#"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/plugins/${CMAKE_SHARED_LIBRARY_PREFIX}cl_lang_csharp_ref${CMAKE_SHARED_LIBRARY_SUFFIX}"
)

Expand All @@ -43,9 +43,16 @@ add_custom_command(
COMMAND ${CMAKE_COMMAND} -E copy ${dependencies} .
WORKING_DIRECTORY "${CL_BINARY_DIR}/$<CONFIG>/examples"
)

add_custom_command(
TARGET ${BINARY_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:cl_core> ./${CMAKE_SHARED_LIBRARY_PREFIX}cl_core${CMAKE_SHARED_LIBRARY_SUFFIX}
WORKING_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/plugins"
)
add_custom_command(
TARGET ${BINARY_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:cl_lang> ./${CMAKE_SHARED_LIBRARY_PREFIX}cl_lang${CMAKE_SHARED_LIBRARY_SUFFIX}
WORKING_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/plugins"
)
33 changes: 18 additions & 15 deletions examples/c/parse/src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
# include <dlfcn.h>
#endif

const char source_code[] =
u8"namespace D3.Shapes {\n"
u8"\tclass Vector {\n"
u8"\t\tdata: byte[];\n"
u8"\t\tlength: size;\n"
u8"\t\tpublic static VectorImpl = class {\n"
u8"\t\t\ttext: string;\n"
u8"\t\t}\n"
u8"\t}\n"
u8"}\n";
const char source_type[] = "text/plain";

static void*
library_init(const char* path) {
#if CL_WINDOWS == 1
Expand Down Expand Up @@ -53,7 +65,7 @@ const struct cl_node* parse_input(struct cl_config* config, const struct cl_reso
#if CL_WINDOWS == 1
"./../bin/plugins/cl_lang_typescript.dll";
#elif CL_LINUX == 1
"./../bin/plugins/libcl_lang_typescript.so";
"./../bin/plugins/libcl_lang_ecmascript.so";
#elif CL_MACOS == 1
"./../bin/plugins/libcl_lang_typescript.so";
#endif
Expand Down Expand Up @@ -248,21 +260,12 @@ const struct cl_resource* parse_output(struct cl_config* config, const struct cl

int
main(int argc, char **argv) {
const char code[] =
u8"export namespace D3.Shapes {"
u8"export class Vector {"
u8"data: byte[];"
u8"length: size;"
u8"}"
u8"}";
const char type[] = "text/plain";

// Setup resource.

struct cl_resource resourceIn;
resourceIn.content = (const uint8_t*)code;
resourceIn.content_type = type;
resourceIn.content_size = sizeof(code);
resourceIn.content = (const uint8_t*)source_code;
resourceIn.content_type = source_type;
resourceIn.content_size = sizeof(source_code);

printf("Input is below:\n%s\n", (const char*)resourceIn.content);

Expand All @@ -277,8 +280,8 @@ main(int argc, char **argv) {
cl_config_string_set(config, "Memory/Release", memoryFreeString);

char parseProcessUnsupportedString[6];
sprintf(parseProcessUnsupportedString, "true");
cl_config_string_set(config, "Parsing/ProcessUnsupported", parseProcessUnsupportedString);
sprintf(parseProcessUnsupportedString, "pass");
cl_config_string_set(config, "Parsing/UnsupportedBehavior", parseProcessUnsupportedString);

const struct cl_node* node = parse_input(config, &resourceIn);
print_tree(node);
Expand Down
2 changes: 2 additions & 0 deletions examples/c/parse/src/visualize.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const char* print_tree_node_type(size_t type) {
return "scope";
case heap_type:
return "heap_type";
case access_modifier:
return "access_modifier";
default:
return "unknown";
}
Expand Down
1 change: 1 addition & 0 deletions libs/c/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ set(PUBLIC_HEADERS_FILES
"${LIBRARY_BASE_PATH}/include/core/nodes/source_root.h"
"${LIBRARY_BASE_PATH}/include/core/nodes/scope.h"
"${LIBRARY_BASE_PATH}/include/core/nodes/heap_type.h"
"${LIBRARY_BASE_PATH}/include/core/nodes/access_modifier.h"
)

set(PRIVATE_HEADERS_FILES
Expand Down
44 changes: 44 additions & 0 deletions libs/c/core/include/core/nodes/access_modifier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @file core/nodes/access_modifier.h
* @brief The access_modifier node API.
*
* @author Mykola Morozov
* @copyright (c) 2023 Evenfall-Tech
*
* This library is released under MPL-2.0 <https://github.com/Evenfall-Tech/Crosslight/blob/master/LICENSE> license.
*/

#pragma once

#include <stdlib.h>
#include "core/definitions.h"

CL_BEGIN_C_DECLS

/**
* @brief Access modifier type. Access modifiers do not support type combinations.
*/
enum cl_node_access_modifier_type {
access_modifier_none = 0, /**< No access modifier. Acts as a fallback, should not be used normally. */
access_modifier_public = 1, /**< The type or member can be accessed by any other code in the current or other compilation units. */
access_modifier_protected = 2, /**< The type or member can be accessed only by sibling members or derivatives of the parent type. */
access_modifier_private = 3, /**< The type or member can be accessed only by sibling members. */
};

/**
* @brief Access modifier for types, members, and other nodes.
*/
struct cl_node_access_modifier {
enum cl_node_access_modifier_type type; /**< Type of the access modifier. Does not support type combinations. */
};

/**
* @brief Delete the content of and the payload itself.
*
* @param[in] payload The payload to delete along with its content.
* @param[in] term The memory termination function.
* @return `0` if deletion failed, `1` otherwise.
*/
CL_API size_t cl_node_access_modifier_term(struct cl_node_access_modifier* payload, void(*term)(void*));

CL_END_C_DECLS
1 change: 1 addition & 0 deletions libs/c/core/include/core/nodes/node_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ enum cl_node_type {
source_root = 1, /**< Textual source file root. */
scope = 2, /**< Declaration scope. */
heap_type = 3, /**< Complex type stored on the heap. */
access_modifier = 4, /**< Accessibility level modifier. */
};

CL_END_C_DECLS
21 changes: 21 additions & 0 deletions libs/c/core/src/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "core/nodes/source_root.h"
#include "core/nodes/scope.h"
#include "core/nodes/heap_type.h"
#include "core/nodes/access_modifier.h"

static size_t
cl_node_term_internal(struct cl_node* root, size_t term_children, void(*term)(void*), size_t term_root) {
Expand Down Expand Up @@ -31,6 +32,11 @@ cl_node_term_internal(struct cl_node* root, size_t term_children, void(*term)(vo
return 0;
}
break;
case access_modifier:
if (cl_node_access_modifier_term((struct cl_node_access_modifier*)root->payload, term) == 0) {
return 0;
}
break;
}
}

Expand Down Expand Up @@ -112,3 +118,18 @@ cl_node_heap_type_term(struct cl_node_heap_type* payload, void(*term)(void*)) {

return 1;
}

size_t
cl_node_access_modifier_term(struct cl_node_access_modifier* payload, void(*term)(void*)) {
if (term == 0) {
return 0;
}

if (payload == 0) {
return 1;
}

term(payload);

return 1;
}
1 change: 1 addition & 0 deletions libs/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ project(

add_subdirectory("lang")
add_subdirectory("lang_base_antlr")
add_subdirectory("lang_ecmascript")
add_subdirectory("lang_typescript")
9 changes: 9 additions & 0 deletions libs/cpp/lang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ set(CMAKE_CXX_STANDARD 17)
set(PUBLIC_HEADERS_FILES
"${LIBRARY_BASE_PATH}/include/lang/config.hpp"
"${LIBRARY_BASE_PATH}/include/lang/language.hpp"
"${LIBRARY_BASE_PATH}/include/lang/language_options_base.hpp"
"${LIBRARY_BASE_PATH}/include/lang/utils.hpp"
"${LIBRARY_BASE_PATH}/include/lang/exceptions/parsing_exception.hpp"
"${LIBRARY_BASE_PATH}/include/lang/exceptions/not_implemented_parsing_exception.hpp"
"${LIBRARY_BASE_PATH}/include/lang/exceptions/not_supported_parsing_exception.hpp"
"${LIBRARY_BASE_PATH}/include/lang/builders/allocator.hpp"
"${LIBRARY_BASE_PATH}/include/lang/builders/builder.hpp"
"${LIBRARY_BASE_PATH}/include/lang/builders/builders.hpp"
"${LIBRARY_BASE_PATH}/include/lang/builders/source_root.hpp"
"${LIBRARY_BASE_PATH}/include/lang/builders/scope.hpp"
"${LIBRARY_BASE_PATH}/include/lang/builders/heap_type.hpp"
"${LIBRARY_BASE_PATH}/include/lang/builders/access_modifier.hpp"
)

# add generated grammar to demo binary target
Expand All @@ -26,12 +31,16 @@ add_library(
"${LIBRARY_BASE_PATH}/src/language.cpp"
"${LIBRARY_BASE_PATH}/src/config.cpp"
"${LIBRARY_BASE_PATH}/src/utils.cpp"
"${LIBRARY_BASE_PATH}/src/exceptions/parsing_exception.cpp"
"${LIBRARY_BASE_PATH}/src/exceptions/not_implemented_parsing_exception.cpp"
"${LIBRARY_BASE_PATH}/src/exceptions/not_supported_parsing_exception.cpp"
"${LIBRARY_BASE_PATH}/src/builders/allocator.cpp"
"${LIBRARY_BASE_PATH}/src/builders/builder.cpp"
"${LIBRARY_BASE_PATH}/src/builders/builders.cpp"
"${LIBRARY_BASE_PATH}/src/builders/source_root.cpp"
"${LIBRARY_BASE_PATH}/src/builders/scope.cpp"
"${LIBRARY_BASE_PATH}/src/builders/heap_type.cpp"
"${LIBRARY_BASE_PATH}/src/builders/access_modifier.cpp"
)
add_library(CL::${BINARY_NAME} ALIAS ${BINARY_NAME})

Expand Down
13 changes: 13 additions & 0 deletions libs/cpp/lang/include/lang/builders/access_modifier.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "core/nodes/access_modifier.h"
#include "lang/builders/allocator.hpp"

namespace cl::lang::builders {

class builder;
class allocator;

builder access_modifier(const allocator& m, enum cl_node_access_modifier_type type);

}
2 changes: 2 additions & 0 deletions libs/cpp/lang/include/lang/builders/allocator.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "core/nodes/access_modifier.h"
#include "lang/language.hpp"

namespace cl::lang::builders {
Expand All @@ -15,6 +16,7 @@ class allocator {
builder source_root(const char* filename) const;
builder scope(const char* identifier) const;
builder heap_type(const char* identifier) const;
builder access_modifier(enum cl_node_access_modifier_type type) const;

static bool equal(const allocator& left, const allocator& right);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "lang/exceptions/parsing_exception.hpp"

namespace cl::lang::exceptions {

class CL_API_OBJ not_implemented_parsing_exception : public parsing_exception {
public:
explicit not_implemented_parsing_exception(const std::string& what_arg);
explicit not_implemented_parsing_exception(const char* what_arg);
[[nodiscard]] const char* what() const noexcept override;

protected:
std::string _message;
};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "lang/exceptions/parsing_exception.hpp"

namespace cl::lang::exceptions {

class CL_API_OBJ not_supported_parsing_exception : public parsing_exception {
public:
explicit not_supported_parsing_exception(const std::string& what_arg);
explicit not_supported_parsing_exception(const char* what_arg);
[[nodiscard]] const char* what() const noexcept override;

protected:
std::string _message;
};

}
19 changes: 19 additions & 0 deletions libs/cpp/lang/include/lang/exceptions/parsing_exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <exception>
#include <string>
#include "lang/def_visibility.hpp"

namespace cl::lang::exceptions {

class CL_API_OBJ parsing_exception : public std::exception {
public:
explicit parsing_exception(const std::string& what_arg);
explicit parsing_exception(const char* what_arg);
[[nodiscard]] const char* what() const noexcept override;

protected:
std::string _message;
};

}
6 changes: 6 additions & 0 deletions libs/cpp/lang/include/lang/language.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ struct cl_node;
namespace cl::lang
{

/**
* @brief Delegate to allocate a piece of memory.
*/
using AcquireT = void*(*)(std::size_t);
/**
* @brief Delegate to free a piece of memory.
*/
using ReleaseT = void(*)(void*);

/**
Expand Down
Loading

0 comments on commit 9ea1059

Please sign in to comment.