aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/robertkrimen/otto/ast/node.go
blob: 49ae375d12df2d340a13bc8f56ac5805eacda984 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
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 }
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() }