1068
vendor/github.com/robertkrimen/otto/ast/README.markdown
generated
vendored
Normal file
1068
vendor/github.com/robertkrimen/otto/ast/README.markdown
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
278
vendor/github.com/robertkrimen/otto/ast/comments.go
generated
vendored
Normal file
278
vendor/github.com/robertkrimen/otto/ast/comments.go
generated
vendored
Normal file
@@ -0,0 +1,278 @@
|
||||
package ast
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/robertkrimen/otto/file"
|
||||
)
|
||||
|
||||
// CommentPosition determines where the comment is in a given context
|
||||
type CommentPosition int
|
||||
|
||||
const (
|
||||
_ CommentPosition = iota
|
||||
LEADING // Before the pertinent expression
|
||||
TRAILING // After the pertinent expression
|
||||
KEY // Before a key in an object
|
||||
COLON // After a colon in a field declaration
|
||||
FINAL // Final comments in a block, not belonging to a specific expression or the comment after a trailing , in an array or object literal
|
||||
IF // After an if keyword
|
||||
WHILE // After a while keyword
|
||||
DO // After do keyword
|
||||
FOR // After a for keyword
|
||||
WITH // After a with keyword
|
||||
TBD
|
||||
)
|
||||
|
||||
// Comment contains the data of the comment
|
||||
type Comment struct {
|
||||
Begin file.Idx
|
||||
Text string
|
||||
Position CommentPosition
|
||||
}
|
||||
|
||||
// NewComment creates a new comment
|
||||
func NewComment(text string, idx file.Idx) *Comment {
|
||||
comment := &Comment{
|
||||
Begin: idx,
|
||||
Text: text,
|
||||
Position: TBD,
|
||||
}
|
||||
|
||||
return comment
|
||||
}
|
||||
|
||||
// String returns a stringified version of the position
|
||||
func (cp CommentPosition) String() string {
|
||||
switch cp {
|
||||
case LEADING:
|
||||
return "Leading"
|
||||
case TRAILING:
|
||||
return "Trailing"
|
||||
case KEY:
|
||||
return "Key"
|
||||
case COLON:
|
||||
return "Colon"
|
||||
case FINAL:
|
||||
return "Final"
|
||||
case IF:
|
||||
return "If"
|
||||
case WHILE:
|
||||
return "While"
|
||||
case DO:
|
||||
return "Do"
|
||||
case FOR:
|
||||
return "For"
|
||||
case WITH:
|
||||
return "With"
|
||||
default:
|
||||
return "???"
|
||||
}
|
||||
}
|
||||
|
||||
// String returns a stringified version of the comment
|
||||
func (c Comment) String() string {
|
||||
return fmt.Sprintf("Comment: %v", c.Text)
|
||||
}
|
||||
|
||||
// Comments defines the current view of comments from the parser
|
||||
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
|
||||
// future lists the comments after a line break during a sequence of comments
|
||||
future []*Comment
|
||||
// Current is node for which comments are linked to
|
||||
Current Expression
|
||||
|
||||
// wasLineBreak determines if a line break occured while scanning for comments
|
||||
wasLineBreak bool
|
||||
// primary determines whether or not processing a primary expression
|
||||
primary bool
|
||||
// afterBlock determines whether or not being after a block statement
|
||||
afterBlock bool
|
||||
}
|
||||
|
||||
func NewComments() *Comments {
|
||||
comments := &Comments{
|
||||
CommentMap: CommentMap{},
|
||||
}
|
||||
|
||||
return comments
|
||||
}
|
||||
|
||||
func (c *Comments) String() string {
|
||||
return fmt.Sprintf("NODE: %v, Comments: %v, Future: %v(LINEBREAK:%v)", c.Current, len(c.Comments), len(c.future), c.wasLineBreak)
|
||||
}
|
||||
|
||||
// FetchAll returns all the currently scanned comments,
|
||||
// including those from the next line
|
||||
func (c *Comments) FetchAll() []*Comment {
|
||||
defer func() {
|
||||
c.Comments = nil
|
||||
c.future = nil
|
||||
}()
|
||||
|
||||
return append(c.Comments, c.future...)
|
||||
}
|
||||
|
||||
// Fetch returns all the currently scanned comments
|
||||
func (c *Comments) Fetch() []*Comment {
|
||||
defer func() {
|
||||
c.Comments = nil
|
||||
}()
|
||||
|
||||
return c.Comments
|
||||
}
|
||||
|
||||
// ResetLineBreak marks the beginning of a new statement
|
||||
func (c *Comments) ResetLineBreak() {
|
||||
c.wasLineBreak = false
|
||||
}
|
||||
|
||||
// MarkPrimary will mark the context as processing a primary expression
|
||||
func (c *Comments) MarkPrimary() {
|
||||
c.primary = true
|
||||
c.wasLineBreak = false
|
||||
}
|
||||
|
||||
// AfterBlock will mark the context as being after a block.
|
||||
func (c *Comments) AfterBlock() {
|
||||
c.afterBlock = true
|
||||
}
|
||||
|
||||
// AddComment adds a comment to the view.
|
||||
// Depending on the context, comments are added normally or as post line break.
|
||||
func (c *Comments) AddComment(comment *Comment) {
|
||||
if c.primary {
|
||||
if !c.wasLineBreak {
|
||||
c.Comments = append(c.Comments, comment)
|
||||
} else {
|
||||
c.future = append(c.future, comment)
|
||||
}
|
||||
} else {
|
||||
if !c.wasLineBreak || (c.Current == nil && !c.afterBlock) {
|
||||
c.Comments = append(c.Comments, comment)
|
||||
} else {
|
||||
c.future = append(c.future, comment)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MarkComments will mark the found comments as the given position.
|
||||
func (c *Comments) MarkComments(position CommentPosition) {
|
||||
for _, comment := range c.Comments {
|
||||
if comment.Position == TBD {
|
||||
comment.Position = position
|
||||
}
|
||||
}
|
||||
for _, c := range c.future {
|
||||
if c.Position == TBD {
|
||||
c.Position = position
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Unset the current node and apply the comments to the current expression.
|
||||
// Resets context variables.
|
||||
func (c *Comments) Unset() {
|
||||
if c.Current != nil {
|
||||
c.applyComments(c.Current, c.Current, TRAILING)
|
||||
c.Current = nil
|
||||
}
|
||||
c.wasLineBreak = false
|
||||
c.primary = false
|
||||
c.afterBlock = false
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (c *Comments) SetExpression(node Expression) {
|
||||
// Skipping same node
|
||||
if c.Current == node {
|
||||
return
|
||||
}
|
||||
if c.Current != nil && c.Current.Idx1() == node.Idx1() {
|
||||
c.Current = node
|
||||
return
|
||||
}
|
||||
previous := c.Current
|
||||
c.Current = node
|
||||
|
||||
// Apply the found comments and futures to the node and the previous.
|
||||
c.applyComments(node, previous, TRAILING)
|
||||
}
|
||||
|
||||
// PostProcessNode applies all found comments to the given node
|
||||
func (c *Comments) PostProcessNode(node Node) {
|
||||
c.applyComments(node, nil, TRAILING)
|
||||
}
|
||||
|
||||
// applyComments applies both the comments and the future comments to the given node and the previous one,
|
||||
// based on the context.
|
||||
func (c *Comments) applyComments(node, previous Node, position CommentPosition) {
|
||||
if previous != nil {
|
||||
c.CommentMap.AddComments(previous, c.Comments, position)
|
||||
c.Comments = nil
|
||||
} else {
|
||||
c.CommentMap.AddComments(node, c.Comments, position)
|
||||
c.Comments = nil
|
||||
}
|
||||
// Only apply the future comments to the node if the previous is set.
|
||||
// This is for detecting end of line comments and which node comments on the following lines belongs to
|
||||
if previous != nil {
|
||||
c.CommentMap.AddComments(node, c.future, position)
|
||||
c.future = nil
|
||||
}
|
||||
}
|
||||
|
||||
// AtLineBreak will mark a line break
|
||||
func (c *Comments) AtLineBreak() {
|
||||
c.wasLineBreak = true
|
||||
}
|
||||
|
||||
// CommentMap is the data structure where all found comments are stored
|
||||
type CommentMap map[Node][]*Comment
|
||||
|
||||
// AddComment adds a single comment to the map
|
||||
func (cm CommentMap) AddComment(node Node, comment *Comment) {
|
||||
list := cm[node]
|
||||
list = append(list, comment)
|
||||
|
||||
cm[node] = list
|
||||
}
|
||||
|
||||
// AddComments adds a slice of comments, given a node and an updated position
|
||||
func (cm CommentMap) AddComments(node Node, comments []*Comment, position CommentPosition) {
|
||||
for _, comment := range comments {
|
||||
if comment.Position == TBD {
|
||||
comment.Position = position
|
||||
}
|
||||
cm.AddComment(node, comment)
|
||||
}
|
||||
}
|
||||
|
||||
// Size returns the size of the map
|
||||
func (cm CommentMap) Size() int {
|
||||
size := 0
|
||||
for _, comments := range cm {
|
||||
size += len(comments)
|
||||
}
|
||||
|
||||
return size
|
||||
}
|
||||
|
||||
// MoveComments moves comments with a given position from a node to another
|
||||
func (cm CommentMap) MoveComments(from, to Node, position CommentPosition) {
|
||||
for i, c := range cm[from] {
|
||||
if c.Position == position {
|
||||
cm.AddComment(to, c)
|
||||
|
||||
// Remove the comment from the "from" slice
|
||||
cm[from][i] = cm[from][len(cm[from])-1]
|
||||
cm[from][len(cm[from])-1] = nil
|
||||
cm[from] = cm[from][:len(cm[from])-1]
|
||||
}
|
||||
}
|
||||
}
|
515
vendor/github.com/robertkrimen/otto/ast/node.go
generated
vendored
Normal file
515
vendor/github.com/robertkrimen/otto/ast/node.go
generated
vendored
Normal file
@@ -0,0 +1,515 @@
|
||||
/*
|
||||
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.
|
||||
|
||||
*/
|
||||
package ast
|
||||
|
||||
import (
|
||||
"github.com/robertkrimen/otto/file"
|
||||
"github.com/robertkrimen/otto/token"
|
||||
)
|
||||
|
||||
// All nodes implement the Node interface.
|
||||
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
|
||||
}
|
||||
|
||||
// ========== //
|
||||
// Expression //
|
||||
// ========== //
|
||||
|
||||
type (
|
||||
// All expression nodes implement the Expression interface.
|
||||
Expression interface {
|
||||
Node
|
||||
_expressionNode()
|
||||
}
|
||||
|
||||
ArrayLiteral struct {
|
||||
LeftBracket file.Idx
|
||||
RightBracket file.Idx
|
||||
Value []Expression
|
||||
}
|
||||
|
||||
AssignExpression struct {
|
||||
Operator token.Token
|
||||
Left Expression
|
||||
Right Expression
|
||||
}
|
||||
|
||||
BadExpression struct {
|
||||
From file.Idx
|
||||
To file.Idx
|
||||
}
|
||||
|
||||
BinaryExpression struct {
|
||||
Operator token.Token
|
||||
Left Expression
|
||||
Right Expression
|
||||
Comparison bool
|
||||
}
|
||||
|
||||
BooleanLiteral struct {
|
||||
Idx file.Idx
|
||||
Literal string
|
||||
Value bool
|
||||
}
|
||||
|
||||
BracketExpression struct {
|
||||
Left Expression
|
||||
Member Expression
|
||||
LeftBracket file.Idx
|
||||
RightBracket file.Idx
|
||||
}
|
||||
|
||||
CallExpression struct {
|
||||
Callee Expression
|
||||
LeftParenthesis file.Idx
|
||||
ArgumentList []Expression
|
||||
RightParenthesis file.Idx
|
||||
}
|
||||
|
||||
ConditionalExpression struct {
|
||||
Test Expression
|
||||
Consequent Expression
|
||||
Alternate Expression
|
||||
}
|
||||
|
||||
DotExpression struct {
|
||||
Left Expression
|
||||
Identifier *Identifier
|
||||
}
|
||||
|
||||
EmptyExpression struct {
|
||||
Begin file.Idx
|
||||
End file.Idx
|
||||
}
|
||||
|
||||
FunctionLiteral struct {
|
||||
Function file.Idx
|
||||
Name *Identifier
|
||||
ParameterList *ParameterList
|
||||
Body Statement
|
||||
Source string
|
||||
|
||||
DeclarationList []Declaration
|
||||
}
|
||||
|
||||
Identifier struct {
|
||||
Name string
|
||||
Idx file.Idx
|
||||
}
|
||||
|
||||
NewExpression struct {
|
||||
New file.Idx
|
||||
Callee Expression
|
||||
LeftParenthesis file.Idx
|
||||
ArgumentList []Expression
|
||||
RightParenthesis file.Idx
|
||||
}
|
||||
|
||||
NullLiteral struct {
|
||||
Idx file.Idx
|
||||
Literal string
|
||||
}
|
||||
|
||||
NumberLiteral struct {
|
||||
Idx file.Idx
|
||||
Literal string
|
||||
Value interface{}
|
||||
}
|
||||
|
||||
ObjectLiteral struct {
|
||||
LeftBrace file.Idx
|
||||
RightBrace file.Idx
|
||||
Value []Property
|
||||
}
|
||||
|
||||
ParameterList struct {
|
||||
Opening file.Idx
|
||||
List []*Identifier
|
||||
Closing file.Idx
|
||||
}
|
||||
|
||||
Property struct {
|
||||
Key string
|
||||
Kind string
|
||||
Value Expression
|
||||
}
|
||||
|
||||
RegExpLiteral struct {
|
||||
Idx file.Idx
|
||||
Literal string
|
||||
Pattern string
|
||||
Flags string
|
||||
Value string
|
||||
}
|
||||
|
||||
SequenceExpression struct {
|
||||
Sequence []Expression
|
||||
}
|
||||
|
||||
StringLiteral struct {
|
||||
Idx file.Idx
|
||||
Literal string
|
||||
Value string
|
||||
}
|
||||
|
||||
ThisExpression struct {
|
||||
Idx file.Idx
|
||||
}
|
||||
|
||||
UnaryExpression struct {
|
||||
Operator token.Token
|
||||
Idx file.Idx // If a prefix operation
|
||||
Operand Expression
|
||||
Postfix bool
|
||||
}
|
||||
|
||||
VariableExpression struct {
|
||||
Name string
|
||||
Idx file.Idx
|
||||
Initializer Expression
|
||||
}
|
||||
)
|
||||
|
||||
// _expressionNode
|
||||
|
||||
func (*ArrayLiteral) _expressionNode() {}
|
||||
func (*AssignExpression) _expressionNode() {}
|
||||
func (*BadExpression) _expressionNode() {}
|
||||
func (*BinaryExpression) _expressionNode() {}
|
||||
func (*BooleanLiteral) _expressionNode() {}
|
||||
func (*BracketExpression) _expressionNode() {}
|
||||
func (*CallExpression) _expressionNode() {}
|
||||
func (*ConditionalExpression) _expressionNode() {}
|
||||
func (*DotExpression) _expressionNode() {}
|
||||
func (*EmptyExpression) _expressionNode() {}
|
||||
func (*FunctionLiteral) _expressionNode() {}
|
||||
func (*Identifier) _expressionNode() {}
|
||||
func (*NewExpression) _expressionNode() {}
|
||||
func (*NullLiteral) _expressionNode() {}
|
||||
func (*NumberLiteral) _expressionNode() {}
|
||||
func (*ObjectLiteral) _expressionNode() {}
|
||||
func (*RegExpLiteral) _expressionNode() {}
|
||||
func (*SequenceExpression) _expressionNode() {}
|
||||
func (*StringLiteral) _expressionNode() {}
|
||||
func (*ThisExpression) _expressionNode() {}
|
||||
func (*UnaryExpression) _expressionNode() {}
|
||||
func (*VariableExpression) _expressionNode() {}
|
||||
|
||||
// ========= //
|
||||
// Statement //
|
||||
// ========= //
|
||||
|
||||
type (
|
||||
// All statement nodes implement the Statement interface.
|
||||
Statement interface {
|
||||
Node
|
||||
_statementNode()
|
||||
}
|
||||
|
||||
BadStatement struct {
|
||||
From file.Idx
|
||||
To file.Idx
|
||||
}
|
||||
|
||||
BlockStatement struct {
|
||||
LeftBrace file.Idx
|
||||
List []Statement
|
||||
RightBrace file.Idx
|
||||
}
|
||||
|
||||
BranchStatement struct {
|
||||
Idx file.Idx
|
||||
Token token.Token
|
||||
Label *Identifier
|
||||
}
|
||||
|
||||
CaseStatement struct {
|
||||
Case file.Idx
|
||||
Test Expression
|
||||
Consequent []Statement
|
||||
}
|
||||
|
||||
CatchStatement struct {
|
||||
Catch file.Idx
|
||||
Parameter *Identifier
|
||||
Body Statement
|
||||
}
|
||||
|
||||
DebuggerStatement struct {
|
||||
Debugger file.Idx
|
||||
}
|
||||
|
||||
DoWhileStatement struct {
|
||||
Do file.Idx
|
||||
Test Expression
|
||||
Body Statement
|
||||
}
|
||||
|
||||
EmptyStatement struct {
|
||||
Semicolon file.Idx
|
||||
}
|
||||
|
||||
ExpressionStatement struct {
|
||||
Expression Expression
|
||||
}
|
||||
|
||||
ForInStatement struct {
|
||||
For file.Idx
|
||||
Into Expression
|
||||
Source Expression
|
||||
Body Statement
|
||||
}
|
||||
|
||||
ForStatement struct {
|
||||
For file.Idx
|
||||
Initializer Expression
|
||||
Update Expression
|
||||
Test Expression
|
||||
Body Statement
|
||||
}
|
||||
|
||||
FunctionStatement struct {
|
||||
Function *FunctionLiteral
|
||||
}
|
||||
|
||||
IfStatement struct {
|
||||
If file.Idx
|
||||
Test Expression
|
||||
Consequent Statement
|
||||
Alternate Statement
|
||||
}
|
||||
|
||||
LabelledStatement struct {
|
||||
Label *Identifier
|
||||
Colon file.Idx
|
||||
Statement Statement
|
||||
}
|
||||
|
||||
ReturnStatement struct {
|
||||
Return file.Idx
|
||||
Argument Expression
|
||||
}
|
||||
|
||||
SwitchStatement struct {
|
||||
Switch file.Idx
|
||||
Discriminant Expression
|
||||
Default int
|
||||
Body []*CaseStatement
|
||||
}
|
||||
|
||||
ThrowStatement struct {
|
||||
Throw file.Idx
|
||||
Argument Expression
|
||||
}
|
||||
|
||||
TryStatement struct {
|
||||
Try file.Idx
|
||||
Body Statement
|
||||
Catch *CatchStatement
|
||||
Finally Statement
|
||||
}
|
||||
|
||||
VariableStatement struct {
|
||||
Var file.Idx
|
||||
List []Expression
|
||||
}
|
||||
|
||||
WhileStatement struct {
|
||||
While file.Idx
|
||||
Test Expression
|
||||
Body Statement
|
||||
}
|
||||
|
||||
WithStatement struct {
|
||||
With file.Idx
|
||||
Object Expression
|
||||
Body Statement
|
||||
}
|
||||
)
|
||||
|
||||
// _statementNode
|
||||
|
||||
func (*BadStatement) _statementNode() {}
|
||||
func (*BlockStatement) _statementNode() {}
|
||||
func (*BranchStatement) _statementNode() {}
|
||||
func (*CaseStatement) _statementNode() {}
|
||||
func (*CatchStatement) _statementNode() {}
|
||||
func (*DebuggerStatement) _statementNode() {}
|
||||
func (*DoWhileStatement) _statementNode() {}
|
||||
func (*EmptyStatement) _statementNode() {}
|
||||
func (*ExpressionStatement) _statementNode() {}
|
||||
func (*ForInStatement) _statementNode() {}
|
||||
func (*ForStatement) _statementNode() {}
|
||||
func (*FunctionStatement) _statementNode() {}
|
||||
func (*IfStatement) _statementNode() {}
|
||||
func (*LabelledStatement) _statementNode() {}
|
||||
func (*ReturnStatement) _statementNode() {}
|
||||
func (*SwitchStatement) _statementNode() {}
|
||||
func (*ThrowStatement) _statementNode() {}
|
||||
func (*TryStatement) _statementNode() {}
|
||||
func (*VariableStatement) _statementNode() {}
|
||||
func (*WhileStatement) _statementNode() {}
|
||||
func (*WithStatement) _statementNode() {}
|
||||
|
||||
// =========== //
|
||||
// Declaration //
|
||||
// =========== //
|
||||
|
||||
type (
|
||||
// All declaration nodes implement the Declaration interface.
|
||||
Declaration interface {
|
||||
_declarationNode()
|
||||
}
|
||||
|
||||
FunctionDeclaration struct {
|
||||
Function *FunctionLiteral
|
||||
}
|
||||
|
||||
VariableDeclaration struct {
|
||||
Var file.Idx
|
||||
List []*VariableExpression
|
||||
}
|
||||
)
|
||||
|
||||
// _declarationNode
|
||||
|
||||
func (*FunctionDeclaration) _declarationNode() {}
|
||||
func (*VariableDeclaration) _declarationNode() {}
|
||||
|
||||
// ==== //
|
||||
// Node //
|
||||
// ==== //
|
||||
|
||||
type Program struct {
|
||||
Body []Statement
|
||||
|
||||
DeclarationList []Declaration
|
||||
|
||||
File *file.File
|
||||
|
||||
Comments CommentMap
|
||||
}
|
||||
|
||||
// ==== //
|
||||
// Idx0 //
|
||||
// ==== //
|
||||
|
||||
func (self *ArrayLiteral) Idx0() file.Idx { return self.LeftBracket }
|
||||
func (self *AssignExpression) Idx0() file.Idx { return self.Left.Idx0() }
|
||||
func (self *BadExpression) Idx0() file.Idx { return self.From }
|
||||
func (self *BinaryExpression) Idx0() file.Idx { return self.Left.Idx0() }
|
||||
func (self *BooleanLiteral) Idx0() file.Idx { return self.Idx }
|
||||
func (self *BracketExpression) Idx0() file.Idx { return self.Left.Idx0() }
|
||||
func (self *CallExpression) Idx0() file.Idx { return self.Callee.Idx0() }
|
||||
func (self *ConditionalExpression) Idx0() file.Idx { return self.Test.Idx0() }
|
||||
func (self *DotExpression) Idx0() file.Idx { return self.Left.Idx0() }
|
||||
func (self *EmptyExpression) Idx0() file.Idx { return self.Begin }
|
||||
func (self *FunctionLiteral) Idx0() file.Idx { return self.Function }
|
||||
func (self *Identifier) Idx0() file.Idx { return self.Idx }
|
||||
func (self *NewExpression) Idx0() file.Idx { return self.New }
|
||||
func (self *NullLiteral) Idx0() file.Idx { return self.Idx }
|
||||
func (self *NumberLiteral) Idx0() file.Idx { return self.Idx }
|
||||
func (self *ObjectLiteral) Idx0() file.Idx { return self.LeftBrace }
|
||||
func (self *RegExpLiteral) Idx0() file.Idx { return self.Idx }
|
||||
func (self *SequenceExpression) Idx0() file.Idx { return self.Sequence[0].Idx0() }
|
||||
func (self *StringLiteral) Idx0() file.Idx { return self.Idx }
|
||||
func (self *ThisExpression) Idx0() file.Idx { return self.Idx }
|
||||
func (self *UnaryExpression) Idx0() file.Idx { return self.Idx }
|
||||
func (self *VariableExpression) Idx0() file.Idx { return self.Idx }
|
||||
|
||||
func (self *BadStatement) Idx0() file.Idx { return self.From }
|
||||
func (self *BlockStatement) Idx0() file.Idx { return self.LeftBrace }
|
||||
func (self *BranchStatement) Idx0() file.Idx { return self.Idx }
|
||||
func (self *CaseStatement) Idx0() file.Idx { return self.Case }
|
||||
func (self *CatchStatement) Idx0() file.Idx { return self.Catch }
|
||||
func (self *DebuggerStatement) Idx0() file.Idx { return self.Debugger }
|
||||
func (self *DoWhileStatement) Idx0() file.Idx { return self.Do }
|
||||
func (self *EmptyStatement) Idx0() file.Idx { return self.Semicolon }
|
||||
func (self *ExpressionStatement) Idx0() file.Idx { return self.Expression.Idx0() }
|
||||
func (self *ForInStatement) Idx0() file.Idx { return self.For }
|
||||
func (self *ForStatement) Idx0() file.Idx { return self.For }
|
||||
func (self *FunctionStatement) Idx0() file.Idx { return self.Function.Idx0() }
|
||||
func (self *IfStatement) Idx0() file.Idx { return self.If }
|
||||
func (self *LabelledStatement) Idx0() file.Idx { return self.Label.Idx0() }
|
||||
func (self *Program) Idx0() file.Idx { return self.Body[0].Idx0() }
|
||||
func (self *ReturnStatement) Idx0() file.Idx { return self.Return }
|
||||
func (self *SwitchStatement) Idx0() file.Idx { return self.Switch }
|
||||
func (self *ThrowStatement) Idx0() file.Idx { return self.Throw }
|
||||
func (self *TryStatement) Idx0() file.Idx { return self.Try }
|
||||
func (self *VariableStatement) Idx0() file.Idx { return self.Var }
|
||||
func (self *WhileStatement) Idx0() file.Idx { return self.While }
|
||||
func (self *WithStatement) Idx0() file.Idx { return self.With }
|
||||
|
||||
// ==== //
|
||||
// Idx1 //
|
||||
// ==== //
|
||||
|
||||
func (self *ArrayLiteral) Idx1() file.Idx { return self.RightBracket }
|
||||
func (self *AssignExpression) Idx1() file.Idx { return self.Right.Idx1() }
|
||||
func (self *BadExpression) Idx1() file.Idx { return self.To }
|
||||
func (self *BinaryExpression) Idx1() file.Idx { return self.Right.Idx1() }
|
||||
func (self *BooleanLiteral) Idx1() file.Idx { return file.Idx(int(self.Idx) + len(self.Literal)) }
|
||||
func (self *BracketExpression) Idx1() file.Idx { return self.RightBracket + 1 }
|
||||
func (self *CallExpression) Idx1() file.Idx { return self.RightParenthesis + 1 }
|
||||
func (self *ConditionalExpression) Idx1() file.Idx { return self.Test.Idx1() }
|
||||
func (self *DotExpression) Idx1() file.Idx { return self.Identifier.Idx1() }
|
||||
func (self *EmptyExpression) Idx1() file.Idx { return self.End }
|
||||
func (self *FunctionLiteral) Idx1() file.Idx { return self.Body.Idx1() }
|
||||
func (self *Identifier) Idx1() file.Idx { return file.Idx(int(self.Idx) + len(self.Name)) }
|
||||
func (self *NewExpression) Idx1() file.Idx { return self.RightParenthesis + 1 }
|
||||
func (self *NullLiteral) Idx1() file.Idx { return file.Idx(int(self.Idx) + 4) } // "null"
|
||||
func (self *NumberLiteral) Idx1() file.Idx { return file.Idx(int(self.Idx) + len(self.Literal)) }
|
||||
func (self *ObjectLiteral) Idx1() file.Idx { return self.RightBrace }
|
||||
func (self *RegExpLiteral) Idx1() file.Idx { return file.Idx(int(self.Idx) + len(self.Literal)) }
|
||||
func (self *SequenceExpression) Idx1() file.Idx { return self.Sequence[0].Idx1() }
|
||||
func (self *StringLiteral) Idx1() file.Idx { return file.Idx(int(self.Idx) + len(self.Literal)) }
|
||||
func (self *ThisExpression) Idx1() file.Idx { return self.Idx + 4 }
|
||||
func (self *UnaryExpression) Idx1() file.Idx {
|
||||
if self.Postfix {
|
||||
return self.Operand.Idx1() + 2 // ++ --
|
||||
}
|
||||
return self.Operand.Idx1()
|
||||
}
|
||||
func (self *VariableExpression) Idx1() file.Idx {
|
||||
if self.Initializer == nil {
|
||||
return file.Idx(int(self.Idx) + len(self.Name) + 1)
|
||||
}
|
||||
return self.Initializer.Idx1()
|
||||
}
|
||||
|
||||
func (self *BadStatement) Idx1() file.Idx { return self.To }
|
||||
func (self *BlockStatement) Idx1() file.Idx { return self.RightBrace + 1 }
|
||||
func (self *BranchStatement) Idx1() file.Idx { return self.Idx }
|
||||
func (self *CaseStatement) Idx1() file.Idx { return self.Consequent[len(self.Consequent)-1].Idx1() }
|
||||
func (self *CatchStatement) Idx1() file.Idx { return self.Body.Idx1() }
|
||||
func (self *DebuggerStatement) Idx1() file.Idx { return self.Debugger + 8 }
|
||||
func (self *DoWhileStatement) Idx1() file.Idx { return self.Test.Idx1() }
|
||||
func (self *EmptyStatement) Idx1() file.Idx { return self.Semicolon + 1 }
|
||||
func (self *ExpressionStatement) Idx1() file.Idx { return self.Expression.Idx1() }
|
||||
func (self *ForInStatement) Idx1() file.Idx { return self.Body.Idx1() }
|
||||
func (self *ForStatement) Idx1() file.Idx { return self.Body.Idx1() }
|
||||
func (self *FunctionStatement) Idx1() file.Idx { return self.Function.Idx1() }
|
||||
func (self *IfStatement) Idx1() file.Idx {
|
||||
if self.Alternate != nil {
|
||||
return self.Alternate.Idx1()
|
||||
}
|
||||
return self.Consequent.Idx1()
|
||||
}
|
||||
func (self *LabelledStatement) Idx1() file.Idx { return self.Colon + 1 }
|
||||
func (self *Program) Idx1() file.Idx { return self.Body[len(self.Body)-1].Idx1() }
|
||||
func (self *ReturnStatement) Idx1() file.Idx { return self.Return }
|
||||
func (self *SwitchStatement) Idx1() file.Idx { return self.Body[len(self.Body)-1].Idx1() }
|
||||
func (self *ThrowStatement) Idx1() file.Idx { return self.Throw }
|
||||
func (self *TryStatement) Idx1() file.Idx { return self.Try }
|
||||
func (self *VariableStatement) Idx1() file.Idx { return self.List[len(self.List)-1].Idx1() }
|
||||
func (self *WhileStatement) Idx1() file.Idx { return self.Body.Idx1() }
|
||||
func (self *WithStatement) Idx1() file.Idx { return self.Body.Idx1() }
|
217
vendor/github.com/robertkrimen/otto/ast/walk.go
generated
vendored
Normal file
217
vendor/github.com/robertkrimen/otto/ast/walk.go
generated
vendored
Normal file
@@ -0,0 +1,217 @@
|
||||
package ast
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 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.
|
||||
type Visitor interface {
|
||||
Enter(n Node) (v Visitor)
|
||||
Exit(n Node)
|
||||
}
|
||||
|
||||
// Walk traverses an AST in depth-first order: It starts by calling
|
||||
// v.Enter(node); node must not be nil. If the visitor v returned by
|
||||
// v.Enter(node) is not nil, Walk is invoked recursively with visitor
|
||||
// v for each of the non-nil children of node, followed by a call
|
||||
// of v.Exit(node).
|
||||
func Walk(v Visitor, n Node) {
|
||||
if n == nil {
|
||||
return
|
||||
}
|
||||
if v = v.Enter(n); v == nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer v.Exit(n)
|
||||
|
||||
switch n := n.(type) {
|
||||
case *ArrayLiteral:
|
||||
if n != nil {
|
||||
for _, ex := range n.Value {
|
||||
Walk(v, ex)
|
||||
}
|
||||
}
|
||||
case *AssignExpression:
|
||||
if n != nil {
|
||||
Walk(v, n.Left)
|
||||
Walk(v, n.Right)
|
||||
}
|
||||
case *BadExpression:
|
||||
case *BinaryExpression:
|
||||
if n != nil {
|
||||
Walk(v, n.Left)
|
||||
Walk(v, n.Right)
|
||||
}
|
||||
case *BlockStatement:
|
||||
if n != nil {
|
||||
for _, s := range n.List {
|
||||
Walk(v, s)
|
||||
}
|
||||
}
|
||||
case *BooleanLiteral:
|
||||
case *BracketExpression:
|
||||
if n != nil {
|
||||
Walk(v, n.Left)
|
||||
Walk(v, n.Member)
|
||||
}
|
||||
case *BranchStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Label)
|
||||
}
|
||||
case *CallExpression:
|
||||
if n != nil {
|
||||
Walk(v, n.Callee)
|
||||
for _, a := range n.ArgumentList {
|
||||
Walk(v, a)
|
||||
}
|
||||
}
|
||||
case *CaseStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Test)
|
||||
for _, c := range n.Consequent {
|
||||
Walk(v, c)
|
||||
}
|
||||
}
|
||||
case *CatchStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Parameter)
|
||||
Walk(v, n.Body)
|
||||
}
|
||||
case *ConditionalExpression:
|
||||
if n != nil {
|
||||
Walk(v, n.Test)
|
||||
Walk(v, n.Consequent)
|
||||
Walk(v, n.Alternate)
|
||||
}
|
||||
case *DebuggerStatement:
|
||||
case *DoWhileStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Test)
|
||||
Walk(v, n.Body)
|
||||
}
|
||||
case *DotExpression:
|
||||
if n != nil {
|
||||
Walk(v, n.Left)
|
||||
}
|
||||
case *EmptyExpression:
|
||||
case *EmptyStatement:
|
||||
case *ExpressionStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Expression)
|
||||
}
|
||||
case *ForInStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Into)
|
||||
Walk(v, n.Source)
|
||||
Walk(v, n.Body)
|
||||
}
|
||||
case *ForStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Initializer)
|
||||
Walk(v, n.Update)
|
||||
Walk(v, n.Test)
|
||||
Walk(v, n.Body)
|
||||
}
|
||||
case *FunctionLiteral:
|
||||
if n != nil {
|
||||
Walk(v, n.Name)
|
||||
for _, p := range n.ParameterList.List {
|
||||
Walk(v, p)
|
||||
}
|
||||
Walk(v, n.Body)
|
||||
}
|
||||
case *FunctionStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Function)
|
||||
}
|
||||
case *Identifier:
|
||||
case *IfStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Test)
|
||||
Walk(v, n.Consequent)
|
||||
Walk(v, n.Alternate)
|
||||
}
|
||||
case *LabelledStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Statement)
|
||||
}
|
||||
case *NewExpression:
|
||||
if n != nil {
|
||||
Walk(v, n.Callee)
|
||||
for _, a := range n.ArgumentList {
|
||||
Walk(v, a)
|
||||
}
|
||||
}
|
||||
case *NullLiteral:
|
||||
case *NumberLiteral:
|
||||
case *ObjectLiteral:
|
||||
if n != nil {
|
||||
for _, p := range n.Value {
|
||||
Walk(v, p.Value)
|
||||
}
|
||||
}
|
||||
case *Program:
|
||||
if n != nil {
|
||||
for _, b := range n.Body {
|
||||
Walk(v, b)
|
||||
}
|
||||
}
|
||||
case *RegExpLiteral:
|
||||
case *ReturnStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Argument)
|
||||
}
|
||||
case *SequenceExpression:
|
||||
if n != nil {
|
||||
for _, e := range n.Sequence {
|
||||
Walk(v, e)
|
||||
}
|
||||
}
|
||||
case *StringLiteral:
|
||||
case *SwitchStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Discriminant)
|
||||
for _, c := range n.Body {
|
||||
Walk(v, c)
|
||||
}
|
||||
}
|
||||
case *ThisExpression:
|
||||
case *ThrowStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Argument)
|
||||
}
|
||||
case *TryStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Body)
|
||||
Walk(v, n.Catch)
|
||||
Walk(v, n.Finally)
|
||||
}
|
||||
case *UnaryExpression:
|
||||
if n != nil {
|
||||
Walk(v, n.Operand)
|
||||
}
|
||||
case *VariableExpression:
|
||||
if n != nil {
|
||||
Walk(v, n.Initializer)
|
||||
}
|
||||
case *VariableStatement:
|
||||
if n != nil {
|
||||
for _, e := range n.List {
|
||||
Walk(v, e)
|
||||
}
|
||||
}
|
||||
case *WhileStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Test)
|
||||
Walk(v, n.Body)
|
||||
}
|
||||
case *WithStatement:
|
||||
if n != nil {
|
||||
Walk(v, n.Object)
|
||||
Walk(v, n.Body)
|
||||
}
|
||||
default:
|
||||
panic(fmt.Sprintf("Walk: unexpected node type %T", n))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user