Jeffrey Arnold avatar Jeffrey Arnold committed ed53063

SLexer: allowed for escaped quotes, added better example file

Comments (0)

Files changed (2)

pygments/lexers/math.py

             (r'[a-zA-Z][0-9a-zA-Z\._]+', Text),
             # can begin with ., but not if that is followed by a digit
             (r'\.[a-zA-Z_][0-9a-zA-Z\._]+', Text),
-            (r'`.+`', String.Backtick),
         ],
         'punctuation': [
             (r'\[{1,2}|\]{1,2}|\(|\)|;|,', Punctuation),
              Keyword.Reserved)
         ],
         'operators': [
-            (r'<<?-|->>?|-|==|<=|>=|<|>|&&?|!=|\|\|?', Operator),
+            (r'<<?-|->>?|-|==|<=|>=|<|>|&&?|!=|\|\|?|\?', Operator),
             (r'\*|\+|\^|/|!|%[^%]*%|=|~|\$|@|:{1,3}', Operator)
         ],
         'builtin_symbols': [
             (r'(T|F)\b', Keyword.Variable),
         ],
         'numbers': [
+            # hex number
+            (r'0[xX][a-fA-F0-9]+([pP][0-9]+)?[Li]?', Number.Hex),
+            # decimal number
             (r'[+-]?([0-9]+(\.[0-9]+)?|\.[0-9]+)([eE][+-]?[0-9]+)?[Li]?',
              Number),
-            # hex number
-            (r'0[xX][a-fA-F0-9]+([pP][0-9]+)?[Li]?', Number.Hex),
         ],
         'statements': [
             include('comments'),
             # whitespaces
             (r'\s+', Text),
+            (r'`.*?`', String.Backtick),
             (r'\'', String, 'string_squote'),
             (r'\"', String, 'string_dquote'),
             include('builtin_symbols'),
         #    ('\}', Punctuation, '#pop')
         #],
         'string_squote': [
-            (r'[^\']*\'', String, '#pop'),
+            (r'([^\'\\]|\\.)*\'', String, '#pop'),
         ],
         'string_dquote': [
-            (r'[^\"]*\"', String, '#pop'),
+            (r'([^"\\]|\\.)*"', String, '#pop'),
         ],
     }
 

tests/examplefiles/test.R

-###################################
-#######  emplikH1.test() ##########
-###################################
+#!/usr/bin/env Rscript
+### Example R script for syntax highlighting
 
-emplikH1.test <- function(x, d, theta, fun,
-	              tola = .Machine$double.eps^.25)
-{
-n <- length(x)
-if( n <= 2 ) stop("Need more observations")
-if( length(d) != n ) stop("length of x and d must agree")
-if(any((d!=0)&(d!=1))) stop("d must be 0/1's for censor/not-censor")
-if(!is.numeric(x)) stop("x must be numeric values --- observed times")
+# This is also a comment
 
-#temp<-summary(survfit(Surv(x,d),se.fit=F,type="fleming",conf.type="none"))
-#
-newdata <- Wdataclean2(x,d)
-temp <- DnR(newdata$value, newdata$dd, newdata$weight)
+## Valid names
+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV0123456789._a <- NULL
+.foo_ <- NULL
+._foo <- NULL
 
-time <- temp$time         # only uncensored time?  Yes.
-risk <- temp$n.risk
-jump <- (temp$n.event)/risk
+## Invalid names
+0abc <- NULL
+.0abc <- NULL
+abc+cde <- NULL
 
-funtime <- fun(time)
-funh <- (n/risk) * funtime    # that is Zi
-funtimeTjump <- funtime * jump
+## Reserved Words
+NA
+NA_integer_
+NA_real_
+NA_character_
+NA_complex_
+NULL
+NaN
+Inf
+## Not reserved
+NULLa <- NULL
+NULL1 <- NULL
+NULL. <- NULL
+NA_foo_ <- NULL
 
-if(jump[length(jump)] >= 1) funh[length(jump)] <- 0  #for inthaz and weights
+## Numbers
+12345678901
+123456.78901
+123e3
+123E3
+1.23e-3
+1.23e3
+1.23e-3
+## integer constants
+123L
+1.23L
+## imaginary numbers
+123i
+-123i
+123e4i
+123e-4i
+## Hex numbers
+0xabcdefABCDEF01234
+0xabcp123
+0xabcP123
+## Not hex
+0xg
 
-inthaz <- function(x, ftj, fh, thet){ sum(ftj/(1 + x * fh)) - thet }
+## Special operators %xyz%
+## %xyz%
+1 %% 2
+diag(2) %*% diag(2)
+1 %/% 2
+1 %in% 1:10
+diag(2) %o% diag(2)
+diag(2) %x% diag(2)
+`%foo bar%` <- function(x, y) x + y
+1 %foo bar% 2
 
-diff <- inthaz(0, funtimeTjump, funh, theta)
+## Control Structures (3.2) and Function
+## if, else
+if (TRUE) print("foo") else print("bar")
+## For, in
+for(i in 1:5) {
+    print(i)
+}
+## While, break
+i <- 1
+while (TRUE) {
+    i <- i + 1
+    if (i > 3) break
+}
+## Repeat
+repeat {1+1}
+## Switch
+x <- 3
+switch(x, 2+2, mean(1:10), rnorm(5))
+## Function, dot-dot-dot, return
+foo <- function(...) {
+    return(sum(...))
+}
+# Not keywords
+functiona <- 2 + 2
+function. <- 2 + 2
+function1 <- 2 + 2
 
-if( diff == 0 ) { lam <- 0 } else {
-    step <- 0.2/sqrt(n)
-    if(abs(diff) > 6*log(n)*step )
-    stop("given theta value is too far away from theta0")
 
-    mini<-0
-    maxi<-0
-    if(diff > 0) {
-    maxi <- step
-    while(inthaz(maxi, funtimeTjump, funh, theta) > 0 && maxi < 50*log(n)*step)
-    maxi <- maxi+step
-    }
-    else {
-    mini <- -step
-    while(inthaz(mini, funtimeTjump, funh, theta) < 0 && mini > - 50*log(n)*step)
-    mini <- mini - step
-    }
-
-    if(inthaz(mini, funtimeTjump, funh, theta)*inthaz(maxi, funtimeTjump, funh, theta) > 0 )
-    stop("given theta is too far away from theta0")
-
-    temp2 <- uniroot(inthaz,c(mini,maxi), tol = tola,
-                  ftj=funtimeTjump, fh=funh, thet=theta)
-    lam <- temp2$root
+## Grouping Tokens 10.3.7
+## Parentheses
+1 + (2 + 3)
+## brackets
+foo <- function(a) {
+    a + 1
 }
 
-onepluslamh<- 1 + lam * funh   ### this is 1 + lam Zi in Ref.
+## Indexing 10.3.8
+## []
+bar <- 1:10
+bar[3]
+## [[]]
+foo <- list(a=1, b=2, c=3)
+foo[["a"]]
+## $
+foo$a
+foo$"a"
 
-weights <- jump/onepluslamh  #need to change last jump to 1? NO. see above
+## Operators
+2 - 2
+2 + 2
+2 ~ 2
+! TRUE
+?"help"
+1:2
+2 * 2
+2 / 2
+2^2
+2 < 2
+2 > 2
+2 == 2
+2 >= 2
+2 <= 2
+2 != 2
+TRUE & FALSE
+TRUE && FALSE
+TRUE | FALSE
+TRUE || FALSE
+foo <- 2 + 2
+2 + 2 -> foo
+foo <<- 2 + 2
+2 + 2 ->> foo
 
-loglik <- 2*(sum(log(onepluslamh)) - sum((onepluslamh-1)/onepluslamh) )
-#?is that right? YES  see (3.2) in Ref. above. This ALR, or Poisson LR.
+## Strings
+foo <- "hello, world!"
+foo <- 'hello, world!'
+foo <- "Hello, 'world!"
+foo <- 'Hello, "world!'
+foo <- 'Hello, \'world!\''
+foo <- "Hello, \"world!\""
+foo <- "Hello,
+world!"
+foo <- 'Hello,
+world!'
 
-#last <- length(jump)    ## to compute loglik2, we need to drop last jump
-#if (jump[last] == 1) {
-#                     risk1 <- risk[-last]
-#                     jump1 <- jump[-last]
-#                     weights1 <- weights[-last]
-#                     } else {
-#                            risk1 <- risk
-#                            jump1 <- jump
-#                            weights1 <- weights
-#                            }
-#loglik2 <- 2*( sum(log(onepluslamh)) +
-#          sum( (risk1 -1)*log((1-jump1)/(1- weights1) ) )  )
-##? this likelihood seems have negative values sometimes???
-
-list( logemlik=loglik,  ### logemlikv2=loglik2,
-      lambda=lam, times=time, wts=weights,
-      nits=temp2$nf, message=temp2$message )
-}
-
-library("graphics")
-
-par(mfrow = c(1, 2))
-# plot histogram
-x <- rnorm(100)
-if (max(x) > 100)
-  stop("Quite unexpected.")
-else
-  hist(x, plot=TRUE, col="ivory")
-
-# from doc: lowess
-plot(cars, main = "lowess(cars)")
-     lines(lowess(cars), col = 2)
-     lines(lowess(cars, f=.2), col = 3)
-     legend(5, 120, c(paste("f = ", c("2/3", ".2"))), lty = 1, col = 2:3)
-
-# from doc: is.na
-is.na(c(1, NA))
-
-# from doc: Extract
-y <- list(1,2,a=4,5)
-y[c(3,4)]             # a list containing elements 3 and 4 of y
-y$a                   # the element of y named a
-
-# from doc: for
-for(n in c(2,5,10,20,50)) {
-  x <- stats::rnorm(n)
-  cat(n,":", sum(x2),"\n")
-}
-
-class(fo <- y ~ x1*x2) # "formula"
-
-
-
-
+## Backtick strings
+`foo123 +!"bar'baz` <- 2 + 2
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.