On this page:
Term
3.1 Declarations
Declaration
Function  Declaration
Import  Declaration
Export  Declaration
Variable  Declaration
Let  Declaration
3.2 Expressions
Expression
String  Literal
Regexp  Literal
Numeric  Literal
Boolean  Literal
Null  Literal
Array  Literal
Object  Literal
This  Reference
Var  Reference
Bracket  Reference
Dot  Reference
New  Expression
Postfix  Expression
Prefix  Expression
Infix  Expression
Conditional  Expression
Assignment  Expression
Function  Expression
Let  Expression
Call  Expression
Paren  Expression
List  Expression
3.3 Statements
Statement
Block  Statement
Empty  Statement
Expression  Statement
If  Statement
Do  While  Statement
While  Statement
For  Statement
For  In  Statement
Continue  Statement
Break  Statement
Return  Statement
Let  Statement
With  Statement
Switch  Statement
Labelled  Statement
Throw  Statement
Try  Statement
3.4 Miscellaneous Terms
Identifier
Case  Clause
Catch  Clause
Variable  Initializer
Import  Specifier
Import  Binding
Exclusion  List
Module  Specifier
Reexport  Specifier
Export  Bindings
3.5 Utility Functions
has-location?
ast-location
ast-source
ast-start
ast-end
@
with-location
Property?
Sub  Statement?
Source  Element?
Identifier=?
Term=?
postfix-operators
prefix-operators
infix-operators
assignment-operators
assignment-operator->infix-operator
postfix-operator?
prefix-operator?
infix-operator?
assignment-operator?
Postfix  Operator/  c
Prefix  Operator/  c
Infix  Operator/  c
Assignment  Operator/  c
3.6 Extending the Language
Expression-predicates
Statement-predicates
Expression  List-predicates
Statement  List-predicates
Expression/  X?
Statement/  X?
Sub  Statement/  X?
Expression  List/  X?
Statement  List/  X?
Sub  Statement  List/  X?
Term/  X?

3 Abstract Syntax🔗ℹ

This library provides an extensible hierarchy of structure types representing abstract JavaScript syntax. It can be required via:

 (require javascript/ast) package: javascript

struct

(struct Term (location)
    #:extra-constructor-name make-Term)
  location : (maybe/c region?)
The base structure type of all abstract syntax.

3.1 Declarations🔗ℹ

struct

(struct Declaration Term (location)
    #:extra-constructor-name make-Declaration)
  location : (maybe/c region?)

struct

(struct FunctionDeclaration Declaration (location name args body)
    #:extra-constructor-name make-FunctionDeclaration)
  location : (maybe/c region?)
  name : Identifier?
  args : (listof Identifier?)
  body : (listof SourceElement?)

struct

(struct ImportDeclaration Declaration (location specifiers)
    #:extra-constructor-name make-ImportDeclaration)
  location : (maybe/c region?)
  specifiers : (listof ImportSpecifier?)

struct

(struct ExportDeclaration Declaration (location specifiers)
    #:extra-constructor-name make-ExportDeclaration)
  location : (maybe/c region?)
  specifiers : (listof (or/c ExclusionList? ReexportSpecifier? ExportBindings? Identifier?))

struct

(struct VariableDeclaration Declaration (location bindings)
    #:extra-constructor-name make-VariableDeclaration)
  location : (maybe/c region?)
  bindings : (nelistof/c VariableInitializer?)

struct

(struct LetDeclaration Declaration (location bindings)
    #:extra-constructor-name make-LetDeclaration)
  location : (maybe/c region?)
  bindings : 
(or/c (nelistof/c VariableInitializer?)
      (nelistof/c FunctionDeclaration?))

3.2 Expressions🔗ℹ

struct

(struct Expression Term (location)
    #:extra-constructor-name make-Expression)
  location : (maybe/c region?)

struct

(struct StringLiteral Expression (location value)
    #:extra-constructor-name make-StringLiteral)
  location : (maybe/c region?)
  value : string?

struct

(struct RegexpLiteral Expression (location
    pattern
    global?
    case-insensitive?)
    #:extra-constructor-name make-RegexpLiteral)
  location : (maybe/c region?)
  pattern : string?
  global? : boolean?
  case-insensitive? : boolean?

struct

(struct NumericLiteral Expression (location value)
    #:extra-constructor-name make-NumericLiteral)
  location : (maybe/c region?)
  value : number?

struct

(struct BooleanLiteral Expression (location value)
    #:extra-constructor-name make-BooleanLiteral)
  location : (maybe/c region?)
  value : boolean?

struct

(struct NullLiteral Expression (location)
    #:extra-constructor-name make-NullLiteral)
  location : (maybe/c region?)

struct

(struct ArrayLiteral Expression (location elements)
    #:extra-constructor-name make-ArrayLiteral)
  location : (maybe/c region?)
  elements : (listof (maybe/c Expression/X?))

struct

(struct ObjectLiteral Expression (location properties)
    #:extra-constructor-name make-ObjectLiteral)
  location : (maybe/c region?)
  properties : (listof (cons/c Property? Expression/X?))

struct

(struct ThisReference Expression (location)
    #:extra-constructor-name make-ThisReference)
  location : (maybe/c region?)

struct

(struct VarReference Expression (location id)
    #:extra-constructor-name make-VarReference)
  location : (maybe/c region?)
  id : Identifier?

struct

(struct BracketReference Expression (location container key)
    #:extra-constructor-name make-BracketReference)
  location : (maybe/c region?)
  container : Expression/X?
  key : Expression/X?

struct

(struct DotReference Expression (location container id)
    #:extra-constructor-name make-DotReference)
  location : (maybe/c region?)
  container : Expression/X?
  id : Identifier?

struct

(struct NewExpression Expression (location constructor arguments)
    #:extra-constructor-name make-NewExpression)
  location : (maybe/c region?)
  constructor : Expression/X?
  arguments : ExpressionList/X?

struct

(struct PostfixExpression Expression (location expression operator)
    #:extra-constructor-name make-PostfixExpression)
  location : (maybe/c region?)
  expression : Expression/X?
  operator : PostfixOperator/c

struct

(struct PrefixExpression Expression (location operator expression)
    #:extra-constructor-name make-PrefixExpression)
  location : (maybe/c region?)
  operator : PrefixOperator/c
  expression : Expression/X?

struct

(struct InfixExpression Expression (location left operator right)
    #:extra-constructor-name make-InfixExpression)
  location : (maybe/c region?)
  left : Expression/X?
  operator : InfixOperator/c
  right : Expression/X?

struct

(struct ConditionalExpression Expression (location
    test
    consequent
    alternate)
    #:extra-constructor-name make-ConditionalExpression)
  location : (maybe/c region?)
  test : Expression/X?
  consequent : Expression/X?
  alternate : Expression/X?

struct

(struct AssignmentExpression Expression (location lhs operator rhs)
    #:extra-constructor-name make-AssignmentExpression)
  location : (maybe/c region?)
  lhs : Expression/X?
  operator : AssignmentOperator/c
  rhs : Expression/X?

struct

(struct FunctionExpression Expression (location name args body)
    #:extra-constructor-name make-FunctionExpression)
  location : (maybe/c region?)
  name : (maybe/c Identifier?)
  args : (listof Identifier?)
  body : (listof SourceElement?)

struct

(struct LetExpression Expression (location bindings body)
    #:extra-constructor-name make-LetExpression)
  location : (maybe/c region?)
  bindings : (listof VariableInitializer?)
  body : Expression/X?

struct

(struct CallExpression Expression (location method args)
    #:extra-constructor-name make-CallExpression)
  location : (maybe/c region?)
  method : Expression/X?
  args : ExpressionList/X?

struct

(struct ParenExpression Expression (location expression)
    #:extra-constructor-name make-ParenExpression)
  location : (maybe/c region?)
  expression : Expression/X?

struct

(struct ListExpression Expression (location expressions)
    #:extra-constructor-name make-ListExpression)
  location : (maybe/c region?)
  expressions : ExpressionList/X?

3.3 Statements🔗ℹ

struct

(struct Statement Term (location)
    #:extra-constructor-name make-Statement)
  location : (maybe/c region?)

struct

(struct BlockStatement Statement (location statements)
    #:extra-constructor-name make-BlockStatement)
  location : (maybe/c region?)
  statements : SubStatementList/X?

struct

(struct EmptyStatement Statement (location)
    #:extra-constructor-name make-EmptyStatement)
  location : (maybe/c region?)

struct

(struct ExpressionStatement Statement (location expression)
    #:extra-constructor-name make-ExpressionStatement)
  location : (maybe/c region?)
  expression : Expression/X?

struct

(struct IfStatement Statement (location test consequent alternate)
    #:extra-constructor-name make-IfStatement)
  location : (maybe/c region?)
  test : Expression/X?
  consequent : SubStatement/X?
  alternate : (maybe/c SubStatement/X?)

struct

(struct DoWhileStatement Statement (location body test)
    #:extra-constructor-name make-DoWhileStatement)
  location : (maybe/c region?)
  body : SubStatement/X?
  test : Expression/X?

struct

(struct WhileStatement Statement (location test body)
    #:extra-constructor-name make-WhileStatement)
  location : (maybe/c region?)
  test : Expression/X?
  body : SubStatement/X?

struct

(struct ForStatement Statement (location init test incr body)
    #:extra-constructor-name make-ForStatement)
  location : (maybe/c region?)
  init : (or/c (maybe/c Expression/X?) VariableDeclaration? LetDeclaration?)
  test : (maybe/c Expression/X?)
  incr : (maybe/c Expression/X?)
  body : SubStatement/X?

struct

(struct ForInStatement Statement (location lhs container body)
    #:extra-constructor-name make-ForInStatement)
  location : (maybe/c region?)
  lhs : (or/c Expression/X? VariableDeclaration? LetDeclaration?)
  container : Expression/X?
  body : SubStatement/X?

struct

(struct ContinueStatement Statement (location label)
    #:extra-constructor-name make-ContinueStatement)
  location : (maybe/c region?)
  label : (maybe/c Identifier?)

struct

(struct BreakStatement Statement (location label)
    #:extra-constructor-name make-BreakStatement)
  location : (maybe/c region?)
  label : (maybe/c Identifier?)

struct

(struct ReturnStatement Statement (location value)
    #:extra-constructor-name make-ReturnStatement)
  location : (maybe/c region?)
  value : (maybe/c Expression/X?)

struct

(struct LetStatement Statement (location bindings body)
    #:extra-constructor-name make-LetStatement)
  location : (maybe/c region?)
  bindings : (listof VariableInitializer?)
  body : SubStatement/X?

struct

(struct WithStatement Statement (location context body)
    #:extra-constructor-name make-WithStatement)
  location : (maybe/c region?)
  context : Expression/X?
  body : SubStatement/X?

struct

(struct SwitchStatement Statement (location expression cases)
    #:extra-constructor-name make-SwitchStatement)
  location : (maybe/c region?)
  expression : Expression/X?
  cases : (listof CaseClause?)

struct

(struct LabelledStatement Statement (location label statement)
    #:extra-constructor-name make-LabelledStatement)
  location : (maybe/c region?)
  label : Identifier?
  statement : SubStatement/X?

struct

(struct ThrowStatement Statement (location value)
    #:extra-constructor-name make-ThrowStatement)
  location : (maybe/c region?)
  value : Expression/X?

struct

(struct TryStatement Statement (location body catch finally)
    #:extra-constructor-name make-TryStatement)
  location : (maybe/c region?)
  body : Statement/X?
  catch : (listof CatchClause?)
  finally : (maybe/c Statement/X?)

3.4 Miscellaneous Terms🔗ℹ

struct

(struct Identifier Term (location name)
    #:extra-constructor-name make-Identifier)
  location : (maybe/c region?)
  name : symbol?

struct

(struct CaseClause Term (location question answer)
    #:extra-constructor-name make-CaseClause)
  location : (maybe/c region?)
  question : (maybe/c Expression/X?)
  answer : SubStatementList/X?

struct

(struct CatchClause Term (location id body)
    #:extra-constructor-name make-CatchClause)
  location : (maybe/c region?)
  id : Identifier?
  body : Statement/X?

struct

(struct VariableInitializer Term (location id init)
    #:extra-constructor-name make-VariableInitializer)
  location : (maybe/c region?)
  id : Identifier?
  init : (maybe/c Expression/X?)

struct

(struct ImportSpecifier Term (location module bindings)
    #:extra-constructor-name make-ImportSpecifier)
  location : (maybe/c region?)
  module : ModuleSpecifier?
  bindings : (or/c Identifier? (listof ImportBinding?) ExclusionList?)

struct

(struct ImportBinding Term (location label binding)
    #:extra-constructor-name make-ImportBinding)
  location : (maybe/c region?)
  label : Identifier?
  binding : (maybe/c Identifier?)

struct

(struct ExclusionList Term (location ids)
    #:extra-constructor-name make-ExclusionList)
  location : (maybe/c region?)
  ids : (listof Identifier?)

struct

(struct ModuleSpecifier Term (location protocol elements)
    #:extra-constructor-name make-ModuleSpecifier)
  location : (maybe/c region?)
  protocol : (or/c 'file 'planet 'collect)
  elements : (listof (or/c string? integer? symbol? #f))

struct

(struct ReexportSpecifier Term (location module exclusions)
    #:extra-constructor-name make-ReexportSpecifier)
  location : (maybe/c region?)
  module : ModuleSpecifier?
  exclusions : ExclusionList?

struct

(struct ExportBindings Term (location bindings)
    #:extra-constructor-name make-ExportBindings)
  location : (maybe/c region?)
  bindings : (listof (cons/c Identifier? (maybe/c Expression/X?)))

3.5 Utility Functions🔗ℹ

procedure

(has-location? x)  boolean?

  x : Term?
Determines whether x has source location information.

procedure

(ast-location x)  region?

  x : (and/c Term? has-location?)

procedure

(ast-source x)  any

  x : (and/c Term? has-location?)

procedure

(ast-start x)  position?

  x : (and/c Term? has-location?)

procedure

(ast-end x)  position?

  x : (and/c Term? has-location?)

procedure

(@ start-term end-term)  region?

  start-term : Term?
  end-term : Term?

procedure

(with-location loc x)  Term?

  loc : (maybe/c region?)
  x : Term?

procedure

(Property? x)  boolean?

  x : Term?
Determines whether x can be used as a property name in an object literal.

procedure

(SubStatement? x)  boolean?

  x : any
Determines whether x is a valid sub-statement, i.e., a statement not in the top-level position of a function or program.

procedure

(SourceElement? x)  boolean?

  x : any
Determines whether x is a source element, i.e., a statement or declaration.

procedure

(Identifier=? x y)  boolean?

  x : Identifier?
  y : Identifier?
Compares x and y for name equality.

procedure

(Term=? x y)  boolean?

  x : Term?
  y : Term?
Recursively compares two terms, ignoring all source location information and comparing all non-Term components with equal?.

The postfix operators of JavaScript.
The prefix operators of JavaScript.
The infix operators of JavaScript.
The assignment operators of JavaScript.
Produces the binary infix operator corresponding to op.

procedure

(postfix-operator? x)  boolean?

  x : any
Determines whether x is a JavaScript postfix operator.

procedure

(prefix-operator? x)  boolean?

  x : any
Determines whether x is a JavaScript prefix operator.

procedure

(infix-operator? x)  boolean?

  x : any
Determines whether x is a JavaScript infix operator.

procedure

(assignment-operator? x)  boolean?

  x : any
Determines whether x is a JavaScript assignment operator.
Contract form of postfix-operator?.
Contract form of prefix-operator?.
Contract form of infix-operator?.
Contract form of assignment-operator?.

3.6 Extending the Language🔗ℹ

Some libraries in this package support extending the language. The notions of expression, statement, expression list, and statement list can be extended by adding custom predicates to the following parameters:

value

Expression-predicates : (parameter/c (listof (any -> boolean?)))

value

Statement-predicates : (parameter/c (listof (any -> boolean?)))

value

ExpressionList-predicates

 : (parameter/c (listof (any -> boolean?)))

value

StatementList-predicates

 : (parameter/c (listof (any -> boolean?)))

The following predicates represent extensible terms by checking an argument against the standard predicates followed by the corresponding custom predicates:

procedure

(Expression/X? x)  boolean?

  x : any

procedure

(Statement/X? x)  boolean?

  x : any

procedure

(SubStatement/X? x)  boolean?

  x : any

procedure

(ExpressionList/X? x)  boolean?

  x : any

procedure

(StatementList/X? x)  boolean?

  x : any

procedure

(SubStatementList/X? x)  boolean?

  x : any

procedure

(Term/X? x)  boolean?

  x : any