Skip to content

Commit da1490f

Browse files
groodtsbarzowski
andcommitted
Support for std.parseYaml.
Author: Greg Roodt <[email protected]> Co-authored-by: Stanisław Barzowski <[email protected]>
1 parent ffe8e7e commit da1490f

File tree

312 files changed

+83976
-16
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

312 files changed

+83976
-16
lines changed

.travis.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: generic
2+
dist: bionic
23

34
matrix:
45
include:
@@ -9,14 +10,16 @@ matrix:
910
packages:
1011
- g++-5
1112
sources: &sources
12-
- llvm-toolchain-precise-3.8
13+
- llvm-toolchain-bionic-11
14+
- sourceline: 'deb https://apt.llvm.org/bionic/ llvm-toolchain-bionic-11 main'
15+
key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key'
1316
- ubuntu-toolchain-r-test
1417
- os: linux
15-
env: COMPILER_NAME=clang CXX=clang++-3.8 CC=clang-3.8 COMMON_OS_NAME=${TRAVIS_OS_NAME}
18+
env: COMPILER_NAME=clang CXX=clang++-11 CC=clang-11 COMMON_OS_NAME=${TRAVIS_OS_NAME}
1619
addons:
1720
apt:
1821
packages:
19-
- clang-3.8
22+
- clang-11
2023
sources: *sources
2124
- os: osx
2225
osx_image: xcode11.3

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ endif()
121121
add_subdirectory(include)
122122
add_subdirectory(stdlib)
123123
add_subdirectory(third_party/md5)
124+
add_subdirectory(third_party/rapidyaml/rapidyaml ryml)
124125
add_subdirectory(core)
125126
add_subdirectory(cpp)
126127
add_subdirectory(cmd)

Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ OPT ?= -O3
3232
PREFIX ?= /usr/local
3333

3434
CXXFLAGS ?= -g $(OPT) -Wall -Wextra -Woverloaded-virtual -pedantic -std=c++0x -fPIC
35-
CXXFLAGS += -Iinclude -Ithird_party/md5 -Ithird_party/json
35+
CXXFLAGS += -Iinclude -Ithird_party/md5 -Ithird_party/json -Ithird_party/rapidyaml/rapidyaml/src/ -Ithird_party/rapidyaml/rapidyaml/ext/c4core/src/
3636
CFLAGS ?= -g $(OPT) -Wall -Wextra -pedantic -std=c99 -fPIC
3737
CFLAGS += -Iinclude
38-
MAKEDEPENDFLAGS += -Iinclude -Ithird_party/md5 -Ithird_party/json
38+
MAKEDEPENDFLAGS += -Iinclude -Ithird_party/md5 -Ithird_party/json -Ithird_party/rapidyaml/rapidyaml/src/ -Ithird_party/rapidyaml/rapidyaml/ext/c4core/src/
3939
EMCXXFLAGS = $(CXXFLAGS) --memory-init-file 0 -s DISABLE_EXCEPTION_CATCHING=0 -s INLINING_LIMIT=50 -s RESERVED_FUNCTION_POINTERS=20 -s ASSERTIONS=1 -s ALLOW_MEMORY_GROWTH=1
4040
EMCFLAGS = $(CFLAGS) --memory-init-file 0 -s DISABLE_EXCEPTION_CATCHING=0 -s ASSERTIONS=1 -s ALLOW_MEMORY_GROWTH=1
4141
LDFLAGS ?=
@@ -50,6 +50,10 @@ SOVERSION = 0
5050
# End of user-servicable parts
5151
################################################################################
5252

53+
RAPIDYAML_SRC = \
54+
$(wildcard third_party/rapidyaml/rapidyaml/src/c4/yml/*.cpp) \
55+
$(wildcard third_party/rapidyaml/rapidyaml/ext/c4core/src/c4/*.cpp)
56+
5357
LIB_SRC = \
5458
core/desugarer.cpp \
5559
core/formatter.cpp \
@@ -60,7 +64,8 @@ LIB_SRC = \
6064
core/static_analysis.cpp \
6165
core/string_utils.cpp \
6266
core/vm.cpp \
63-
third_party/md5/md5.cpp
67+
third_party/md5/md5.cpp \
68+
$(RAPIDYAML_SRC)
6469

6570
LIB_OBJ = $(LIB_SRC:.cpp=.o)
6671

@@ -110,6 +115,7 @@ ALL_HEADERS = \
110115
core/std.jsonnet.h \
111116
third_party/md5/md5.h \
112117
third_party/json/json.hpp \
118+
third_party/rapidyaml/rapidyaml/src/ryml_std.hpp \
113119
$(INCS)
114120

115121

core/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ cc_library(
3636
"//stdlib:std",
3737
"//third_party/json",
3838
"//third_party/md5:libmd5",
39+
"//third_party/rapidyaml:ryml",
3940
],
4041
)
4142

core/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ set(LIBJSONNET_SOURCE
3030

3131
add_library(libjsonnet SHARED ${LIBJSONNET_HEADERS} ${LIBJSONNET_SOURCE})
3232
add_dependencies(libjsonnet md5 stdlib)
33-
target_link_libraries(libjsonnet md5 nlohmann_json::nlohmann_json)
33+
target_link_libraries(libjsonnet md5 nlohmann_json::nlohmann_json ryml)
3434

3535
file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/../include/libjsonnet.h JSONNET_VERSION_DEF
3636
REGEX "[#]define[ \t]+LIB_JSONNET_VERSION[ \t]+")
@@ -55,7 +55,7 @@ if (BUILD_STATIC_LIBS)
5555
# Static library for jsonnet command-line tool.
5656
add_library(libjsonnet_static STATIC ${LIBJSONNET_SOURCE})
5757
add_dependencies(libjsonnet_static md5 stdlib)
58-
target_link_libraries(libjsonnet_static md5 nlohmann_json::nlohmann_json)
58+
target_link_libraries(libjsonnet_static md5 nlohmann_json::nlohmann_json ryml)
5959
set_target_properties(libjsonnet_static PROPERTIES OUTPUT_NAME jsonnet)
6060
install(TARGETS libjsonnet_static DESTINATION "${CMAKE_INSTALL_LIBDIR}")
6161
target_include_directories(libjsonnet_static INTERFACE

core/desugarer.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct BuiltinDecl {
3434
std::vector<UString> params;
3535
};
3636

37-
static unsigned long max_builtin = 37;
37+
static unsigned long max_builtin = 38;
3838
BuiltinDecl jsonnet_builtin_decl(unsigned long builtin)
3939
{
4040
switch (builtin) {
@@ -74,8 +74,9 @@ BuiltinDecl jsonnet_builtin_decl(unsigned long builtin)
7474
case 33: return {U"asciiUpper", {U"str"}};
7575
case 34: return {U"join", {U"sep", U"arr"}};
7676
case 35: return {U"parseJson", {U"str"}};
77-
case 36: return {U"encodeUTF8", {U"str"}};
78-
case 37: return {U"decodeUTF8", {U"arr"}};
77+
case 36: return {U"parseYaml", {U"str"}};
78+
case 37: return {U"encodeUTF8", {U"str"}};
79+
case 38: return {U"decodeUTF8", {U"arr"}};
7980
default:
8081
std::cerr << "INTERNAL ERROR: Unrecognized builtin function: " << builtin << std::endl;
8182
std::abort();

core/vm.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ limitations under the License.
2626
#include "json.hpp"
2727
#include "md5.h"
2828
#include "parser.h"
29+
#include "ryml_std.hpp" // include this before any other ryml header
30+
#include "ryml.hpp"
2931
#include "state.h"
3032
#include "static_analysis.h"
3133
#include "string_utils.h"
@@ -930,6 +932,7 @@ class Interpreter {
930932
builtins["asciiUpper"] = &Interpreter::builtinAsciiUpper;
931933
builtins["join"] = &Interpreter::builtinJoin;
932934
builtins["parseJson"] = &Interpreter::builtinParseJson;
935+
builtins["parseYaml"] = &Interpreter::builtinParseYaml;
933936
builtins["encodeUTF8"] = &Interpreter::builtinEncodeUTF8;
934937
builtins["decodeUTF8"] = &Interpreter::builtinDecodeUTF8;
935938

@@ -1591,6 +1594,65 @@ class Interpreter {
15911594
return nullptr;
15921595
}
15931596

1597+
const AST *builtinParseYaml(const LocationRange &loc, const std::vector<Value> &args)
1598+
{
1599+
validateBuiltinArgs(loc, "parseYaml", args, {Value::STRING});
1600+
1601+
std::string value = encode_utf8(static_cast<HeapString *>(args[0].v.h)->value);
1602+
1603+
ryml::Tree tree = treeFromString(value);
1604+
1605+
json j;
1606+
if (tree.is_stream(tree.root_id())) {
1607+
// Split into individual yaml documents
1608+
std::stringstream ss;
1609+
ss << tree;
1610+
std::vector<std::string> v = split(ss.str(), "---\n");
1611+
1612+
// Convert yaml to json and push onto json array
1613+
ryml::Tree doc;
1614+
for (int i = 0; i < v.size(); ++i) {
1615+
if (!v[i].empty()) {
1616+
doc = treeFromString(v[i]);
1617+
j.push_back(yamlTreeToJson(doc));
1618+
}
1619+
}
1620+
} else {
1621+
j = yamlTreeToJson(tree);
1622+
}
1623+
1624+
bool filled;
1625+
1626+
otherJsonToHeap(j, filled, scratch);
1627+
1628+
return nullptr;
1629+
}
1630+
1631+
const ryml::Tree treeFromString(const std::string& s) {
1632+
return ryml::parse(c4::to_csubstr(s));
1633+
}
1634+
1635+
const std::vector<std::string> split(const std::string& s, const std::string& delimiter) {
1636+
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
1637+
std::string token;
1638+
std::vector<std::string> res;
1639+
1640+
while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
1641+
token = s.substr(pos_start, pos_end - pos_start);
1642+
pos_start = pos_end + delim_len;
1643+
res.push_back(token);
1644+
}
1645+
1646+
res.push_back(s.substr(pos_start));
1647+
return res;
1648+
}
1649+
1650+
const json yamlTreeToJson(const ryml::Tree& tree) {
1651+
std::ostringstream jsonStream;
1652+
jsonStream << ryml::as_json(tree);
1653+
return json::parse(jsonStream.str());
1654+
}
1655+
15941656
void otherJsonToHeap(const json &v, bool &filled, Value &attach) {
15951657
// In order to not anger the garbage collector, assign to attach immediately after
15961658
// making the heap object.

doc/_stdlib_gen/stdlib-content.jsonnet

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,24 @@ local html = import 'html.libsonnet';
554554
},
555555
],
556556
},
557+
{
558+
name: 'parseYaml',
559+
availableSince: 'x.y.z',
560+
params: ['str'],
561+
description: |||
562+
Parses a YAML string. This is provided as a "best-effort" mechanism and should not be relied on to provide
563+
a fully standards compliant YAML parser. YAML is a superset of JSON, consequently "downcasting" or
564+
manifestation of YAML into JSON or Jsonnet values will only succeed when using the subset of YAML that is
565+
compatible with JSON. The parser does not support YAML documents with scalar values at the root. The
566+
root node of a YAML document must start with either a YAML sequence or map to be successfully parsed.
567+
|||,
568+
examples: [
569+
{
570+
input: 'std.parseYaml(\'foo: bar\')',
571+
output: std.parseYaml('foo: bar'),
572+
},
573+
],
574+
},
557575
{
558576
name: 'encodeUTF8',
559577
params: ['str'],

setup.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,19 @@
2929
'core/static_analysis.o',
3030
'core/string_utils.o',
3131
'core/vm.o',
32-
'third_party/md5/md5.o'
32+
'third_party/md5/md5.o',
33+
'third_party/rapidyaml/rapidyaml/ext/c4core/src/c4/char_traits.o',
34+
'third_party/rapidyaml/rapidyaml/ext/c4core/src/c4/base64.o',
35+
'third_party/rapidyaml/rapidyaml/ext/c4core/src/c4/language.o',
36+
'third_party/rapidyaml/rapidyaml/ext/c4core/src/c4/memory_util.o',
37+
'third_party/rapidyaml/rapidyaml/ext/c4core/src/c4/format.o',
38+
'third_party/rapidyaml/rapidyaml/ext/c4core/src/c4/time.o',
39+
'third_party/rapidyaml/rapidyaml/ext/c4core/src/c4/memory_resource.o',
40+
'third_party/rapidyaml/rapidyaml/ext/c4core/src/c4/error.o',
41+
'third_party/rapidyaml/rapidyaml/src/c4/yml/parse.o',
42+
'third_party/rapidyaml/rapidyaml/src/c4/yml/preprocess.o',
43+
'third_party/rapidyaml/rapidyaml/src/c4/yml/common.o',
44+
'third_party/rapidyaml/rapidyaml/src/c4/yml/tree.o',
3345
]
3446

3547
MODULE_SOURCES = ['python/_jsonnet.c']

test_suite/stdlib.jsonnet

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,47 @@ std.assertEqual(std.parseJson('12'), 12) &&
10351035
std.assertEqual(std.parseJson('12.123'), 12.123) &&
10361036
std.assertEqual(std.parseJson('{"a": {"b": ["c", 42]}}'), { a: { b: ['c', 42] } }) &&
10371037

1038+
std.assertEqual(std.parseYaml('{}'), {}) &&
1039+
std.assertEqual(std.parseYaml('[]'), []) &&
1040+
std.assertEqual(
1041+
std.parseYaml(
1042+
|||
1043+
foo:
1044+
bar:
1045+
- true
1046+
- 42
1047+
- 1.0
1048+
|||
1049+
), { foo: { bar: [true, 42, 1] } }
1050+
) &&
1051+
std.assertEqual(
1052+
std.parseYaml(
1053+
|||
1054+
---
1055+
foo:
1056+
bar:
1057+
- true
1058+
- 42
1059+
- 1.0
1060+
---
1061+
wibble:
1062+
wobble:
1063+
- true
1064+
- 42
1065+
- 1.0
1066+
|||
1067+
), [{ foo: { bar: [true, 42, 1] } }, { wibble: { wobble: [true, 42, 1] } }]
1068+
) &&
1069+
std.assertEqual(
1070+
std.parseYaml(
1071+
|||
1072+
- 1
1073+
- 2
1074+
- 3
1075+
|||
1076+
), [1, 2, 3]
1077+
) &&
1078+
10381079
std.assertEqual(std.asciiUpper('!@#$%&*()asdfghFGHJKL09876 '), '!@#$%&*()ASDFGHFGHJKL09876 ') &&
10391080
std.assertEqual(std.asciiLower('!@#$%&*()asdfghFGHJKL09876 '), '!@#$%&*()asdfghfghjkl09876 ') &&
10401081

test_suite/unicode_bmp.jsonnet

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"unicode_bmp1.json (import)": import 'unicode_bmp1.jsonnet.in',
3-
"unicode_bmp1.json (importstr+parseJson)": std.parseJson(importstr 'unicode_bmp1.jsonnet.in'),
4-
"unicode_bmp2.json (import)": import 'unicode_bmp2.jsonnet.in',
5-
"unicode_bmp2.json (importstr+parseJson)": std.parseJson(importstr 'unicode_bmp2.jsonnet.in'),
2+
'unicode_bmp1.json (import)': import 'unicode_bmp1.jsonnet.in',
3+
'unicode_bmp1.json (importstr+parseJson)': std.parseJson(importstr 'unicode_bmp1.jsonnet.in'),
4+
'unicode_bmp2.json (import)': import 'unicode_bmp2.jsonnet.in',
5+
'unicode_bmp2.json (importstr+parseJson)': std.parseJson(importstr 'unicode_bmp2.jsonnet.in'),
66
}

third_party/rapidyaml/BUILD

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
licenses(["permissive"])
2+
package(default_visibility = ["//visibility:private"])
3+
4+
cc_library(
5+
name = "debugbreak",
6+
hdrs = glob(["rapidyaml/ext/c4core/src/c4/ext/debugbreak/debugbreak.h"]),
7+
srcs = [],
8+
)
9+
10+
cc_library(
11+
name = "fastfloat",
12+
includes = ["rapidyaml/ext/c4core/src/c4/ext"],
13+
hdrs = glob(["rapidyaml/ext/c4core/src/c4/ext/fast_float.hpp", "rapidyaml/ext/c4core/src/c4/ext/fast_float/**/*.h"]),
14+
srcs = [],
15+
)
16+
17+
cc_library(
18+
name = "c4core",
19+
deps = [
20+
":debugbreak",
21+
":fastfloat",
22+
],
23+
includes = ["rapidyaml/ext/c4core/src"],
24+
hdrs = glob(
25+
["rapidyaml/ext/c4core/src/**/*.hpp"],
26+
exclude = ["rapidyaml/ext/c4core/src/c4/ext/**/*"],
27+
),
28+
srcs = glob(
29+
["rapidyaml/ext/c4core/src/**/*.cpp"],
30+
exclude = ["rapidyaml/ext/c4core/src/c4/ext/**/*"],
31+
),
32+
)
33+
34+
cc_library(
35+
name = "ryml",
36+
visibility = ["//visibility:public"],
37+
deps = [
38+
":c4core",
39+
],
40+
includes = ["rapidyaml/src"],
41+
hdrs = glob(["rapidyaml/src/**/*.hpp"]),
42+
srcs = glob(["rapidyaml/src/**/*.cpp"]),
43+
)

third_party/rapidyaml/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Vendoring
2+
3+
```
4+
git clone \
5+
--depth=1 \
6+
--branch=master \
7+
--recursive \
8+
[email protected]:biojppm/rapidyaml \
9+
third_party/rapidyaml/rapidyaml \
10+
&& \
11+
rm -rf third_party/rapidyaml/rapidyaml/.git
12+
```

0 commit comments

Comments
 (0)