github.com/syumai/protoreflect@v1.7.1-0.20200810020253-2ac7e3b3a321/desc/protoparse/ast.go (about)

     1  package protoparse
     2  
     3  import "fmt"
     4  
     5  // This file defines all of the nodes in the proto AST.
     6  
     7  // SourcePos identifies a location in a proto source file.
     8  type SourcePos struct {
     9  	Filename  string
    10  	Line, Col int
    11  	Offset    int
    12  }
    13  
    14  func (pos SourcePos) String() string {
    15  	if pos.Line <= 0 || pos.Col <= 0 {
    16  		return pos.Filename
    17  	}
    18  	return fmt.Sprintf("%s:%d:%d", pos.Filename, pos.Line, pos.Col)
    19  }
    20  
    21  func unknownPos(filename string) *SourcePos {
    22  	return &SourcePos{Filename: filename}
    23  }
    24  
    25  // node is the interface implemented by all nodes in the AST
    26  type node interface {
    27  	start() *SourcePos
    28  	end() *SourcePos
    29  	leadingComments() []comment
    30  	trailingComments() []comment
    31  }
    32  
    33  type terminalNode interface {
    34  	node
    35  	popLeadingComment() comment
    36  	pushTrailingComment(comment)
    37  }
    38  
    39  var _ terminalNode = (*basicNode)(nil)
    40  var _ terminalNode = (*stringLiteralNode)(nil)
    41  var _ terminalNode = (*intLiteralNode)(nil)
    42  var _ terminalNode = (*floatLiteralNode)(nil)
    43  var _ terminalNode = (*identNode)(nil)
    44  
    45  type fileDecl interface {
    46  	node
    47  	getSyntax() node
    48  }
    49  
    50  var _ fileDecl = (*fileNode)(nil)
    51  var _ fileDecl = (*noSourceNode)(nil)
    52  
    53  type optionDecl interface {
    54  	node
    55  	getName() node
    56  	getValue() valueNode
    57  }
    58  
    59  var _ optionDecl = (*optionNode)(nil)
    60  var _ optionDecl = (*noSourceNode)(nil)
    61  
    62  type fieldDecl interface {
    63  	node
    64  	fieldLabel() node
    65  	fieldName() node
    66  	fieldType() node
    67  	fieldTag() node
    68  	fieldExtendee() node
    69  	getGroupKeyword() node
    70  }
    71  
    72  var _ fieldDecl = (*fieldNode)(nil)
    73  var _ fieldDecl = (*groupNode)(nil)
    74  var _ fieldDecl = (*mapFieldNode)(nil)
    75  var _ fieldDecl = (*syntheticMapField)(nil)
    76  var _ fieldDecl = (*noSourceNode)(nil)
    77  
    78  type rangeDecl interface {
    79  	node
    80  	rangeStart() node
    81  	rangeEnd() node
    82  }
    83  
    84  var _ rangeDecl = (*rangeNode)(nil)
    85  var _ rangeDecl = (*noSourceNode)(nil)
    86  
    87  type enumValueDecl interface {
    88  	node
    89  	getName() node
    90  	getNumber() node
    91  }
    92  
    93  var _ enumValueDecl = (*enumValueNode)(nil)
    94  var _ enumValueDecl = (*noSourceNode)(nil)
    95  
    96  type msgDecl interface {
    97  	node
    98  	messageName() node
    99  }
   100  
   101  var _ msgDecl = (*messageNode)(nil)
   102  var _ msgDecl = (*groupNode)(nil)
   103  var _ msgDecl = (*mapFieldNode)(nil)
   104  var _ msgDecl = (*noSourceNode)(nil)
   105  
   106  type methodDecl interface {
   107  	node
   108  	getInputType() node
   109  	getOutputType() node
   110  }
   111  
   112  var _ methodDecl = (*methodNode)(nil)
   113  var _ methodDecl = (*noSourceNode)(nil)
   114  
   115  type posRange struct {
   116  	start, end SourcePos
   117  }
   118  
   119  type basicNode struct {
   120  	posRange
   121  	leading  []comment
   122  	trailing []comment
   123  }
   124  
   125  func (n *basicNode) start() *SourcePos {
   126  	return &n.posRange.start
   127  }
   128  
   129  func (n *basicNode) end() *SourcePos {
   130  	return &n.posRange.end
   131  }
   132  
   133  func (n *basicNode) leadingComments() []comment {
   134  	return n.leading
   135  }
   136  
   137  func (n *basicNode) trailingComments() []comment {
   138  	return n.trailing
   139  }
   140  
   141  func (n *basicNode) popLeadingComment() comment {
   142  	c := n.leading[0]
   143  	n.leading = n.leading[1:]
   144  	return c
   145  }
   146  
   147  func (n *basicNode) pushTrailingComment(c comment) {
   148  	n.trailing = append(n.trailing, c)
   149  }
   150  
   151  type comment struct {
   152  	posRange
   153  	text string
   154  }
   155  
   156  type basicCompositeNode struct {
   157  	first node
   158  	last  node
   159  }
   160  
   161  func (n *basicCompositeNode) start() *SourcePos {
   162  	return n.first.start()
   163  }
   164  
   165  func (n *basicCompositeNode) end() *SourcePos {
   166  	return n.last.end()
   167  }
   168  
   169  func (n *basicCompositeNode) leadingComments() []comment {
   170  	return n.first.leadingComments()
   171  }
   172  
   173  func (n *basicCompositeNode) trailingComments() []comment {
   174  	return n.last.trailingComments()
   175  }
   176  
   177  func (n *basicCompositeNode) setRange(first, last node) {
   178  	n.first = first
   179  	n.last = last
   180  }
   181  
   182  type fileNode struct {
   183  	basicCompositeNode
   184  	syntax *syntaxNode
   185  	decls  []*fileElement
   186  
   187  	// This field is populated after parsing, to make it easier to find
   188  	// source locations by import name for constructing link errors.
   189  	imports []*importNode
   190  }
   191  
   192  func (n *fileNode) getSyntax() node {
   193  	return n.syntax
   194  }
   195  
   196  type fileElement struct {
   197  	// a discriminated union: only one field will be set
   198  	imp     *importNode
   199  	pkg     *packageNode
   200  	option  *optionNode
   201  	message *messageNode
   202  	enum    *enumNode
   203  	extend  *extendNode
   204  	service *serviceNode
   205  	empty   *basicNode
   206  }
   207  
   208  func (n *fileElement) start() *SourcePos {
   209  	return n.get().start()
   210  }
   211  
   212  func (n *fileElement) end() *SourcePos {
   213  	return n.get().end()
   214  }
   215  
   216  func (n *fileElement) leadingComments() []comment {
   217  	return n.get().leadingComments()
   218  }
   219  
   220  func (n *fileElement) trailingComments() []comment {
   221  	return n.get().trailingComments()
   222  }
   223  
   224  func (n *fileElement) get() node {
   225  	switch {
   226  	case n.imp != nil:
   227  		return n.imp
   228  	case n.pkg != nil:
   229  		return n.pkg
   230  	case n.option != nil:
   231  		return n.option
   232  	case n.message != nil:
   233  		return n.message
   234  	case n.enum != nil:
   235  		return n.enum
   236  	case n.extend != nil:
   237  		return n.extend
   238  	case n.service != nil:
   239  		return n.service
   240  	default:
   241  		return n.empty
   242  	}
   243  }
   244  
   245  type syntaxNode struct {
   246  	basicCompositeNode
   247  	syntax *compoundStringNode
   248  }
   249  
   250  type importNode struct {
   251  	basicCompositeNode
   252  	name   *compoundStringNode
   253  	public bool
   254  	weak   bool
   255  }
   256  
   257  type packageNode struct {
   258  	basicCompositeNode
   259  	name *compoundIdentNode
   260  }
   261  
   262  type identifier string
   263  
   264  type identNode struct {
   265  	basicNode
   266  	val string
   267  }
   268  
   269  func (n *identNode) value() interface{} {
   270  	return identifier(n.val)
   271  }
   272  
   273  type compoundIdentNode struct {
   274  	basicCompositeNode
   275  	val string
   276  }
   277  
   278  func (n *compoundIdentNode) value() interface{} {
   279  	return identifier(n.val)
   280  }
   281  
   282  type compactOptionsNode struct {
   283  	basicCompositeNode
   284  	decls []*optionNode
   285  }
   286  
   287  func (n *compactOptionsNode) Elements() []*optionNode {
   288  	if n == nil {
   289  		return nil
   290  	}
   291  	return n.decls
   292  }
   293  
   294  type optionNode struct {
   295  	basicCompositeNode
   296  	name *optionNameNode
   297  	val  valueNode
   298  }
   299  
   300  func (n *optionNode) getName() node {
   301  	return n.name
   302  }
   303  
   304  func (n *optionNode) getValue() valueNode {
   305  	return n.val
   306  }
   307  
   308  type optionNameNode struct {
   309  	basicCompositeNode
   310  	parts []*optionNamePartNode
   311  }
   312  
   313  type optionNamePartNode struct {
   314  	basicCompositeNode
   315  	text        *compoundIdentNode
   316  	offset      int
   317  	length      int
   318  	isExtension bool
   319  	st, en      *SourcePos
   320  }
   321  
   322  func (n *optionNamePartNode) start() *SourcePos {
   323  	if n.isExtension {
   324  		return n.basicCompositeNode.start()
   325  	}
   326  	return n.st
   327  }
   328  
   329  func (n *optionNamePartNode) end() *SourcePos {
   330  	if n.isExtension {
   331  		return n.basicCompositeNode.end()
   332  	}
   333  	return n.en
   334  }
   335  
   336  func (n *optionNamePartNode) setRange(first, last node) {
   337  	n.basicCompositeNode.setRange(first, last)
   338  	if !n.isExtension {
   339  		st := *first.start()
   340  		st.Col += n.offset
   341  		n.st = &st
   342  		en := st
   343  		en.Col += n.length
   344  		n.en = &en
   345  	}
   346  }
   347  
   348  type valueNode interface {
   349  	node
   350  	value() interface{}
   351  }
   352  
   353  var _ valueNode = (*identNode)(nil)
   354  var _ valueNode = (*compoundIdentNode)(nil)
   355  var _ valueNode = (*stringLiteralNode)(nil)
   356  var _ valueNode = (*compoundStringNode)(nil)
   357  var _ valueNode = (*intLiteralNode)(nil)
   358  var _ valueNode = (*compoundIntNode)(nil)
   359  var _ valueNode = (*compoundUintNode)(nil)
   360  var _ valueNode = (*floatLiteralNode)(nil)
   361  var _ valueNode = (*compoundFloatNode)(nil)
   362  var _ valueNode = (*boolLiteralNode)(nil)
   363  var _ valueNode = (*sliceLiteralNode)(nil)
   364  var _ valueNode = (*aggregateLiteralNode)(nil)
   365  var _ valueNode = (*noSourceNode)(nil)
   366  
   367  type stringLiteralNode struct {
   368  	basicNode
   369  	val string
   370  }
   371  
   372  func (n *stringLiteralNode) value() interface{} {
   373  	return n.val
   374  }
   375  
   376  type compoundStringNode struct {
   377  	basicCompositeNode
   378  	val string
   379  }
   380  
   381  func (n *compoundStringNode) value() interface{} {
   382  	return n.val
   383  }
   384  
   385  type intLiteral interface {
   386  	asInt32(min, max int32) (int32, bool)
   387  	value() interface{}
   388  }
   389  
   390  type intLiteralNode struct {
   391  	basicNode
   392  	val uint64
   393  }
   394  
   395  var _ intLiteral = (*intLiteralNode)(nil)
   396  
   397  func (n *intLiteralNode) value() interface{} {
   398  	return n.val
   399  }
   400  
   401  func (n *intLiteralNode) asInt32(min, max int32) (int32, bool) {
   402  	if (min >= 0 && n.val < uint64(min)) || n.val > uint64(max) {
   403  		return 0, false
   404  	}
   405  	return int32(n.val), true
   406  }
   407  
   408  type compoundUintNode struct {
   409  	basicCompositeNode
   410  	val uint64
   411  }
   412  
   413  var _ intLiteral = (*compoundUintNode)(nil)
   414  
   415  func (n *compoundUintNode) value() interface{} {
   416  	return n.val
   417  }
   418  
   419  func (n *compoundUintNode) asInt32(min, max int32) (int32, bool) {
   420  	if (min >= 0 && n.val < uint64(min)) || n.val > uint64(max) {
   421  		return 0, false
   422  	}
   423  	return int32(n.val), true
   424  }
   425  
   426  type compoundIntNode struct {
   427  	basicCompositeNode
   428  	val int64
   429  }
   430  
   431  var _ intLiteral = (*compoundIntNode)(nil)
   432  
   433  func (n *compoundIntNode) value() interface{} {
   434  	return n.val
   435  }
   436  
   437  func (n *compoundIntNode) asInt32(min, max int32) (int32, bool) {
   438  	if n.val < int64(min) || n.val > int64(max) {
   439  		return 0, false
   440  	}
   441  	return int32(n.val), true
   442  }
   443  
   444  type floatLiteralNode struct {
   445  	basicNode
   446  	val float64
   447  }
   448  
   449  func (n *floatLiteralNode) value() interface{} {
   450  	return n.val
   451  }
   452  
   453  type compoundFloatNode struct {
   454  	basicCompositeNode
   455  	val float64
   456  }
   457  
   458  func (n *compoundFloatNode) value() interface{} {
   459  	return n.val
   460  }
   461  
   462  type boolLiteralNode struct {
   463  	*identNode
   464  	val bool
   465  }
   466  
   467  func (n *boolLiteralNode) value() interface{} {
   468  	return n.val
   469  }
   470  
   471  type sliceLiteralNode struct {
   472  	basicCompositeNode
   473  	elements []valueNode
   474  }
   475  
   476  func (n *sliceLiteralNode) value() interface{} {
   477  	return n.elements
   478  }
   479  
   480  type aggregateLiteralNode struct {
   481  	basicCompositeNode
   482  	elements []*aggregateEntryNode
   483  }
   484  
   485  func (n *aggregateLiteralNode) value() interface{} {
   486  	return n.elements
   487  }
   488  
   489  type aggregateEntryNode struct {
   490  	basicCompositeNode
   491  	name *aggregateNameNode
   492  	val  valueNode
   493  }
   494  
   495  type aggregateNameNode struct {
   496  	basicCompositeNode
   497  	name        *compoundIdentNode
   498  	isExtension bool
   499  }
   500  
   501  func (a *aggregateNameNode) value() string {
   502  	if a.isExtension {
   503  		return "[" + a.name.val + "]"
   504  	} else {
   505  		return a.name.val
   506  	}
   507  }
   508  
   509  type fieldNode struct {
   510  	basicCompositeNode
   511  	label   fieldLabel
   512  	fldType *compoundIdentNode
   513  	name    *identNode
   514  	tag     *intLiteralNode
   515  	options *compactOptionsNode
   516  
   517  	// This field is populated after parsing, to allow lookup of extendee source
   518  	// locations when field extendees cannot be linked. (Otherwise, this is just
   519  	// stored as a string in the field descriptors defined inside the extend
   520  	// block).
   521  	extendee *extendNode
   522  }
   523  
   524  func (n *fieldNode) fieldLabel() node {
   525  	// proto3 fields and fields inside one-ofs will not have a label and we need
   526  	// this check in order to return a nil node -- otherwise we'd return a
   527  	// non-nil node that has a nil pointer value in it :/
   528  	if n.label.identNode == nil {
   529  		return nil
   530  	}
   531  	return n.label.identNode
   532  }
   533  
   534  func (n *fieldNode) fieldName() node {
   535  	return n.name
   536  }
   537  
   538  func (n *fieldNode) fieldType() node {
   539  	return n.fldType
   540  }
   541  
   542  func (n *fieldNode) fieldTag() node {
   543  	return n.tag
   544  }
   545  
   546  func (n *fieldNode) fieldExtendee() node {
   547  	if n.extendee != nil {
   548  		return n.extendee.extendee
   549  	}
   550  	return nil
   551  }
   552  
   553  func (n *fieldNode) getGroupKeyword() node {
   554  	return nil
   555  }
   556  
   557  type fieldLabel struct {
   558  	*identNode
   559  	repeated bool
   560  	required bool
   561  }
   562  
   563  type groupNode struct {
   564  	basicCompositeNode
   565  	groupKeyword *identNode
   566  	label        fieldLabel
   567  	name         *identNode
   568  	tag          *intLiteralNode
   569  	decls        []*messageElement
   570  	options      *compactOptionsNode
   571  
   572  	// This field is populated after parsing, to allow lookup of extendee source
   573  	// locations when field extendees cannot be linked. (Otherwise, this is just
   574  	// stored as a string in the field descriptors defined inside the extend
   575  	// block).
   576  	extendee *extendNode
   577  }
   578  
   579  func (n *groupNode) fieldLabel() node {
   580  	if n.label.identNode == nil {
   581  		// return nil interface to indicate absence, not a typed nil
   582  		return nil
   583  	}
   584  	return n.label.identNode
   585  }
   586  
   587  func (n *groupNode) fieldName() node {
   588  	return n.name
   589  }
   590  
   591  func (n *groupNode) fieldType() node {
   592  	return n.groupKeyword
   593  }
   594  
   595  func (n *groupNode) fieldTag() node {
   596  	return n.tag
   597  }
   598  
   599  func (n *groupNode) fieldExtendee() node {
   600  	if n.extendee != nil {
   601  		return n.extendee.extendee
   602  	}
   603  	return nil
   604  }
   605  
   606  func (n *groupNode) getGroupKeyword() node {
   607  	return n.groupKeyword
   608  }
   609  
   610  func (n *groupNode) messageName() node {
   611  	return n.name
   612  }
   613  
   614  type oneOfNode struct {
   615  	basicCompositeNode
   616  	name  *identNode
   617  	decls []*oneOfElement
   618  }
   619  
   620  type oneOfElement struct {
   621  	// a discriminated union: only one field will be set
   622  	option *optionNode
   623  	field  *fieldNode
   624  	group  *groupNode
   625  	empty  *basicNode
   626  }
   627  
   628  func (n *oneOfElement) start() *SourcePos {
   629  	return n.get().start()
   630  }
   631  
   632  func (n *oneOfElement) end() *SourcePos {
   633  	return n.get().end()
   634  }
   635  
   636  func (n *oneOfElement) leadingComments() []comment {
   637  	return n.get().leadingComments()
   638  }
   639  
   640  func (n *oneOfElement) trailingComments() []comment {
   641  	return n.get().trailingComments()
   642  }
   643  
   644  func (n *oneOfElement) get() node {
   645  	switch {
   646  	case n.option != nil:
   647  		return n.option
   648  	case n.field != nil:
   649  		return n.field
   650  	default:
   651  		return n.empty
   652  	}
   653  }
   654  
   655  type mapTypeNode struct {
   656  	basicCompositeNode
   657  	mapKeyword *identNode
   658  	keyType    *identNode
   659  	valueType  *compoundIdentNode
   660  }
   661  
   662  type mapFieldNode struct {
   663  	basicCompositeNode
   664  	mapType *mapTypeNode
   665  	name    *identNode
   666  	tag     *intLiteralNode
   667  	options *compactOptionsNode
   668  }
   669  
   670  func (n *mapFieldNode) fieldLabel() node {
   671  	return nil
   672  }
   673  
   674  func (n *mapFieldNode) fieldName() node {
   675  	return n.name
   676  }
   677  
   678  func (n *mapFieldNode) fieldType() node {
   679  	return n.mapType
   680  }
   681  
   682  func (n *mapFieldNode) fieldTag() node {
   683  	return n.tag
   684  }
   685  
   686  func (n *mapFieldNode) fieldExtendee() node {
   687  	return nil
   688  }
   689  
   690  func (n *mapFieldNode) getGroupKeyword() node {
   691  	return nil
   692  }
   693  
   694  func (n *mapFieldNode) messageName() node {
   695  	return n.name
   696  }
   697  
   698  func (n *mapFieldNode) keyField() *syntheticMapField {
   699  	k := n.mapType.keyType
   700  	t := &compoundIdentNode{val: k.val}
   701  	t.setRange(k, k)
   702  	return newSyntheticMapField(t, 1)
   703  }
   704  
   705  func (n *mapFieldNode) valueField() *syntheticMapField {
   706  	return newSyntheticMapField(n.mapType.valueType, 2)
   707  }
   708  
   709  func newSyntheticMapField(ident *compoundIdentNode, tagNum uint64) *syntheticMapField {
   710  	tag := &intLiteralNode{
   711  		basicNode: basicNode{
   712  			posRange: posRange{start: *ident.start(), end: *ident.end()},
   713  		},
   714  		val: tagNum,
   715  	}
   716  	return &syntheticMapField{ident: ident, tag: tag}
   717  }
   718  
   719  type syntheticMapField struct {
   720  	ident *compoundIdentNode
   721  	tag   *intLiteralNode
   722  }
   723  
   724  func (n *syntheticMapField) start() *SourcePos {
   725  	return n.ident.start()
   726  }
   727  
   728  func (n *syntheticMapField) end() *SourcePos {
   729  	return n.ident.end()
   730  }
   731  
   732  func (n *syntheticMapField) leadingComments() []comment {
   733  	return nil
   734  }
   735  
   736  func (n *syntheticMapField) trailingComments() []comment {
   737  	return nil
   738  }
   739  
   740  func (n *syntheticMapField) fieldLabel() node {
   741  	return n.ident
   742  }
   743  
   744  func (n *syntheticMapField) fieldName() node {
   745  	return n.ident
   746  }
   747  
   748  func (n *syntheticMapField) fieldType() node {
   749  	return n.ident
   750  }
   751  
   752  func (n *syntheticMapField) fieldTag() node {
   753  	return n.tag
   754  }
   755  
   756  func (n *syntheticMapField) fieldExtendee() node {
   757  	return nil
   758  }
   759  
   760  func (n *syntheticMapField) getGroupKeyword() node {
   761  	return nil
   762  }
   763  
   764  type extensionRangeNode struct {
   765  	basicCompositeNode
   766  	ranges  []*rangeNode
   767  	options *compactOptionsNode
   768  }
   769  
   770  type rangeNode struct {
   771  	basicCompositeNode
   772  	startNode, endNode node
   773  	endMax             bool
   774  }
   775  
   776  func (n *rangeNode) rangeStart() node {
   777  	return n.startNode
   778  }
   779  
   780  func (n *rangeNode) rangeEnd() node {
   781  	if n.endNode == nil {
   782  		return n.startNode
   783  	}
   784  	return n.endNode
   785  }
   786  
   787  func (n *rangeNode) startValue() interface{} {
   788  	return n.startNode.(intLiteral).value()
   789  }
   790  
   791  func (n *rangeNode) startValueAsInt32(min, max int32) (int32, bool) {
   792  	return n.startNode.(intLiteral).asInt32(min, max)
   793  }
   794  
   795  func (n *rangeNode) endValue() interface{} {
   796  	l, ok := n.endNode.(intLiteral)
   797  	if !ok {
   798  		return nil
   799  	}
   800  	return l.value()
   801  }
   802  
   803  func (n *rangeNode) endValueAsInt32(min, max int32) (int32, bool) {
   804  	if n.endMax {
   805  		return max, true
   806  	}
   807  	if n.endNode == nil {
   808  		return n.startValueAsInt32(min, max)
   809  	}
   810  	return n.endNode.(intLiteral).asInt32(min, max)
   811  }
   812  
   813  type reservedNode struct {
   814  	basicCompositeNode
   815  	ranges []*rangeNode
   816  	names  []*compoundStringNode
   817  }
   818  
   819  type enumNode struct {
   820  	basicCompositeNode
   821  	name  *identNode
   822  	decls []*enumElement
   823  }
   824  
   825  type enumElement struct {
   826  	// a discriminated union: only one field will be set
   827  	option   *optionNode
   828  	value    *enumValueNode
   829  	reserved *reservedNode
   830  	empty    *basicNode
   831  }
   832  
   833  func (n *enumElement) start() *SourcePos {
   834  	return n.get().start()
   835  }
   836  
   837  func (n *enumElement) end() *SourcePos {
   838  	return n.get().end()
   839  }
   840  
   841  func (n *enumElement) leadingComments() []comment {
   842  	return n.get().leadingComments()
   843  }
   844  
   845  func (n *enumElement) trailingComments() []comment {
   846  	return n.get().trailingComments()
   847  }
   848  
   849  func (n *enumElement) get() node {
   850  	switch {
   851  	case n.option != nil:
   852  		return n.option
   853  	case n.value != nil:
   854  		return n.value
   855  	default:
   856  		return n.empty
   857  	}
   858  }
   859  
   860  type enumValueNode struct {
   861  	basicCompositeNode
   862  	name    *identNode
   863  	options *compactOptionsNode
   864  	number  *compoundIntNode
   865  }
   866  
   867  func (n *enumValueNode) getName() node {
   868  	return n.name
   869  }
   870  
   871  func (n *enumValueNode) getNumber() node {
   872  	return n.number
   873  }
   874  
   875  type messageNode struct {
   876  	basicCompositeNode
   877  	name  *identNode
   878  	decls []*messageElement
   879  }
   880  
   881  func (n *messageNode) messageName() node {
   882  	return n.name
   883  }
   884  
   885  type messageElement struct {
   886  	// a discriminated union: only one field will be set
   887  	option         *optionNode
   888  	field          *fieldNode
   889  	mapField       *mapFieldNode
   890  	oneOf          *oneOfNode
   891  	group          *groupNode
   892  	nested         *messageNode
   893  	enum           *enumNode
   894  	extend         *extendNode
   895  	extensionRange *extensionRangeNode
   896  	reserved       *reservedNode
   897  	empty          *basicNode
   898  }
   899  
   900  func (n *messageElement) start() *SourcePos {
   901  	return n.get().start()
   902  }
   903  
   904  func (n *messageElement) end() *SourcePos {
   905  	return n.get().end()
   906  }
   907  
   908  func (n *messageElement) leadingComments() []comment {
   909  	return n.get().leadingComments()
   910  }
   911  
   912  func (n *messageElement) trailingComments() []comment {
   913  	return n.get().trailingComments()
   914  }
   915  
   916  func (n *messageElement) get() node {
   917  	switch {
   918  	case n.option != nil:
   919  		return n.option
   920  	case n.field != nil:
   921  		return n.field
   922  	case n.mapField != nil:
   923  		return n.mapField
   924  	case n.oneOf != nil:
   925  		return n.oneOf
   926  	case n.group != nil:
   927  		return n.group
   928  	case n.nested != nil:
   929  		return n.nested
   930  	case n.enum != nil:
   931  		return n.enum
   932  	case n.extend != nil:
   933  		return n.extend
   934  	case n.extensionRange != nil:
   935  		return n.extensionRange
   936  	case n.reserved != nil:
   937  		return n.reserved
   938  	default:
   939  		return n.empty
   940  	}
   941  }
   942  
   943  type extendNode struct {
   944  	basicCompositeNode
   945  	extendee *compoundIdentNode
   946  	decls    []*extendElement
   947  }
   948  
   949  type extendElement struct {
   950  	// a discriminated union: only one field will be set
   951  	field *fieldNode
   952  	group *groupNode
   953  	empty *basicNode
   954  }
   955  
   956  func (n *extendElement) start() *SourcePos {
   957  	return n.get().start()
   958  }
   959  
   960  func (n *extendElement) end() *SourcePos {
   961  	return n.get().end()
   962  }
   963  
   964  func (n *extendElement) leadingComments() []comment {
   965  	return n.get().leadingComments()
   966  }
   967  
   968  func (n *extendElement) trailingComments() []comment {
   969  	return n.get().trailingComments()
   970  }
   971  
   972  func (n *extendElement) get() node {
   973  	switch {
   974  	case n.field != nil:
   975  		return n.field
   976  	case n.group != nil:
   977  		return n.group
   978  	default:
   979  		return n.empty
   980  	}
   981  }
   982  
   983  type serviceNode struct {
   984  	basicCompositeNode
   985  	name  *identNode
   986  	decls []*serviceElement
   987  }
   988  
   989  type serviceElement struct {
   990  	// a discriminated union: only one field will be set
   991  	option *optionNode
   992  	rpc    *methodNode
   993  	empty  *basicNode
   994  }
   995  
   996  func (n *serviceElement) start() *SourcePos {
   997  	return n.get().start()
   998  }
   999  
  1000  func (n *serviceElement) end() *SourcePos {
  1001  	return n.get().end()
  1002  }
  1003  
  1004  func (n *serviceElement) leadingComments() []comment {
  1005  	return n.get().leadingComments()
  1006  }
  1007  
  1008  func (n *serviceElement) trailingComments() []comment {
  1009  	return n.get().trailingComments()
  1010  }
  1011  
  1012  func (n *serviceElement) get() node {
  1013  	switch {
  1014  	case n.option != nil:
  1015  		return n.option
  1016  	case n.rpc != nil:
  1017  		return n.rpc
  1018  	default:
  1019  		return n.empty
  1020  	}
  1021  }
  1022  
  1023  type methodNode struct {
  1024  	basicCompositeNode
  1025  	name    *identNode
  1026  	input   *rpcTypeNode
  1027  	output  *rpcTypeNode
  1028  	options []*optionNode
  1029  }
  1030  
  1031  func (n *methodNode) getInputType() node {
  1032  	return n.input.msgType
  1033  }
  1034  
  1035  func (n *methodNode) getOutputType() node {
  1036  	return n.output.msgType
  1037  }
  1038  
  1039  type rpcTypeNode struct {
  1040  	basicCompositeNode
  1041  	msgType       *compoundIdentNode
  1042  	streamKeyword node
  1043  }
  1044  
  1045  type noSourceNode struct {
  1046  	pos *SourcePos
  1047  }
  1048  
  1049  func (n noSourceNode) start() *SourcePos {
  1050  	return n.pos
  1051  }
  1052  
  1053  func (n noSourceNode) end() *SourcePos {
  1054  	return n.pos
  1055  }
  1056  
  1057  func (n noSourceNode) leadingComments() []comment {
  1058  	return nil
  1059  }
  1060  
  1061  func (n noSourceNode) trailingComments() []comment {
  1062  	return nil
  1063  }
  1064  
  1065  func (n noSourceNode) getSyntax() node {
  1066  	return n
  1067  }
  1068  
  1069  func (n noSourceNode) getName() node {
  1070  	return n
  1071  }
  1072  
  1073  func (n noSourceNode) getValue() valueNode {
  1074  	return n
  1075  }
  1076  
  1077  func (n noSourceNode) fieldLabel() node {
  1078  	return n
  1079  }
  1080  
  1081  func (n noSourceNode) fieldName() node {
  1082  	return n
  1083  }
  1084  
  1085  func (n noSourceNode) fieldType() node {
  1086  	return n
  1087  }
  1088  
  1089  func (n noSourceNode) fieldTag() node {
  1090  	return n
  1091  }
  1092  
  1093  func (n noSourceNode) fieldExtendee() node {
  1094  	return n
  1095  }
  1096  
  1097  func (n noSourceNode) getGroupKeyword() node {
  1098  	return n
  1099  }
  1100  
  1101  func (n noSourceNode) rangeStart() node {
  1102  	return n
  1103  }
  1104  
  1105  func (n noSourceNode) rangeEnd() node {
  1106  	return n
  1107  }
  1108  
  1109  func (n noSourceNode) getNumber() node {
  1110  	return n
  1111  }
  1112  
  1113  func (n noSourceNode) messageName() node {
  1114  	return n
  1115  }
  1116  
  1117  func (n noSourceNode) getInputType() node {
  1118  	return n
  1119  }
  1120  
  1121  func (n noSourceNode) getOutputType() node {
  1122  	return n
  1123  }
  1124  
  1125  func (n noSourceNode) value() interface{} {
  1126  	return nil
  1127  }