S-Expression

S-Expression Parser, Serializer, and Tree Constructor / Walker Utilities for JavaScript in Browsers and Node.js

Zero-dependencies. Tree structure as plain JSON. Ideal for custom data transfer format and making DSLs.

LICENSE PATREON PAYPAL

SExpr

Class of S-Expression resolver that includes parser, serializer, tree constructors, and tree walker utilities.

Creates an instance of SExpr. Optional options input for configuring default behavior, such as how to recognize null, boolean values as it's up to the programmer to decide the syntax. Nevertheless, here is the default that you can override.

{
 truthy: ['true', '#t'],
 falsy: ['false', '#f'],
 nully: ['null', '#nil']
}
new SExpr(options: any)
Parameters
options (any = {})
Instance Members
context

Public field for programmers to store arbitrary data that might be useful for parsing expressions

context
parse(str)

Parse a S-expression string into a JSON object representing an expression tree

parse(str: string): json
Parameters
str (string) S-expression string
Returns
json: an expression tree in form of list that can include nested lists similar to the structure of the input S-expression
serialize(L)

Serialize an expression tree into an S-expression string

serialize(L: any): any
Parameters
L (any)
Returns
any:
identifier(id)

Create an identifier symbol

identifier(id: string): string
Parameters
id (string)
Returns
string: symbol
Example
const S = new SExpr()
const node = S.expression(S.identifier('a'))
// ['a']
isIdentifier(e, id)

Check if a node is an identifier, optionally compare to a given name

isIdentifier(e: any, id: string): boolean
Parameters
e (any) a node to check
id (string = undefined) optional id name to compare to
Returns
boolean: true if it is an identifier
Example
const S = new SExpr()
const node = S.expression(S.identifier('a'))
console.log(S.isIdentifier(S.first(node)))
// true
console.log(S.isIdentifier(S.first(node, 'a')))
// true
isEqual(a, b)

Compare whether 2 nodes are identical

isEqual(a: any, b: any): boolean
Parameters
a (any) a node
b (any) another node to compare to
Returns
boolean: true if they are the same
expression(exps)

Create an expression node

expression(exps: rest): json
Parameters
exps (rest) optional initialization list of elements
Returns
json: a tree node
isExpression(e, s)

Check if a node is an expression, and optionally compare to a given expression

isExpression(e: any, s: json): boolean
Parameters
e (any) a node to check whether it's an expression
s (json = undefined) optional expression to compare to
Returns
boolean: true if it's an expression (and equals the compared expression if provided)
boolean(v)

Create a boolean node with given state

boolean(v: boolean): string
Parameters
v (boolean) boolean value
Returns
string: a node with name corresponding to a boolean value
isBoolean(e, b)

Check if a node is a boolean value, optionally compare to a given state

isBoolean(e: any, b: boolean): boolean
Parameters
e (any) a node to check whether it's a boolean
b (boolean = undefined) optional state to compare to
Returns
boolean: true if it's a boolean (and equals the given state if provided)
isTruthy(e)

Check if a node is considered truthy. Anything but an explicit false value is truthy.

isTruthy(e: any): boolean
Parameters
e (any) a node to check if it's truthy
Returns
boolean: true if it's truthy
null()

Create a null node.

null(): string
Returns
string: a node with name representing null value
isNull(e)

Check if a node is null.

isNull(e: any): boolean
Parameters
e (any) a node to check if it's null
Returns
boolean: true if it's null
number(n)

Create a number node

number(n: number): number
Parameters
n (number) value of the new node
Returns
number: a node with number value
isNumber(e, n)

Check if a node is a number

isNumber(e: any, n: number): boolean
Parameters
e (any) a node to check if it's a number, optionally compare to a given value
n (number = undefined) an optional value to compare to
Returns
boolean: true if it's a number (and equals the given value if provided)
string(str)

Create a string node.

string(str: string): string
Parameters
str (string) string value of the node
Returns
string: a node with string value
isString(e, s)

Check if a node is a string, optionally compare to a given string.

isString(e: any, s: string): any
Parameters
e (any) a node to check if it's a string
s (string = undefined) optional string to compare to
Returns
any: true if it's a string (and equals the given string if provided)
valueOf(e)

Get a value content of a symbol (not expression).

valueOf(e: any): any
Parameters
e (any) a node to extract value
Returns
any: value
first(e)

Get the first child of a node.

first(e: any): any
Parameters
e (any) a node to get its child
Returns
any: a child node if exists
second(e)

Get the second child of a node.

second(e: any): any
Parameters
e (any) a node to get its child
Returns
any: a child node if exists
third(e)

Get the third child of a node.

third(e: any): any
Parameters
e (any) a node to get its child
Returns
any: a child node if exists
nth(e, n)

Get the n-th child of a node. Similar to the shorthand first, second, third, fourth, fifth ... tenth, but at any position provided.

nth(e: any, n: number): any
Parameters
e (any) a node to get its child
n (number) position of the child node, starting from 1
Returns
any: a child node if exists