From 71e27e032e8a30c3ef50d3494c4bb0793f39c359 Mon Sep 17 00:00:00 2001 From: Lukas Stadler <lukas.stadler@oracle.com> Date: Mon, 16 Oct 2017 13:37:21 +0200 Subject: [PATCH] allow arbitrary user-defined binary ops --- .../truffle/r/parser/ParserGeneration.java | 3 ++- .../src/com/oracle/truffle/r/parser/R.g | 2 +- .../truffle/r/test/ExpectedTestOutput.test | 16 ++++++++++++++++ .../oracle/truffle/r/test/parser/TestParser.java | 8 ++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/ParserGeneration.java b/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/ParserGeneration.java index 2e3a8e7bc7..d8a65710b8 100644 --- a/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/ParserGeneration.java +++ b/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/ParserGeneration.java @@ -94,6 +94,7 @@ public class ParserGeneration { "pass along TruffleRLanguage", "convert line endings", "handle four and more dots as identifier", - "allow greek characters in identifiers" + "allow greek characters in identifiers", + "allow everything but newlines in %<ident>% operators" }; } diff --git a/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g b/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g index 8e11dfacc9..ee30927021 100644 --- a/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g +++ b/com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g @@ -695,7 +695,7 @@ ID | '`' BACKTICK_NAME ; -OP : '%' OP_NAME+ '%' ; +OP : '%' (~('%' | '\n' | '\r' | '\f'))+ '%' ; fragment BACKTICK_NAME @init { final StringBuilder buf = new StringBuilder(); } diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test index 2a5c05f9d5..4231476011 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test @@ -160766,6 +160766,22 @@ non-integer value 12345678909876543212L qualified with L; using numeric value #'\ ' == ' ' [1] TRUE +##com.oracle.truffle.r.test.parser.TestParser.testUserOp# +#`%!@#$^&*()%` <- function(a,b) 1; 10 %!@#$^&*()% 20 +[1] 1 + +##com.oracle.truffle.r.test.parser.TestParser.testUserOp# +#`%5%` <- function(a,b) 1; 10 %5% 20 +[1] 1 + +##com.oracle.truffle.r.test.parser.TestParser.testUserOp# +#`%foo%` <- function(a,b) 1; 10 %foo% 20 +[1] 1 + +##com.oracle.truffle.r.test.parser.TestParser.testUserOp# +#`%Å %` <- function(a,b) 1; 10 %Å % 20 +[1] 1 + ##com.oracle.truffle.r.test.rffi.TestUserRNG.testUserRNG# #{ dyn.load("tmptest/userrng/liburand.so"); RNGkind("user"); print(RNGkind()); set.seed(4567); runif(10) } [1] "user-supplied" "Inversion" diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/parser/TestParser.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/parser/TestParser.java index 9f9de365a6..f2689a28d2 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/parser/TestParser.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/parser/TestParser.java @@ -150,6 +150,14 @@ public class TestParser extends TestBase { assertEval("{ ...... <- 42; cat(......); }"); } + @Test + public void testUserOp() { + assertEval("`%foo%` <- function(a,b) 1; 10 %foo% 20"); + assertEval("`%5%` <- function(a,b) 1; 10 %5% 20"); + assertEval("`%Å %` <- function(a,b) 1; 10 %Å % 20"); + assertEval("`%!@#$^&*()%` <- function(a,b) 1; 10 %!@#$^&*()% 20"); + } + /** * Recursively look for .r source files in the args[0] directory and parse them. */ -- GitLab