Skip to content
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
27 changes: 26 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_subdirectory(nillsp)
add_subdirectory(nildebugger)
add_subdirectory(nilassembler)
add_subdirectory(nillinker)
add_subdirectory(nilbuild)

if(NIL_BUILD_EXPERIMENTAL_TOOLS)
add_subdirectory(nilschema)
Expand Down Expand Up @@ -388,7 +389,7 @@ if(BUILD_TESTING)
find_package(Python3 3.8 REQUIRED COMPONENTS Interpreter)
if(Python3_Interpreter_FOUND)
set(NY_IDENTITY_TEST_ARGUMENTS)
foreach(tool IN ITEMS nilcompiler nilinterpreter nilassembler nillinker nillsp nildebugger nilschema nilagent)
foreach(tool IN ITEMS nilcompiler nilinterpreter nilassembler nillinker nillsp nildebugger nilschema nilagent nilbuild)
if(TARGET ${tool})
list(APPEND NY_IDENTITY_TEST_ARGUMENTS --tool "${tool}=$<TARGET_FILE:${tool}>")
endif()
Expand All @@ -402,6 +403,17 @@ if(BUILD_TESTING)
${NY_IDENTITY_TEST_ARGUMENTS}
)
set_tests_properties(ny.release-identity PROPERTIES LABELS "tools;release;rc" TIMEOUT 60)
add_test(
NAME nilbuild.project-contract
COMMAND "${Python3_EXECUTABLE}"
"${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/project_build_contract.py"
--builder $<TARGET_FILE:nilbuild>
--compiler $<TARGET_FILE:nilcompiler>
--interpreter $<TARGET_FILE:nilinterpreter>
--stdlib "${CMAKE_CURRENT_SOURCE_DIR}/nilstd/src"
--llvm "${NIL_ENABLE_LLVM}"
)
set_tests_properties(nilbuild.project-contract PROPERTIES LABELS "tools;project;analysis;rc" TIMEOUT 180)
add_test(
NAME ny.inline-assembly
COMMAND "${Python3_EXECUTABLE}"
Expand Down Expand Up @@ -1002,6 +1014,19 @@ if(BUILD_TESTING)
--stdlib "${CMAKE_CURRENT_SOURCE_DIR}/nilstd/src"
)
set_tests_properties(nil.analysis PROPERTIES LABELS "analysis;typecheck" TIMEOUT 90)
add_executable(ny_source_span_tests tests/source_span_tests.cpp)
target_link_libraries(ny_source_span_tests PRIVATE nilcompiler_core)
target_compile_features(ny_source_span_tests PRIVATE cxx_std_17)
add_test(NAME parser.source-spans COMMAND ny_source_span_tests)
add_executable(nil_matrix_source_spans tests/matrix_source_spans.cpp)
target_link_libraries(nil_matrix_source_spans PRIVATE nilcompiler_core)
target_compile_features(nil_matrix_source_spans PRIVATE cxx_std_17)
add_test(NAME parser.matrix-source-spans COMMAND nil_matrix_source_spans)
add_test(NAME nil.semantic-index
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/semantic_contract.py"
--compiler $<TARGET_FILE:nilcompiler>)
set_tests_properties(parser.source-spans parser.matrix-source-spans nil.semantic-index
PROPERTIES LABELS "analysis;semantic" TIMEOUT 60)
add_test(NAME nil.reflection-consumer
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/reflection_contract.py"
--compiler $<TARGET_FILE:nilcompiler> --interpreter $<TARGET_FILE:nilinterpreter>
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ The framework includes:
- **Program analysis and debugging.** Structured diagnostics, declaration
metadata, and source debugging provide a common basis for editor support
and analyzers written in Ny or Matrix.
- **Project tooling.** `nilbuild` shares project settings and default analyzer
rules with VS Code, and checks applications before VM or native builds.
Live editor analysis includes unsaved source dependencies through a shared
workspace snapshot. Compiler-resolved source bindings support Go to Definition
for local variables, parameters and imported functions across both languages.

See the [standard-library reference](docs/standard-library/index.md),
[analysis API](docs/analysis-api.md), and [tool reference](docs/tools/index.md)
Expand Down Expand Up @@ -138,6 +143,10 @@ source debugging. Follow its installation instructions, then set `ny.sdkPath`
to the SDK directory. The extension also discovers `sdk/` when working in this
repository.

For projects with multiple source modules, add a
[`nil.project.json`](docs/tools/projects.md) manifest. Project check, build and
run commands use the same import paths and analyzer policy as live checks.

## Examples

[`validation/ny-3d`](validation/ny-3d) is an independent consumer of the SDK:
Expand Down
165 changes: 165 additions & 0 deletions common/strict_json.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#pragma once

// Small, strict JSON value parser shared by framework command-line tools.
// It rejects duplicate keys, malformed UTF-8, non-JSON numbers and excessive
// nesting rather than silently normalizing ambiguous configuration.
#include <cctype>
#include <map>
#include <stdexcept>
#include <string>
#include <vector>

namespace nil::detail {
struct json {
enum class kind { null, boolean, number, string, array, object } type = kind::null;
bool boolean = false;
std::string text;
std::vector<json> array;
std::map<std::string, json> object;
static json str(std::string value) { json j; j.type = kind::string; j.text = std::move(value); return j; }
static json flag(bool value) { json j; j.type = kind::boolean; j.boolean = value; return j; }
static json list() { json j; j.type = kind::array; return j; }
static json map() { json j; j.type = kind::object; return j; }
const json &at(const std::string &key) const {
const auto found = object.find(key);
if (type != kind::object || found == object.end()) throw std::runtime_error("missing JSON field: " + key);
return found->second;
}
};

inline void valid_utf8(const std::string &s) {
for (std::size_t i = 0; i < s.size();) {
unsigned char c = s[i++];
if (c < 0x80) continue;
unsigned count, value, minimum;
if (c >= 0xc2 && c <= 0xdf) { count = 1; value = c & 31; minimum = 0x80; }
else if (c >= 0xe0 && c <= 0xef) { count = 2; value = c & 15; minimum = 0x800; }
else if (c >= 0xf0 && c <= 0xf4) { count = 3; value = c & 7; minimum = 0x10000; }
else throw std::runtime_error("JSON requires valid UTF-8");
while (count--) {
if (i == s.size() || (static_cast<unsigned char>(s[i]) & 0xc0) != 0x80)
throw std::runtime_error("JSON requires valid UTF-8");
value = (value << 6) | (static_cast<unsigned char>(s[i++]) & 63);
}
if (value < minimum || value > 0x10ffff || (value >= 0xd800 && value <= 0xdfff))
throw std::runtime_error("JSON requires valid UTF-8");
}
}

class json_parser {
const std::string &source;
std::size_t position = 0;
[[noreturn]] void fail(const std::string &message) const {
throw std::runtime_error(message + " at JSON byte " + std::to_string(position));
}
void space() { while (position < source.size() && (source[position] == ' ' || source[position] == '\n' || source[position] == '\r' || source[position] == '\t')) ++position; }
bool consume(char c) { if (position < source.size() && source[position] == c) { ++position; return true; } return false; }
unsigned hex4() {
unsigned n = 0;
for (int i = 0; i < 4; ++i) {
if (position == source.size()) fail("incomplete Unicode escape");
char c = source[position++];
unsigned digit;
if (c >= '0' && c <= '9') digit = c - '0';
else if (c >= 'a' && c <= 'f') digit = c - 'a' + 10;
else if (c >= 'A' && c <= 'F') digit = c - 'A' + 10;
else fail("invalid Unicode escape");
n = n * 16 + digit;
}
return n;
}
void scalar(std::string &out, unsigned n) {
if (n < 0x80) out += char(n);
else if (n < 0x800) { out += char(0xc0 | (n >> 6)); out += char(0x80 | (n & 63)); }
else if (n < 0x10000) { out += char(0xe0 | (n >> 12)); out += char(0x80 | ((n >> 6) & 63)); out += char(0x80 | (n & 63)); }
else { out += char(0xf0 | (n >> 18)); out += char(0x80 | ((n >> 12) & 63)); out += char(0x80 | ((n >> 6) & 63)); out += char(0x80 | (n & 63)); }
}
std::string string() {
if (!consume('"')) fail("expected JSON string");
std::string out;
while (position < source.size()) {
unsigned char c = source[position++];
if (c == '"') return out;
if (c < 0x20) fail("unescaped control character");
if (c != '\\') { out += char(c); continue; }
if (position == source.size()) fail("incomplete escape");
switch (source[position++]) {
case '"': out += '"'; break; case '\\': out += '\\'; break; case '/': out += '/'; break;
case 'b': out += '\b'; break; case 'f': out += '\f'; break; case 'n': out += '\n'; break;
case 'r': out += '\r'; break; case 't': out += '\t'; break;
case 'u': {
unsigned n = hex4();
if (n >= 0xd800 && n <= 0xdbff) {
if (!consume('\\') || !consume('u')) fail("missing low surrogate");
unsigned low = hex4();
if (low < 0xdc00 || low > 0xdfff) fail("invalid low surrogate");
n = 0x10000 + ((n - 0xd800) << 10) + low - 0xdc00;
} else if (n >= 0xdc00 && n <= 0xdfff) fail("unpaired low surrogate");
scalar(out, n); break;
}
default: fail("invalid string escape");
}
}
fail("unterminated JSON string");
}
bool digit() const { return position < source.size() && source[position] >= '0' && source[position] <= '9'; }
json value(unsigned depth) {
if (depth > 32) fail("JSON nesting exceeds 32 levels");
space();
if (position == source.size()) fail("expected JSON value");
if (source[position] == '"') return json::str(string());
if (consume('{')) {
auto out = json::map(); space();
if (consume('}')) return out;
do {
space(); auto key = string(); space();
if (!consume(':')) fail("expected ':'");
auto item = value(depth + 1);
if (!out.object.emplace(key, std::move(item)).second) fail("duplicate JSON field: " + key);
space(); if (consume('}')) return out;
} while (consume(','));
fail("expected ',' or '}'");
}
if (consume('[')) {
auto out = json::list(); space();
if (consume(']')) return out;
do { out.array.push_back(value(depth + 1)); space(); if (consume(']')) return out; } while (consume(','));
fail("expected ',' or ']'");
}
for (const auto &word : {std::string("true"), std::string("false"), std::string("null")}) {
if (source.compare(position, word.size(), word) == 0) { position += word.size(); return word == "null" ? json{} : json::flag(word == "true"); }
}
auto start = position;
consume('-');
if (!consume('0')) { if (!digit()) fail("expected JSON value"); while (digit()) ++position; }
if (consume('.')) { if (!digit()) fail("missing decimal digits"); while (digit()) ++position; }
if (consume('e') || consume('E')) { if (!consume('+')) consume('-'); if (!digit()) fail("missing exponent digits"); while (digit()) ++position; }
json out; out.type = json::kind::number; out.text = source.substr(start, position - start); return out;
}
public:
explicit json_parser(const std::string &input) : source(input) {}
json parse() { valid_utf8(source); auto out = value(0); space(); if (position != source.size()) fail("unexpected trailing JSON data"); return out; }
};

inline std::string quote(const std::string &text) {
std::string out = "\"";
constexpr char hex[] = "0123456789abcdef";
for (unsigned char c : text) {
if (c == '"' || c == '\\') { out += '\\'; out += char(c); }
else if (c < 32) { out += "\\u00"; out += hex[c >> 4]; out += hex[c & 15]; }
else out += char(c);
}
return out + '"';
}
inline std::string dump(const json &value) {
switch (value.type) {
case json::kind::null: return "null";
case json::kind::boolean: return value.boolean ? "true" : "false";
case json::kind::string: return quote(value.text);
case json::kind::number: return value.text;
case json::kind::array: { std::string out = "["; for (const auto &v : value.array) { if (out.size() > 1) out += ','; out += dump(v); } return out + ']'; }
case json::kind::object: { std::string out = "{"; for (const auto &v : value.object) { if (out.size() > 1) out += ','; out += quote(v.first) + ':' + dump(v.second); } return out + '}'; }
}
throw std::runtime_error("invalid JSON kind");
}
} // namespace nil::detail
7 changes: 6 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ command-line help, and executable tests as bugs.
program, and run NIL in `nilinterpreter`.
- [Tool reference](tools/index.md) — current command lines and explicit prototype
boundaries.
- [Projects and analysis](tools/projects.md) — one configuration for checking,
VM/native builds, running and live editor analysis.
- [Developer tooling](developer-tooling.md) — current editor support and the
sequence toward shared workspace semantics.
- [Standard-library status](standard-library/index.md) — layout, maturity, and
what is deliberately outside the core library.
- [Examples](../examples/README.md) — small examples tied to repository fixtures.
Expand Down Expand Up @@ -50,5 +54,6 @@ do not assume every documented combination is accepted by every backend yet.

The canonical component directories are directly under the project root: `nilcompiler`, `nilinterpreter`,
`nildebugger`, `nillsp`, `nilstd`, `nilaix`, `nilassembler`, `nillinker`,
`nilschema`, and `nilagent`. First-party package experiments live under
`nilschema`, `nilagent`, and `nilbuild`. Shared tooling utilities live in `common`.
First-party package experiments live under
`packages`.
Loading
Loading