tests/testthat/references/calls.R

# ------------------------------------------------------------------------------
# subset

foo[bar]
foo[1, 2]
foo[1, ]
foo[1,, ]
foo[1,,2]
foo[x=1,,y=3,4]
foo[]

# ------------------------------------------------------------------------------
# subset2

foo[[x]]
foo[[x, y]]
foo[[x,]]
foo[[x,,]]
foo[[x,,y]]
foo[[]]

# ------------------------------------------------------------------------------
# subset and subset2 precedence

a[[b[1]]]
a[b[[1]]]

# ------------------------------------------------------------------------------
# switch

switch(foo,
  x = 1,
  "y" = 2,
  z = ,
  3
)

# ------------------------------------------------------------------------------
# calls

f()
f(x)
f(1+1)
f(1 ~ 1)
f(x, )
f(x,,y)
f(x, y)
f(x, y = 2)
f(x = 1 + 1)
f(x, y =)
f(f2(x, y))
f(,)
f(x,)
f(,y)
f(x=,)
f("x"=,)
f(,y=)

# Dots as unnamed and named argument
f(...)
f(, ..., 1)
f(... = 1)
f(... = ,)
f(... = ...)

# `..i` as unnamed and named argument
f(..1)
f(, ..1, 1)
f(..1 = 1)
f(..1 = ,)
f(..1 = ..1)

# ------------------------------------------------------------------------------
# not a call, subset, or subset2

f
(x)

foo
[bar]

foo
[[x]]

# ------------------------------------------------------------------------------
# not a call, subset, or subset2 due to sequential arguments

f(x y)
foo[x y]
foo[[x y]]

f(x, y z)
foo[x, y z]
foo[[x, y z]]

f(, x y)
foo[, x y]
foo[[, x y]]

# ------------------------------------------------------------------------------
# braced expression

{}

{1}

{1; 2}

{1;
2}

{1
2
}

{
1
2
}

# https://github.com/r-lib/tree-sitter-r/issues/44
{
  1
2
  3
}

# ------------------------------------------------------------------------------
# parenthesized expression

(1)
((1))
(1 + 1)
(fn(a, b))
fn((a), (b))
(function() {
  body
})

# ------------------------------------------------------------------------------
# not a parenthesized expression 1

()

# ------------------------------------------------------------------------------
# not a parenthesized expression 2

(
  1
  2
)

# ------------------------------------------------------------------------------
# not a parenthesized expression 3

(1; 2)

Try the treesitter.r package in your browser

Any scripts or data that you put into this service are public.

treesitter.r documentation built on June 8, 2025, 2:03 p.m.