Documentation
¶
Overview ¶
Package ast declares types representing a JavaScript AST.
# Warning The parser and AST interfaces are still works-in-progress (particularly where node types are concerned) and may change in the future.
Index ¶
- func Walk(v Visitor, n Node)
- type ArrayLiteral
- type AssignExpression
- type BadExpression
- type BadStatement
- type BinaryExpression
- type BlockStatement
- type BooleanLiteral
- type BracketExpression
- type BranchStatement
- type CallExpression
- type CaseStatement
- type CatchStatement
- type Comment
- type CommentMap
- type CommentPosition
- type Comments
- func (c *Comments) AddComment(comment *Comment)
- func (c *Comments) AfterBlock()
- func (c *Comments) AtLineBreak()
- func (c *Comments) Fetch() []*Comment
- func (c *Comments) FetchAll() []*Comment
- func (c *Comments) MarkComments(position CommentPosition)
- func (c *Comments) MarkPrimary()
- func (c *Comments) PostProcessNode(node Node)
- func (c *Comments) ResetLineBreak()
- func (c *Comments) SetExpression(node Expression)
- func (c *Comments) String() string
- func (c *Comments) Unset()
- type ConditionalExpression
- type DebuggerStatement
- type Declaration
- type DoWhileStatement
- type DotExpression
- type EmptyExpression
- type EmptyStatement
- type Expression
- type ExpressionStatement
- type ForInStatement
- type ForStatement
- type FunctionDeclaration
- type FunctionLiteral
- type FunctionStatement
- type Identifier
- type IfStatement
- type LabelledStatement
- type NewExpression
- type Node
- type NullLiteral
- type NumberLiteral
- type ObjectLiteral
- type ParameterList
- type Program
- type Property
- type RegExpLiteral
- type ReturnStatement
- type SequenceExpression
- type Statement
- type StringLiteral
- type SwitchStatement
- type ThisExpression
- type ThrowStatement
- type TryStatement
- type UnaryExpression
- type VariableDeclaration
- type VariableExpression
- type VariableStatement
- type Visitor
- type WhileStatement
- type WithStatement
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ArrayLiteral ¶
type ArrayLiteral struct {
Value []Expression
LeftBracket file.Idx
RightBracket file.Idx
}
ArrayLiteral represents an array literal.
type AssignExpression ¶
type AssignExpression struct {
Left Expression
Right Expression
Operator token.Token
}
AssignExpression represents an assignment expression.
type BadExpression ¶
BadExpression represents a bad expression.
type BadStatement ¶
BadStatement represents a bad statement.
type BinaryExpression ¶
type BinaryExpression struct {
Left Expression
Right Expression
Operator token.Token
Comparison bool
}
BinaryExpression represents a binary expression.
type BlockStatement ¶
BlockStatement represents a block statement.
type BooleanLiteral ¶
BooleanLiteral represents a boolean expression.
type BracketExpression ¶
type BracketExpression struct {
Left Expression
Member Expression
LeftBracket file.Idx
RightBracket file.Idx
}
BracketExpression represents a bracketed expression.
type BranchStatement ¶
type BranchStatement struct {
Label *Identifier
Idx file.Idx
Token token.Token
}
BranchStatement represents a branch statement.
type CallExpression ¶
type CallExpression struct {
Callee Expression
ArgumentList []Expression
LeftParenthesis file.Idx
RightParenthesis file.Idx
}
CallExpression represents a call expression.
type CaseStatement ¶
type CaseStatement struct {
Test Expression
Consequent []Statement
Case file.Idx
}
CaseStatement represents a case statement.
type CatchStatement ¶
type CatchStatement struct {
Body Statement
Parameter *Identifier
Catch file.Idx
}
CatchStatement represents a catch statement.
type Comment ¶
type Comment struct {
Text string
Begin file.Idx
Position CommentPosition
}
Comment contains the data of the comment.
func NewComment ¶
NewComment creates a new comment.
type CommentMap ¶
CommentMap is the data structure where all found comments are stored.
func (CommentMap) AddComment ¶
func (cm CommentMap) AddComment(node Node, comment *Comment)
AddComment adds a single comment to the map.
func (CommentMap) AddComments ¶
func (cm CommentMap) AddComments(node Node, comments []*Comment, position CommentPosition)
AddComments adds a slice of comments, given a node and an updated position.
func (CommentMap) MoveComments ¶
func (cm CommentMap) MoveComments(from, to Node, position CommentPosition)
MoveComments moves comments with a given position from a node to another.
type CommentPosition ¶
type CommentPosition int
CommentPosition determines where the comment is in a given context.
const ( // LEADING is before the pertinent expression. LEADING CommentPosition // TRAILING is after the pertinent expression. TRAILING // KEY is before a key in an object. KEY // COLON is after a colon in a field declaration. COLON // FINAL is the final comments in a block, not belonging to a specific expression or the comment after a trailing , in an array or object literal. FINAL // IF is after an if keyword. IF // WHILE is after a while keyword. WHILE // DO is after do keyword. DO // FOR is after a for keyword. FOR // WITH is after a with keyword. WITH // TBD is unknown. TBD )
Available comment positions.
func (CommentPosition) String ¶
func (cp CommentPosition) String() string
String returns a stringified version of the position.
type Comments ¶
type Comments struct {
// CommentMap is a reference to the parser comment map
CommentMap CommentMap
// Comments lists the comments scanned, not linked to a node yet
Comments []*Comment
// Current is node for which comments are linked to
Current Expression
// contains filtered or unexported fields
}
Comments defines the current view of comments from the parser.
func (*Comments) AddComment ¶
AddComment adds a comment to the view. Depending on the context, comments are added normally or as post line break.
func (*Comments) AfterBlock ¶
func (c *Comments) AfterBlock()
AfterBlock will mark the context as being after a block.
func (*Comments) FetchAll ¶
FetchAll returns all the currently scanned comments, including those from the next line.
func (*Comments) MarkComments ¶
func (c *Comments) MarkComments(position CommentPosition)
MarkComments will mark the found comments as the given position.
func (*Comments) MarkPrimary ¶
func (c *Comments) MarkPrimary()
MarkPrimary will mark the context as processing a primary expression.
func (*Comments) PostProcessNode ¶
PostProcessNode applies all found comments to the given node.
func (*Comments) ResetLineBreak ¶
func (c *Comments) ResetLineBreak()
ResetLineBreak marks the beginning of a new statement.
func (*Comments) SetExpression ¶
func (c *Comments) SetExpression(node Expression)
SetExpression sets the current expression. It is applied the found comments, unless the previous expression has not been unset. It is skipped if the node is already set or if it is a part of the previous node.
type ConditionalExpression ¶
type ConditionalExpression struct {
Test Expression
Consequent Expression
Alternate Expression
}
ConditionalExpression represents a conditional expression.
func (*ConditionalExpression) Idx0 ¶
func (ce *ConditionalExpression) Idx0() file.Idx
Idx0 implements Node.
func (*ConditionalExpression) Idx1 ¶
func (ce *ConditionalExpression) Idx1() file.Idx
Idx1 implements Node.
type DebuggerStatement ¶
DebuggerStatement represents a debugger statement.
type Declaration ¶
type Declaration interface {
// contains filtered or unexported methods
}
Declaration is implemented by type which represent declarations.
type DoWhileStatement ¶
type DoWhileStatement struct {
Test Expression
Body Statement
Do file.Idx
RightParenthesis file.Idx
}
DoWhileStatement represents a do while statement.
type DotExpression ¶
type DotExpression struct {
Left Expression
Identifier *Identifier
}
DotExpression represents a dot expression.
type EmptyExpression ¶
EmptyExpression represents an empty expression.
type EmptyStatement ¶
EmptyStatement represents a empty statement.
type Expression ¶
type Expression interface {
Node
// contains filtered or unexported methods
}
Expression is implemented by types that represent an Expression.
type ExpressionStatement ¶
type ExpressionStatement struct {
Expression Expression
}
ExpressionStatement represents a expression statement.
func (*ExpressionStatement) Idx0 ¶
func (es *ExpressionStatement) Idx0() file.Idx
Idx0 implements Node.
func (*ExpressionStatement) Idx1 ¶
func (es *ExpressionStatement) Idx1() file.Idx
Idx1 implements Node.
type ForInStatement ¶
type ForInStatement struct {
Into Expression
Source Expression
Body Statement
For file.Idx
}
ForInStatement represents a for in statement.
type ForStatement ¶
type ForStatement struct {
Initializer Expression
Update Expression
Test Expression
Body Statement
For file.Idx
}
ForStatement represents a for statement.
type FunctionDeclaration ¶
type FunctionDeclaration struct {
Function *FunctionLiteral
}
FunctionDeclaration represents a function declaration.
type FunctionLiteral ¶
type FunctionLiteral struct {
Body Statement
Name *Identifier
ParameterList *ParameterList
Source string
DeclarationList []Declaration
Function file.Idx
}
FunctionLiteral represents a function literal.
type FunctionStatement ¶
type FunctionStatement struct {
Function *FunctionLiteral
}
FunctionStatement represents a function statement.
type Identifier ¶
Identifier represents an identifier.
type IfStatement ¶
type IfStatement struct {
Test Expression
Consequent Statement
Alternate Statement
If file.Idx
}
IfStatement represents a if statement.
type LabelledStatement ¶
type LabelledStatement struct {
Statement Statement
Label *Identifier
Colon file.Idx
}
LabelledStatement represents a labelled statement.
type NewExpression ¶
type NewExpression struct {
Callee Expression
ArgumentList []Expression
New file.Idx
LeftParenthesis file.Idx
RightParenthesis file.Idx
}
NewExpression represents a new expression.
type Node ¶
type Node interface {
Idx0() file.Idx // The index of the first character belonging to the node
Idx1() file.Idx // The index of the first character immediately after the node
}
Node is implemented by types that represent a node.
type NullLiteral ¶
NullLiteral represents a null literal.
type NumberLiteral ¶
NumberLiteral represents a number literal.
type ObjectLiteral ¶
ObjectLiteral represents an object literal.
type ParameterList ¶
type ParameterList struct {
List []*Identifier
Opening file.Idx
Closing file.Idx
}
ParameterList represents a parameter list.
type Program ¶
type Program struct {
File *file.File
Comments CommentMap
Body []Statement
DeclarationList []Declaration
}
Program represents a full program.
type Property ¶
type Property struct {
Value Expression
Key string
Kind string
}
Property represents a property.
type RegExpLiteral ¶
RegExpLiteral represents a regular expression literal.
type ReturnStatement ¶
type ReturnStatement struct {
Argument Expression
Return file.Idx
}
ReturnStatement represents a return statement.
type SequenceExpression ¶
type SequenceExpression struct {
Sequence []Expression
}
SequenceExpression represents a sequence literal.
func (*SequenceExpression) Idx0 ¶
func (se *SequenceExpression) Idx0() file.Idx
Idx0 implements Node.
func (*SequenceExpression) Idx1 ¶
func (se *SequenceExpression) Idx1() file.Idx
Idx1 implements Node.
type Statement ¶
type Statement interface {
Node
// contains filtered or unexported methods
}
Statement is implemented by types which represent a statement.
type StringLiteral ¶
StringLiteral represents a string literal.
type SwitchStatement ¶
type SwitchStatement struct {
Discriminant Expression
Body []*CaseStatement
Switch file.Idx
Default int
RightBrace file.Idx
}
SwitchStatement represents a switch statement.
type ThisExpression ¶
ThisExpression represents a this expression.
type ThrowStatement ¶
type ThrowStatement struct {
Argument Expression
Throw file.Idx
}
ThrowStatement represents a throw statement.
type TryStatement ¶
type TryStatement struct {
Body Statement
Finally Statement
Catch *CatchStatement
Try file.Idx
}
TryStatement represents a try statement.
type UnaryExpression ¶
UnaryExpression represents a unary expression.
type VariableDeclaration ¶
type VariableDeclaration struct {
List []*VariableExpression
Var file.Idx
}
VariableDeclaration represents a variable declaration.
type VariableExpression ¶
type VariableExpression struct {
Initializer Expression
Name string
Idx file.Idx
}
VariableExpression represents a variable expression.
func (*VariableExpression) Idx0 ¶
func (ve *VariableExpression) Idx0() file.Idx
Idx0 implements Node.
func (*VariableExpression) Idx1 ¶
func (ve *VariableExpression) Idx1() file.Idx
Idx1 implements Node.
type VariableStatement ¶
type VariableStatement struct {
List []Expression
Var file.Idx
}
VariableStatement represents a variable statement.
type Visitor ¶
Visitor Enter method is invoked for each node encountered by Walk. If the result visitor w is not nil, Walk visits each of the children of node with the visitor v, followed by a call of the Exit method.
Example (CodeRewrite) ¶
package main
import (
"fmt"
"log"
"github.com/robertkrimen/otto/ast"
"github.com/robertkrimen/otto/file"
"github.com/robertkrimen/otto/parser"
)
type walkExample struct {
source string
shift file.Idx
}
func (w *walkExample) Enter(n ast.Node) ast.Visitor {
if id, ok := n.(*ast.Identifier); ok && id != nil {
idx := n.Idx0() + w.shift - 1
s := w.source[:idx] + "new_" + w.source[idx:]
w.source = s
w.shift += 4
}
if v, ok := n.(*ast.VariableExpression); ok && v != nil {
idx := n.Idx0() + w.shift - 1
s := w.source[:idx] + "varnew_" + w.source[idx:]
w.source = s
w.shift += 7
}
return w
}
func (w *walkExample) Exit(n ast.Node) {
// AST node n has had all its children walked. Pop it out of your
// stack, or do whatever processing you need to do, if any.
}
func main() {
source := `var b = function() {test(); try {} catch(e) {} var test = "test(); var test = 1"} // test`
program, err := parser.ParseFile(nil, "", source, 0)
if err != nil {
log.Fatal(err)
}
w := &walkExample{source: source}
ast.Walk(w, program)
fmt.Println(w.source)
}
Output: var varnew_b = function() {new_test(); try {} catch(new_e) {} var varnew_test = "test(); var test = 1"} // test
type WhileStatement ¶
type WhileStatement struct {
Test Expression
Body Statement
While file.Idx
}
WhileStatement represents a while statement.
type WithStatement ¶
type WithStatement struct {
Object Expression
Body Statement
With file.Idx
}
WithStatement represents a with statement.