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:9:1/ast)) |
(struct Term (location)) |
location : (optional/c region?) |
The base structure type of all abstract syntax.
3.1 Declarations
(struct (Declaration Term) (location)) |
location : (optional/c region?) |
(struct (FunctionDeclaration Declaration) (location name args body)) |
location : (optional/c region?) |
name : Identifier? |
args : (listof Identifier?) |
body : (listof SourceElement?) |
(struct (ImportDeclaration Declaration) (location specifiers)) |
location : (optional/c region?) |
specifiers : (listof ImportSpecifier?) |
(struct (ExportDeclaration Declaration) (location specifiers)) |
location : (optional/c region?) |
specifiers : (listof (or/c ExclusionList? ReexportSpecifier? ExportBindings? Identifier?)) |
(struct (VariableDeclaration Declaration) (location bindings)) |
location : (optional/c region?) |
bindings : (nelistof/c VariableInitializer?) |
(struct (LetDeclaration Declaration) (location bindings)) | ||||||||
location : (optional/c region?) | ||||||||
|
3.2 Expressions
(struct (Expression Term) (location)) |
location : (optional/c region?) |
(struct (StringLiteral Expression) (location value)) |
location : (optional/c region?) |
value : string? |
| ||||||||||||||||||||
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? |
| ||||||||||||||||||||
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/c 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? LetDeclaration?) |
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? LetDeclaration?) |
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/c region?) |
name : symbol? |
(struct (CaseClause Term) (location question answer)) |
location : (optional/c region?) |
question : (optional/c Expression/X?) |
answer : SubStatementList/X? |
(struct (CatchClause Term) (location id body)) |
location : (optional/c region?) |
id : Identifier? |
body : Statement/X? |
(struct (VariableInitializer Term) (location id init)) |
location : (optional/c region?) |
id : Identifier? |
init : (optional/c Expression/X?) |
(struct (ImportSpecifier Term) (location module bindings)) |
location : (optional/c region?) |
module : ModuleSpecifier? |
bindings : (or/c Identifier? (listof ImportBinding?) ExclusionList?) |
(struct (ImportBinding Term) (location label binding)) |
location : (optional/c region?) |
label : Identifier? |
binding : (optional/c Identifier?) |
(struct (ExclusionList Term) (location ids)) |
location : (optional/c region?) |
ids : (listof Identifier?) |
(struct (ModuleSpecifier Term) (location protocol elements)) |
location : (optional/c region?) |
protocol : (or/c 'file 'planet 'collect) |
elements : (listof (or/c string? integer? symbol? #f)) |
(struct (ReexportSpecifier Term) (location module exclusions)) |
location : (optional/c region?) |
module : ModuleSpecifier? |
exclusions : ExclusionList? |
(struct (ExportBindings Term) (location bindings)) |
location : (optional/c region?) |
bindings : (listof (cons/c Identifier? (optional/c Expression/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? |
(with-location loc x) → Term? |
loc : (optional/c region?) |
x : 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.
(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?.
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/c 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:
The following predicates represent extensible terms by checking an argument against the standard predicates followed by the corresponding custom predicates:
| ||
| ||
| ||
| ||
| ||
| ||
|