this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cue/ast: reorganization of node types

Change-Id: Ib61eb58bb35c301898a67460bde783a7ead7cd70
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3484
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>

+103 -67
+102 -66
cue/ast/ast.go
··· 90 90 exprNode() 91 91 } 92 92 93 - func (*BadExpr) exprNode() {} 94 - func (*Ident) exprNode() {} 95 - func (*BasicLit) exprNode() {} 96 - func (*Interpolation) exprNode() {} 97 - func (*StructLit) exprNode() {} 98 - func (*ListLit) exprNode() {} 99 - func (*Ellipsis) exprNode() {} 93 + type expr struct{} 100 94 101 - // func (*Comprehension) exprNode() {} 102 - func (*ListComprehension) exprNode() {} 103 - func (*ParenExpr) exprNode() {} 104 - func (*SelectorExpr) exprNode() {} 105 - func (*IndexExpr) exprNode() {} 106 - func (*SliceExpr) exprNode() {} 107 - func (*CallExpr) exprNode() {} 108 - func (*UnaryExpr) exprNode() {} 109 - func (*BinaryExpr) exprNode() {} 110 - func (*BottomLit) exprNode() {} 95 + func (expr) exprNode() {} 111 96 112 97 // A Decl node is implemented by all declarations. 113 98 type Decl interface { ··· 115 100 declNode() 116 101 } 117 102 118 - func (*Field) declNode() {} 119 - func (*Comprehension) declNode() {} 120 - func (*ImportDecl) declNode() {} 121 - func (*BadDecl) declNode() {} 122 - func (*EmbedDecl) declNode() {} 123 - func (*Alias) declNode() {} 124 - func (*Ellipsis) declNode() {} 103 + type decl struct{} 125 104 126 - // Not technically declarations, but appearing at the same level. 127 - func (*Package) declNode() {} 128 - func (*CommentGroup) declNode() {} 105 + func (decl) declNode() {} 129 106 130 107 // A Label is any production that can be used as a LHS label. 131 108 type Label interface { ··· 143 120 clauseNode() 144 121 } 145 122 123 + type clause struct{} 124 + 125 + func (clause) clauseNode() {} 126 + 146 127 func (x *ForClause) clauseNode() {} 147 128 func (x *IfClause) clauseNode() {} 148 129 func (x *Alias) clauseNode() {} ··· 213 194 // <0> Label <1> ":" <2> Expr <3> "," <4> 214 195 Position int8 215 196 List []*Comment // len(List) > 0 197 + 198 + decl 216 199 } 217 200 218 201 func (g *CommentGroup) Pos() token.Pos { return getPos(g) } ··· 294 277 295 278 // An Attribute provides meta data about a field. 296 279 type Attribute struct { 297 - comments 298 280 At token.Pos 299 281 Text string // must be a valid attribute format. 282 + 283 + comments 300 284 } 301 285 302 286 func (a *Attribute) Pos() token.Pos { return a.At } ··· 305 289 306 290 // A Field represents a field declaration in a struct. 307 291 type Field struct { 308 - comments 309 292 Label Label // must have at least one element. 310 293 Optional token.Pos 311 294 ··· 316 299 Value Expr // the value associated with this field. 317 300 318 301 Attrs []*Attribute 302 + 303 + comments 304 + decl 319 305 } 320 306 321 307 func (d *Field) Pos() token.Pos { return d.Label.Pos() } ··· 332 318 333 319 // An Alias binds another field to the alias name in the current struct. 334 320 type Alias struct { 335 - comments 336 321 Ident *Ident // field name, always an Ident 337 322 Equal token.Pos // position of "=" 338 323 Expr Expr // An Ident or SelectorExpr 324 + 325 + comments 326 + decl 339 327 } 340 328 341 329 func (a *Alias) Pos() token.Pos { return a.Ident.Pos() } ··· 344 332 345 333 // A Comprehension node represents a comprehension declaration. 346 334 type Comprehension struct { 347 - comments 348 335 Clauses []Clause // There must be at least one clause. 349 336 Value Expr // Must be a struct 337 + 338 + comments 339 + decl 350 340 } 351 341 352 342 func (x *Comprehension) Pos() token.Pos { return getPos(x) } ··· 366 356 // created. This is different from an ErrorExpr which represents 367 357 // an explicitly marked error in the source. 368 358 type BadExpr struct { 359 + From, To token.Pos // position range of bad expression 360 + 369 361 comments 370 - From, To token.Pos // position range of bad expression 362 + expr 371 363 } 372 364 373 365 // A BottomLit indicates an error. 374 366 type BottomLit struct { 375 - comments 376 367 Bottom token.Pos 368 + 369 + comments 370 + expr 377 371 } 378 372 379 373 // An Ident node represents an left-hand side identifier. 380 374 type Ident struct { 381 - label 382 - comments 383 375 NamePos token.Pos // identifier position 384 376 385 377 // This LHS path element may be an identifier. Possible forms: ··· 390 382 391 383 Scope Node // scope in which node was found or nil if referring directly 392 384 Node Node 385 + 386 + comments 387 + label 388 + expr 393 389 } 394 390 395 391 // A TemplateLabel represents a field template declaration in a struct. 396 392 type TemplateLabel struct { 397 - label 398 - comments 399 393 Langle token.Pos 400 394 Ident *Ident 401 395 Rangle token.Pos 396 + 397 + comments 398 + label 402 399 } 403 400 404 401 // A BasicLit node represents a literal of basic type. 405 402 type BasicLit struct { 406 - label 407 - comments 408 403 ValuePos token.Pos // literal position 409 404 Kind token.Token // INT, FLOAT, DURATION, or STRING 410 405 Value string // literal string; e.g. 42, 0x7f, 3.14, 1_234_567, 1e-9, 2.4i, 'a', '\x7f', "foo", or '\m\n\o' 406 + 407 + comments 408 + expr 409 + label 411 410 } 412 411 413 412 // NewString creates a new BasicLit with a string value without position. ··· 425 424 426 425 // A Interpolation node represents a string or bytes interpolation. 427 426 type Interpolation struct { 427 + Elts []Expr // interleaving of strings and expressions. 428 + 429 + comments 430 + expr 428 431 label 429 - comments 430 - Elts []Expr // interleaving of strings and expressions. 431 432 } 432 433 433 434 // A StructLit node represents a literal struct. 434 435 type StructLit struct { 435 - comments 436 436 Lbrace token.Pos // position of "{" 437 437 Elts []Decl // list of elements; or nil 438 438 Rbrace token.Pos // position of "}" 439 + 440 + comments 441 + expr 439 442 } 440 443 441 444 // A ListLit node represents a literal list. 442 445 type ListLit struct { 443 - comments 444 446 Lbrack token.Pos // position of "[" 445 447 Elts []Expr // list of composite elements; or nil 446 448 Rbrack token.Pos // position of "]" 449 + 450 + comments 451 + expr 447 452 } 448 453 449 454 type Ellipsis struct { 450 - comments 451 455 Ellipsis token.Pos // open list if set 452 456 Type Expr // type for the remaining elements 457 + 458 + comments 459 + decl 460 + expr 453 461 } 454 462 455 463 // A ListComprehension node represents as list comprehension. 456 464 type ListComprehension struct { 457 - comments 458 465 Lbrack token.Pos // position of "[" 459 466 Expr Expr 460 467 Clauses []Clause // Feed or Guard (TODO let) 461 468 Rbrack token.Pos // position of "]" 469 + 470 + comments 471 + expr 462 472 } 463 473 464 474 // A ForClause node represents a for clause in a comprehension. 465 475 type ForClause struct { 466 - comments 467 476 For token.Pos 468 477 Key *Ident // allow pattern matching? 469 478 // TODO: change to Comma ··· 471 480 Value *Ident // allow pattern matching? 472 481 In token.Pos 473 482 Source Expr 483 + 484 + comments 485 + clause 474 486 } 475 487 476 488 // A IfClause node represents an if guard clause in a comprehension. 477 489 type IfClause struct { 478 - comments 479 490 If token.Pos 480 491 Condition Expr 492 + 493 + comments 494 + clause 481 495 } 482 496 483 497 // A ParenExpr node represents a parenthesized expression. 484 498 type ParenExpr struct { 485 - comments 486 499 Lparen token.Pos // position of "(" 487 500 X Expr // parenthesized expression 488 501 Rparen token.Pos // position of ")" 502 + 503 + comments 504 + expr 489 505 } 490 506 491 507 // A SelectorExpr node represents an expression followed by a selector. 492 508 type SelectorExpr struct { 493 - comments 494 509 X Expr // expression 495 510 Sel *Ident // field selector 511 + 512 + comments 513 + expr 496 514 } 497 515 498 516 // NewSel creates a sequence of selectors. ··· 506 524 507 525 // An IndexExpr node represents an expression followed by an index. 508 526 type IndexExpr struct { 509 - comments 510 527 X Expr // expression 511 528 Lbrack token.Pos // position of "[" 512 529 Index Expr // index expression 513 530 Rbrack token.Pos // position of "]" 531 + 532 + comments 533 + expr 514 534 } 515 535 516 536 // An SliceExpr node represents an expression followed by slice indices. 517 537 type SliceExpr struct { 518 - comments 519 538 X Expr // expression 520 539 Lbrack token.Pos // position of "[" 521 540 Low Expr // begin of slice range; or nil 522 541 High Expr // end of slice range; or nil 523 542 Rbrack token.Pos // position of "]" 543 + 544 + comments 545 + expr 524 546 } 525 547 526 548 // A CallExpr node represents an expression followed by an argument list. 527 549 type CallExpr struct { 528 - comments 529 550 Fun Expr // function expression 530 551 Lparen token.Pos // position of "(" 531 552 Args []Expr // function arguments; or nil 532 553 Rparen token.Pos // position of ")" 554 + 555 + comments 556 + expr 533 557 } 534 558 535 559 // NewCall creates a new CallExpr. ··· 540 564 541 565 // A UnaryExpr node represents a unary expression. 542 566 type UnaryExpr struct { 543 - comments 544 567 OpPos token.Pos // position of Op 545 568 Op token.Token // operator 546 569 X Expr // operand 570 + 571 + comments 572 + expr 547 573 } 548 574 549 575 // A BinaryExpr node represents a binary expression. 550 576 type BinaryExpr struct { 551 - comments 552 577 X Expr // left operand 553 578 OpPos token.Pos // position of Op 554 579 Op token.Token // operator 555 580 Y Expr // right operand 581 + 582 + comments 583 + expr 556 584 } 557 585 558 586 // token.Pos and End implementations for expression/type nodes. ··· 641 669 // NewIdent creates a new Ident without position. 642 670 // Useful for ASTs generated by code other than the Go 643 671 func NewIdent(name string) *Ident { 644 - return &Ident{label{}, comments{}, token.NoPos, name, nil, nil} 672 + return &Ident{token.NoPos, name, nil, nil, comments{}, label{}, expr{}} 645 673 } 646 674 647 675 func (id *Ident) String() string { ··· 656 684 657 685 // An ImportSpec node represents a single package import. 658 686 type ImportSpec struct { 659 - comments 660 687 Name *Ident // local package name (including "."); or nil 661 688 Path *BasicLit // import path 662 689 EndPos token.Pos // end of spec (overrides Path.Pos if nonzero) 690 + 691 + comments 663 692 } 693 + 694 + func (*ImportSpec) specNode() {} 664 695 665 696 func NewImport(name *Ident, importPath string) *ImportSpec { 666 697 importPath = strconv.Quote(importPath) ··· 693 724 // syntax errors for which no correct declaration nodes can be 694 725 // created. 695 726 type BadDecl struct { 696 - comments 697 727 From, To token.Pos // position range of bad declaration 728 + 729 + comments 730 + decl 698 731 } 699 732 700 733 // A ImportDecl node represents a series of import declarations. A valid 701 734 // Lparen position (Lparen.Line > 0) indicates a parenthesized declaration. 702 735 type ImportDecl struct { 703 - comments 704 736 Import token.Pos 705 737 Lparen token.Pos // position of '(', if any 706 738 Specs []*ImportSpec 707 739 Rparen token.Pos // position of ')', if any 740 + 741 + comments 742 + decl 708 743 } 709 744 710 745 type Spec interface { ··· 712 747 specNode() 713 748 } 714 749 715 - func (*ImportSpec) specNode() {} 716 - 717 - func (*Package) specNode() {} 718 - 719 750 // An EmbedDecl node represents a single expression used as a declaration. 720 751 // The expressions in this declaration is what will be emitted as 721 752 // configuration output. 722 753 // 723 754 // An EmbedDecl may only appear at the top level. 724 755 type EmbedDecl struct { 756 + Expr Expr 757 + 725 758 comments 726 - Expr Expr 759 + decl 727 760 } 728 761 729 762 // Pos and End implementations for declaration nodes. ··· 757 790 // via Doc and Comment fields. 758 791 type File struct { 759 792 Filename string 760 - comments 761 - Decls []Decl // top-level declarations; or nil 793 + Decls []Decl // top-level declarations; or nil 762 794 763 795 Imports []*ImportSpec // imports in this file 764 796 Unresolved []*Ident // unresolved identifiers in this file 797 + 798 + comments 765 799 } 766 800 767 801 func (f *File) Pos() token.Pos { ··· 794 828 795 829 // A Package represents a package clause. 796 830 type Package struct { 797 - comments 798 831 PackagePos token.Pos // position of "package" pseudo-keyword 799 832 Name *Ident // package name 833 + 834 + comments 835 + decl 800 836 } 801 837 802 838 func (p *Package) Pos() token.Pos { return getPos(p) }
+1 -1
cue/parser/parser_test.go
··· 544 544 if sel == nil { 545 545 t.Fatalf("found no *SelectorExpr: %#v %s", f.Decls[0], debugStr(f)) 546 546 } 547 - const wantSel = "&{{<nil>} fmt _}" 547 + const wantSel = "&{fmt _ {<nil>} {}}" 548 548 if fmt.Sprint(sel) != wantSel { 549 549 t.Fatalf("found selector %v, want %s", sel, wantSel) 550 550 }