
JScheme Syntax: Literals, Quasi-strings, and S-expressions
JScheme syntax agrees with R4RS Scheme syntax except for the syntax for literals (which incorporates all of the Java literal syntax as well as most of R4RS literal syntax) and string syntax (JScheme allows multi-line strings and various sorts of quasi-strings as well).Literals
Jscheme Literals are as in Scheme with a few extensions (shown in red) which allow a simpler interface with Java.Jscheme also has support for quasi-strings and a heavier but more powerful strong quasi-string notation. These both provide a simple way to generate complex string data in Scheme (e.g. generating HTML, SQL queries, Java code, Jython code,....)
- The null pointer value:
#null
- Boolean values -- the true and false values:
#t #f
- Integer values: we allow all Java syntax for integers, e.g.
12 -- an integer, the default 040 -- octal 0xFFCCAA -- hex 132L -- a long
- Floating point values: we allow all Java floating point syntax,
123.45 -- a double, the default 1.2345E-2 -- scientific notation for doubles 123.45F -- a float 42D -- a double
- Characters: Jscheme allows either Scheme syntax or Java syntax, where the
latter has a prepended # sign:
#\space -- scheme syntax #\a -- scheme syntax #'a' -- java-like syntax #'\n' -- java-like syntax #'\040' -- octal escapes #'\u4FA3' -- unicode escapes
- Strings and Symbols: as in Scheme
"ab\n\040\uAAC4" -- a string with various Java escapes car -- a symbol Math.sin -- a symbol
Quasi-string notation
Quasi-strings provide a mechanism for constructing a complex string where parts of the string are generated by evaluating Scheme expressions.
Quasi-string notation is now the default in Jscheme. There are two flavors of quasi-strings, regular and strong.
Regular Quasi-strings
The regular notation allows you to write code in which {...} denotes a string (instead of "...."). The quasi-string notation also allows you to escape into Scheme using [...] to generate an expression which will be inserted into the string. In quasi-strings you quote the curly braces {} and the square brackets [] which you want to appear literally in the string by preceding them with a backslash:\{ \} \[ \] You don't need to quote the double quotes, which is convenient for HTML generation.
For example, the following procedure creates an li element with class attribute c:> (define (li c x) { - [x] }) li > (li 'warning {Licensing information})
- Licensing information The quasistring notation is converted (at read time) into the following scheme code
- " x " ")) Because []{} have a special role in Jscheme, they must not appear in variables or symbols. There is one exception which is that the pair of characters [] may appear inside symbols names, thus
.
Strong Quasi-string notation
One problem with the quasi-string notation is that you still need to quote the close curly braces and open square brackets that occur within. This is inconvenient when generating program code that contains many curly braces and square brackets (e.g. javascript, CSS, or Java code).
The Strong Quasi-string notation provides a simple solution. The idea is to use #{ and }# instead of { and }, and to escape
from the strong quasi-strings using #[ and ]# instead of [ and ]. For example, we write a program that generates a Java class
> (define (prog version) #{ public class Test { int [] a = new int[] { 1,2,3,4,5}; String version = "#[ version ]#"; public static void main(String[] args) {;} } }# ) (lambda prog (version)...) > (begin (display (prog "abc 6/16/04")) (newline) (newline) 'yes) public class Test { int [] a = new int[] { 1,2,3,4,5}; String version = "abc 6/16/04"; public static void main(String[] args) {;} } yes >
Nothing prevents you from nesting quasi-strings inside strong-quasi-string escapes, or vice-versa, e.g.
#{ some text #[ (list 'and 'an {excape "here" with reg quasi-strings }) ]# more text }#The idea is that strong quasi-strings are useful when quoting program text, but standard quasi-strings are more convenient for other text....
.
S-expressions
JScheme syntax is identical to R4RS Scheme syntax except for the literal and quasi-string syntax described above. In particular, a JScheme program is a sequence of s-expressions.An S-expression is one of the following:
- a literal
boolean, char, short, int, long, float, double, string - a quasistring
{...[---]...[---]... } or #{...#[---]#...#[---]#... } - a sequence of zero or more sexpressions
enclosed in parentheses:
(S1 S2 ... Sn) - a quoted or quasi-quoted expression (as in R4RS Scheme)
'(a b c (()) (de f)) `( 1 () ,(+ 3 4) ,@(3 (4 5) 6) apple )
- a vector (as in R4RS Scheme)
#(1 'red 1.2)