Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Encoding: UTF-8
Package: ppn
Type: Package
Title: Portable Piecepack Notation Parser
Version: 0.3.0-1
Version: 0.3.0-2
Authors@R: c(person("Trevor L.", "Davis", role=c("aut", "cre"),
email="trevor.l.davis@gmail.com",
comment = c(ORCID = "0000-0001-6341-4639")))
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ppn 0.3.0 (development)
New features
------------

* Macros may now reference other macros (recursive macros). Macro values are fully expanded at parse time; circular references produce an error.
* We now export (and document their arguments) the following movetext parsers:

+ `piecepack_parser()`
Expand Down
2 changes: 1 addition & 1 deletion R/default_parser.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ create_state <- function(df, metadata = list()) {
}
as.environment(list(
df_move_start = df,
macros = c(metadata$Macros, attr(df, "macros"), macros),
macros = expand_macro_values(c(metadata$Macros, attr(df, "macros"), macros)),
max_id = nrow(df),
active_id = character(),
scale_factor = as.numeric(scale_factor)
Expand Down
33 changes: 31 additions & 2 deletions R/parse_moves.R
Original file line number Diff line number Diff line change
Expand Up @@ -1170,10 +1170,39 @@ get_y <- function(coords) {
}
}

expand_macro_values <- function(macros) {
max_iter <- 100L
for (i in seq_len(max_iter)) {
changed <- FALSE
for (name in names(macros)) {
value <- macros[[name]]
ml <- str_locate_all(value, "`[^']+'")[[1L]]
if (nrow(ml) == 0L) {
next
}
for (r in rev(seq_len(nrow(ml)))) {
ref <- str_sub(value, ml[r, 1L] + 1L, ml[r, 2L] - 1L)
if (!is.null(macros[[ref]])) {
str_sub(value, ml[r, 1L], ml[r, 2L]) <- macros[[ref]]
changed <- TRUE
}
}
macros[[name]] <- value
}
if (!changed) {
break
}
if (i == max_iter) {
abort("Circular macro reference detected", class = "circular_macro")
}
}
macros
}

replace_macros <- function(df, text, state = create_state(df)) {
ml <- str_locate_all(text, "`[^']+'")[[1]]
ml <- str_locate_all(text, "`[^']+'")[[1L]]
for (r in rev(seq_len(nrow(ml)))) {
mtext <- str_sub(text, ml[r, 1] + 1, ml[r, 2] - 1)
mtext <- str_sub(text, ml[r, 1L] + 1L, ml[r, 2L] - 1L)
if (is.null(state$macros[[mtext]])) {
abort(paste("Macro", mtext, "is unknown"), class = "identify_macro")
}
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/ppn.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,11 @@



# Setup and GameType work as expected

Code
default_parser(c("1. `a'@b2"), meta_circular)
Condition
Error in `expand_macro_values()`:
! Circular macro reference detected

26 changes: 26 additions & 0 deletions tests/testthat/test_ppn.R
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,32 @@ test_that("Setup and GameType work as expected", {
expect_equal(df$suit, 1)
expect_equal(df$piece_side, "pawn_face")

# macro expanding to multiple submoves (e.g. chess castling)
meta_castle <- list(
GameType = "International Chess",
Macros = list("O-O" = "e1-g1 h1-f1")
)
g_castle <- default_parser(c("1. `O-O'"), meta_castle)
df_before <- g_castle$dfs[[1]]
df_after <- g_castle$dfs[[2]]
king_id <- df_before$id[near(df_before$x, 5) & near(df_before$y, 1)]
rook_id <- df_before$id[near(df_before$x, 8) & near(df_before$y, 1)]
expect_equal(df_after$x[df_after$id == king_id], 7)
expect_equal(df_after$x[df_after$id == rook_id], 6)

# recursive macro: macro value references another macro
meta_recursive <- list(Macros = list(sun = "S", sp = "`sun'p"))
df_recursive <- default_parser(c("1. `sp'@b2"), meta_recursive)$dfs[[2]]
expect_equal(df_recursive$piece_side, "pawn_face")
expect_equal(df_recursive$suit, 1L)

# circular macro reference is an error
meta_circular <- list(Macros = list(a = "`b'", b = "`a'"))
expect_snapshot(
error = TRUE,
default_parser(c("1. `a'@b2"), meta_circular)
)

none1 <- ""
df1 <- read_ppn(textConnection(none1))[[1]]$dfs[[1]]
expect_equal(nrow(df1), 0)
Expand Down
Loading