| 1 |
package parser |
| 2 |
|
| 3 |
import ( |
| 4 |
"regexp" |
| 5 |
|
| 6 |
"github.com/robertkrimen/otto/ast" |
| 7 |
"github.com/robertkrimen/otto/file" |
| 8 |
"github.com/robertkrimen/otto/token" |
| 9 |
) |
| 10 |
|
| 11 |
func (p *parser) parseIdentifier() *ast.Identifier { |
| 12 |
literal := p.literal |
| 13 |
idx := p.idx |
| 14 |
if p.mode&StoreComments != 0 { |
| 15 |
p.comments.MarkComments(ast.LEADING) |
| 16 |
} |
| 17 |
p.next() |
| 18 |
exp := &ast.Identifier{ |
| 19 |
Name: literal, |
| 20 |
Idx: idx, |
| 21 |
} |
| 22 |
|
| 23 |
if p.mode&StoreComments != 0 { |
| 24 |
p.comments.SetExpression(exp) |
| 25 |
} |
| 26 |
|
| 27 |
return exp |
| 28 |
} |
| 29 |
|
| 30 |
func (p *parser) parsePrimaryExpression() ast.Expression { |
| 31 |
literal := p.literal |
| 32 |
idx := p.idx |
| 33 |
switch p.token { |
| 34 |
case token.IDENTIFIER: |
| 35 |
p.next() |
| 36 |
if len(literal) > 1 { |
| 37 |
tkn, strict := token.IsKeyword(literal) |
| 38 |
if tkn == token.KEYWORD { |
| 39 |
if !strict { |
| 40 |
p.error(idx, "Unexpected reserved word") |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
return &ast.Identifier{ |
| 45 |
Name: literal, |
| 46 |
Idx: idx, |
| 47 |
} |
| 48 |
case token.NULL: |
| 49 |
p.next() |
| 50 |
return &ast.NullLiteral{ |
| 51 |
Idx: idx, |
| 52 |
Literal: literal, |
| 53 |
} |
| 54 |
case token.BOOLEAN: |
| 55 |
p.next() |
| 56 |
value := false |
| 57 |
switch literal { |
| 58 |
case "true": |
| 59 |
value = true |
| 60 |
case "false": |
| 61 |
value = false |
| 62 |
default: |
| 63 |
p.error(idx, "Illegal boolean literal") |
| 64 |
} |
| 65 |
return &ast.BooleanLiteral{ |
| 66 |
Idx: idx, |
| 67 |
Literal: literal, |
| 68 |
Value: value, |
| 69 |
} |
| 70 |
case token.STRING: |
| 71 |
p.next() |
| 72 |
value, err := parseStringLiteral(literal[1 : len(literal)-1]) |
| 73 |
if err != nil { |
| 74 |
p.error(idx, err.Error()) |
| 75 |
} |
| 76 |
return &ast.StringLiteral{ |
| 77 |
Idx: idx, |
| 78 |
Literal: literal, |
| 79 |
Value: value, |
| 80 |
} |
| 81 |
case token.NUMBER: |
| 82 |
p.next() |
| 83 |
value, err := parseNumberLiteral(literal) |
| 84 |
if err != nil { |
| 85 |
p.error(idx, err.Error()) |
| 86 |
value = 0 |
| 87 |
} |
| 88 |
return &ast.NumberLiteral{ |
| 89 |
Idx: idx, |
| 90 |
Literal: literal, |
| 91 |
Value: value, |
| 92 |
} |
| 93 |
case token.SLASH, token.QUOTIENT_ASSIGN: |
| 94 |
return p.parseRegExpLiteral() |
| 95 |
case token.LEFT_BRACE: |
| 96 |
return p.parseObjectLiteral() |
| 97 |
case token.LEFT_BRACKET: |
| 98 |
return p.parseArrayLiteral() |
| 99 |
case token.LEFT_PARENTHESIS: |
| 100 |
p.expect(token.LEFT_PARENTHESIS) |
| 101 |
expression := p.parseExpression() |
| 102 |
if p.mode&StoreComments != 0 { |
| 103 |
p.comments.Unset() |
| 104 |
} |
| 105 |
p.expect(token.RIGHT_PARENTHESIS) |
| 106 |
return expression |
| 107 |
case token.THIS: |
| 108 |
p.next() |
| 109 |
return &ast.ThisExpression{ |
| 110 |
Idx: idx, |
| 111 |
} |
| 112 |
case token.FUNCTION: |
| 113 |
return p.parseFunction(false) |
| 114 |
} |
| 115 |
|
| 116 |
p.errorUnexpectedToken(p.token) |
| 117 |
p.nextStatement() |
| 118 |
return &ast.BadExpression{From: idx, To: p.idx} |
| 119 |
} |
| 120 |
|
| 121 |
func (p *parser) parseRegExpLiteral() *ast.RegExpLiteral { |
| 122 |
offset := p.chrOffset - 1 |
| 123 |
if p.token == token.QUOTIENT_ASSIGN { |
| 124 |
offset-- |
| 125 |
} |
| 126 |
idx := p.idxOf(offset) |
| 127 |
|
| 128 |
pattern, err := p.scanString(offset) |
| 129 |
endOffset := p.chrOffset |
| 130 |
|
| 131 |
p.next() |
| 132 |
if err == nil { |
| 133 |
pattern = pattern[1 : len(pattern)-1] |
| 134 |
} |
| 135 |
|
| 136 |
flags := "" |
| 137 |
if p.token == token.IDENTIFIER { |
| 138 |
flags = p.literal |
| 139 |
endOffset = p.chrOffset |
| 140 |
p.next() |
| 141 |
} |
| 142 |
|
| 143 |
var value string |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
pat, err := TransformRegExp(pattern) |
| 148 |
if err != nil { |
| 149 |
if pat == "" || p.mode&IgnoreRegExpErrors == 0 { |
| 150 |
p.error(idx, "Invalid regular expression: %s", err.Error()) |
| 151 |
} |
| 152 |
} else { |
| 153 |
_, err = regexp.Compile(pat) |
| 154 |
if err != nil { |
| 155 |
|
| 156 |
p.error(idx, "Invalid regular expression: %s", err.Error()[22:]) |
| 157 |
} else { |
| 158 |
value = pat |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
literal := p.str[offset:endOffset] |
| 163 |
|
| 164 |
return &ast.RegExpLiteral{ |
| 165 |
Idx: idx, |
| 166 |
Literal: literal, |
| 167 |
Pattern: pattern, |
| 168 |
Flags: flags, |
| 169 |
Value: value, |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func (p *parser) parseVariableDeclaration(declarationList *[]*ast.VariableExpression) ast.Expression { |
| 174 |
if p.token != token.IDENTIFIER { |
| 175 |
idx := p.expect(token.IDENTIFIER) |
| 176 |
p.nextStatement() |
| 177 |
return &ast.BadExpression{From: idx, To: p.idx} |
| 178 |
} |
| 179 |
|
| 180 |
literal := p.literal |
| 181 |
idx := p.idx |
| 182 |
p.next() |
| 183 |
node := &ast.VariableExpression{ |
| 184 |
Name: literal, |
| 185 |
Idx: idx, |
| 186 |
} |
| 187 |
if p.mode&StoreComments != 0 { |
| 188 |
p.comments.SetExpression(node) |
| 189 |
} |
| 190 |
|
| 191 |
if declarationList != nil { |
| 192 |
*declarationList = append(*declarationList, node) |
| 193 |
} |
| 194 |
|
| 195 |
if p.token == token.ASSIGN { |
| 196 |
if p.mode&StoreComments != 0 { |
| 197 |
p.comments.Unset() |
| 198 |
} |
| 199 |
p.next() |
| 200 |
node.Initializer = p.parseAssignmentExpression() |
| 201 |
} |
| 202 |
|
| 203 |
return node |
| 204 |
} |
| 205 |
|
| 206 |
func (p *parser) parseVariableDeclarationList(idx file.Idx) []ast.Expression { |
| 207 |
var declarationList []*ast.VariableExpression |
| 208 |
var list []ast.Expression |
| 209 |
|
| 210 |
for { |
| 211 |
if p.mode&StoreComments != 0 { |
| 212 |
p.comments.MarkComments(ast.LEADING) |
| 213 |
} |
| 214 |
decl := p.parseVariableDeclaration(&declarationList) |
| 215 |
list = append(list, decl) |
| 216 |
if p.token != token.COMMA { |
| 217 |
break |
| 218 |
} |
| 219 |
if p.mode&StoreComments != 0 { |
| 220 |
p.comments.Unset() |
| 221 |
} |
| 222 |
p.next() |
| 223 |
} |
| 224 |
|
| 225 |
p.scope.declare(&ast.VariableDeclaration{ |
| 226 |
Var: idx, |
| 227 |
List: declarationList, |
| 228 |
}) |
| 229 |
|
| 230 |
return list |
| 231 |
} |
| 232 |
|
| 233 |
func (p *parser) parseObjectPropertyKey() (string, string) { |
| 234 |
idx, tkn, literal := p.idx, p.token, p.literal |
| 235 |
value := "" |
| 236 |
if p.mode&StoreComments != 0 { |
| 237 |
p.comments.MarkComments(ast.KEY) |
| 238 |
} |
| 239 |
p.next() |
| 240 |
|
| 241 |
switch tkn { |
| 242 |
case token.IDENTIFIER: |
| 243 |
value = literal |
| 244 |
case token.NUMBER: |
| 245 |
var err error |
| 246 |
_, err = parseNumberLiteral(literal) |
| 247 |
if err != nil { |
| 248 |
p.error(idx, err.Error()) |
| 249 |
} else { |
| 250 |
value = literal |
| 251 |
} |
| 252 |
case token.STRING: |
| 253 |
var err error |
| 254 |
value, err = parseStringLiteral(literal[1 : len(literal)-1]) |
| 255 |
if err != nil { |
| 256 |
p.error(idx, err.Error()) |
| 257 |
} |
| 258 |
default: |
| 259 |
|
| 260 |
if matchIdentifier.MatchString(literal) { |
| 261 |
value = literal |
| 262 |
} |
| 263 |
} |
| 264 |
return literal, value |
| 265 |
} |
| 266 |
|
| 267 |
func (p *parser) parseObjectProperty() ast.Property { |
| 268 |
literal, value := p.parseObjectPropertyKey() |
| 269 |
if literal == "get" && p.token != token.COLON { |
| 270 |
idx := p.idx |
| 271 |
_, value = p.parseObjectPropertyKey() |
| 272 |
parameterList := p.parseFunctionParameterList() |
| 273 |
|
| 274 |
node := &ast.FunctionLiteral{ |
| 275 |
Function: idx, |
| 276 |
ParameterList: parameterList, |
| 277 |
} |
| 278 |
p.parseFunctionBlock(node) |
| 279 |
return ast.Property{ |
| 280 |
Key: value, |
| 281 |
Kind: "get", |
| 282 |
Value: node, |
| 283 |
} |
| 284 |
} else if literal == "set" && p.token != token.COLON { |
| 285 |
idx := p.idx |
| 286 |
_, value = p.parseObjectPropertyKey() |
| 287 |
parameterList := p.parseFunctionParameterList() |
| 288 |
|
| 289 |
node := &ast.FunctionLiteral{ |
| 290 |
Function: idx, |
| 291 |
ParameterList: parameterList, |
| 292 |
} |
| 293 |
p.parseFunctionBlock(node) |
| 294 |
return ast.Property{ |
| 295 |
Key: value, |
| 296 |
Kind: "set", |
| 297 |
Value: node, |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
if p.mode&StoreComments != 0 { |
| 302 |
p.comments.MarkComments(ast.COLON) |
| 303 |
} |
| 304 |
p.expect(token.COLON) |
| 305 |
|
| 306 |
exp := ast.Property{ |
| 307 |
Key: value, |
| 308 |
Kind: "value", |
| 309 |
Value: p.parseAssignmentExpression(), |
| 310 |
} |
| 311 |
|
| 312 |
if p.mode&StoreComments != 0 { |
| 313 |
p.comments.SetExpression(exp.Value) |
| 314 |
} |
| 315 |
return exp |
| 316 |
} |
| 317 |
|
| 318 |
func (p *parser) parseObjectLiteral() ast.Expression { |
| 319 |
var value []ast.Property |
| 320 |
idx0 := p.expect(token.LEFT_BRACE) |
| 321 |
for p.token != token.RIGHT_BRACE && p.token != token.EOF { |
| 322 |
value = append(value, p.parseObjectProperty()) |
| 323 |
if p.token == token.COMMA { |
| 324 |
if p.mode&StoreComments != 0 { |
| 325 |
p.comments.Unset() |
| 326 |
} |
| 327 |
p.next() |
| 328 |
continue |
| 329 |
} |
| 330 |
} |
| 331 |
if p.mode&StoreComments != 0 { |
| 332 |
p.comments.MarkComments(ast.FINAL) |
| 333 |
} |
| 334 |
idx1 := p.expect(token.RIGHT_BRACE) |
| 335 |
|
| 336 |
return &ast.ObjectLiteral{ |
| 337 |
LeftBrace: idx0, |
| 338 |
RightBrace: idx1, |
| 339 |
Value: value, |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
func (p *parser) parseArrayLiteral() ast.Expression { |
| 344 |
idx0 := p.expect(token.LEFT_BRACKET) |
| 345 |
var value []ast.Expression |
| 346 |
for p.token != token.RIGHT_BRACKET && p.token != token.EOF { |
| 347 |
if p.token == token.COMMA { |
| 348 |
|
| 349 |
empty := &ast.EmptyExpression{Begin: p.idx, End: p.idx} |
| 350 |
|
| 351 |
if p.mode&StoreComments != 0 { |
| 352 |
p.comments.SetExpression(empty) |
| 353 |
p.comments.Unset() |
| 354 |
} |
| 355 |
value = append(value, empty) |
| 356 |
p.next() |
| 357 |
continue |
| 358 |
} |
| 359 |
|
| 360 |
exp := p.parseAssignmentExpression() |
| 361 |
|
| 362 |
value = append(value, exp) |
| 363 |
if p.token != token.RIGHT_BRACKET { |
| 364 |
if p.mode&StoreComments != 0 { |
| 365 |
p.comments.Unset() |
| 366 |
} |
| 367 |
p.expect(token.COMMA) |
| 368 |
} |
| 369 |
} |
| 370 |
if p.mode&StoreComments != 0 { |
| 371 |
p.comments.MarkComments(ast.FINAL) |
| 372 |
} |
| 373 |
idx1 := p.expect(token.RIGHT_BRACKET) |
| 374 |
|
| 375 |
return &ast.ArrayLiteral{ |
| 376 |
LeftBracket: idx0, |
| 377 |
RightBracket: idx1, |
| 378 |
Value: value, |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
func (p *parser) parseArgumentList() (argumentList []ast.Expression, idx0, idx1 file.Idx) { |
| 383 |
if p.mode&StoreComments != 0 { |
| 384 |
p.comments.Unset() |
| 385 |
} |
| 386 |
idx0 = p.expect(token.LEFT_PARENTHESIS) |
| 387 |
for p.token != token.RIGHT_PARENTHESIS { |
| 388 |
exp := p.parseAssignmentExpression() |
| 389 |
if p.mode&StoreComments != 0 { |
| 390 |
p.comments.SetExpression(exp) |
| 391 |
} |
| 392 |
argumentList = append(argumentList, exp) |
| 393 |
if p.token != token.COMMA { |
| 394 |
break |
| 395 |
} |
| 396 |
if p.mode&StoreComments != 0 { |
| 397 |
p.comments.Unset() |
| 398 |
} |
| 399 |
p.next() |
| 400 |
} |
| 401 |
if p.mode&StoreComments != 0 { |
| 402 |
p.comments.Unset() |
| 403 |
} |
| 404 |
idx1 = p.expect(token.RIGHT_PARENTHESIS) |
| 405 |
return |
| 406 |
} |
| 407 |
|
| 408 |
func (p *parser) parseCallExpression(left ast.Expression) ast.Expression { |
| 409 |
argumentList, idx0, idx1 := p.parseArgumentList() |
| 410 |
exp := &ast.CallExpression{ |
| 411 |
Callee: left, |
| 412 |
LeftParenthesis: idx0, |
| 413 |
ArgumentList: argumentList, |
| 414 |
RightParenthesis: idx1, |
| 415 |
} |
| 416 |
|
| 417 |
if p.mode&StoreComments != 0 { |
| 418 |
p.comments.SetExpression(exp) |
| 419 |
} |
| 420 |
return exp |
| 421 |
} |
| 422 |
|
| 423 |
func (p *parser) parseDotMember(left ast.Expression) ast.Expression { |
| 424 |
period := p.expect(token.PERIOD) |
| 425 |
|
| 426 |
literal := p.literal |
| 427 |
idx := p.idx |
| 428 |
|
| 429 |
if !matchIdentifier.MatchString(literal) { |
| 430 |
p.expect(token.IDENTIFIER) |
| 431 |
p.nextStatement() |
| 432 |
return &ast.BadExpression{From: period, To: p.idx} |
| 433 |
} |
| 434 |
|
| 435 |
p.next() |
| 436 |
|
| 437 |
return &ast.DotExpression{ |
| 438 |
Left: left, |
| 439 |
Identifier: &ast.Identifier{ |
| 440 |
Idx: idx, |
| 441 |
Name: literal, |
| 442 |
}, |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
func (p *parser) parseBracketMember(left ast.Expression) ast.Expression { |
| 447 |
idx0 := p.expect(token.LEFT_BRACKET) |
| 448 |
member := p.parseExpression() |
| 449 |
idx1 := p.expect(token.RIGHT_BRACKET) |
| 450 |
return &ast.BracketExpression{ |
| 451 |
LeftBracket: idx0, |
| 452 |
Left: left, |
| 453 |
Member: member, |
| 454 |
RightBracket: idx1, |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
func (p *parser) parseNewExpression() ast.Expression { |
| 459 |
idx := p.expect(token.NEW) |
| 460 |
callee := p.parseLeftHandSideExpression() |
| 461 |
node := &ast.NewExpression{ |
| 462 |
New: idx, |
| 463 |
Callee: callee, |
| 464 |
} |
| 465 |
if p.token == token.LEFT_PARENTHESIS { |
| 466 |
argumentList, idx0, idx1 := p.parseArgumentList() |
| 467 |
node.ArgumentList = argumentList |
| 468 |
node.LeftParenthesis = idx0 |
| 469 |
node.RightParenthesis = idx1 |
| 470 |
} |
| 471 |
|
| 472 |
if p.mode&StoreComments != 0 { |
| 473 |
p.comments.SetExpression(node) |
| 474 |
} |
| 475 |
|
| 476 |
return node |
| 477 |
} |
| 478 |
|
| 479 |
func (p *parser) parseLeftHandSideExpression() ast.Expression { |
| 480 |
var left ast.Expression |
| 481 |
if p.token == token.NEW { |
| 482 |
left = p.parseNewExpression() |
| 483 |
} else { |
| 484 |
if p.mode&StoreComments != 0 { |
| 485 |
p.comments.MarkComments(ast.LEADING) |
| 486 |
p.comments.MarkPrimary() |
| 487 |
} |
| 488 |
left = p.parsePrimaryExpression() |
| 489 |
} |
| 490 |
|
| 491 |
if p.mode&StoreComments != 0 { |
| 492 |
p.comments.SetExpression(left) |
| 493 |
} |
| 494 |
|
| 495 |
for { |
| 496 |
switch p.token { |
| 497 |
case token.PERIOD: |
| 498 |
left = p.parseDotMember(left) |
| 499 |
case token.LEFT_BRACKET: |
| 500 |
left = p.parseBracketMember(left) |
| 501 |
default: |
| 502 |
return left |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
func (p *parser) parseLeftHandSideExpressionAllowCall() ast.Expression { |
| 508 |
allowIn := p.scope.allowIn |
| 509 |
p.scope.allowIn = true |
| 510 |
defer func() { |
| 511 |
p.scope.allowIn = allowIn |
| 512 |
}() |
| 513 |
|
| 514 |
var left ast.Expression |
| 515 |
if p.token == token.NEW { |
| 516 |
var newComments []*ast.Comment |
| 517 |
if p.mode&StoreComments != 0 { |
| 518 |
newComments = p.comments.FetchAll() |
| 519 |
p.comments.MarkComments(ast.LEADING) |
| 520 |
p.comments.MarkPrimary() |
| 521 |
} |
| 522 |
left = p.parseNewExpression() |
| 523 |
if p.mode&StoreComments != 0 { |
| 524 |
p.comments.CommentMap.AddComments(left, newComments, ast.LEADING) |
| 525 |
} |
| 526 |
} else { |
| 527 |
if p.mode&StoreComments != 0 { |
| 528 |
p.comments.MarkComments(ast.LEADING) |
| 529 |
p.comments.MarkPrimary() |
| 530 |
} |
| 531 |
left = p.parsePrimaryExpression() |
| 532 |
} |
| 533 |
|
| 534 |
if p.mode&StoreComments != 0 { |
| 535 |
p.comments.SetExpression(left) |
| 536 |
} |
| 537 |
|
| 538 |
for { |
| 539 |
switch p.token { |
| 540 |
case token.PERIOD: |
| 541 |
left = p.parseDotMember(left) |
| 542 |
case token.LEFT_BRACKET: |
| 543 |
left = p.parseBracketMember(left) |
| 544 |
case token.LEFT_PARENTHESIS: |
| 545 |
left = p.parseCallExpression(left) |
| 546 |
default: |
| 547 |
return left |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
func (p *parser) parsePostfixExpression() ast.Expression { |
| 553 |
operand := p.parseLeftHandSideExpressionAllowCall() |
| 554 |
|
| 555 |
switch p.token { |
| 556 |
case token.INCREMENT, token.DECREMENT: |
| 557 |
|
| 558 |
if p.implicitSemicolon { |
| 559 |
break |
| 560 |
} |
| 561 |
tkn := p.token |
| 562 |
idx := p.idx |
| 563 |
if p.mode&StoreComments != 0 { |
| 564 |
p.comments.Unset() |
| 565 |
} |
| 566 |
p.next() |
| 567 |
switch operand.(type) { |
| 568 |
case *ast.Identifier, *ast.DotExpression, *ast.BracketExpression: |
| 569 |
default: |
| 570 |
p.error(idx, "invalid left-hand side in assignment") |
| 571 |
p.nextStatement() |
| 572 |
return &ast.BadExpression{From: idx, To: p.idx} |
| 573 |
} |
| 574 |
exp := &ast.UnaryExpression{ |
| 575 |
Operator: tkn, |
| 576 |
Idx: idx, |
| 577 |
Operand: operand, |
| 578 |
Postfix: true, |
| 579 |
} |
| 580 |
|
| 581 |
if p.mode&StoreComments != 0 { |
| 582 |
p.comments.SetExpression(exp) |
| 583 |
} |
| 584 |
|
| 585 |
return exp |
| 586 |
} |
| 587 |
|
| 588 |
return operand |
| 589 |
} |
| 590 |
|
| 591 |
func (p *parser) parseUnaryExpression() ast.Expression { |
| 592 |
switch p.token { |
| 593 |
case token.PLUS, token.MINUS, token.NOT, token.BITWISE_NOT: |
| 594 |
fallthrough |
| 595 |
case token.DELETE, token.VOID, token.TYPEOF: |
| 596 |
tkn := p.token |
| 597 |
idx := p.idx |
| 598 |
if p.mode&StoreComments != 0 { |
| 599 |
p.comments.Unset() |
| 600 |
} |
| 601 |
p.next() |
| 602 |
|
| 603 |
return &ast.UnaryExpression{ |
| 604 |
Operator: tkn, |
| 605 |
Idx: idx, |
| 606 |
Operand: p.parseUnaryExpression(), |
| 607 |
} |
| 608 |
case token.INCREMENT, token.DECREMENT: |
| 609 |
tkn := p.token |
| 610 |
idx := p.idx |
| 611 |
if p.mode&StoreComments != 0 { |
| 612 |
p.comments.Unset() |
| 613 |
} |
| 614 |
p.next() |
| 615 |
operand := p.parseUnaryExpression() |
| 616 |
switch operand.(type) { |
| 617 |
case *ast.Identifier, *ast.DotExpression, *ast.BracketExpression: |
| 618 |
default: |
| 619 |
p.error(idx, "invalid left-hand side in assignment") |
| 620 |
p.nextStatement() |
| 621 |
return &ast.BadExpression{From: idx, To: p.idx} |
| 622 |
} |
| 623 |
return &ast.UnaryExpression{ |
| 624 |
Operator: tkn, |
| 625 |
Idx: idx, |
| 626 |
Operand: operand, |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
return p.parsePostfixExpression() |
| 631 |
} |
| 632 |
|
| 633 |
func (p *parser) parseMultiplicativeExpression() ast.Expression { |
| 634 |
next := p.parseUnaryExpression |
| 635 |
left := next() |
| 636 |
|
| 637 |
for p.token == token.MULTIPLY || p.token == token.SLASH || |
| 638 |
p.token == token.REMAINDER { |
| 639 |
tkn := p.token |
| 640 |
if p.mode&StoreComments != 0 { |
| 641 |
p.comments.Unset() |
| 642 |
} |
| 643 |
p.next() |
| 644 |
|
| 645 |
left = &ast.BinaryExpression{ |
| 646 |
Operator: tkn, |
| 647 |
Left: left, |
| 648 |
Right: next(), |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
return left |
| 653 |
} |
| 654 |
|
| 655 |
func (p *parser) parseAdditiveExpression() ast.Expression { |
| 656 |
next := p.parseMultiplicativeExpression |
| 657 |
left := next() |
| 658 |
|
| 659 |
for p.token == token.PLUS || p.token == token.MINUS { |
| 660 |
tkn := p.token |
| 661 |
if p.mode&StoreComments != 0 { |
| 662 |
p.comments.Unset() |
| 663 |
} |
| 664 |
p.next() |
| 665 |
|
| 666 |
left = &ast.BinaryExpression{ |
| 667 |
Operator: tkn, |
| 668 |
Left: left, |
| 669 |
Right: next(), |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
return left |
| 674 |
} |
| 675 |
|
| 676 |
func (p *parser) parseShiftExpression() ast.Expression { |
| 677 |
next := p.parseAdditiveExpression |
| 678 |
left := next() |
| 679 |
|
| 680 |
for p.token == token.SHIFT_LEFT || p.token == token.SHIFT_RIGHT || |
| 681 |
p.token == token.UNSIGNED_SHIFT_RIGHT { |
| 682 |
tkn := p.token |
| 683 |
if p.mode&StoreComments != 0 { |
| 684 |
p.comments.Unset() |
| 685 |
} |
| 686 |
p.next() |
| 687 |
|
| 688 |
left = &ast.BinaryExpression{ |
| 689 |
Operator: tkn, |
| 690 |
Left: left, |
| 691 |
Right: next(), |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
return left |
| 696 |
} |
| 697 |
|
| 698 |
func (p *parser) parseRelationalExpression() ast.Expression { |
| 699 |
next := p.parseShiftExpression |
| 700 |
left := next() |
| 701 |
|
| 702 |
allowIn := p.scope.allowIn |
| 703 |
p.scope.allowIn = true |
| 704 |
defer func() { |
| 705 |
p.scope.allowIn = allowIn |
| 706 |
}() |
| 707 |
|
| 708 |
switch p.token { |
| 709 |
case token.LESS, token.LESS_OR_EQUAL, token.GREATER, token.GREATER_OR_EQUAL: |
| 710 |
tkn := p.token |
| 711 |
if p.mode&StoreComments != 0 { |
| 712 |
p.comments.Unset() |
| 713 |
} |
| 714 |
p.next() |
| 715 |
|
| 716 |
exp := &ast.BinaryExpression{ |
| 717 |
Operator: tkn, |
| 718 |
Left: left, |
| 719 |
Right: p.parseRelationalExpression(), |
| 720 |
Comparison: true, |
| 721 |
} |
| 722 |
return exp |
| 723 |
case token.INSTANCEOF: |
| 724 |
tkn := p.token |
| 725 |
if p.mode&StoreComments != 0 { |
| 726 |
p.comments.Unset() |
| 727 |
} |
| 728 |
p.next() |
| 729 |
|
| 730 |
exp := &ast.BinaryExpression{ |
| 731 |
Operator: tkn, |
| 732 |
Left: left, |
| 733 |
Right: p.parseRelationalExpression(), |
| 734 |
} |
| 735 |
return exp |
| 736 |
case token.IN: |
| 737 |
if !allowIn { |
| 738 |
return left |
| 739 |
} |
| 740 |
tkn := p.token |
| 741 |
if p.mode&StoreComments != 0 { |
| 742 |
p.comments.Unset() |
| 743 |
} |
| 744 |
p.next() |
| 745 |
|
| 746 |
exp := &ast.BinaryExpression{ |
| 747 |
Operator: tkn, |
| 748 |
Left: left, |
| 749 |
Right: p.parseRelationalExpression(), |
| 750 |
} |
| 751 |
return exp |
| 752 |
} |
| 753 |
|
| 754 |
return left |
| 755 |
} |
| 756 |
|
| 757 |
func (p *parser) parseEqualityExpression() ast.Expression { |
| 758 |
next := p.parseRelationalExpression |
| 759 |
left := next() |
| 760 |
|
| 761 |
for p.token == token.EQUAL || p.token == token.NOT_EQUAL || |
| 762 |
p.token == token.STRICT_EQUAL || p.token == token.STRICT_NOT_EQUAL { |
| 763 |
tkn := p.token |
| 764 |
if p.mode&StoreComments != 0 { |
| 765 |
p.comments.Unset() |
| 766 |
} |
| 767 |
p.next() |
| 768 |
|
| 769 |
left = &ast.BinaryExpression{ |
| 770 |
Operator: tkn, |
| 771 |
Left: left, |
| 772 |
Right: next(), |
| 773 |
Comparison: true, |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
return left |
| 778 |
} |
| 779 |
|
| 780 |
func (p *parser) parseBitwiseAndExpression() ast.Expression { |
| 781 |
next := p.parseEqualityExpression |
| 782 |
left := next() |
| 783 |
|
| 784 |
for p.token == token.AND { |
| 785 |
if p.mode&StoreComments != 0 { |
| 786 |
p.comments.Unset() |
| 787 |
} |
| 788 |
tkn := p.token |
| 789 |
p.next() |
| 790 |
|
| 791 |
left = &ast.BinaryExpression{ |
| 792 |
Operator: tkn, |
| 793 |
Left: left, |
| 794 |
Right: next(), |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
return left |
| 799 |
} |
| 800 |
|
| 801 |
func (p *parser) parseBitwiseExclusiveOrExpression() ast.Expression { |
| 802 |
next := p.parseBitwiseAndExpression |
| 803 |
left := next() |
| 804 |
|
| 805 |
for p.token == token.EXCLUSIVE_OR { |
| 806 |
if p.mode&StoreComments != 0 { |
| 807 |
p.comments.Unset() |
| 808 |
} |
| 809 |
tkn := p.token |
| 810 |
p.next() |
| 811 |
|
| 812 |
left = &ast.BinaryExpression{ |
| 813 |
Operator: tkn, |
| 814 |
Left: left, |
| 815 |
Right: next(), |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
return left |
| 820 |
} |
| 821 |
|
| 822 |
func (p *parser) parseBitwiseOrExpression() ast.Expression { |
| 823 |
next := p.parseBitwiseExclusiveOrExpression |
| 824 |
left := next() |
| 825 |
|
| 826 |
for p.token == token.OR { |
| 827 |
if p.mode&StoreComments != 0 { |
| 828 |
p.comments.Unset() |
| 829 |
} |
| 830 |
tkn := p.token |
| 831 |
p.next() |
| 832 |
|
| 833 |
left = &ast.BinaryExpression{ |
| 834 |
Operator: tkn, |
| 835 |
Left: left, |
| 836 |
Right: next(), |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
return left |
| 841 |
} |
| 842 |
|
| 843 |
func (p *parser) parseLogicalAndExpression() ast.Expression { |
| 844 |
next := p.parseBitwiseOrExpression |
| 845 |
left := next() |
| 846 |
|
| 847 |
for p.token == token.LOGICAL_AND { |
| 848 |
if p.mode&StoreComments != 0 { |
| 849 |
p.comments.Unset() |
| 850 |
} |
| 851 |
tkn := p.token |
| 852 |
p.next() |
| 853 |
|
| 854 |
left = &ast.BinaryExpression{ |
| 855 |
Operator: tkn, |
| 856 |
Left: left, |
| 857 |
Right: next(), |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
return left |
| 862 |
} |
| 863 |
|
| 864 |
func (p *parser) parseLogicalOrExpression() ast.Expression { |
| 865 |
next := p.parseLogicalAndExpression |
| 866 |
left := next() |
| 867 |
|
| 868 |
for p.token == token.LOGICAL_OR { |
| 869 |
if p.mode&StoreComments != 0 { |
| 870 |
p.comments.Unset() |
| 871 |
} |
| 872 |
tkn := p.token |
| 873 |
p.next() |
| 874 |
|
| 875 |
left = &ast.BinaryExpression{ |
| 876 |
Operator: tkn, |
| 877 |
Left: left, |
| 878 |
Right: next(), |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
return left |
| 883 |
} |
| 884 |
|
| 885 |
func (p *parser) parseConditionalExpression() ast.Expression { |
| 886 |
left := p.parseLogicalOrExpression() |
| 887 |
|
| 888 |
if p.token == token.QUESTION_MARK { |
| 889 |
if p.mode&StoreComments != 0 { |
| 890 |
p.comments.Unset() |
| 891 |
} |
| 892 |
p.next() |
| 893 |
|
| 894 |
consequent := p.parseAssignmentExpression() |
| 895 |
if p.mode&StoreComments != 0 { |
| 896 |
p.comments.Unset() |
| 897 |
} |
| 898 |
p.expect(token.COLON) |
| 899 |
exp := &ast.ConditionalExpression{ |
| 900 |
Test: left, |
| 901 |
Consequent: consequent, |
| 902 |
Alternate: p.parseAssignmentExpression(), |
| 903 |
} |
| 904 |
|
| 905 |
return exp |
| 906 |
} |
| 907 |
|
| 908 |
return left |
| 909 |
} |
| 910 |
|
| 911 |
func (p *parser) parseAssignmentExpression() ast.Expression { |
| 912 |
left := p.parseConditionalExpression() |
| 913 |
var operator token.Token |
| 914 |
switch p.token { |
| 915 |
case token.ASSIGN: |
| 916 |
operator = p.token |
| 917 |
case token.ADD_ASSIGN: |
| 918 |
operator = token.PLUS |
| 919 |
case token.SUBTRACT_ASSIGN: |
| 920 |
operator = token.MINUS |
| 921 |
case token.MULTIPLY_ASSIGN: |
| 922 |
operator = token.MULTIPLY |
| 923 |
case token.QUOTIENT_ASSIGN: |
| 924 |
operator = token.SLASH |
| 925 |
case token.REMAINDER_ASSIGN: |
| 926 |
operator = token.REMAINDER |
| 927 |
case token.AND_ASSIGN: |
| 928 |
operator = token.AND |
| 929 |
case token.AND_NOT_ASSIGN: |
| 930 |
operator = token.AND_NOT |
| 931 |
case token.OR_ASSIGN: |
| 932 |
operator = token.OR |
| 933 |
case token.EXCLUSIVE_OR_ASSIGN: |
| 934 |
operator = token.EXCLUSIVE_OR |
| 935 |
case token.SHIFT_LEFT_ASSIGN: |
| 936 |
operator = token.SHIFT_LEFT |
| 937 |
case token.SHIFT_RIGHT_ASSIGN: |
| 938 |
operator = token.SHIFT_RIGHT |
| 939 |
case token.UNSIGNED_SHIFT_RIGHT_ASSIGN: |
| 940 |
operator = token.UNSIGNED_SHIFT_RIGHT |
| 941 |
} |
| 942 |
|
| 943 |
if operator != 0 { |
| 944 |
idx := p.idx |
| 945 |
if p.mode&StoreComments != 0 { |
| 946 |
p.comments.Unset() |
| 947 |
} |
| 948 |
p.next() |
| 949 |
switch left.(type) { |
| 950 |
case *ast.Identifier, *ast.DotExpression, *ast.BracketExpression: |
| 951 |
default: |
| 952 |
p.error(left.Idx0(), "invalid left-hand side in assignment") |
| 953 |
p.nextStatement() |
| 954 |
return &ast.BadExpression{From: idx, To: p.idx} |
| 955 |
} |
| 956 |
|
| 957 |
exp := &ast.AssignExpression{ |
| 958 |
Left: left, |
| 959 |
Operator: operator, |
| 960 |
Right: p.parseAssignmentExpression(), |
| 961 |
} |
| 962 |
|
| 963 |
if p.mode&StoreComments != 0 { |
| 964 |
p.comments.SetExpression(exp) |
| 965 |
} |
| 966 |
|
| 967 |
return exp |
| 968 |
} |
| 969 |
|
| 970 |
return left |
| 971 |
} |
| 972 |
|
| 973 |
func (p *parser) parseExpression() ast.Expression { |
| 974 |
next := p.parseAssignmentExpression |
| 975 |
left := next() |
| 976 |
|
| 977 |
if p.token == token.COMMA { |
| 978 |
sequence := []ast.Expression{left} |
| 979 |
for { |
| 980 |
if p.token != token.COMMA { |
| 981 |
break |
| 982 |
} |
| 983 |
p.next() |
| 984 |
sequence = append(sequence, next()) |
| 985 |
} |
| 986 |
return &ast.SequenceExpression{ |
| 987 |
Sequence: sequence, |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
return left |
| 992 |
} |