knitr::opts_chunk$set(echo = TRUE)
options(keep.source = TRUE)
library(straw)

plot_ast <- . %>%
  parse(text = .) %>%
  create_parse_graph(type = "ast") %>%
  tbl_to_cyto() %>%
  cytoscape_dagre(width = "100%")

plot_parse <- . %>%
  parse(text = .) %>%
  create_parse_graph() %>%
  tbl_to_cyto() %>%
  cytoscape_dagre(width = "100%")

Functions

expr <- parse(text = "function(a = 1) {2}")
# function node consists of 3 entries
#   1. function keyword
#   2. function args (a pairlist)
#   3. function body (expression | anything)
fun <- expr[[1]]

# the pairlist is the trickiest to handle,
# since in the parse tree each arg is a node
fun[[2]]
wzxhzdk:3
wzxhzdk:4
g <- create_parse_graph(expr)
g_ast <- create_parse_graph(expr, type = 'ast')

match_expr(g, g_ast, g[2,], g_ast[2,])

Nesting exceptions

function formals

expr <- "function(a, b = 1) 2"
wzxhzdk:7
wzxhzdk:8

= operator

Need to dig down in AST

expr2 <- "a + 1= 1 = 2 = 3; 1 + 1"
wzxhzdk:10
wzxhzdk:11
expr3 <- "
a = 1 + 2
b = 3
"
wzxhzdk:13
wzxhzdk:14
p3 <- parse(text = expr3)
p3[[1]]
pd <- create_parse_graph(p3)
pd[pd$children[[1]],]
expr4 <- "function(a) b = 1"
wzxhzdk:17
wzxhzdk:18
wzxhzdk:19
wzxhzdk:20


machow/straw documentation built on Sept. 9, 2020, 12:40 a.m.