Compare commits
2 Commits
v0.24.10
...
achalmers/
Author | SHA1 | Date | |
---|---|---|---|
a73e3ae1b7 | |||
fc38fff327 |
12
src/wasm-lib/Cargo.lock
generated
12
src/wasm-lib/Cargo.lock
generated
@ -1425,6 +1425,18 @@ dependencies = [
|
|||||||
"treediff",
|
"treediff",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "kcl-compile-macro"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"databake",
|
||||||
|
"kcl-lib",
|
||||||
|
"pretty_assertions",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.39",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kcl-lib"
|
name = "kcl-lib"
|
||||||
version = "0.1.35"
|
version = "0.1.35"
|
||||||
|
@ -53,6 +53,7 @@ debug = true
|
|||||||
members = [
|
members = [
|
||||||
"derive-docs",
|
"derive-docs",
|
||||||
"kcl",
|
"kcl",
|
||||||
|
"kcl-compile-macro",
|
||||||
]
|
]
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
|
22
src/wasm-lib/kcl-compile-macro/Cargo.toml
Normal file
22
src/wasm-lib/kcl-compile-macro/Cargo.toml
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
[package]
|
||||||
|
name = "kcl-compile-macro"
|
||||||
|
description = "Macro for compiling KCL to its AST during Rust compile-time"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
license = "MIT"
|
||||||
|
repository = "https://github.com/KittyCAD/modeling-app"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
proc-macro = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
databake = "0.1.6"
|
||||||
|
kcl-lib = { path = "../kcl" }
|
||||||
|
proc-macro2 = "1"
|
||||||
|
quote = "1"
|
||||||
|
syn = { version = "2.0.39", features = ["full"] }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
pretty_assertions = "1.4.0"
|
22
src/wasm-lib/kcl-compile-macro/src/lib.rs
Normal file
22
src/wasm-lib/kcl-compile-macro/src/lib.rs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
use databake::*;
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
use quote::quote;
|
||||||
|
use syn::{parse_macro_input, LitStr};
|
||||||
|
|
||||||
|
/// Parses KCL into its AST at compile-time.
|
||||||
|
/// This macro takes exactly one argument: A string literal containing KCL.
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// extern crate alloc;
|
||||||
|
/// use kcl_compile_macro::parse_kcl;
|
||||||
|
/// let ast: kcl_lib::ast::types::Program = parse_kcl!("const y = 4");
|
||||||
|
/// ```
|
||||||
|
#[proc_macro]
|
||||||
|
pub fn parse_kcl(input: TokenStream) -> TokenStream {
|
||||||
|
let input = parse_macro_input!(input as LitStr);
|
||||||
|
let kcl_src = input.value();
|
||||||
|
let tokens = kcl_lib::token::lexer(&kcl_src);
|
||||||
|
let ast = kcl_lib::parser::Parser::new(tokens).ast().unwrap();
|
||||||
|
let ast_struct = ast.bake(&Default::default());
|
||||||
|
quote!(#ast_struct).into()
|
||||||
|
}
|
0
src/wasm-lib/kcl-compile-macro/src/tests.rs
Normal file
0
src/wasm-lib/kcl-compile-macro/src/tests.rs
Normal file
38
src/wasm-lib/kcl-compile-macro/tests/macro_test.rs
Normal file
38
src/wasm-lib/kcl-compile-macro/tests/macro_test.rs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
extern crate alloc;
|
||||||
|
use kcl_compile_macro::parse_kcl;
|
||||||
|
use kcl_lib::ast::types::{
|
||||||
|
BodyItem, Identifier, Literal, LiteralValue, NonCodeMeta, Program, Value, VariableDeclaration, VariableDeclarator,
|
||||||
|
VariableKind,
|
||||||
|
};
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn basic() {
|
||||||
|
let actual = parse_kcl!("const y = 4");
|
||||||
|
let expected = Program {
|
||||||
|
start: 0,
|
||||||
|
end: 11,
|
||||||
|
body: vec![BodyItem::VariableDeclaration(VariableDeclaration {
|
||||||
|
start: 0,
|
||||||
|
end: 11,
|
||||||
|
declarations: vec![VariableDeclarator {
|
||||||
|
start: 6,
|
||||||
|
end: 11,
|
||||||
|
id: Identifier {
|
||||||
|
start: 6,
|
||||||
|
end: 7,
|
||||||
|
name: "y".to_owned(),
|
||||||
|
},
|
||||||
|
init: Value::Literal(Box::new(Literal {
|
||||||
|
start: 10,
|
||||||
|
end: 11,
|
||||||
|
value: LiteralValue::IInteger(4),
|
||||||
|
raw: "4".to_owned(),
|
||||||
|
})),
|
||||||
|
}],
|
||||||
|
kind: VariableKind::Const,
|
||||||
|
})],
|
||||||
|
non_code_meta: NonCodeMeta::default(),
|
||||||
|
};
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
}
|
@ -22,7 +22,7 @@ use crate::{
|
|||||||
mod literal_value;
|
mod literal_value;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct Program {
|
pub struct Program {
|
||||||
@ -352,7 +352,7 @@ macro_rules! impl_value_meta {
|
|||||||
pub(crate) use impl_value_meta;
|
pub(crate) use impl_value_meta;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub enum BodyItem {
|
pub enum BodyItem {
|
||||||
@ -392,7 +392,7 @@ impl From<&BodyItem> for SourceRange {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub enum Value {
|
pub enum Value {
|
||||||
@ -556,7 +556,7 @@ impl From<&Value> for SourceRange {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub enum BinaryPart {
|
pub enum BinaryPart {
|
||||||
@ -713,7 +713,7 @@ impl BinaryPart {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct NonCodeNode {
|
pub struct NonCodeNode {
|
||||||
@ -762,7 +762,7 @@ impl NonCodeNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub enum CommentStyle {
|
pub enum CommentStyle {
|
||||||
@ -773,7 +773,7 @@ pub enum CommentStyle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type", rename_all = "camelCase")]
|
#[serde(tag = "type", rename_all = "camelCase")]
|
||||||
pub enum NonCodeValue {
|
pub enum NonCodeValue {
|
||||||
@ -820,11 +820,11 @@ pub struct NonCodeMeta {
|
|||||||
|
|
||||||
impl Bake for NonCodeMeta {
|
impl Bake for NonCodeMeta {
|
||||||
fn bake(&self, env: &CrateEnv) -> TokenStream {
|
fn bake(&self, env: &CrateEnv) -> TokenStream {
|
||||||
env.insert("kcl_lib");
|
env.insert("kcl_lib::ast::types");
|
||||||
let start = self.start.bake(env);
|
let start = self.start.bake(env);
|
||||||
databake::quote! {
|
databake::quote! {
|
||||||
kcl_lib::NonCodeMeta {
|
kcl_lib::ast::types::NonCodeMeta {
|
||||||
non_code_nodes: HashMap::new(),
|
non_code_nodes: std::collections::HashMap::new(),
|
||||||
start: #start,
|
start: #start,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -865,7 +865,7 @@ impl NonCodeMeta {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct ExpressionStatement {
|
pub struct ExpressionStatement {
|
||||||
@ -877,7 +877,7 @@ pub struct ExpressionStatement {
|
|||||||
impl_value_meta!(ExpressionStatement);
|
impl_value_meta!(ExpressionStatement);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct CallExpression {
|
pub struct CallExpression {
|
||||||
@ -1146,7 +1146,7 @@ impl PartialEq for Function {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct VariableDeclaration {
|
pub struct VariableDeclaration {
|
||||||
@ -1297,7 +1297,7 @@ impl VariableDeclaration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
#[display(style = "snake_case")]
|
#[display(style = "snake_case")]
|
||||||
@ -1342,7 +1342,7 @@ impl VariableKind {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct VariableDeclarator {
|
pub struct VariableDeclarator {
|
||||||
@ -1372,7 +1372,7 @@ impl VariableDeclarator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct Literal {
|
pub struct Literal {
|
||||||
@ -1443,7 +1443,7 @@ impl From<&Box<Literal>> for MemoryItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct Identifier {
|
pub struct Identifier {
|
||||||
@ -1480,7 +1480,7 @@ impl Identifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct PipeSubstitution {
|
pub struct PipeSubstitution {
|
||||||
@ -1509,7 +1509,7 @@ impl From<PipeSubstitution> for Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct ArrayExpression {
|
pub struct ArrayExpression {
|
||||||
@ -1670,7 +1670,7 @@ impl ArrayExpression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct ObjectExpression {
|
pub struct ObjectExpression {
|
||||||
@ -1828,7 +1828,7 @@ impl ObjectExpression {
|
|||||||
impl_value_meta!(ObjectExpression);
|
impl_value_meta!(ObjectExpression);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct ObjectProperty {
|
pub struct ObjectProperty {
|
||||||
@ -1871,7 +1871,7 @@ impl ObjectProperty {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub enum MemberObject {
|
pub enum MemberObject {
|
||||||
@ -1918,7 +1918,7 @@ impl From<&MemberObject> for SourceRange {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub enum LiteralIdentifier {
|
pub enum LiteralIdentifier {
|
||||||
@ -1955,7 +1955,7 @@ impl From<&LiteralIdentifier> for SourceRange {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct MemberExpression {
|
pub struct MemberExpression {
|
||||||
@ -2119,7 +2119,7 @@ pub struct ObjectKeyInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct BinaryExpression {
|
pub struct BinaryExpression {
|
||||||
@ -2299,7 +2299,7 @@ pub fn parse_json_value_as_string(j: &serde_json::Value) -> Option<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
#[display(style = "snake_case")]
|
#[display(style = "snake_case")]
|
||||||
@ -2368,7 +2368,7 @@ impl BinaryOperator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct UnaryExpression {
|
pub struct UnaryExpression {
|
||||||
@ -2447,7 +2447,7 @@ impl UnaryExpression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
#[display(style = "snake_case")]
|
#[display(style = "snake_case")]
|
||||||
@ -2463,7 +2463,7 @@ pub enum UnaryOperator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(rename_all = "camelCase", tag = "type")]
|
#[serde(rename_all = "camelCase", tag = "type")]
|
||||||
pub struct PipeExpression {
|
pub struct PipeExpression {
|
||||||
@ -2622,7 +2622,7 @@ async fn execute_pipe_body(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct FunctionExpression {
|
pub struct FunctionExpression {
|
||||||
@ -2673,7 +2673,7 @@ impl FunctionExpression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub struct ReturnStatement {
|
pub struct ReturnStatement {
|
||||||
|
@ -6,7 +6,7 @@ use serde_json::Value as JValue;
|
|||||||
use super::{Literal, Value};
|
use super::{Literal, Value};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||||
#[databake(path = kcl_lib)]
|
#[databake(path = kcl_lib::ast::types)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(untagged, rename_all = "snake_case")]
|
#[serde(untagged, rename_all = "snake_case")]
|
||||||
pub enum LiteralValue {
|
pub enum LiteralValue {
|
||||||
|
@ -2801,4 +2801,5 @@ mod snapshot_tests {
|
|||||||
snapshot_test!(ar, r#"5 + "a""#);
|
snapshot_test!(ar, r#"5 + "a""#);
|
||||||
snapshot_test!(at, "line([0, l], %)");
|
snapshot_test!(at, "line([0, l], %)");
|
||||||
snapshot_test!(au, include_str!("../../../tests/executor/inputs/cylinder.kcl"));
|
snapshot_test!(au, include_str!("../../../tests/executor/inputs/cylinder.kcl"));
|
||||||
|
snapshot_test!(av, "fn f = (angle) => { return default(angle, 360) }");
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,94 @@
|
|||||||
|
---
|
||||||
|
source: kcl/src/parser/parser_impl.rs
|
||||||
|
expression: actual
|
||||||
|
---
|
||||||
|
{
|
||||||
|
"start": 0,
|
||||||
|
"end": 48,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "VariableDeclaration",
|
||||||
|
"type": "VariableDeclaration",
|
||||||
|
"start": 0,
|
||||||
|
"end": 48,
|
||||||
|
"declarations": [
|
||||||
|
{
|
||||||
|
"type": "VariableDeclarator",
|
||||||
|
"start": 3,
|
||||||
|
"end": 48,
|
||||||
|
"id": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 3,
|
||||||
|
"end": 4,
|
||||||
|
"name": "f"
|
||||||
|
},
|
||||||
|
"init": {
|
||||||
|
"type": "FunctionExpression",
|
||||||
|
"type": "FunctionExpression",
|
||||||
|
"start": 7,
|
||||||
|
"end": 48,
|
||||||
|
"params": [
|
||||||
|
{
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 8,
|
||||||
|
"end": 13,
|
||||||
|
"name": "angle"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"body": {
|
||||||
|
"start": 18,
|
||||||
|
"end": 48,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ReturnStatement",
|
||||||
|
"type": "ReturnStatement",
|
||||||
|
"start": 20,
|
||||||
|
"end": 46,
|
||||||
|
"argument": {
|
||||||
|
"type": "CallExpression",
|
||||||
|
"type": "CallExpression",
|
||||||
|
"start": 27,
|
||||||
|
"end": 46,
|
||||||
|
"callee": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 27,
|
||||||
|
"end": 34,
|
||||||
|
"name": "default"
|
||||||
|
},
|
||||||
|
"arguments": [
|
||||||
|
{
|
||||||
|
"type": "Identifier",
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 35,
|
||||||
|
"end": 40,
|
||||||
|
"name": "angle"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "Literal",
|
||||||
|
"type": "Literal",
|
||||||
|
"start": 42,
|
||||||
|
"end": 45,
|
||||||
|
"value": 360,
|
||||||
|
"raw": "360"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"optional": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nonCodeMeta": {
|
||||||
|
"nonCodeNodes": {},
|
||||||
|
"start": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"kind": "fn"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nonCodeMeta": {
|
||||||
|
"nonCodeNodes": {},
|
||||||
|
"start": []
|
||||||
|
}
|
||||||
|
}
|
@ -47,6 +47,8 @@ pub enum TokenType {
|
|||||||
Function,
|
Function,
|
||||||
/// Unknown lexemes.
|
/// Unknown lexemes.
|
||||||
Unknown,
|
Unknown,
|
||||||
|
/// The ? symbol, used for optional values.
|
||||||
|
QuestionMark,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Most KCL tokens correspond to LSP semantic tokens (but not all).
|
/// Most KCL tokens correspond to LSP semantic tokens (but not all).
|
||||||
@ -58,6 +60,7 @@ impl TryFrom<TokenType> for SemanticTokenType {
|
|||||||
TokenType::Word => Self::VARIABLE,
|
TokenType::Word => Self::VARIABLE,
|
||||||
TokenType::Keyword => Self::KEYWORD,
|
TokenType::Keyword => Self::KEYWORD,
|
||||||
TokenType::Operator => Self::OPERATOR,
|
TokenType::Operator => Self::OPERATOR,
|
||||||
|
TokenType::QuestionMark => Self::OPERATOR,
|
||||||
TokenType::String => Self::STRING,
|
TokenType::String => Self::STRING,
|
||||||
TokenType::LineComment => Self::COMMENT,
|
TokenType::LineComment => Self::COMMENT,
|
||||||
TokenType::BlockComment => Self::COMMENT,
|
TokenType::BlockComment => Self::COMMENT,
|
||||||
|
@ -21,6 +21,7 @@ pub fn token(i: &mut Located<&str>) -> PResult<Token> {
|
|||||||
'{' | '(' | '[' => brace_start,
|
'{' | '(' | '[' => brace_start,
|
||||||
'}' | ')' | ']' => brace_end,
|
'}' | ')' | ']' => brace_end,
|
||||||
',' => comma,
|
',' => comma,
|
||||||
|
'?' => question_mark,
|
||||||
'0'..='9' => number,
|
'0'..='9' => number,
|
||||||
':' => colon,
|
':' => colon,
|
||||||
'.' => alt((number, double_period, period)),
|
'.' => alt((number, double_period, period)),
|
||||||
@ -108,6 +109,11 @@ fn comma(i: &mut Located<&str>) -> PResult<Token> {
|
|||||||
Ok(Token::from_range(range, TokenType::Comma, value.to_string()))
|
Ok(Token::from_range(range, TokenType::Comma, value.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn question_mark(i: &mut Located<&str>) -> PResult<Token> {
|
||||||
|
let (value, range) = '?'.with_span().parse_next(i)?;
|
||||||
|
Ok(Token::from_range(range, TokenType::QuestionMark, value.to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
fn colon(i: &mut Located<&str>) -> PResult<Token> {
|
fn colon(i: &mut Located<&str>) -> PResult<Token> {
|
||||||
let (value, range) = ':'.with_span().parse_next(i)?;
|
let (value, range) = ':'.with_span().parse_next(i)?;
|
||||||
Ok(Token::from_range(range, TokenType::Colon, value.to_string()))
|
Ok(Token::from_range(range, TokenType::Colon, value.to_string()))
|
||||||
|
Reference in New Issue
Block a user