Skip to main content

module lang::sexp::\syntax::SExp

rascal-0.40.16

Syntax definition for S-Expressions, based on http://people.csail.mit.edu/rivest/Sexp.txt.

Usage

import lang::sexp::\syntax::SExp;

Dependencies

import String;
import IO;

syntax SExp

start syntax SExp
= string: String
| \list: "(" SExp* ")"
;

syntax String

syntax String
= simple: SimpleString
| display: Display SimpleString
;

syntax SimpleString

syntax SimpleString
= raw: Raw
| token: Token
| base64: Base64
| hex: HexaDecimal
| quoted: QuotedString
;

syntax Display

syntax Display
= bracket "[" SimpleString "]"
;

syntax Raw

syntax Raw
= raw: Decimal >> [:] ":" !>> [\ \t\n\r] Bytes
;

syntax Decimal

lexical Decimal
= [1-9][0-9]* !>> [0-9]
| [0]
;

syntax Bytes

lexical Bytes
= ![]*;

syntax Token

lexical Token
= TokenChar+ !>> [a-zA-Z0-9\-./_:*+=]
;

syntax Base64

syntax Base64
= bracket "|" Base64Char* "|" // nb: whitespace allowed
;

syntax HexaDecimal

syntax HexaDecimal
= bracket "#" HexDigit* "#";

syntax QuotedString

lexical QuotedString
= [\"] QSChar* [\"]
;

syntax QSChar

lexical QSChar
= ![\"\'\\\n\r]
| [\\][btvnfr\"\'\\]
| [\\][0-7][0-7][0-7]
| [\\][x] HexDigit HexDigit
| [\\][\n\r]
| [\\][\r][\n]
| [\\][\n][\r]
;

syntax Whitespace

layout Whitespace
= WS* !>> [\ \t\n\r]
;

syntax WS

lexical WS
= [\ \t\n\r]
;

syntax TokenChar

lexical TokenChar
= Alpha
| DecimalDigit
| SimplePunc
;

syntax Alpha

lexical Alpha
= [a-zA-Z]
;

syntax DecimalDigit

lexical DecimalDigit
= [0-9]
;

syntax HexDigit

lexical HexDigit
= [0-9A-Fa-f]
;

syntax SimplePunc

lexical SimplePunc
= [\-./_:*+=]
;

syntax Base64Char

lexical Base64Char
= Alpha
| DecimalDigit
| [+/=]
;

function raw

Raw raw(Decimal d, Bytes bs)

function unparse

str unparse(Bytes _)

str unparse(Decimal _)