Skip to content

Commit

Permalink
Support escaped named arguments (#1103)
Browse files Browse the repository at this point in the history
* wip: wokring on tests for quoted arguments

* feat: support backticks in argument names
  • Loading branch information
EagleoutIce authored Oct 29, 2024
1 parent f7e36f3 commit 0db57ea
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';


/**
Expand Down Expand Up @@ -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: {
Expand Down
109 changes: 48 additions & 61 deletions test/functionality/r-bridge/lang/ast/parse-function-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,67 +193,54 @@ 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',
namespace: undefined,
info: {}
},
lexeme: '\'x\'',
info: {},
value: {
type: RType.Number,
location: rangeFrom(1, 13, 1, 13),
lexeme: '2',
content: numVal(2),
info: {}
}
}
],
}));
for(const quote of ['"', "'", '`']) {
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,
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']),
Expand Down

2 comments on commit 0db57ea

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"artificial" Benchmark Suite

Benchmark suite Current: 0db57ea Previous: 96e933e Ratio
Retrieve AST from R code 238.3782930909091 ms (99.52438414543978) 246.50067822727272 ms (108.0778534921102) 0.97
Normalize R AST 17.098992272727273 ms (30.213296446408247) 17.822801272727272 ms (32.47501104567212) 0.96
Produce dataflow information 60.06176118181818 ms (127.22969252753728) 40.41399131818182 ms (86.99830114822697) 1.49
Total per-file 837.5500917272727 ms (1513.3781002791347) 829.6500495454545 ms (1498.0426737610057) 1.01
Static slicing 2.080660617228449 ms (1.1496579713539947) 2.0971564864344034 ms (1.2085738336418979) 0.99
Reconstruct code 0.23302067708681917 ms (0.17159433434907945) 0.24421041798721546 ms (0.1931884846808244) 0.95
Total per-slice 2.3272067658477518 ms (1.2143447464496093) 2.356082671207842 ms (1.2841644653561477) 0.99
failed to reconstruct/re-parse 0 # 0 # 1
times hit threshold 0 # 0 # 1
reduction (characters) 0.7869360165281424 # 0.7869360165281424 # 1
reduction (normalized tokens) 0.7639690077689504 # 0.7639690077689504 # 1
memory (df-graph) 95.46617542613636 KiB (244.77619956879823) 95.46617542613636 KiB (244.77619956879823) 1

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"social-science" Benchmark Suite

Benchmark suite Current: 0db57ea Previous: 96e933e Ratio
Retrieve AST from R code 250.65810782 ms (48.263411912742654) 239.76002830000002 ms (44.476154875177215) 1.05
Normalize R AST 19.1448689 ms (14.742475422522414) 19.04045506 ms (14.770405721401682) 1.01
Produce dataflow information 74.15739382 ms (69.94235450012927) 74.39786466 ms (87.80796950166253) 1.00
Total per-file 7830.80649482 ms (29076.181856049032) 7666.33215246 ms (28737.408915639426) 1.02
Static slicing 16.14269424853659 ms (44.46248018817527) 15.907723437298863 ms (43.83669809749617) 1.01
Reconstruct code 0.3083234403109085 ms (0.15902978983682572) 0.2509487217116593 ms (0.14943631432024615) 1.23
Total per-slice 16.459660411289956 ms (44.494670756823005) 16.166379499642126 ms (43.873427530614464) 1.02
failed to reconstruct/re-parse 0 # 0 # 1
times hit threshold 0 # 0 # 1
reduction (characters) 0.8712997340230448 # 0.8712997340230448 # 1
reduction (normalized tokens) 0.8102441553774778 # 0.8102441553774778 # 1
memory (df-graph) 99.47955078125 KiB (113.62321662710413) 99.8990234375 KiB (113.72812769327498) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.