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
44 changes: 42 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,38 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CXX_LIBCXX_WITH_CLANG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()

# if CMAKE_SYSTEM_NAME is WASI disable the exceptions
# if CMAKE_SYSTEM_NAME is WASI disable the exceptions, and the tests
if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
set(CXX_BUILD_TESTS OFF)

# set the executable suffix to .wasm
set(CMAKE_EXECUTABLE_SUFFIX ".wasm")
endif()

set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
#
# google test
#
if(CXX_BUILD_TESTS)

set(CMAKE_CXX_SCAN_FOR_MODULES OFF)

set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)

FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.15.2.tar.gz
)

FetchContent_MakeAvailable(googletest)

include(GoogleTest)

endif()

#
# utfcpp
#
FetchContent_Declare(
utfcpp
URL https://github.com/nemtrif/utfcpp/archive/refs/tags/v4.0.5.tar.gz
Expand All @@ -64,6 +89,21 @@ FetchContent_Declare(
FetchContent_MakeAvailable(utfcpp)
FetchContent_GetProperties(utfcpp)

#
# nlohmann_json
#
FetchContent_Declare(
nlohmann_json
URL https://github.com/nlohmann/json/releases/download/v3.11.3/include.zip
URL_HASH SHA256=a22461d13119ac5c78f205d3df1db13403e58ce1bb1794edc9313677313f4a9d
)

FetchContent_MakeAvailable(nlohmann_json)

add_library(nlohmann_json::nlohmann_json INTERFACE IMPORTED)
set_target_properties(nlohmann_json::nlohmann_json PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${nlohmann_json_SOURCE_DIR}/single_include")

FetchContent_Declare(
wasi_sysroot
URL https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-22/wasi-sysroot-22.0.tar.gz
Expand Down
3 changes: 3 additions & 0 deletions packages/cxx-gen-lsp/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"printWidth": 120
}
2 changes: 1 addition & 1 deletion packages/cxx-gen-lsp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
"dependencies": {
"prettier": "^3.3.3"
}
}
}
191 changes: 182 additions & 9 deletions packages/cxx-gen-lsp/src/MetaModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,67 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

export type Type = BaseType;
export type Type =
| BaseType
| TupleType
| OrType
| AndType
| ReferenceType
| ArrayType
| MapType
| StringLiteralType
| LiteralType;

export type BaseTypeName = "string" | "integer" | "uinteger";
export type BaseTypeName = "null" | "string" | "integer" | "uinteger" | "decimal" | "boolean" | "DocumentUri" | "URI";

export type BaseType = {
kind: "base";
name: BaseTypeName;
};

export type TupleType = {
kind: "tuple";
items: Type[];
};

export type OrType = {
kind: "or";
items: Type[];
};

export type AndType = {
kind: "and";
items: Type[];
};

export type ReferenceType = {
kind: "reference";
name: string;
};

export type ArrayType = {
kind: "array";
element: Type;
};

export type MapType = {
kind: "map";
key: Type;
value: Type;
};

export type StringLiteralType = {
kind: "stringLiteral";
value: string;
};

export type LiteralType = {
kind: "literal";
value: {
properties: unknown[];
};
};

export type EnumerationValue = {
name: string;
value: string;
Expand All @@ -47,9 +99,25 @@ export type Notification = {};

export type Request = {};

export type Structure = {};
export type Property = {
documentation?: string;
name: string;
type: Type;
optional?: boolean;
};

export type Structure = {
extends?: ReferenceType[];
mixins?: ReferenceType[];
name: string;
properties: Property[];
};

export type TypeAlias = {};
export type TypeAlias = {
documentation?: string;
name: string;
type: Type;
};

export type MetaModel = {
metaData: MetaData;
Expand All @@ -65,7 +133,7 @@ export function getEnumBaseType(enumeration: Enumeration) {
case "integer":
return " : int";
case "uinteger":
return " : unsigned int";
return " : long";
default:
return "";
}
Expand All @@ -80,12 +148,117 @@ export function getEnumeratorName(enumerator: EnumerationValue) {
return `k${name}`;
}

export function getEnumeratorInitializer(
enumeration: Enumeration,
enumerator: EnumerationValue
) {
export function getEnumeratorInitializer(enumeration: Enumeration, enumerator: EnumerationValue) {
if (enumeration.type.name === "string") {
return "";
}
return ` = ${enumerator.value}`;
}

export function toCppType(type: Type): string {
switch (type.kind) {
case "base": {
switch (type.name) {
case "null":
return "std::nullptr_t";
case "string":
return "std::string";
case "integer":
return "int";
case "uinteger":
return "long";
case "decimal":
return "double";
case "boolean":
return "bool";
case "DocumentUri":
return "std::string";
case "URI":
return "std::string";
default:
throw new Error(`Unknown base type: ${JSON.stringify(type)}`);
} // switch type.name
} // case "base"

case "stringLiteral":
return "std::string";

case "literal":
return "json";

case "reference":
return type.name;

case "array":
return `Vector<${toCppType(type.element)}>`;

case "map":
return `Map<${toCppType(type.key)}, ${toCppType(type.value)}>`;

case "tuple":
return `std::tuple<${type.items.map(toCppType).join(", ")}>`;

case "or":
return `std::variant<std::monostate, ${type.items.map(toCppType).join(", ")}>`;

case "and":
return `std::tuple<${type.items.map(toCppType).join(", ")}>`;

default:
throw new Error(`Unknown type kind: ${JSON.stringify(type)}`);
} // switch
}

export function getStructureProperties(model: MetaModel, structure: Structure): Property[] {
const structByName = new Map(model.structures.map((s) => [s.name, s]));
const added = new Set<string>();
return getStructurePropertiesHelper({ structure, added, structByName });
}

function getStructurePropertiesHelper({
structure,
added,
structByName,
}: {
structure: Structure;
added: Set<string>;
structByName: Map<string, Structure>;
}): Property[] {
const properties: Property[] = [];

for (const property of structure.properties) {
if (added.has(property.name)) {
continue;
}
added.add(property.name);
properties.push(property);
}

structure.extends?.forEach((ref) => {
const extend = structByName.get(ref.name);

if (!extend) {
throw new Error(`Unknown extends ${ref.name}`);
}

properties.push(
...getStructurePropertiesHelper({
structure: extend,
added,
structByName,
}),
);
});

structure.mixins?.forEach((ref) => {
const mixin = structByName.get(ref.name);

if (!mixin) {
throw new Error(`Unknown mixin ${ref.name}`);
}

properties.push(...getStructurePropertiesHelper({ structure: mixin, added, structByName }));
});

return properties;
}
20 changes: 9 additions & 11 deletions packages/cxx-gen-lsp/src/gen_enums_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,21 @@
import { getEnumeratorName, MetaModel } from "./MetaModel.js";

import path from "node:path";
import { writeFile } from "node:fs/promises";
import { writeFileSync } from "node:fs";
import { copyrightHeader } from "./copyrightHeader.js";

export async function gen_enums_cc({
model,
outputDirectory,
}: {
model: MetaModel;
outputDirectory: string;
}) {
export function gen_enums_cc({ model, outputDirectory }: { model: MetaModel; outputDirectory: string }) {
let out = "";

const emit = (s: string = "") => {
out += `${s}\n`;
};

emit(copyrightHeader);
emit();
emit(`#include <cxx/lsp/enums.h>`);
emit();

emit(`namespace cxx::lsp {`);

model.enumerations.forEach((enumeration) => {
Expand All @@ -49,20 +46,21 @@ export async function gen_enums_cc({
enumeration.values.forEach((enumerator) => {
const enumeratorName = getEnumeratorName(enumerator);

const text =
enumeration.type.name === "string" ? enumerator.value : enumerator.name;
const text = enumeration.type.name === "string" ? enumerator.value : enumerator.name;

emit(` case ${enumeration.name}::${enumeratorName}:`);
emit(` return "${text}";`);
});

emit(` }`);
emit();
emit(` lsp_runtime_error("invalid enumerator value");`);
emit(`}`);
});

emit();
emit(`} // namespace cxx::lsp`);

const outputFile = path.join(outputDirectory, "enums.cc");
await writeFile(outputFile, out);
writeFileSync(outputFile, out);
}
Loading