KCL: Keyword fn args like "x = 1" not like "x: 1" (#4770)

Aligns with how we're doing objects.
This commit is contained in:
Adam Chalmers
2024-12-12 11:53:35 -06:00
committed by GitHub
parent 05163fdded
commit 191b9b71fd
21 changed files with 79 additions and 68 deletions

View File

@ -2256,12 +2256,16 @@ fn arguments(i: &mut TokenSlice) -> PResult<Vec<Expr>> {
}
fn labeled_argument(i: &mut TokenSlice) -> PResult<LabeledArg> {
separated_pair(identifier, (one_of(TokenType::Colon), opt(whitespace)), expression)
.map(|(label, arg)| LabeledArg {
label: label.inner,
arg,
})
.parse_next(i)
separated_pair(
terminated(identifier, opt(whitespace)),
terminated(one_of((TokenType::Operator, "=")), opt(whitespace)),
expression,
)
.map(|(label, arg)| LabeledArg {
label: label.inner,
arg,
})
.parse_next(i)
}
/// Arguments are passed into a function,
@ -4057,7 +4061,7 @@ let myBox = box([0,0], -3, -16, -10)
#[test]
fn kw_fn() {
for input in ["val = foo(x, y: z)", "val = foo(y: z)"] {
for input in ["val = foo(x, y = z)", "val = foo(y = z)"] {
let module_id = ModuleId::default();
let tokens = crate::parsing::token::lex(input, module_id).unwrap();
super::program.parse(tokens.as_slice()).unwrap();
@ -4497,8 +4501,8 @@ my14 = 4 ^ 2 - 3 ^ 2 * 2
r#"x = 3
obj = { x, y: 4}"#
);
snapshot_test!(kw_function_unnamed_first, r#"val = foo(x, y: z)"#);
snapshot_test!(kw_function_all_named, r#"val = foo(x: a, y: b)"#);
snapshot_test!(kw_function_unnamed_first, r#"val = foo(x, y = z)"#);
snapshot_test!(kw_function_all_named, r#"val = foo(x = a, y = b)"#);
snapshot_test!(kw_function_decl_all_labeled, r#"fn foo(x, y) { return 1 }"#);
snapshot_test!(kw_function_decl_first_unlabeled, r#"fn foo(@x, y) { return 1 }"#);
snapshot_test!(kw_function_decl_with_default_no_type, r#"fn foo(x? = 2) { return 1 }"#);

View File

@ -1,12 +1,13 @@
---
source: kcl/src/parsing/parser.rs
expression: actual
snapshot_kind: text
---
{
"body": [
{
"declaration": {
"end": 21,
"end": 23,
"id": {
"end": 3,
"name": "val",
@ -22,9 +23,9 @@ expression: actual
"name": "x"
},
"arg": {
"end": 14,
"end": 15,
"name": "a",
"start": 13,
"start": 14,
"type": "Identifier",
"type": "Identifier"
}
@ -36,9 +37,9 @@ expression: actual
"name": "y"
},
"arg": {
"end": 20,
"end": 22,
"name": "b",
"start": 19,
"start": 21,
"type": "Identifier",
"type": "Identifier"
}
@ -50,7 +51,7 @@ expression: actual
"start": 6,
"type": "Identifier"
},
"end": 21,
"end": 23,
"start": 6,
"type": "CallExpressionKw",
"type": "CallExpressionKw",
@ -59,13 +60,13 @@ expression: actual
"start": 0,
"type": "VariableDeclarator"
},
"end": 21,
"end": 23,
"kind": "const",
"start": 0,
"type": "VariableDeclaration",
"type": "VariableDeclaration"
}
],
"end": 21,
"end": 23,
"start": 0
}

View File

@ -1,12 +1,13 @@
---
source: kcl/src/parsing/parser.rs
expression: actual
snapshot_kind: text
---
{
"body": [
{
"declaration": {
"end": 18,
"end": 19,
"id": {
"end": 3,
"name": "val",
@ -22,9 +23,9 @@ expression: actual
"name": "y"
},
"arg": {
"end": 17,
"end": 18,
"name": "z",
"start": 16,
"start": 17,
"type": "Identifier",
"type": "Identifier"
}
@ -36,7 +37,7 @@ expression: actual
"start": 6,
"type": "Identifier"
},
"end": 18,
"end": 19,
"start": 6,
"type": "CallExpressionKw",
"type": "CallExpressionKw",
@ -51,13 +52,13 @@ expression: actual
"start": 0,
"type": "VariableDeclarator"
},
"end": 18,
"end": 19,
"kind": "const",
"start": 0,
"type": "VariableDeclaration",
"type": "VariableDeclaration"
}
],
"end": 18,
"end": 19,
"start": 0
}

View File

@ -23,9 +23,9 @@ pub async fn rem(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kc
/// If `num` is negative, the result will be too.
///
/// ```no_run
/// assertEqual(rem(7, divisor: 4), 3, 0.01, "remainder is 3")
/// assertEqual(rem(-7, divisor: 4), -3, 0.01, "remainder is 3")
/// assertEqual(rem(7, divisor: -4), 3, 0.01, "remainder is 3")
/// assertEqual(rem( 7, divisor = 4), 3, 0.01, "remainder is 3" )
/// assertEqual(rem(-7, divisor = 4), -3, 0.01, "remainder is -3")
/// assertEqual(rem( 7, divisor = -4), 3, 0.01, "remainder is 3" )
/// ```
#[stdlib {
name = "rem",

View File

@ -272,7 +272,7 @@ impl LabeledArg {
fn recast(&self, options: &FormatOptions, indentation_level: usize, ctxt: ExprContext) -> String {
let label = &self.label.name;
let arg = self.arg.recast(options, indentation_level, ctxt);
format!("{label}: {arg}")
format!("{label} = {arg}")
}
}

View File

@ -196,7 +196,7 @@ snapshot_kind: text
},
{
"declaration": {
"end": 122,
"end": 123,
"id": {
"end": 103,
"name": "three",
@ -212,9 +212,9 @@ snapshot_kind: text
"name": "delta"
},
"arg": {
"end": 121,
"end": 122,
"raw": "2",
"start": 120,
"start": 121,
"type": "Literal",
"type": "Literal",
"value": 2.0
@ -227,7 +227,7 @@ snapshot_kind: text
"start": 106,
"type": "Identifier"
},
"end": 122,
"end": 123,
"start": 106,
"type": "CallExpressionKw",
"type": "CallExpressionKw",
@ -243,14 +243,14 @@ snapshot_kind: text
"start": 98,
"type": "VariableDeclarator"
},
"end": 122,
"end": 123,
"kind": "const",
"start": 98,
"type": "VariableDeclaration",
"type": "VariableDeclaration"
}
],
"end": 123,
"end": 124,
"nonCodeMeta": {
"nonCodeNodes": {
"0": [

View File

@ -7,4 +7,4 @@ fn add(@x, delta) {
}
two = increment(1)
three = add(1, delta: 2)
three = add(1, delta = 2)

View File

@ -330,8 +330,8 @@ snapshot_kind: text
},
{
"sourceRange": [
120,
121,
122,
0
]
}

View File

@ -85,7 +85,7 @@ snapshot_kind: text
},
{
"declaration": {
"end": 50,
"end": 51,
"id": {
"end": 38,
"name": "three",
@ -101,9 +101,9 @@ snapshot_kind: text
"name": "x"
},
"arg": {
"end": 49,
"end": 50,
"raw": "1",
"start": 48,
"start": 49,
"type": "Literal",
"type": "Literal",
"value": 1.0
@ -116,7 +116,7 @@ snapshot_kind: text
"start": 41,
"type": "Identifier"
},
"end": 50,
"end": 51,
"start": 41,
"type": "CallExpressionKw",
"type": "CallExpressionKw",
@ -125,14 +125,14 @@ snapshot_kind: text
"start": 33,
"type": "VariableDeclarator"
},
"end": 50,
"end": 51,
"kind": "const",
"start": 33,
"type": "VariableDeclaration",
"type": "VariableDeclaration"
}
],
"end": 51,
"end": 52,
"nonCodeMeta": {
"nonCodeNodes": {
"0": [

View File

@ -12,6 +12,6 @@ KCL Semantic error
2 │ │ return x + y
3 │ ╰─▶ }
4 │
5 │ three = add(x: 1)
· ─────────
5 │ three = add(x = 1)
· ─────────
╰────

View File

@ -2,4 +2,4 @@ fn add(x, y) {
return x + y
}
three = add(x: 1)
three = add(x = 1)

View File

@ -78,7 +78,7 @@ snapshot_kind: text
},
{
"declaration": {
"end": 46,
"end": 47,
"id": {
"end": 34,
"name": "two",
@ -94,9 +94,9 @@ snapshot_kind: text
"name": "x"
},
"arg": {
"end": 45,
"end": 46,
"raw": "1",
"start": 44,
"start": 45,
"type": "Literal",
"type": "Literal",
"value": 1.0
@ -109,7 +109,7 @@ snapshot_kind: text
"start": 37,
"type": "Identifier"
},
"end": 46,
"end": 47,
"start": 37,
"type": "CallExpressionKw",
"type": "CallExpressionKw",
@ -118,14 +118,14 @@ snapshot_kind: text
"start": 31,
"type": "VariableDeclarator"
},
"end": 46,
"end": 47,
"kind": "const",
"start": 31,
"type": "VariableDeclaration",
"type": "VariableDeclaration"
}
],
"end": 47,
"end": 48,
"nonCodeMeta": {
"nonCodeNodes": {
"0": [

View File

@ -12,6 +12,6 @@ KCL Semantic error
2 │ │ return x + 1
3 │ ╰─▶ }
4 │
5 │ two = add(x: 1)
· ─────────
5 │ two = add(x = 1)
· ─────────
╰────

View File

@ -2,4 +2,4 @@ fn add(@x) {
return x + 1
}
two = add(x: 1)
two = add(x = 1)

View File

@ -132,7 +132,7 @@ snapshot_kind: text
},
{
"declaration": {
"end": 98,
"end": 99,
"id": {
"end": 75,
"name": "twentyOne",
@ -148,9 +148,9 @@ snapshot_kind: text
"name": "by"
},
"arg": {
"end": 97,
"end": 98,
"raw": "20",
"start": 95,
"start": 96,
"type": "Literal",
"type": "Literal",
"value": 20.0
@ -163,7 +163,7 @@ snapshot_kind: text
"start": 78,
"type": "Identifier"
},
"end": 98,
"end": 99,
"start": 78,
"type": "CallExpressionKw",
"type": "CallExpressionKw",
@ -179,14 +179,14 @@ snapshot_kind: text
"start": 66,
"type": "VariableDeclarator"
},
"end": 98,
"end": 99,
"kind": "const",
"start": 66,
"type": "VariableDeclaration",
"type": "VariableDeclaration"
}
],
"end": 99,
"end": 100,
"nonCodeMeta": {
"nonCodeNodes": {
"0": [

View File

@ -3,4 +3,4 @@ fn increment(@x, by? = 1) {
}
two = increment(1)
twentyOne = increment(1, by: 20)
twentyOne = increment(1, by = 20)

View File

@ -148,8 +148,8 @@ snapshot_kind: text
},
{
"sourceRange": [
95,
97,
96,
98,
0
]
}

View File

@ -77,8 +77,8 @@ fn setLength = (state, _q) => {
fn Gt2 = (state) => { return setLength(state, state.currentLength * state.factor) }
fn Lt2 = (state) => { return setLength(state, state.currentLength / state.factor) }
fn Add = (state) => { return setAngle(state, rem(int(state.currentAngle - state.angle), divisor: 360)) }
fn Sub = (state) => { return setAngle(state, rem(int(state.currentAngle + state.angle), divisor: 360)) }
fn Add = (state) => { return setAngle(state, rem(int(state.currentAngle - state.angle), divisor = 360)) }
fn Sub = (state) => { return setAngle(state, rem(int(state.currentAngle + state.angle), divisor = 360)) }
// Only necessary to get around recursion limitations...
fn F = (state, F) => {