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 |
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?
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
(with-location loc x) → Term?
loc : (maybe/c region?) x : Term?
procedure
(SubStatement? x) → boolean?
x : any
procedure
(SourceElement? x) → boolean?
x : any
procedure
(Identifier=? x y) → boolean?
x : Identifier? y : Identifier?
value
value
value
value
procedure
→ (maybe/c infix-operator?) op : assignment-operator?
procedure
(postfix-operator? x) → boolean?
x : any
procedure
(prefix-operator? x) → boolean?
x : any
procedure
(infix-operator? x) → boolean?
x : any
procedure
(assignment-operator? x) → boolean?
x : any
value
value
value
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
: (parameter/c (listof (any -> boolean?)))
value
: (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
x : any