On this page:
Term
3.1 Declarations
Declaration
Function 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
3.5 Utility Functions
has-location?
ast-location
ast-source
ast-start
ast-end
@
Property?
Sub Statement?
Source Element?
Identifier=?
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?
Version: 4.1.0.2

3 Abstract Syntax

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

 (require (planet dherman/javascript:6/ast))

(struct

 

Term

 

(location))

  location : (optional region?)

The base structure type of all abstract syntax.

3.1 Declarations

(struct

 

(Declaration Term)

 

(location))

  location : (optional region?)

(struct

 

(FunctionDeclaration Declaration)

 

(location name args body))

  location : (optional/c region?)

  name : Identifier?

  args : (listof Identifier?)

  body : (listof SourceElement?)

(struct

 

(VariableDeclaration Declaration)

 

(location bindings))

  location : (optional/c region?)

  bindings : (nelistof/c VariableInitializer?)

(struct

 

(LetDeclaration Declaration)

 

(location bindings))

  location : (optional/c region?)

  

bindings

 

:

 

(or/c (nelistof/c VariableInitializer?)

      (nelistof/c FunctionDeclaration?))

3.2 Expressions

(struct

 

(Expression Term)

 

(location))

  location : (optional region?)

(struct

 

(StringLiteral Expression)

 

(location value))

  location : (optional/c region?)

  value : string?

(struct

 

(RegexpLiteral Expression)

 (

location

 

 

 

 

pattern

 

 

 

 

global?

 

 

 

 

case-insensitive?))

  location : (optional/c region?)

  pattern : string?

  global? : boolean?

  case-insensitive? : boolean?

(struct

 

(NumericLiteral Expression)

 

(location value))

  location : (optional/c region?)

  value : number?

(struct

 

(BooleanLiteral Expression)

 

(location value))

  location : (optional/c region?)

  value : boolean?

(struct

 

(NullLiteral Expression)

 

(location))

  location : (optional/c region?)

(struct

 

(ArrayLiteral Expression)

 

(location elements))

  location : (optional/c region?)

  elements : (listof (optional/c Expression/X?))

(struct

 

(ObjectLiteral Expression)

 

(location properties))

  location : (optional/c region?)

  properties : (listof (cons/c Property? Expression/X?))

(struct

 

(ThisReference Expression)

 

(location))

  location : (optional/c region?)

(struct

 

(VarReference Expression)

 

(location id))

  location : (optional/c region?)

  id : Identifier?

(struct

 

(BracketReference Expression)

 

(location container key))

  location : (optional/c region?)

  container : Expression/X?

  key : Expression/X?

(struct

 

(DotReference Expression)

 

(location container id))

  location : (optional/c region?)

  container : Expression/X?

  id : Identifier?

(struct

 

(NewExpression Expression)

 

(location constructor arguments))

  location : (optional/c region?)

  constructor : Expression/X?

  arguments : ExpressionList/X?

(struct

 

(PostfixExpression Expression)

 

(location expression operator))

  location : (optional/c region?)

  expression : Expression/X?

  operator : PostfixOperator/c

(struct

 

(PrefixExpression Expression)

 

(location operator expression))

  location : (optional/c region?)

  operator : PrefixOperator/c

  expression : Expression/X?

(struct

 

(InfixExpression Expression)

 

(location left operator right))

  location : (optional/c region?)

  left : Expression/X?

  operator : InfixOperator/c

  right : Expression/X?

(struct

 

(ConditionalExpression Expression)

 (

location

 

 

 

 

test

 

 

 

 

consequent

 

 

 

 

alternate))

  location : (optional/c region?)

  test : Expression/X?

  consequent : Expression/X?

  alternate : Expression/X?

(struct

 

(AssignmentExpression Expression)

 

(location lhs operator rhs))

  location : (optional/c region?)

  lhs : Expression/X?

  operator : AssignmentOperator/c

  rhs : Expression/X?

(struct

 

(FunctionExpression Expression)

 

(location name args body))

  location : (optional/c region?)

  name : (optional/c Identifier?)

  args : (listof Identifier?)

  body : (listof SourceElement?)

(struct

 

(LetExpression Expression)

 

(location bindings body))

  location : (optional/c region?)

  bindings : (listof VariableInitializer?)

  body : Expression/X?

(struct

 

(CallExpression Expression)

 

(location method args))

  location : (optional/c region?)

  method : Expression/X?

  args : ExpressionList/X?

(struct

 

(ParenExpression Expression)

 

(location expression))

  location : (optional/c region?)

  expression : Expression/X?

(struct

 

(ListExpression Expression)

 

(location expressions))

  location : (optional/c region?)

  expressions : ExpressionList/X?

3.3 Statements

(struct

 

(Statement Term)

 

(location))

  location : (optional region?)

(struct

 

(BlockStatement Statement)

 

(location statements))

  location : (optional/c region?)

  statements : SubStatementList/X?

(struct

 

(EmptyStatement Statement)

 

(location))

  location : (optional/c region?)

(struct

 

(ExpressionStatement Statement)

 

(location expression))

  location : (optional/c region?)

  expression : Expression/X?

(struct

 

(IfStatement Statement)

 

(location test consequent alternate))

  location : (optional/c region?)

  test : Expression/X?

  consequent : SubStatement/X?

  alternate : (optional/c SubStatement/X?)

(struct

 

(DoWhileStatement Statement)

 

(location body test))

  location : (optional/c region?)

  body : SubStatement/X?

  test : Expression/X?

(struct

 

(WhileStatement Statement)

 

(location test body))

  location : (optional/c region?)

  test : Expression/X?

  body : SubStatement/X?

(struct

 

(ForStatement Statement)

 

(location init test incr body))

  location : (optional/c region?)

  init : (or/c (optional/c Expression/X?) VariableDeclaration?)

  test : (optional/c Expression/X?)

  incr : (optional/c Expression/X?)

  body : SubStatement/X?

(struct

 

(ForInStatement Statement)

 

(location lhs container body))

  location : (optional/c region?)

  lhs : (or/c Expression/X? VariableDeclaration?)

  container : Expression/X?

  body : SubStatement/X?

(struct

 

(ContinueStatement Statement)

 

(location label))

  location : (optional/c region?)

  label : (optional/c Identifier?)

(struct

 

(BreakStatement Statement)

 

(location label))

  location : (optional/c region?)

  label : (optional/c Identifier?)

(struct

 

(ReturnStatement Statement)

 

(location value))

  location : (optional/c region?)

  value : (optional/c Expression/X?)

(struct

 

(LetStatement Statement)

 

(location bindings body))

  location : (optional/c region?)

  bindings : (listof VariableInitializer?)

  body : SubStatement/X?

(struct

 

(WithStatement Statement)

 

(location context body))

  location : (optional/c region?)

  context : Expression/X?

  body : SubStatement/X?

(struct

 

(SwitchStatement Statement)

 

(location expression cases))

  location : (optional/c region?)

  expression : Expression/X?

  cases : (listof CaseClause?)

(struct

 

(LabelledStatement Statement)

 

(location label statement))

  location : (optional/c region?)

  label : Identifier?

  statement : SubStatement/X?

(struct

 

(ThrowStatement Statement)

 

(location value))

  location : (optional/c region?)

  value : Expression/X?

(struct

 

(TryStatement Statement)

 

(location body catch finally))

  location : (optional/c region?)

  body : Statement/X?

  catch : (listof CatchClause?)

  finally : (optional/c Statement/X?)

3.4 Miscellaneous Terms

(struct

 

(Identifier Term)

 

(location name))

  location : (optional region?)

  name : symbol?

(struct

 

(CaseClause Term)

 

(location question answer))

  location : (optional region?)

  question : (optional Expression/X?)

  answer : SubStatementList/X?

(struct

 

(CatchClause Term)

 

(location id body))

  location : (optional region?)

  id : Identifier?

  body : Statement/X?

(struct

 

(VariableInitializer Term)

 

(location id body))

  location : (optional region?)

  id : Identifier?

  body : Statement/X?

3.5 Utility Functions

(has-location? x)  boolean?

  x : Term?

Determines whether x has source location information.

(ast-location x)  region?

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

(ast-source x)  any

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

(ast-start x)  position?

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

(ast-end x)  position?

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

(@ start-term end-term)  region?

  start-term : Term?

  end-term : Term?

(Property? x)  boolean?

  x : Term?

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

(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.

(SourceElement? x)  boolean?

  x : any

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

(Identifier=? x y)  boolean?

  x : Identifier?

  y : Identifier?

Compares x and y for name equality.

postfix-operators : (listof symbol?)

The postfix operators of JavaScript.

prefix-operators : (listof symbol?)

The prefix operators of JavaScript.

infix-operators : (listof symbol?)

The infix operators of JavaScript.

assignment-operators : (listof symbol?)

The assignment operators of JavaScript.

(assignment-operator->infix-operator op)

  (optional infix-operator?)

  op : assignment-operator?

Produces the binary infix operator corresponding to op.

(postfix-operator? x)  boolean?

  x : any

Determines whether x is a JavaScript postfix operator.

(prefix-operator? x)  boolean?

  x : any

Determines whether x is a JavaScript prefix operator.

(infix-operator? x)  boolean?

  x : any

Determines whether x is a JavaScript infix operator.

(assignment-operator? x)  boolean?

  x : any

Determines whether x is a JavaScript assignment operator.

PostfixOperator/c : flat-contract?

Contract form of postfix-operator?.

PrefixOperator/c : flat-contract?

Contract form of prefix-operator?.

InfixOperator/c : flat-contract?

Contract form of infix-operator?.

AssignmentOperator/c : flat-contract?

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:

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

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

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

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:

(Expression/X? x)  boolean?

  x : any

(Statement/X? x)  boolean?

  x : any

(SubStatement/X? x)  boolean?

  x : any

(ExpressionList/X? x)  boolean?

  x : any

(StatementList/X? x)  boolean?

  x : any

(SubStatementList/X? x)  boolean?

  x : any

(Term/X? x)  boolean?

  x : any