Compare commits

..

2 Commits

Author SHA1 Message Date
a73e3ae1b7 Add ? token 2023-11-17 15:26:55 -06:00
fc38fff327 Macro for parsing KCL at Rust compile-time
This adds a new `parse_kcl!` macro which takes a string as input. It parses the string into KCL source code _at compile-time_. Invalid KCL will make your Rust project fail to compile.
2023-11-09 17:30:53 -06:00
27 changed files with 691 additions and 929 deletions

View File

@ -11,7 +11,6 @@
"semi": [ "semi": [
"error", "error",
"never" "never"
], ]
"react-hooks/exhaustive-deps": "off"
} }
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "untitled-app", "name": "untitled-app",
"version": "0.12.0", "version": "0.11.3",
"private": true, "private": true,
"dependencies": { "dependencies": {
"@codemirror/autocomplete": "^6.10.2", "@codemirror/autocomplete": "^6.10.2",
@ -8,7 +8,7 @@
"@fortawesome/free-brands-svg-icons": "^6.4.2", "@fortawesome/free-brands-svg-icons": "^6.4.2",
"@fortawesome/free-solid-svg-icons": "^6.4.2", "@fortawesome/free-solid-svg-icons": "^6.4.2",
"@fortawesome/react-fontawesome": "^0.2.0", "@fortawesome/react-fontawesome": "^0.2.0",
"@headlessui/react": "^1.7.17", "@headlessui/react": "^1.7.13",
"@headlessui/tailwindcss": "^0.2.0", "@headlessui/tailwindcss": "^0.2.0",
"@kittycad/lib": "^0.0.45", "@kittycad/lib": "^0.0.45",
"@lezer/javascript": "^1.4.7", "@lezer/javascript": "^1.4.7",
@ -30,7 +30,7 @@
"crypto-js": "^4.2.0", "crypto-js": "^4.2.0",
"debounce-promise": "^3.1.2", "debounce-promise": "^3.1.2",
"formik": "^2.4.3", "formik": "^2.4.3",
"fuse.js": "^7.0.0", "fuse.js": "^6.6.2",
"http-server": "^14.1.1", "http-server": "^14.1.1",
"json-rpc-2.0": "^1.6.0", "json-rpc-2.0": "^1.6.0",
"re-resizable": "^6.9.11", "re-resizable": "^6.9.11",
@ -102,7 +102,7 @@
}, },
"devDependencies": { "devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11", "@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@babel/preset-env": "^7.23.3", "@babel/preset-env": "^7.22.9",
"@tauri-apps/cli": "^1.5.6", "@tauri-apps/cli": "^1.5.6",
"@types/crypto-js": "^4.1.1", "@types/crypto-js": "^4.1.1",
"@types/debounce-promise": "^3.1.8", "@types/debounce-promise": "^3.1.8",
@ -111,18 +111,18 @@
"@types/uuid": "^9.0.4", "@types/uuid": "^9.0.4",
"@types/wicg-file-system-access": "^2020.9.6", "@types/wicg-file-system-access": "^2020.9.6",
"@types/ws": "^8.5.5", "@types/ws": "^8.5.5",
"@vitejs/plugin-react": "^4.1.1", "@vitejs/plugin-react": "^4.0.3",
"@vitest/coverage-istanbul": "^0.34.6", "@vitest/coverage-istanbul": "^0.34.1",
"autoprefixer": "^10.4.13", "autoprefixer": "^10.4.13",
"eslint": "^8.53.0", "eslint": "^8.53.0",
"eslint-config-react-app": "^7.0.1", "eslint-config-react-app": "^7.0.1",
"eslint-plugin-css-modules": "^2.12.0", "eslint-plugin-css-modules": "^2.11.0",
"happy-dom": "^10.8.0", "happy-dom": "^10.8.0",
"husky": "^8.0.3", "husky": "^8.0.3",
"postcss": "^8.4.31", "postcss": "^8.4.31",
"prettier": "^2.8.0", "prettier": "^2.8.0",
"setimmediate": "^1.0.5", "setimmediate": "^1.0.5",
"tailwindcss": "^3.3.5", "tailwindcss": "^3.2.4",
"vite": "^4.5.0", "vite": "^4.5.0",
"vite-plugin-eslint": "^1.8.1", "vite-plugin-eslint": "^1.8.1",
"vite-tsconfig-paths": "^4.2.1", "vite-tsconfig-paths": "^4.2.1",

32
src-tauri/Cargo.lock generated
View File

@ -1664,9 +1664,9 @@ dependencies = [
[[package]] [[package]]
name = "kittycad" name = "kittycad"
version = "0.2.42" version = "0.2.41"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6aa554d86b6dbbd976a659c912ae25ce817b4378eb12a5684907e263410f0a7b" checksum = "874914cd40bfd43674406683bb3f0924d41780698a4ade96f2e180a73678bdd1"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
@ -1732,9 +1732,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.150" version = "0.2.148"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b"
[[package]] [[package]]
name = "libm" name = "libm"
@ -1940,9 +1940,9 @@ dependencies = [
[[package]] [[package]]
name = "mio" name = "mio"
version = "0.8.9" version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dce281c5e46beae905d4de1870d8b1509a9142b62eedf18b443b011ca8343d0" checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2"
dependencies = [ dependencies = [
"libc", "libc",
"wasi 0.11.0+wasi-snapshot-preview1", "wasi 0.11.0+wasi-snapshot-preview1",
@ -3112,9 +3112,9 @@ dependencies = [
[[package]] [[package]]
name = "schemars" name = "schemars"
version = "0.8.16" version = "0.8.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45a28f4c49489add4ce10783f7911893516f15afe45d015608d41faca6bc4d29" checksum = "1f7b0ce13155372a76ee2e1c5ffba1fe61ede73fbea5630d61eee6fac4929c0c"
dependencies = [ dependencies = [
"bigdecimal", "bigdecimal",
"bytes", "bytes",
@ -3129,9 +3129,9 @@ dependencies = [
[[package]] [[package]]
name = "schemars_derive" name = "schemars_derive"
version = "0.8.16" version = "0.8.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c767fd6fa65d9ccf9cf026122c1b555f2ef9a4f0cea69da4d7dbc3e258d30967" checksum = "e85e2a16b12bdb763244c69ab79363d71db2b4b918a2def53f80b02e0574b13c"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -3438,9 +3438,9 @@ dependencies = [
[[package]] [[package]]
name = "socket2" name = "socket2"
version = "0.5.5" version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9" checksum = "4031e820eb552adee9295814c0ced9e5cf38ddf1e8b7d566d6de8e2538ea989e"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys 0.48.0", "windows-sys 0.48.0",
@ -3856,7 +3856,7 @@ dependencies = [
[[package]] [[package]]
name = "tauri-plugin-fs-extra" name = "tauri-plugin-fs-extra"
version = "0.0.0" version = "0.0.0"
source = "git+https://github.com/tauri-apps/plugins-workspace?branch=v1#20ef22fc3ab76cab770c2c0dac0cc75dfc982c22" source = "git+https://github.com/tauri-apps/plugins-workspace?branch=v1#6865299149ffd183365e3ff291acf3edac78ca61"
dependencies = [ dependencies = [
"log", "log",
"serde", "serde",
@ -4035,9 +4035,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.34.0" version = "1.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0c014766411e834f7af5b8f4cf46257aab4036ca95e9d2c144a10f59ad6f5b9" checksum = "4f38200e3ef7995e5ef13baec2f432a6da0aa9ac495b2c0e8f3b7eec2c92d653"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"bytes", "bytes",
@ -4045,7 +4045,7 @@ dependencies = [
"mio", "mio",
"num_cpus", "num_cpus",
"pin-project-lite", "pin-project-lite",
"socket2 0.5.5", "socket2 0.5.4",
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]

View File

@ -16,13 +16,13 @@ tauri-build = { version = "1.5.0", features = [] }
[dependencies] [dependencies]
anyhow = "1" anyhow = "1"
kittycad = "0.2.42" kittycad = "0.2.41"
oauth2 = "4.4.2" oauth2 = "4.4.2"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
tauri = { version = "1.5.2", features = [ "os-all", "dialog-all", "fs-all", "http-request", "path-all", "shell-open", "shell-open-api", "updater", "devtools"] } tauri = { version = "1.5.2", features = [ "os-all", "dialog-all", "fs-all", "http-request", "path-all", "shell-open", "shell-open-api", "updater", "devtools"] }
tauri-plugin-fs-extra = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" } tauri-plugin-fs-extra = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tokio = { version = "1.34.0", features = ["time"] } tokio = { version = "1.33.0", features = ["time"] }
toml = "0.8.2" toml = "0.8.2"
[features] [features]

View File

@ -8,7 +8,7 @@
}, },
"package": { "package": {
"productName": "kittycad-modeling", "productName": "kittycad-modeling",
"version": "0.12.0" "version": "0.11.3"
}, },
"tauri": { "tauri": {
"allowlist": { "allowlist": {

View File

@ -9,6 +9,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faChevronRight, faTrashAlt } from '@fortawesome/free-solid-svg-icons' import { faChevronRight, faTrashAlt } from '@fortawesome/free-solid-svg-icons'
import { useFileContext } from 'hooks/useFileContext' import { useFileContext } from 'hooks/useFileContext'
import { useHotkeys } from 'react-hotkeys-hook' import { useHotkeys } from 'react-hotkeys-hook'
import { kclManager } from 'lang/KclSinglton'
import styles from './FileTree.module.css' import styles from './FileTree.module.css'
import { sortProject } from 'lib/tauriFS' import { sortProject } from 'lib/tauriFS'

View File

@ -82,14 +82,9 @@ export function useSetupEngineManager(
} }
function getDimensions(streamWidth?: number, streamHeight?: number) { function getDimensions(streamWidth?: number, streamHeight?: number) {
const maxResolution = 2000
const width = streamWidth ? streamWidth : 0 const width = streamWidth ? streamWidth : 0
const quadWidth = Math.round(width / 4) * 4
const height = streamHeight ? streamHeight : 0 const height = streamHeight ? streamHeight : 0
const ratio = Math.min( const quadHeight = Math.round(height / 4) * 4
Math.min(maxResolution / width, maxResolution / height),
1.0
)
const quadWidth = Math.round((width * ratio) / 4) * 4
const quadHeight = Math.round((height * ratio) / 4) * 4
return { width: quadWidth, height: quadHeight } return { width: quadWidth, height: quadHeight }
} }

View File

@ -169,24 +169,16 @@ describe('testing function declaration', () => {
end: 39, end: 39,
params: [ params: [
{ {
type: 'Parameter', type: 'Identifier',
identifier: { start: 12,
type: 'Identifier', end: 13,
start: 12, name: 'a',
end: 13,
name: 'a',
},
optional: false,
}, },
{ {
type: 'Parameter', type: 'Identifier',
identifier: { start: 15,
type: 'Identifier', end: 16,
start: 15, name: 'b',
end: 16,
name: 'b',
},
optional: false,
}, },
], ],
body: { body: {
@ -252,24 +244,16 @@ const myVar = funcN(1, 2)`
end: 37, end: 37,
params: [ params: [
{ {
type: 'Parameter', type: 'Identifier',
identifier: { start: 12,
type: 'Identifier', end: 13,
start: 12, name: 'a',
end: 13,
name: 'a',
},
optional: false,
}, },
{ {
type: 'Parameter', type: 'Identifier',
identifier: { start: 15,
type: 'Identifier', end: 16,
start: 15, name: 'b',
end: 16,
name: 'b',
},
optional: false,
}, },
], ],
body: { body: {

View File

@ -1,5 +1,5 @@
import { getNodePathFromSourceRange, getNodeFromPath } from './queryAst' import { getNodePathFromSourceRange, getNodeFromPath } from './queryAst'
import { Identifier, parse, initPromise, Parameter } from './wasm' import { Identifier, parse, initPromise } from './wasm'
beforeAll(() => initPromise) beforeAll(() => initPromise)
@ -46,7 +46,7 @@ const b1 = cube([0,0], 10)`
const ast = parse(code) const ast = parse(code)
const nodePath = getNodePathFromSourceRange(ast, sourceRange) const nodePath = getNodePathFromSourceRange(ast, sourceRange)
const node = getNodeFromPath<Parameter>(ast, nodePath).node const node = getNodeFromPath<Identifier>(ast, nodePath).node
expect(nodePath).toEqual([ expect(nodePath).toEqual([
['body', ''], ['body', ''],
@ -57,8 +57,8 @@ const b1 = cube([0,0], 10)`
['params', 'FunctionExpression'], ['params', 'FunctionExpression'],
[0, 'index'], [0, 'index'],
]) ])
expect(node.type).toBe('Parameter') expect(node.type).toBe('Identifier')
expect(node.identifier.name).toBe('pos') expect(node.name).toBe('pos')
}) })
it('gets path right for deep within function definition body', () => { it('gets path right for deep within function definition body', () => {
const code = `fn cube = (pos, scale) => { const code = `fn cube = (pos, scale) => {

View File

@ -247,10 +247,10 @@ function moreNodePathFromSourceRange(
if (_node.type === 'FunctionExpression' && isInRange) { if (_node.type === 'FunctionExpression' && isInRange) {
for (let i = 0; i < _node.params.length; i++) { for (let i = 0; i < _node.params.length; i++) {
const param = _node.params[i] const param = _node.params[i]
if (param.identifier.start <= start && param.identifier.end >= end) { if (param.start <= start && param.end >= end) {
path.push(['params', 'FunctionExpression']) path.push(['params', 'FunctionExpression'])
path.push([i, 'index']) path.push([i, 'index'])
return moreNodePathFromSourceRange(param.identifier, sourceRange, path) return moreNodePathFromSourceRange(param, sourceRange, path)
} }
} }
if (_node.body.start <= start && _node.body.end >= end) { if (_node.body.start <= start && _node.body.end >= end) {

View File

@ -216,26 +216,6 @@ export class EngineConnection {
} }
}) })
this.pc.addEventListener('icecandidateerror', (_event) => {
const event = _event as RTCPeerConnectionIceErrorEvent
console.error(
`ICE candidate returned an error: ${event.errorCode}: ${event.errorText} for ${event.url}`
)
})
this.pc.addEventListener('connectionstatechange', (event) => {
if (this.pc?.iceConnectionState === 'connected') {
if (this.shouldTrace()) {
iceSpan.resolve?.()
}
} else if (this.pc?.iceConnectionState === 'failed') {
// failed is a terminal state; let's explicitly kill the
// connection to the server at this point.
console.log('failed to negotiate ice connection; restarting')
this.close()
}
})
this.websocket.addEventListener('open', (event) => { this.websocket.addEventListener('open', (event) => {
if (this.shouldTrace()) { if (this.shouldTrace()) {
websocketSpan.resolve?.() websocketSpan.resolve?.()
@ -371,6 +351,19 @@ export class EngineConnection {
// until the end of this function is setup of our end of the // until the end of this function is setup of our end of the
// PeerConnection and waiting for events to fire our callbacks. // PeerConnection and waiting for events to fire our callbacks.
this.pc.addEventListener('connectionstatechange', (event) => {
if (this.pc?.iceConnectionState === 'connected') {
if (this.shouldTrace()) {
iceSpan.resolve?.()
}
} else if (this.pc?.iceConnectionState === 'failed') {
// failed is a terminal state; let's explicitly kill the
// connection to the server at this point.
console.log('failed to negotiate ice connection; restarting')
this.close()
}
})
this.pc.addEventListener('icecandidate', (event) => { this.pc.addEventListener('icecandidate', (event) => {
if (!this.pc || !this.websocket) return if (!this.pc || !this.websocket) return
if (event.candidate !== null) { if (event.candidate !== null) {
@ -640,10 +633,7 @@ export class EngineCommandManager {
// If we already have an engine connection, just need to resize the stream. // If we already have an engine connection, just need to resize the stream.
if (this.engineConnection) { if (this.engineConnection) {
this.handleResize({ this.handleResize({ streamWidth: width, streamHeight: height })
streamWidth: width,
streamHeight: height,
})
return return
} }

View File

@ -20,7 +20,6 @@ export type { ObjectExpression } from '../wasm-lib/kcl/bindings/ObjectExpression
export type { MemberExpression } from '../wasm-lib/kcl/bindings/MemberExpression' export type { MemberExpression } from '../wasm-lib/kcl/bindings/MemberExpression'
export type { PipeExpression } from '../wasm-lib/kcl/bindings/PipeExpression' export type { PipeExpression } from '../wasm-lib/kcl/bindings/PipeExpression'
export type { VariableDeclaration } from '../wasm-lib/kcl/bindings/VariableDeclaration' export type { VariableDeclaration } from '../wasm-lib/kcl/bindings/VariableDeclaration'
export type { Parameter } from '../wasm-lib/kcl/bindings/Parameter'
export type { PipeSubstitution } from '../wasm-lib/kcl/bindings/PipeSubstitution' export type { PipeSubstitution } from '../wasm-lib/kcl/bindings/PipeSubstitution'
export type { Identifier } from '../wasm-lib/kcl/bindings/Identifier' export type { Identifier } from '../wasm-lib/kcl/bindings/Identifier'
export type { UnaryExpression } from '../wasm-lib/kcl/bindings/UnaryExpression' export type { UnaryExpression } from '../wasm-lib/kcl/bindings/UnaryExpression'

View File

@ -447,9 +447,9 @@ dependencies = [
[[package]] [[package]]
name = "clap" name = "clap"
version = "4.4.8" version = "4.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2275f18819641850fa26c89acc84d465c1bf91ce57bc2748b28c420473352f64" checksum = "ac495e00dcec98c83465d5ad66c5c4fabd652fd6686e7c6269b117e729a6f17b"
dependencies = [ dependencies = [
"clap_builder", "clap_builder",
"clap_derive", "clap_derive",
@ -457,9 +457,9 @@ dependencies = [
[[package]] [[package]]
name = "clap_builder" name = "clap_builder"
version = "4.4.8" version = "4.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07cdf1b148b25c1e1f7a42225e30a0d99a615cd4637eae7365548dd4529b95bc" checksum = "c77ed9a32a62e6ca27175d00d29d05ca32e396ea1eb5fb01d8256b669cec7663"
dependencies = [ dependencies = [
"anstream", "anstream",
"anstyle", "anstyle",
@ -683,20 +683,21 @@ checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308"
[[package]] [[package]]
name = "databake" name = "databake"
version = "0.1.7" version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82175d72e69414ceafbe2b49686794d3a8bed846e0d50267355f83ea8fdd953a" checksum = "959b676312ba1aaafb2219c475560082e6b20c3bc572ec1483f93cecd748cf3d"
dependencies = [ dependencies = [
"databake-derive", "databake-derive",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.39",
] ]
[[package]] [[package]]
name = "databake-derive" name = "databake-derive"
version = "0.1.7" version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "377af281d8f23663862a7c84623bc5dcf7f8c44b13c7496a590bdc157f941a43" checksum = "5f0694dfe255f1af0289d3d1b40787bb955e8603d96e96a6b14b225926e108fb"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1382,9 +1383,9 @@ dependencies = [
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.12.0" version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
dependencies = [ dependencies = [
"either", "either",
] ]
@ -1424,9 +1425,21 @@ 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.37" version = "0.1.35"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-recursion", "async-recursion",
@ -1440,7 +1453,7 @@ dependencies = [
"expectorate", "expectorate",
"futures", "futures",
"insta", "insta",
"itertools 0.12.0", "itertools 0.11.0",
"js-sys", "js-sys",
"kittycad", "kittycad",
"lazy_static", "lazy_static",
@ -1462,23 +1475,11 @@ dependencies = [
"winnow", "winnow",
] ]
[[package]]
name = "kcl-macros"
version = "0.1.0"
dependencies = [
"databake",
"kcl-lib",
"pretty_assertions",
"proc-macro2",
"quote",
"syn 2.0.39",
]
[[package]] [[package]]
name = "kittycad" name = "kittycad"
version = "0.2.43" version = "0.2.41"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41ab6de34cc4ab06519d65a613d4030ade14036ac619d8fee5ce6f35d1766c11" checksum = "874914cd40bfd43674406683bb3f0924d41780698a4ade96f2e180a73678bdd1"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
@ -1783,7 +1784,7 @@ checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
[[package]] [[package]]
name = "openapitor" name = "openapitor"
version = "0.0.9" version = "0.0.9"
source = "git+https://github.com/KittyCAD/kittycad.rs?branch=main#9caff1f4f00c64632ff5a226251e3dc68210b455" source = "git+https://github.com/KittyCAD/kittycad.rs?branch=main#1b2562f4b3ecd26a3683c3fc48a0d33707097a36"
dependencies = [ dependencies = [
"Inflector", "Inflector",
"anyhow", "anyhow",
@ -1953,7 +1954,7 @@ dependencies = [
"bincode", "bincode",
"either", "either",
"fnv", "fnv",
"itertools 0.10.5", "itertools 0.11.0",
"lazy_static", "lazy_static",
"nom", "nom",
"quick-xml", "quick-xml",
@ -2520,9 +2521,9 @@ dependencies = [
[[package]] [[package]]
name = "schemars" name = "schemars"
version = "0.8.16" version = "0.8.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45a28f4c49489add4ce10783f7911893516f15afe45d015608d41faca6bc4d29" checksum = "1f7b0ce13155372a76ee2e1c5ffba1fe61ede73fbea5630d61eee6fac4929c0c"
dependencies = [ dependencies = [
"bigdecimal", "bigdecimal",
"bytes", "bytes",
@ -2537,9 +2538,9 @@ dependencies = [
[[package]] [[package]]
name = "schemars_derive" name = "schemars_derive"
version = "0.8.16" version = "0.8.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c767fd6fa65d9ccf9cf026122c1b555f2ef9a4f0cea69da4d7dbc3e258d30967" checksum = "e85e2a16b12bdb763244c69ab79363d71db2b4b918a2def53f80b02e0574b13c"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -3138,9 +3139,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.34.0" version = "1.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0c014766411e834f7af5b8f4cf46257aab4036ca95e9d2c144a10f59ad6f5b9" checksum = "4f38200e3ef7995e5ef13baec2f432a6da0aa9ac495b2c0e8f3b7eec2c92d653"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"bytes", "bytes",
@ -3157,9 +3158,9 @@ dependencies = [
[[package]] [[package]]
name = "tokio-macros" name = "tokio-macros"
version = "2.2.0" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -3547,9 +3548,9 @@ checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.6.0" version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c58fe91d841bc04822c9801002db4ea904b9e4b8e6bbad25127b46eff8dc516b" checksum = "88ad59a7560b41a70d191093a945f0b87bc1deeda46fb237479708a1d6b6cdfc"
dependencies = [ dependencies = [
"atomic", "atomic",
"getrandom", "getrandom",
@ -3627,9 +3628,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-futures" name = "wasm-bindgen-futures"
version = "0.4.38" version = "0.4.37"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9afec9963e3d0994cac82455b2b3502b81a7f40f9a0d32181f7528d9f4b43e02" checksum = "c02dbc21516f9f1f04f187958890d7e6026df8d16540b7ad9492bc34a67cea03"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"futures-core", "futures-core",
@ -3722,9 +3723,9 @@ dependencies = [
[[package]] [[package]]
name = "web-sys" name = "web-sys"
version = "0.3.65" version = "0.3.64"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5db499c5f66323272151db0e666cd34f78617522fb0c1604d31a27c50c206a85" checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b"
dependencies = [ dependencies = [
"js-sys", "js-sys",
"wasm-bindgen", "wasm-bindgen",

View File

@ -15,9 +15,9 @@ gloo-utils = "0.2.0"
kcl-lib = { path = "kcl" } kcl-lib = { path = "kcl" }
kittycad = { workspace = true } kittycad = { workspace = true }
serde_json = "1.0.108" serde_json = "1.0.108"
uuid = { version = "1.6.0", features = ["v4", "js", "serde"] } uuid = { version = "1.5.0", features = ["v4", "js", "serde"] }
wasm-bindgen = "0.2.88" wasm-bindgen = "0.2.88"
wasm-bindgen-futures = "0.4.38" wasm-bindgen-futures = "0.4.37"
[dev-dependencies] [dev-dependencies]
anyhow = "1" anyhow = "1"
@ -25,9 +25,9 @@ image = "0.24.7"
kittycad = { workspace = true, default-features = true } kittycad = { workspace = true, default-features = true }
pretty_assertions = "1.4.0" pretty_assertions = "1.4.0"
reqwest = { version = "0.11.22", default-features = false } reqwest = { version = "0.11.22", default-features = false }
tokio = { version = "1.34.0", features = ["rt-multi-thread", "macros", "time"] } tokio = { version = "1.33.0", features = ["rt-multi-thread", "macros", "time"] }
twenty-twenty = "0.6.1" twenty-twenty = "0.6.1"
uuid = { version = "1.6.0", features = ["v4", "js", "serde"] } uuid = { version = "1.5.0", features = ["v4", "js", "serde"] }
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
futures = "0.3.29" futures = "0.3.29"
@ -37,7 +37,7 @@ wasm-bindgen-futures = { version = "0.4.37", features = ["futures-core-03-stream
wasm-streams = "0.4.0" wasm-streams = "0.4.0"
[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys] [target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
version = "0.3.65" version = "0.3.57"
features = [ features = [
"console", "console",
"HtmlTextAreaElement", "HtmlTextAreaElement",
@ -53,11 +53,11 @@ debug = true
members = [ members = [
"derive-docs", "derive-docs",
"kcl", "kcl",
"kcl-macros", "kcl-compile-macro",
] ]
[workspace.dependencies] [workspace.dependencies]
kittycad = { version = "0.2.43", default-features = false, features = ["js"] } kittycad = { version = "0.2.41", default-features = false, features = ["js"] }
[[test]] [[test]]
name = "executor" name = "executor"

View File

@ -1,5 +1,5 @@
[package] [package]
name = "kcl-macros" name = "kcl-compile-macro"
description = "Macro for compiling KCL to its AST during Rust compile-time" description = "Macro for compiling KCL to its AST during Rust compile-time"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
@ -12,7 +12,7 @@ repository = "https://github.com/KittyCAD/modeling-app"
proc-macro = true proc-macro = true
[dependencies] [dependencies]
databake = "0.1.7" databake = "0.1.6"
kcl-lib = { path = "../kcl" } kcl-lib = { path = "../kcl" }
proc-macro2 = "1" proc-macro2 = "1"
quote = "1" quote = "1"

View File

@ -1,4 +1,3 @@
//! This crate contains macros for parsing KCL at Rust compile-time.
use databake::*; use databake::*;
use proc_macro::TokenStream; use proc_macro::TokenStream;
use quote::quote; use quote::quote;
@ -13,7 +12,7 @@ use syn::{parse_macro_input, LitStr};
/// let ast: kcl_lib::ast::types::Program = parse_kcl!("const y = 4"); /// let ast: kcl_lib::ast::types::Program = parse_kcl!("const y = 4");
/// ``` /// ```
#[proc_macro] #[proc_macro]
pub fn parse(input: TokenStream) -> TokenStream { pub fn parse_kcl(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as LitStr); let input = parse_macro_input!(input as LitStr);
let kcl_src = input.value(); let kcl_src = input.value();
let tokens = kcl_lib::token::lexer(&kcl_src); let tokens = kcl_lib::token::lexer(&kcl_src);

View File

@ -1,14 +1,14 @@
extern crate alloc; extern crate alloc;
use kcl_compile_macro::parse_kcl;
use kcl_lib::ast::types::{ use kcl_lib::ast::types::{
BodyItem, Identifier, Literal, LiteralValue, NonCodeMeta, Program, Value, VariableDeclaration, VariableDeclarator, BodyItem, Identifier, Literal, LiteralValue, NonCodeMeta, Program, Value, VariableDeclaration, VariableDeclarator,
VariableKind, VariableKind,
}; };
use kcl_macros::parse;
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
#[test] #[test]
fn basic() { fn basic() {
let actual = parse!("const y = 4"); let actual = parse_kcl!("const y = 4");
let expected = Program { let expected = Program {
start: 0, start: 0,
end: 11, end: 11,

View File

@ -1,13 +1,11 @@
[package] [package]
name = "kcl-lib" name = "kcl-lib"
description = "KittyCAD Language implementation and tools" description = "KittyCAD Language"
version = "0.1.37" version = "0.1.35"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
repository = "https://github.com/KittyCAD/modeling-app" repository = "https://github.com/KittyCAD/modeling-app"
rust-version = "1.73" rust-version = "1.73"
authors = ["Jess Frazelle", "Adam Chalmers", "KittyCAD, Inc"]
keywords = ["kcl", "KittyCAD", "CAD"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -15,34 +13,34 @@ keywords = ["kcl", "KittyCAD", "CAD"]
anyhow = { version = "1.0.75", features = ["backtrace"] } anyhow = { version = "1.0.75", features = ["backtrace"] }
async-recursion = "1.0.5" async-recursion = "1.0.5"
async-trait = "0.1.73" async-trait = "0.1.73"
clap = { version = "4.4.8", features = ["cargo", "derive", "env", "unicode"], optional = true } clap = { version = "4.4.7", features = ["cargo", "derive", "env", "unicode"], optional = true }
dashmap = "5.5.3" dashmap = "5.5.3"
databake = { version = "0.1.7", features = ["derive"] } databake = { version = "0.1.6", features = ["derive"] }
derive-docs = { version = "0.1.4" } derive-docs = { version = "0.1.4" }
#derive-docs = { path = "../derive-docs" } #derive-docs = { path = "../derive-docs" }
kittycad = { workspace = true } kittycad = { workspace = true }
lazy_static = "1.4.0" lazy_static = "1.4.0"
parse-display = "0.8.2" parse-display = "0.8.2"
schemars = { version = "0.8.16", features = ["impl_json_schema", "url", "uuid1"] } schemars = { version = "0.8", features = ["impl_json_schema", "url", "uuid1"] }
serde = { version = "1.0.192", features = ["derive"] } serde = { version = "1.0.192", features = ["derive"] }
serde_json = "1.0.108" serde_json = "1.0.108"
thiserror = "1.0.50" thiserror = "1.0.50"
ts-rs = { version = "7", package = "ts-rs-json-value", features = ["serde-json-impl", "schemars-impl", "uuid-impl"] } ts-rs = { version = "7", package = "ts-rs-json-value", features = ["serde-json-impl", "schemars-impl", "uuid-impl"] }
uuid = { version = "1.6.0", features = ["v4", "js", "serde"] } uuid = { version = "1.5.0", features = ["v4", "js", "serde"] }
winnow = "0.5.18" winnow = "0.5.18"
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = { version = "0.3.65" } js-sys = { version = "0.3.65" }
tower-lsp = { version = "0.20.0", default-features = false, features = ["runtime-agnostic"] } tower-lsp = { version = "0.20.0", default-features = false, features = ["runtime-agnostic"] }
wasm-bindgen = "0.2.88" wasm-bindgen = "0.2.88"
wasm-bindgen-futures = "0.4.38" wasm-bindgen-futures = "0.4.37"
web-sys = { version = "0.3.65", features = ["console"] } web-sys = { version = "0.3.64", features = ["console"] }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
bson = { version = "2.7.0", features = ["uuid-1", "chrono"] } bson = { version = "2.7.0", features = ["uuid-1", "chrono"] }
futures = { version = "0.3.29" } futures = { version = "0.3.29" }
reqwest = { version = "0.11.22", default-features = false } reqwest = { version = "0.11.22", default-features = false }
tokio = { version = "1.34.0", features = ["full"] } tokio = { version = "1.33.0", features = ["full"] }
tokio-tungstenite = { version = "0.20.0", features = ["rustls-tls-native-roots"] } tokio-tungstenite = { version = "0.20.0", features = ["rustls-tls-native-roots"] }
tower-lsp = { version = "0.20.0", features = ["proposed"] } tower-lsp = { version = "0.20.0", features = ["proposed"] }
@ -62,9 +60,9 @@ debug = true # Flamegraphs of benchmarks require accurate debug symbols
criterion = "0.5.1" criterion = "0.5.1"
expectorate = "1.1.0" expectorate = "1.1.0"
insta = { version = "1.34.0", features = ["json"] } insta = { version = "1.34.0", features = ["json"] }
itertools = "0.12.0" itertools = "0.11.0"
pretty_assertions = "1.4.0" pretty_assertions = "1.4.0"
tokio = { version = "1.34.0", features = ["rt-multi-thread", "macros", "time"] } tokio = { version = "1.33.0", features = ["rt-multi-thread", "macros", "time"] }
[[bench]] [[bench]]
name = "compiler_benchmark" name = "compiler_benchmark"

View File

@ -221,11 +221,11 @@ impl Program {
if let Some(Value::FunctionExpression(ref mut function_expression)) = &mut value { if let Some(Value::FunctionExpression(ref mut function_expression)) = &mut value {
// Check if the params to the function expression contain the position. // Check if the params to the function expression contain the position.
for param in &mut function_expression.params { for param in &mut function_expression.params {
let param_source_range: SourceRange = (&param.identifier).into(); let param_source_range: SourceRange = param.clone().into();
if param_source_range.contains(pos) { if param_source_range.contains(pos) {
let old_name = param.identifier.name.clone(); let old_name = param.name.clone();
// Rename the param. // Rename the param.
param.identifier.rename(&old_name, new_name); param.rename(&old_name, new_name);
// Now rename all the identifiers in the rest of the program. // Now rename all the identifiers in the rest of the program.
function_expression.body.rename_identifiers(&old_name, new_name); function_expression.body.rename_identifiers(&old_name, new_name);
return; return;
@ -810,8 +810,7 @@ pub enum NonCodeValue {
NewLine, NewLine,
} }
#[derive(Debug, Default, Clone, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)] #[derive(Debug, Default, Clone, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[databake(path = kcl_lib::ast::types)]
#[ts(export)] #[ts(export)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct NonCodeMeta { pub struct NonCodeMeta {
@ -819,6 +818,19 @@ pub struct NonCodeMeta {
pub start: Vec<NonCodeNode>, pub start: Vec<NonCodeNode>,
} }
impl Bake for NonCodeMeta {
fn bake(&self, env: &CrateEnv) -> TokenStream {
env.insert("kcl_lib::ast::types");
let start = self.start.bake(env);
databake::quote! {
kcl_lib::ast::types::NonCodeMeta {
non_code_nodes: std::collections::HashMap::new(),
start: #start,
}
}
}
}
// implement Deserialize manually because we to force the keys of non_code_nodes to be usize // implement Deserialize manually because we to force the keys of non_code_nodes to be usize
// and by default the ts type { [statementIndex: number]: NonCodeNode } serializes to a string i.e. "0", "1", etc. // and by default the ts type { [statementIndex: number]: NonCodeNode } serializes to a string i.e. "0", "1", etc.
impl<'de> Deserialize<'de> for NonCodeMeta { impl<'de> Deserialize<'de> for NonCodeMeta {
@ -1002,11 +1014,7 @@ impl CallExpression {
// Add the arguments to the memory. // Add the arguments to the memory.
let mut fn_memory = memory.clone(); let mut fn_memory = memory.clone();
for (index, param) in function_expression.params.iter().enumerate() { for (index, param) in function_expression.params.iter().enumerate() {
fn_memory.add( fn_memory.add(&param.name, fn_args.get(index).unwrap().clone(), param.into())?;
&param.identifier.name,
fn_args.get(index).unwrap().clone(),
param.identifier.clone().into(),
)?;
} }
// Call the stdlib function // Call the stdlib function
@ -1241,10 +1249,10 @@ impl VariableDeclaration {
symbol_kind = SymbolKind::FUNCTION; symbol_kind = SymbolKind::FUNCTION;
let mut children = vec![]; let mut children = vec![];
for param in &function_expression.params { for param in &function_expression.params {
let param_source_range: SourceRange = (&param.identifier).into(); let param_source_range: SourceRange = param.into();
#[allow(deprecated)] #[allow(deprecated)]
children.push(DocumentSymbol { children.push(DocumentSymbol {
name: param.identifier.name.clone(), name: param.name.clone(),
detail: None, detail: None,
kind: SymbolKind::VARIABLE, kind: SymbolKind::VARIABLE,
range: param_source_range.to_lsp_range(code), range: param_source_range.to_lsp_range(code),
@ -1434,7 +1442,7 @@ impl From<&Box<Literal>> for MemoryItem {
} }
} }
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake, Eq)] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib::ast::types)] #[databake(path = kcl_lib::ast::types)]
#[ts(export)] #[ts(export)]
#[serde(tag = "type")] #[serde(tag = "type")]
@ -2613,18 +2621,6 @@ async fn execute_pipe_body(
} }
} }
/// Parameter of a KCL function.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
#[databake(path = kcl_lib::ast::types)]
#[ts(export)]
#[serde(tag = "type")]
pub struct Parameter {
/// The parameter's label or name.
pub identifier: Identifier,
/// Is the parameter optional?
pub optional: bool,
}
#[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::ast::types)] #[databake(path = kcl_lib::ast::types)]
#[ts(export)] #[ts(export)]
@ -2632,7 +2628,7 @@ pub struct Parameter {
pub struct FunctionExpression { pub struct FunctionExpression {
pub start: usize, pub start: usize,
pub end: usize, pub end: usize,
pub params: Vec<Parameter>, pub params: Vec<Identifier>,
pub body: Program, pub body: Program,
} }
@ -2658,7 +2654,7 @@ impl FunctionExpression {
"({}) => {{\n{}{}\n}}", "({}) => {{\n{}{}\n}}",
self.params self.params
.iter() .iter()
.map(|param| param.identifier.name.clone()) .map(|param| param.name.clone())
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(", "), .join(", "),
options.get_indentation(indentation_level + 1), options.get_indentation(indentation_level + 1),

View File

@ -88,7 +88,11 @@ impl EngineConnection {
let ws_stream = tokio_tungstenite::WebSocketStream::from_raw_socket( let ws_stream = tokio_tungstenite::WebSocketStream::from_raw_socket(
ws, ws,
tokio_tungstenite::tungstenite::protocol::Role::Client, tokio_tungstenite::tungstenite::protocol::Role::Client,
Some(tokio_tungstenite::tungstenite::protocol::WebSocketConfig { ..Default::default() }), Some(tokio_tungstenite::tungstenite::protocol::WebSocketConfig {
write_buffer_size: 1024 * 128,
max_write_buffer_size: 1024 * 256,
..Default::default()
}),
) )
.await; .await;

View File

@ -908,9 +908,9 @@ pub async fn execute(
// Add the arguments to the memory. // Add the arguments to the memory.
for (index, param) in function_expression.params.iter().enumerate() { for (index, param) in function_expression.params.iter().enumerate() {
fn_memory.add( fn_memory.add(
&param.identifier.name, &param.name,
args.get(index).unwrap().clone(), args.get(index).unwrap().clone(),
(&param.identifier).into(), param.into(),
)?; )?;
} }

View File

@ -1,7 +1,3 @@
//! Rust support for KCL (aka the KittyCAD Language).
//!
//! KCL is written in Rust. This crate contains the compiler tooling (e.g. parser, lexer, code generation),
//! the standard library implementation, a LSP implementation, generator for the docs, and more.
#![recursion_limit = "1024"] #![recursion_limit = "1024"]
pub mod ast; pub mod ast;

View File

@ -12,7 +12,7 @@ use crate::{
ArrayExpression, BinaryExpression, BinaryOperator, BinaryPart, BodyItem, CallExpression, CommentStyle, ArrayExpression, BinaryExpression, BinaryOperator, BinaryPart, BodyItem, CallExpression, CommentStyle,
ExpressionStatement, FunctionExpression, Identifier, Literal, LiteralIdentifier, LiteralValue, ExpressionStatement, FunctionExpression, Identifier, Literal, LiteralIdentifier, LiteralValue,
MemberExpression, MemberObject, NonCodeMeta, NonCodeNode, NonCodeValue, ObjectExpression, ObjectProperty, MemberExpression, MemberObject, NonCodeMeta, NonCodeNode, NonCodeValue, ObjectExpression, ObjectProperty,
Parameter, PipeExpression, PipeSubstitution, Program, ReturnStatement, UnaryExpression, UnaryOperator, Value, PipeExpression, PipeSubstitution, Program, ReturnStatement, UnaryExpression, UnaryOperator, Value,
VariableDeclaration, VariableDeclarator, VariableKind, VariableDeclaration, VariableDeclarator, VariableKind,
}, },
errors::{KclError, KclErrorDetails}, errors::{KclError, KclErrorDetails},
@ -1208,62 +1208,26 @@ fn arguments(i: TokenSlice) -> PResult<Vec<Value>> {
.parse_next(i) .parse_next(i)
} }
fn required_param(i: TokenSlice) -> PResult<Token> { fn not_close_paren(i: TokenSlice) -> PResult<Token> {
any.verify(|token: &Token| !matches!(token.token_type, TokenType::Brace) || token.value != ")") any.verify(|token: &Token| !matches!(token.token_type, TokenType::Brace) || token.value != ")")
.parse_next(i) .parse_next(i)
} }
fn optional_param(i: TokenSlice) -> PResult<Token> {
let token = required_param.parse_next(i)?;
let _question_mark = one_of(TokenType::QuestionMark).parse_next(i)?;
Ok(token)
}
/// Parameters are declared in a function signature, and used within a function. /// Parameters are declared in a function signature, and used within a function.
fn parameters(i: TokenSlice) -> PResult<Vec<Parameter>> { fn parameters(i: TokenSlice) -> PResult<Vec<Identifier>> {
// Get all tokens until the next ), because that ends the parameter list. // Get all tokens until the next ), because that ends the parameter list.
let candidates: Vec<_> = separated( let candidates: Vec<Token> = separated(0.., not_close_paren, comma_sep)
0.., .context(expected("function parameters"))
alt((optional_param.map(|t| (t, true)), required_param.map(|t| (t, false)))), .parse_next(i)?;
comma_sep,
)
.context(expected("function parameters"))
.parse_next(i)?;
// Make sure all those tokens are valid parameters. // Make sure all those tokens are valid parameters.
let params: Vec<Parameter> = candidates let params = candidates
.into_iter() .into_iter()
.map(|(token, optional)| { .map(|token| Identifier::try_from(token).and_then(Identifier::into_valid_binding_name))
let identifier = Identifier::try_from(token).and_then(Identifier::into_valid_binding_name)?;
Ok(Parameter { identifier, optional })
})
.collect::<Result<_, _>>() .collect::<Result<_, _>>()
.map_err(|e: KclError| ErrMode::Backtrack(ContextError::from(e)))?; .map_err(|e| ErrMode::Backtrack(ContextError::from(e)))?;
// Make sure optional parameters are last.
if let Err(e) = optional_after_required(&params) {
return Err(ErrMode::Cut(ContextError::from(e)));
}
Ok(params) Ok(params)
} }
fn optional_after_required(params: &[Parameter]) -> Result<(), KclError> {
let mut found_optional = false;
for p in params {
if p.optional {
found_optional = true;
}
if !p.optional && found_optional {
let e = KclError::Syntax(KclErrorDetails {
source_ranges: vec![(&p.identifier).into()],
message: "mandatory parameters must be declared before optional parameters".to_owned(),
});
return Err(e);
}
}
Ok(())
}
impl Identifier { impl Identifier {
fn into_valid_binding_name(self) -> Result<Identifier, KclError> { fn into_valid_binding_name(self) -> Result<Identifier, KclError> {
// Make sure they are not assigning a variable to a stdlib function. // Make sure they are not assigning a variable to a stdlib function.
@ -1931,7 +1895,7 @@ const mySk1 = startSketchAt([0, 0])"#;
let tokens = crate::token::lexer(input); let tokens = crate::token::lexer(input);
let actual = parameters.parse(&tokens); let actual = parameters.parse(&tokens);
assert!(actual.is_ok(), "could not parse test {i}"); assert!(actual.is_ok(), "could not parse test {i}");
let actual_ids: Vec<_> = actual.unwrap().into_iter().map(|p| p.identifier.name).collect(); let actual_ids: Vec<_> = actual.unwrap().into_iter().map(|id| id.name).collect();
assert_eq!(actual_ids, expected); assert_eq!(actual_ids, expected);
} }
} }
@ -2358,82 +2322,6 @@ e
assert!(result.err().unwrap().to_string().contains("Unexpected token")); assert!(result.err().unwrap().to_string().contains("Unexpected token"));
} }
#[test]
fn test_optional_param_order() {
for (i, (params, expect_ok)) in [
(
vec![Parameter {
identifier: Identifier {
start: 0,
end: 0,
name: "a".to_owned(),
},
optional: true,
}],
true,
),
(
vec![Parameter {
identifier: Identifier {
start: 0,
end: 0,
name: "a".to_owned(),
},
optional: false,
}],
true,
),
(
vec![
Parameter {
identifier: Identifier {
start: 0,
end: 0,
name: "a".to_owned(),
},
optional: false,
},
Parameter {
identifier: Identifier {
start: 0,
end: 0,
name: "b".to_owned(),
},
optional: true,
},
],
true,
),
(
vec![
Parameter {
identifier: Identifier {
start: 0,
end: 0,
name: "a".to_owned(),
},
optional: true,
},
Parameter {
identifier: Identifier {
start: 0,
end: 0,
name: "b".to_owned(),
},
optional: false,
},
],
false,
),
]
.into_iter()
.enumerate()
{
let actual = optional_after_required(&params);
assert_eq!(actual.is_ok(), expect_ok, "failed test {i}");
}
}
#[test] #[test]
fn test_parse_expand_array() { fn test_parse_expand_array() {
let code = "const myArray = [0..10]"; let code = "const myArray = [0..10]";
@ -2913,5 +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) }"); snapshot_test!(av, "fn f = (angle) => { return default(angle, 360) }");
} }

View File

@ -29,13 +29,10 @@ expression: actual
"end": 49, "end": 49,
"params": [ "params": [
{ {
"identifier": { "type": "Identifier",
"type": "Identifier", "start": 12,
"start": 12, "end": 17,
"end": 17, "name": "param"
"name": "param"
},
"optional": false
} }
], ],
"body": { "body": {

View File

@ -4,18 +4,18 @@ expression: actual
--- ---
{ {
"start": 0, "start": 0,
"end": 49, "end": 48,
"body": [ "body": [
{ {
"type": "VariableDeclaration", "type": "VariableDeclaration",
"type": "VariableDeclaration", "type": "VariableDeclaration",
"start": 0, "start": 0,
"end": 49, "end": 48,
"declarations": [ "declarations": [
{ {
"type": "VariableDeclarator", "type": "VariableDeclarator",
"start": 3, "start": 3,
"end": 49, "end": 48,
"id": { "id": {
"type": "Identifier", "type": "Identifier",
"start": 3, "start": 3,
@ -26,51 +26,48 @@ expression: actual
"type": "FunctionExpression", "type": "FunctionExpression",
"type": "FunctionExpression", "type": "FunctionExpression",
"start": 7, "start": 7,
"end": 49, "end": 48,
"params": [ "params": [
{ {
"identifier": { "type": "Identifier",
"type": "Identifier", "start": 8,
"start": 8, "end": 13,
"end": 13, "name": "angle"
"name": "angle"
},
"optional": true
} }
], ],
"body": { "body": {
"start": 19, "start": 18,
"end": 49, "end": 48,
"body": [ "body": [
{ {
"type": "ReturnStatement", "type": "ReturnStatement",
"type": "ReturnStatement", "type": "ReturnStatement",
"start": 21, "start": 20,
"end": 47, "end": 46,
"argument": { "argument": {
"type": "CallExpression", "type": "CallExpression",
"type": "CallExpression", "type": "CallExpression",
"start": 28, "start": 27,
"end": 47, "end": 46,
"callee": { "callee": {
"type": "Identifier", "type": "Identifier",
"start": 28, "start": 27,
"end": 35, "end": 34,
"name": "default" "name": "default"
}, },
"arguments": [ "arguments": [
{ {
"type": "Identifier", "type": "Identifier",
"type": "Identifier", "type": "Identifier",
"start": 36, "start": 35,
"end": 41, "end": 40,
"name": "angle" "name": "angle"
}, },
{ {
"type": "Literal", "type": "Literal",
"type": "Literal", "type": "Literal",
"start": 43, "start": 42,
"end": 46, "end": 45,
"value": 360, "value": 360,
"raw": "360" "raw": "360"
} }

1068
yarn.lock

File diff suppressed because it is too large Load Diff