Skip to content

Instantly share code, notes, and snippets.

@RangerMauve
Created March 21, 2016 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RangerMauve/493e510d78df8496f46b to your computer and use it in GitHub Desktop.
Save RangerMauve/493e510d78df8496f46b to your computer and use it in GitHub Desktop.
S Expressions in PEG.js
(
(
[] 1337 "cats" dogs)
(+ 1 0xFF)
)
expression = whitespace? "(" whitespace? atoms:atom* ")" whitespace? {return {type:"expression",value:atoms}}
atom = content:(string / number / identifier / expression) whitespace? {return {type:"atom",value:content}}
string = '"' chars:[^"]* '"' {return {type:"string",value:chars.join("")}}
number = hex / decimal
decimal = digits:[0-9]+ {return {type:"number",base: 10, value:parseInt(digits.join(""),10)};}
hex = "0x" digits:[0-9A-Fa-f]+ {return {type:"number",base: 16, value:parseInt(digits.join(""),16)};}
identifier = content:[^ \t\n\r()]+ {return {type:"identifier",value:content.join("")}}
whitespace
= [ \t\n\r]*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment