@@ -216,12 +216,12 @@ extract_occurrence <- function(pd) {
216
216
if (! is.null(data_dependency )) {
217
217
return (data_dependency )
218
218
}
219
-
219
+
220
220
assign_dependency <- handle_assign_call(pd )
221
221
if (! is.null(assign_dependency )) {
222
222
return (assign_dependency )
223
223
}
224
-
224
+
225
225
# Process general assignment expressions
226
226
extract_general_assignment(pd )
227
227
}
@@ -239,7 +239,7 @@ handle_data_call <- function(pd) {
239
239
if (data_call_pos == 0 ) {
240
240
return (NULL )
241
241
}
242
-
242
+
243
243
# Extract the object name from data(object)
244
244
object_name <- pd [data_call_pos + 1 , " text" ]
245
245
# Remove quotes if present: data("object") -> object
@@ -260,12 +260,12 @@ handle_assign_call <- function(pd) {
260
260
if (assign_call_pos == 0 ) {
261
261
return (NULL )
262
262
}
263
-
263
+
264
264
param_position <- get_assign_param_position(pd )
265
265
if (param_position == 0 ) {
266
266
return (character (0L ))
267
267
}
268
-
268
+
269
269
# Extract the assigned object name
270
270
object_name <- pd [assign_call_pos + param_position , " text" ]
271
271
# Remove quotes if present: assign("object", value) -> object
@@ -285,14 +285,14 @@ handle_assign_call <- function(pd) {
285
285
get_assign_param_position <- function (pd ) {
286
286
# Check if any parameters are named
287
287
has_named_params <- any(pd $ token == " SYMBOL_SUB" )
288
-
288
+
289
289
if (has_named_params ) {
290
290
# Extract parameter tokens: named parameters (SYMBOL_SUB), commas, equals
291
291
param_tokens <- pd [pd $ token %in% c(" SYMBOL_SUB" , " ','" , " EQ_SUB" ), " text" ]
292
-
292
+
293
293
# Clean up sequences of "=" followed by ","
294
294
cleaned_params <- remove_consecutive_equals_comma(param_tokens )
295
-
295
+
296
296
# Find position of 'x' parameter among named parameters
297
297
pos <- match(" x" , setdiff(cleaned_params , " ," ), nomatch = 0 )
298
298
if (pos == 0 ) {
@@ -316,14 +316,14 @@ remove_consecutive_equals_comma <- function(params) {
316
316
if (length(params ) < = 1 ) {
317
317
return (params )
318
318
}
319
-
319
+
320
320
indices_to_remove <- integer(0 )
321
321
for (i in 2 : length(params )) {
322
322
if (params [i - 1 ] == " =" && params [i ] == " ," ) {
323
323
indices_to_remove <- c(indices_to_remove , i - 1 , i )
324
324
}
325
325
}
326
-
326
+
327
327
if (length(indices_to_remove ) > 0 ) {
328
328
params [- indices_to_remove ]
329
329
} else {
@@ -343,25 +343,25 @@ remove_consecutive_equals_comma <- function(params) {
343
343
extract_general_assignment <- function (pd ) {
344
344
# Filter out symbols that are function parameters
345
345
filtered_pd <- pd [! is_symbol_in_function_body(pd ), ]
346
-
346
+
347
347
# Find all symbol positions
348
348
symbol_indices <- which(filtered_pd $ token %in% c(" SPECIAL" , " SYMBOL" , " SYMBOL_FUNCTION_CALL" ))
349
349
function_call_indices <- which(filtered_pd $ token == " SYMBOL_FUNCTION_CALL" )
350
-
350
+
351
351
if (length(symbol_indices ) == 0 ) {
352
352
return (character (0L ))
353
353
}
354
-
354
+
355
355
# Remove symbols that come after $ or @ operators (e.g., in x$a, remove 'a')
356
356
symbol_indices <- exclude_symbols_after_operators(filtered_pd , symbol_indices )
357
-
357
+
358
358
# Look for assignment operators
359
359
assignment_indices <- grep(" ASSIGN" , filtered_pd $ token )
360
360
if (length(assignment_indices ) == 0 ) {
361
361
# No assignment found, return all symbols as dependencies
362
362
return (c(" <-" , unique(filtered_pd [symbol_indices , " text" ])))
363
363
}
364
-
364
+
365
365
# Process assignment expression
366
366
process_assignment_expression(filtered_pd , symbol_indices , function_call_indices , assignment_indices , pd )
367
367
}
@@ -377,7 +377,7 @@ is_symbol_in_function_body <- function(pd) {
377
377
if (length(function_ids ) == 0 ) {
378
378
return (rep(FALSE , nrow(pd )))
379
379
}
380
-
380
+
381
381
# Get all children of function definitions
382
382
function_children <- get_children(pd , function_ids [1 ])$ id
383
383
pd $ id %in% function_children
@@ -396,11 +396,11 @@ exclude_symbols_after_operators <- function(pd, symbol_indices) {
396
396
if (length(operator_ids ) == 0 ) {
397
397
return (symbol_indices )
398
398
}
399
-
399
+
400
400
# For x$a, a's ID is $'s ID-2, so we exclude symbols with ID = operator_ID - 2
401
401
symbol_ids <- pd [symbol_indices , " id" ]
402
402
symbols_after_operators <- symbol_ids [(symbol_ids - 2 ) %in% operator_ids ]
403
-
403
+
404
404
# Remove these symbols from our indices
405
405
setdiff(symbol_indices , which(pd $ id %in% symbols_after_operators ))
406
406
}
@@ -409,7 +409,7 @@ exclude_symbols_after_operators <- function(pd, symbol_indices) {
409
409
# '
410
410
# ' @param pd `data.frame` filtered parse data
411
411
# ' @param symbol_indices Integer vector of symbol positions
412
- # ' @param function_call_indices Integer vector of function call positions
412
+ # ' @param function_call_indices Integer vector of function call positions
413
413
# ' @param assignment_indices Integer vector of assignment operator positions
414
414
# ' @param original_pd `data.frame` original parse data for bracket processing
415
415
# ' @return Character vector with dependency information
@@ -418,19 +418,19 @@ exclude_symbols_after_operators <- function(pd, symbol_indices) {
418
418
process_assignment_expression <- function (pd , symbol_indices , function_call_indices , assignment_indices , original_pd ) {
419
419
# Remove function calls that appear before assignment (e.g., in eval(expression(c <- b)))
420
420
symbol_indices <- symbol_indices [! (symbol_indices < min(assignment_indices ) & symbol_indices %in% function_call_indices )]
421
-
421
+
422
422
# Handle right-to-left assignment (->) by reversing symbol order
423
423
if (unique(pd $ text [assignment_indices ]) == " ->" ) {
424
424
symbol_indices <- rev(symbol_indices )
425
425
}
426
-
426
+
427
427
# Build dependency vector with assignment operator in correct position
428
428
assignment_pos <- match(min(pd $ id [assignment_indices ]), sort(pd $ id [c(min(assignment_indices ), symbol_indices )])) - 1
429
429
dependency_vector <- append(pd [symbol_indices , " text" ], " <-" , after = max(1 , assignment_pos ))
430
-
430
+
431
431
# Move function names to right side of dependency arrow
432
432
dependency_vector <- move_functions_after_arrow(dependency_vector , unique(pd [function_call_indices , " text" ]))
433
-
433
+
434
434
# Handle symbols in brackets/parentheses
435
435
bracket_symbols <- extract_symbols_in_brackets(original_pd )
436
436
if (length(bracket_symbols ) > 0 ) {
@@ -452,10 +452,10 @@ extract_symbols_in_brackets <- function(pd) {
452
452
if (! has_brackets ) {
453
453
return (NULL )
454
454
}
455
-
455
+
456
456
start_id <- min(pd $ id [pd $ token %in% c(" LBB" , " '['" )])
457
457
end_id <- min(pd $ id [pd $ token == " ']'" ])
458
-
458
+
459
459
# Extract symbols between brackets
460
460
pd $ text [pd $ token == " SYMBOL" & pd $ id > start_id & pd $ id < end_id ]
461
461
}
0 commit comments