Skip to content

Commit

Permalink
rapidyaml: add ys parsing with integer-based events
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Jan 4, 2025
1 parent c7375b8 commit c8e9440
Show file tree
Hide file tree
Showing 20 changed files with 2,050 additions and 435 deletions.
17 changes: 9 additions & 8 deletions rapidyaml/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ RAPIDYAML_JAR_DEPS := \
pom.xml \
)

RAPIDYAML_CLASS := \
src/main/java/org/rapidyaml/Rapidyaml.class \
src/main/java/org/rapidyaml/YamlParseErrorException.class
RAPIDYAML_CLASS := $(RAPIDYAML_JAVA:.java=.class)


#------------------------------------------------------------------------------
Expand All @@ -23,7 +21,7 @@ default::
x:
YS -ce 'foo: var'

build:: $(RAPIDYAML_SO)
build:: $(RAPIDYAML_JNI_H) $(RAPIDYAML_SO)
@:

jar: $(RAPIDYAML_JAR)
Expand Down Expand Up @@ -51,7 +49,7 @@ sysclean::


#------------------------------------------------------------------------------
$(RAPIDYAML_SO):
$(RAPIDYAML_SO): $(RAPIDYAML_JNI_H)
$(MAKE) -C native $@

$(RAPIDYAML_INSTALLED): $(RAPIDYAML_JAR) $(JAVA_INSTALLED)
Expand All @@ -61,9 +59,12 @@ $(RAPIDYAML_JAR): $(RAPIDYAML_CLASS) $(JAVA_INSTALLED)
jar -cvf $@ $<

$(RAPIDYAML_CLASS): $(RAPIDYAML_JAVA) $(JAVA_INSTALLED)
# this doesn't work:
#javac $<
# ... but this does:
@# this doesn't work:
@#javac $<
@# ... but this does:
javac $(RAPIDYAML_JAVA)

$(RAPIDYAML_JNI_H): $(RAPIDYAML_JAVA)
javac -h native $(RAPIDYAML_JAVA)

crl: $(RAPIDYAML_CLASS)
5 changes: 3 additions & 2 deletions rapidyaml/native/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/rapidyaml/
/rapidyaml-build/
/rapidyaml_all.hpp
/_build/
/librapidyaml.*
/.cache
/compile_commands.json
65 changes: 45 additions & 20 deletions rapidyaml/native/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,45 +1,70 @@
cmake_minimum_required(VERSION 3.12)
project(rapidyaml
DESCRIPTION "rapidyaml -> yamlscript"
project(ysparse
DESCRIPTION "ysparse: rapidyaml -> yamlscript"
HOMEPAGE_URL "https://github.com/biojppm/rapidyaml -> https://github.com/yaml/yamlscript"
LANGUAGES CXX)

find_package(JNI REQUIRED)

option(YS2EDN_TIMED "add timings to sections" OFF)
option(YSPARSE_TIMED "add timings to sections" OFF)
option(YSPARSE_DBG "enable debug logs" OFF)

if(UNIX)
set(CMAKE_SHARED_LIBRARY_SUFFIX .so)
endif()

add_library(rapidyaml
set(libname rapidyaml) # TODO rename to ysparse

add_library(${libname}
org_rapidyaml_Rapidyaml.h
org_rapidyaml_Rapidyaml.cpp
rapidyaml_all.hpp
rapidyaml_edn_handler.hpp
rapidyaml_edn_handler.cpp
rapidyaml_edn.hpp
rapidyaml_edn.cpp
#
# ysparse files
ysparse_edn_handler.hpp
ysparse_edn_handler.cpp
ysparse_edn.hpp
ysparse_edn.cpp
ysparse_evt_handler.hpp
ysparse_evt_handler.cpp
ysparse_evt.hpp
ysparse_evt.cpp
#
# files required from rapidyaml
rapidyaml/src/c4/yml/common.cpp
rapidyaml/src/c4/yml/node_type.cpp
rapidyaml/src/c4/yml/parse.cpp
rapidyaml/src/c4/yml/tree.cpp
rapidyaml/src/c4/yml/tag.cpp
rapidyaml/src/c4/yml/reference_resolver.cpp
#
# files required from rapidyaml/ext/c4core
rapidyaml/ext/c4core/src/c4/base64.cpp
rapidyaml/ext/c4core/src/c4/error.cpp
rapidyaml/ext/c4core/src/c4/language.cpp
rapidyaml/ext/c4core/src/c4/utf.cpp
)
target_include_directories(rapidyaml PUBLIC
target_include_directories(${libname} PUBLIC
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/rapidyaml/src
${CMAKE_CURRENT_LIST_DIR}/rapidyaml/test
${CMAKE_CURRENT_LIST_DIR}/rapidyaml/ext/c4core/src
)
target_compile_definitions(rapidyaml PUBLIC
target_compile_definitions(${libname} PUBLIC
RYML_WITH_TAB_TOKENS
RYML_DEFAULT_CALLBACK_USES_EXCEPTIONS
RYML_SINGLE_HEADER
$<$<BOOL:${YS2EDN_TIMED}>:YS2EDN_TIMED>
$<$<BOOL:${YSPARSE_TIMED}>:YS2EDN_TIMED>
$<$<BOOL:${YSPARSE_DBG}>:RYML_DBG>
)
set_target_properties(rapidyaml PROPERTIES CXX_STANDARD 17)
set_target_properties(${libname} PROPERTIES CXX_STANDARD 17)

# target_link_libraries(rapidyaml PUBLIC JNI::JNI)
target_include_directories(rapidyaml PUBLIC ${JNI_INCLUDE_DIRS})
# target_link_libraries(${libname} PUBLIC JNI::JNI)
target_include_directories(${libname} PUBLIC ${JNI_INCLUDE_DIRS})

add_executable(rapidyaml-test rapidyaml_test.cpp)
target_link_libraries(rapidyaml-test rapidyaml)
add_custom_target(rapidyaml-test-run
DEPENDS rapidyaml-test
COMMAND $<TARGET_FILE:rapidyaml-test>
add_executable(${libname}-test ysparse_test.cpp)
target_link_libraries(${libname}-test ${libname})
add_custom_target(${libname}-test-run
DEPENDS ${libname}-test
COMMAND $<TARGET_FILE:${libname}-test>
COMMENT "running C++ tests"
)
42 changes: 24 additions & 18 deletions rapidyaml/native/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,37 @@ include $(COMMON)/java.mk
include $(COMMON)/python.mk
include ../rapidyaml.mk

# TODO change to static library!
# https://www.graalvm.org/latest/reference-manual/native-image/guides/build-static-executables/
# https://www.blog.akhil.cc/static-jni
# https://stackoverflow.com/questions/24493337/linking-static-library-with-jni
SHARED := 1
# enable timing scopes
TIMED := 0
# enable debug logs
DBG := 0

BUILD_TYPE := Release

RAPIDYAML_H := org_rapidyaml_Rapidyaml.h
RAPIDYAML_HPP := rapidyaml_all.hpp
RAPIDYAML_AMALGAMATE_OPTS := --stl

RAPIDYAML_SO_DEPS := \
Makefile \
CMakeLists.txt \
$(JAVA_HOME) \
$(RAPIDYAML_H) \
$(RAPIDYAML_HPP) \
$(wildcard ./*pp) \

# TODO maybe change to static library!
# https://stackoverflow.com/questions/24493337/linking-static-library-with-jni
SHARED := 1
TIMED := 0
THIS_DIR := $(shell pwd)
BDIR := $(THIS_DIR)/_build/$(BUILD_TYPE)-shared$(SHARED)-musl$(MUSL)-timed$(TIMED)-dbg$(DBG)

CMK_FLAGS :=
CMK_FLAGS += -D YS2EDN_TIMED=$(TIMED)
CMK_FLAGS += -D BUILD_SHARED_LIBS=$(SHARED)

BDIR := rapidyaml-build
CMK_ENV :=
CMK_FLAGS := \
-D CMAKE_BUILD_TYPE=$(BUILD_TYPE) \
-D CMAKE_EXPORT_COMPILE_COMMANDS=ON \
-D BUILD_SHARED_LIBS=$(SHARED) \
-D YSPARSE_TIMED=$(TIMED) \
-D YSPARSE_DBG=$(DBG)

CMK_BUILD := cmake --build $(BDIR) --parallel --verbose

Expand All @@ -35,9 +44,10 @@ default::

build:: $(RAPIDYAML_SO)

test_build:: build
$(CMK_BUILD) --target rapidyaml-test
test:: build
$(CMK_BUILD) --target rapidyaml-test-run
./$(BDIR)/rapidyaml-test

rapidyaml:
mkdir -p $@
Expand All @@ -49,7 +59,6 @@ rapidyaml:

clean::
$(RM) librapidyaml.*
$(RM) $(RAPIDYAML_HPP)
$(RM) -r $(BDIR)
$(RM) -r rapidyaml-install

Expand All @@ -65,7 +74,4 @@ $(RAPIDYAML_SO): $(RAPIDYAML_SO_DEPS)
ln -fs $@ librapidyaml.so

$(RAPIDYAML_H): $(RAPIDYAML_JAVA)
javac -h . $^

$(RAPIDYAML_HPP): rapidyaml
$(PYTHON) rapidyaml/tools/amalgamate.py $(RAPIDYAML_AMALGAMATE_OPTS) > $@
javac -h . $(RAPIDYAML_JAVA)
58 changes: 57 additions & 1 deletion rapidyaml/native/org_rapidyaml_Rapidyaml.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <jni.h>
#include "./rapidyaml_edn.hpp"
#include "ysparse_edn.hpp"
#include "ysparse_evt.hpp"
#include <stdio.h>

#ifndef _Included_org_rapidyaml_Rapidyaml
Expand Down Expand Up @@ -50,6 +51,18 @@ Java_org_rapidyaml_Rapidyaml_ys2edn_1init(JNIEnv *, jobject)
return (jlong)obj;
}

/*
* Class: org_rapidyaml_Rapidyaml
* Method: ys2evt_init
* Signature: ()J
*/
JNIEXPORT jlong JNICALL
Java_org_rapidyaml_Rapidyaml_ys2evt_1init(JNIEnv *env, jobject)
{
Ryml2Evt *obj = ys2evt_init();
return (jlong)obj;
}

/*
* Class: org_rapidyaml_Rapidyaml
* Method: ys2edn_destroy
Expand All @@ -61,6 +74,17 @@ Java_org_rapidyaml_Rapidyaml_ys2edn_1destroy(JNIEnv *, jobject, jlong obj)
ys2edn_destroy((Ryml2Edn*)obj);
}

/*
* Class: org_rapidyaml_Rapidyaml
* Method: ys2evt_destroy
* Signature: (Ljava/lang/Object;)V
*/
JNIEXPORT void JNICALL
Java_org_rapidyaml_Rapidyaml_ys2evt_1destroy(JNIEnv *, jobject, jlong obj)
{
ys2evt_destroy((Ryml2Evt*)obj);
}

/*
* Class: org_rapidyaml_Rapidyaml
* Method: ys2edn_parse
Expand Down Expand Up @@ -93,6 +117,38 @@ Java_org_rapidyaml_Rapidyaml_ys2edn_1parse(JNIEnv *env, jobject,
return rc;
}

/*
* Class: org_rapidyaml_Rapidyaml
* Method: ys2evt_parse
* Signature: (Ljava/lang/Object;Ljava/lang/String;[BI[BI)I
*/
JNIEXPORT jint JNICALL
Java_org_rapidyaml_Rapidyaml_ys2evt_1parse(JNIEnv *env, jobject,
jlong obj, jstring jfilename,
jbyteArray src, jint src_len,
jintArray dst, jint dst_len)
{
jboolean src_is_copy, dst_is_copy;
jbyte* src_ = env->GetByteArrayElements(src, &src_is_copy);
int* dst_ = env->GetIntArrayElements(dst, &dst_is_copy);
const char *filename = env->GetStringUTFChars(jfilename, 0);
int rc = 0;
try
{
rc = ys2evt_parse((Ryml2Evt*)obj, filename,
(char*)src_, src_len,
dst_, dst_len);
}
catch (Ryml2EvtParseError const& exc)
{
throw_parse_error(env, exc.location.offset, exc.location.line, exc.location.col, exc.msg.c_str());
}
env->ReleaseByteArrayElements(src, src_, 0);
env->ReleaseIntArrayElements(dst, dst_, 0);
env->ReleaseStringUTFChars(jfilename, filename);
return rc;
}

/*
* Class: org_rapidyaml_Rapidyaml
* Method: ys2edn_retry_get
Expand Down
24 changes: 24 additions & 0 deletions rapidyaml/native/org_rapidyaml_Rapidyaml.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c8e9440

Please sign in to comment.