From 6ddf8c349073451e63b5345e5a5a0700d490dcdd Mon Sep 17 00:00:00 2001 From: EagleoutIce Date: Fri, 25 Oct 2024 13:52:09 +0200 Subject: [PATCH 1/2] wip: wokring on tests for quoted arguments --- .../r-bridge/lang/ast/parse-function-call.ts | 104 ++++++++---------- 1 file changed, 45 insertions(+), 59 deletions(-) diff --git a/test/functionality/r-bridge/lang/ast/parse-function-call.ts b/test/functionality/r-bridge/lang/ast/parse-function-call.ts index 24d275a75e..c043cd7dc1 100644 --- a/test/functionality/r-bridge/lang/ast/parse-function-call.ts +++ b/test/functionality/r-bridge/lang/ast/parse-function-call.ts @@ -193,67 +193,53 @@ describe('Parse function calls', withShell(shell => { ], }) ); - assertAst(label('string arguments', ['name-normal', 'call-normal', 'string-arguments', 'strings']), - shell,'f("a"=3,\'x\'=2)', - exprList({ - type: RType.FunctionCall, - named: true, - location: rangeFrom(1, 1, 1, 1), - lexeme: 'f', - info: {}, - functionName: { - type: RType.Symbol, - location: rangeFrom(1, 1, 1, 1), - lexeme: 'f', - content: 'f', - namespace: undefined, - info: {} - }, - arguments: [ - { - type: RType.Argument, - location: rangeFrom(1, 3, 1, 5), - name: { - type: RType.Symbol, - location: rangeFrom(1, 3, 1, 5), - lexeme: '"a"', - content: 'a', - namespace: undefined, - info: {} - }, - lexeme: '"a"', - info: {}, - value: { - type: RType.Number, - location: rangeFrom(1, 7, 1, 7), - lexeme: '3', - content: numVal(3), - info: {} - } - }, - { - type: RType.Argument, - location: rangeFrom(1, 9, 1, 11), - name: { - type: RType.Symbol, - location: rangeFrom(1, 9, 1, 11), - lexeme: '\'x\'', - content: 'x', + for(const quote of ['"', "'", '`']) { + // TODO: describe quote + for (const firstArgName of ['a', 'a b', 'a(1)']) { + const argLength = firstArgName.length; + const arg = `${quote}${firstArgName}${quote}` + assertAst(label(`escaped argument (${firstArgName})`, ['name-normal', 'call-normal', 'string-arguments', 'strings']), + shell, `f(${arg}=3)`, + exprList({ + type: RType.FunctionCall, + named: true, + location: rangeFrom(1, 1, 1, 1), + lexeme: 'f', + info: {}, + functionName: { + type: RType.Symbol, + location: rangeFrom(1, 1, 1, 1), + lexeme: 'f', + content: 'f', namespace: undefined, - info: {} + info: {} }, - lexeme: '\'x\'', - info: {}, - value: { - type: RType.Number, - location: rangeFrom(1, 13, 1, 13), - lexeme: '2', - content: numVal(2), - info: {} - } - } - ], - })); + arguments: [ + { + type: RType.Argument, + location: rangeFrom(1, 3, 1, 4 + argLength), + name: { + type: RType.Symbol, + location: rangeFrom(1, 3, 1, 4 + argLength), + lexeme: arg, + content: firstArgName, + namespace: undefined, + info: {} + }, + lexeme: arg, + info: {}, + value: { + type: RType.Number, + location: rangeFrom(1, 4 + argLength + 2, 1, 4 + argLength + 2), + lexeme: '3', + content: numVal(3), + info: {} + } + } + ] + })); + } + } }); describe('directly called functions', () => { assertAst(label('Directly call with 2', ['call-anonymous', 'formals-named', 'numbers', 'name-normal', 'normal-definition', 'grouping']), From c597d254701784d2e5293627533e9510294b7609 Mon Sep 17 00:00:00 2001 From: Florian Sihler Date: Fri, 25 Oct 2024 15:19:48 +0200 Subject: [PATCH 2/2] feat: support backticks in argument names --- .../internal/functions/normalize-argument.ts | 3 +- .../r-bridge/lang/ast/parse-function-call.ts | 85 ++++++++++--------- 2 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/r-bridge/lang-4.x/ast/parser/main/internal/functions/normalize-argument.ts b/src/r-bridge/lang-4.x/ast/parser/main/internal/functions/normalize-argument.ts index c0525578dd..e1fb6b6cc3 100644 --- a/src/r-bridge/lang-4.x/ast/parser/main/internal/functions/normalize-argument.ts +++ b/src/r-bridge/lang-4.x/ast/parser/main/internal/functions/normalize-argument.ts @@ -9,6 +9,7 @@ import type { RSymbol } from '../../../../model/nodes/r-symbol'; import { RawRType, RType } from '../../../../model/type'; import { normalizeSingleNode } from '../structure/normalize-single-node'; import type { NamedJsonEntry } from '../../../json/format'; +import { startAndEndsWith } from '../../../../../../../util/strings'; /** @@ -38,7 +39,7 @@ export function tryToNormalizeArgument(data: NormalizerData, objs: readonly Name name = { type: RType.Symbol, location, - content: symbolOrExpr.name === RawRType.StringConst ? content.slice(1,-1) : content, + content: symbolOrExpr.name === RawRType.StringConst ? content.slice(1,-1) : (startAndEndsWith(content, '`') ? content.slice(1, -1) : content), namespace: undefined, lexeme: content, info: { diff --git a/test/functionality/r-bridge/lang/ast/parse-function-call.ts b/test/functionality/r-bridge/lang/ast/parse-function-call.ts index c043cd7dc1..3263446a0b 100644 --- a/test/functionality/r-bridge/lang/ast/parse-function-call.ts +++ b/test/functionality/r-bridge/lang/ast/parse-function-call.ts @@ -194,51 +194,52 @@ describe('Parse function calls', withShell(shell => { }) ); for(const quote of ['"', "'", '`']) { - // TODO: describe quote - for (const firstArgName of ['a', 'a b', 'a(1)']) { - const argLength = firstArgName.length; - const arg = `${quote}${firstArgName}${quote}` - assertAst(label(`escaped argument (${firstArgName})`, ['name-normal', 'call-normal', 'string-arguments', 'strings']), - shell, `f(${arg}=3)`, - exprList({ - type: RType.FunctionCall, - named: true, - location: rangeFrom(1, 1, 1, 1), - lexeme: 'f', - info: {}, - functionName: { - type: RType.Symbol, - location: rangeFrom(1, 1, 1, 1), - lexeme: 'f', - content: 'f', - namespace: undefined, - info: {} - }, - arguments: [ - { - type: RType.Argument, - location: rangeFrom(1, 3, 1, 4 + argLength), - name: { - type: RType.Symbol, + describe(`Escaped Arguments Using Quote ${quote}`, () => { + for(const firstArgName of ['a', 'a b', 'a(1)']) { + const argLength = firstArgName.length; + const arg = `${quote}${firstArgName}${quote}`; + assertAst(label(`${firstArgName}`, ['name-normal', 'call-normal', 'string-arguments', 'strings']), + shell, `f(${arg}=3)`, + exprList({ + type: RType.FunctionCall, + named: true, + location: rangeFrom(1, 1, 1, 1), + lexeme: 'f', + info: {}, + functionName: { + type: RType.Symbol, + location: rangeFrom(1, 1, 1, 1), + lexeme: 'f', + content: 'f', + namespace: undefined, + info: {} + }, + arguments: [ + { + type: RType.Argument, location: rangeFrom(1, 3, 1, 4 + argLength), + name: { + type: RType.Symbol, + location: rangeFrom(1, 3, 1, 4 + argLength), + lexeme: arg, + content: firstArgName, + namespace: undefined, + info: {} + }, lexeme: arg, - content: firstArgName, - namespace: undefined, - info: {} - }, - lexeme: arg, - info: {}, - value: { - type: RType.Number, - location: rangeFrom(1, 4 + argLength + 2, 1, 4 + argLength + 2), - lexeme: '3', - content: numVal(3), - info: {} + info: {}, + value: { + type: RType.Number, + location: rangeFrom(1, 4 + argLength + 2, 1, 4 + argLength + 2), + lexeme: '3', + content: numVal(3), + info: {} + } } - } - ] - })); - } + ] + })); + } + }); } }); describe('directly called functions', () => {