Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validating nomnoml diagrams? #19

Closed
javierluraschi opened this issue Dec 4, 2020 · 2 comments
Closed

Validating nomnoml diagrams? #19

javierluraschi opened this issue Dec 4, 2020 · 2 comments

Comments

@javierluraschi
Copy link
Contributor

Currently there is no way to validate if a nomnoml diagram is valid, say [hello] (valid), vs [hello (invalid).

However, we can use the mlverse/duktape package as a workaround as follows:

remotes::install_github("mlverse/duktape")

Then define the following function:

nomnoml_validate <- function(diagram = "[test]") {
    duktape::eval(
        paste(c(
            readLines(system.file("htmlwidgets/lib/dagre/dagre.min.js", package = "nomnoml")),
            readLines(system.file("htmlwidgets/lib/lodash/lodash.js", package = "nomnoml")),
            readLines(system.file("htmlwidgets/lib/nomnoml/nomnoml.js", package = "nomnoml")),
            paste0("nomnoml.parse('", diagram, "')")
        ), collapse = "\n")
    )
}

And validate nomnoml syntax as follows:

nomnoml_validate("[hello]")

vs

nomnoml_validate("[hello")
 Error in duktape::rcpp_eval(paste(c(readLines(system.file("htmlwidgets/lib/dagre/dagre.min.js",  : 
  Error: Parse error on line 1:
[hello
------^
Expecting 'SEP', '|', ']', got 'EOF' 
@andrie
Copy link
Member

andrie commented Dec 7, 2020

This is great, thank you!

I tried your code, and it works great for the example provided, but I can't get it to work for multi-line diagrams, e.g:

nomnoml_validate("[hello]
                [world]")

results in:

Error in rcpp_eval(code) : SyntaxError: unterminated string (line 19004) 

However, I was able to get the same idea to work using V8 instead of duktape:

nomnoml_validate <- function(diagram = "[test]") {
  ct <- v8()
  ct$source(system.file("htmlwidgets/lib/dagre/dagre.min.js", package = "nomnoml"))
  ct$source(system.file("htmlwidgets/lib/lodash/lodash.js",   package = "nomnoml"))
  ct$source(system.file("htmlwidgets/lib/nomnoml/nomnoml.js", package = "nomnoml"))
  ct$assign("diagram", diagram)
  ct$eval("nomnoml.parse(diagram)"),
}

An invalid diagram:

> nomnoml_validate("[hello")
<std::runtime_error in context_eval(join(src), private$context, serialize): Error: Parse error on line 1:
[hello
------^
Expecting 'SEP', '|', ']', got 'EOF'>

A valid multi-line diagram:

> nomnoml_validate("[hello]
+                 [world]")
[1] "[object Object]"

Is there any disadvantage to use V8 compared to duktape?

@andrie
Copy link
Member

andrie commented Dec 11, 2020

I implemented this in #21

@andrie andrie closed this as completed Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants