modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/parser/parser.y (about)

     1  // This source file is a modification of [0].
     2  //
     3  // [0]: https://github.com/golang/go/blob/release-branch.go1.5/src/cmd/compile/internal/gc/go.y
     4  
     5  // Copyright 2009 The Go Authors. All rights reserved.
     6  // Use of this source code is governed by a BSD-style
     7  // license that can be found in the GO-LICENSE file.
     8  
     9  %token
    10  	ILLEGAL
    11  
    12  	IDENT
    13  	INT
    14  	FLOAT
    15  	IMAG
    16  	CHAR
    17  	STRING
    18  
    19  	ADD_ASSIGN	"+="
    20  	AND_ASSIGN	"&="
    21  	AND_NOT		"&^"
    22  	AND_NOT_ASSIGN	"&^="
    23  	ARROW		"<-"
    24  	DEC		"--"
    25  	DEFINE		":="
    26  	ELLIPSIS	"..."
    27  	EQL		"=="
    28  	GEQ		">="
    29  	GTGT		"»"
    30  	INC		"++"
    31  	LAND		"&&"
    32  	LEQ		"<="
    33  	LOR		"||"
    34  	LTLT		"«"
    35  	MUL_ASSIGN	"*="
    36  	NEQ		"!="
    37  	OR_ASSIGN	"|="
    38  	QUO_ASSIGN	"/="
    39  	REM_ASSIGN	"%="
    40  	SHL		"<<"
    41  	SHL_ASSIGN	"<<="
    42  	SHR		">>"
    43  	SHR_ASSIGN	">>="
    44  	SUB_ASSIGN	"-="
    45  	XOR_ASSIGN	"^="
    46  	 
    47  	BREAK		"break"
    48  	CASE		"case"
    49  	CHAN		"chan"
    50  	CONST		"const"
    51  	CONTINUE	"continue"
    52  	DEFAULT		"default"
    53  	DEFER		"defer"
    54  	ELSE		"else"
    55  	FALLTHROUGH	"fallthrough"
    56  	FOR		"for"
    57  	FUNC		"func"
    58  	GO		"go"
    59  	GOTO		"goto"
    60  	IF		"if"
    61  	IMPORT		"import"
    62  	INTERFACE	"interface"
    63  	MAP		"map"
    64  	PACKAGE		"package"
    65  	RANGE		"range"
    66  	RETURN		"return"
    67  	SELECT		"select"
    68  	STRUCT		"struct"
    69  	SWITCH		"switch"
    70  	TYPE		"type"
    71  	VAR		"var"
    72  
    73  	BODY 		"{"
    74  
    75  %left	ARROW
    76  
    77  %left	LOR
    78  %left	LAND
    79  %left	EQL NEQ LEQ GEQ '<' '>'
    80  %left	'+' '-' '|' '^'
    81  %left	'*' '/' '%' '&' SHL SHR AND_NOT
    82  
    83  %left	_NotPackage
    84  %left	PACKAGE
    85  
    86  %left	_NotParen
    87  %left	'('
    88  
    89  %left	')'
    90  %left	_PreferToRightParen
    91  
    92  %%
    93  
    94  file:
    95  	"package" IDENT ';' imports topLevelDeclList
    96  
    97  imports:
    98  |	imports "import" '(' ')' ';'
    99  |	imports "import" '(' importSpecList semiOpt ')' ';'
   100  |	imports "import" importSpec ';'
   101  
   102  importSpecList:
   103  	importSpec
   104  |	importSpecList ';' importSpec
   105  
   106  importSpec:
   107  	'.' STRING
   108  |	IDENT STRING
   109  |	STRING
   110  
   111  topLevelDeclList:
   112  |	topLevelDeclList "func" '(' paramTypeListCommaOptOpt ')' IDENT genericParamsOpt '(' paramTypeListCommaOptOpt ')' result fnBody ';'
   113  |	topLevelDeclList "func" IDENT genericParamsOpt '(' paramTypeListCommaOptOpt ')' result fnBody ';'
   114  |	topLevelDeclList commonDecl ';'
   115  
   116  commonDecl:
   117  	"const" '(' ')'
   118  |	"const" '(' constSpec ';' constSpecList semiOpt ')'
   119  |	"const" '(' constSpec semiOpt ')'
   120  |	"const" constSpec
   121  |	"type" '(' ')'
   122  |	"type" '(' typeSpecList semiOpt ')'
   123  |	"type" typeSpec
   124  |	"var" '(' ')'
   125  |	"var" '(' varSpecList semiOpt ')'
   126  |	"var" varSpec
   127  
   128  varSpec:
   129  	identList '=' exprList
   130  |	identList typ
   131  |	identList typ '=' exprList
   132  
   133  constSpec:
   134  	identList
   135  |	identList '=' exprList
   136  |	identList typ
   137  |	identList typ '=' exprList
   138  
   139  genericArgsOpt:
   140  |	"«" typeList commaOpt "»"
   141  
   142  genericParamsOpt:
   143  |	"«" identList commaOpt "»"
   144  
   145  typeSpec:
   146  	IDENT genericParamsOpt typ
   147  |	IDENT '=' typ
   148  
   149  simpleStmt:
   150  	expr
   151  |	expr "%=" expr
   152  |	expr "&=" expr
   153  |	expr "&^=" expr
   154  |	expr "*=" expr
   155  |	expr "++"
   156  |	expr "+=" expr
   157  |	expr "--"
   158  |	expr "-=" expr
   159  |	expr "/=" expr
   160  |	expr "<<=" expr
   161  |	expr ">>=" expr
   162  |	expr "^=" expr
   163  |	expr "|=" expr
   164  |	exprList ":=" exprList
   165  |	exprList '=' exprList
   166  
   167  compoundStmt:
   168  	'{' stmtList '}'
   169  
   170  caseBlockList:
   171  |	caseBlockList "case" exprOrTypeList ":=" expr ':' stmtList
   172  |	caseBlockList "case" exprOrTypeList ':' stmtList
   173  |	caseBlockList "case" exprOrTypeList '=' expr ':' stmtList
   174  |	caseBlockList "default" ':' stmtList
   175  
   176  loopBody:
   177  	BODY stmtList '}'
   178  
   179  ifHeader:
   180  	simpleStmtOpt
   181  |	simpleStmtOpt ';' simpleStmtOpt
   182  
   183  elseIfList:
   184  |	elseIfList "else" "if" ifHeader loopBody
   185  
   186  expr:
   187  	expr "!=" expr
   188  |	expr "&&" expr
   189  |	expr "&^" expr
   190  |	expr "<-" expr
   191  |	expr "<<" expr
   192  |	expr "<=" expr
   193  |	expr "==" expr
   194  |	expr ">=" expr
   195  |	expr ">>" expr
   196  |	expr "||" expr
   197  |	expr '%' expr
   198  |	expr '&' expr
   199  |	expr '*' expr
   200  |	expr '+' expr
   201  |	expr '-' expr
   202  |	expr '/' expr
   203  |	expr '<' expr
   204  |	expr '>' expr
   205  |	expr '^' expr
   206  |	expr '|' expr
   207  |	unaryExpr
   208  
   209  unaryExpr:
   210  	"<-" unaryExpr
   211  |	'!' unaryExpr
   212  |	'&' unaryExpr
   213  |	'*' unaryExpr
   214  |	'+' unaryExpr
   215  |	'-' unaryExpr
   216  |	'^' unaryExpr
   217  |	primaryExpr
   218  
   219  keyVal:
   220  	compLitExpr
   221  |	compLitExpr ':' compLitExpr
   222  
   223  compLitExpr:
   224  	'{' bracedKeyValList '}'
   225  |	expr
   226  
   227  primaryExpr:
   228  	'(' exprOrType ')'
   229  |	IDENT genericArgsOpt %prec _NotParen
   230  |	convType '(' expr commaOpt ')'
   231  |	fnType lbrace stmtList '}'
   232  |	literal
   233  |	otherType lbrace bracedKeyValList '}'
   234  |	primaryExpr '(' ')'
   235  |	primaryExpr '(' exprOrTypeList "..." commaOpt ')'
   236  |	primaryExpr '(' exprOrTypeList commaOpt ')'
   237  |	primaryExpr '.' '(' "type" ')'
   238  |	primaryExpr '.' '(' typ ')'
   239  |	primaryExpr '.' IDENT genericArgsOpt
   240  |	primaryExpr '[' expr ']'
   241  |	primaryExpr '[' exprOpt ':' exprOpt ':' exprOpt ']'
   242  |	primaryExpr '[' exprOpt ':' exprOpt ']'
   243  |	primaryExpr '{' bracedKeyValList '}'
   244  
   245  exprOrType:
   246  	expr
   247  |	nonExprType %prec _PreferToRightParen
   248  
   249  lbrace:
   250  	BODY
   251  |	'{'
   252  
   253  identOpt:
   254  |	IDENT
   255  
   256  dddType:
   257  	"..." typ
   258  
   259  typ:
   260  	'(' typ ')'
   261  |	qualifiedIdent genericArgsOpt
   262  |	fnType
   263  |	otherType
   264  |	ptrType
   265  |	rxChanType
   266  
   267  typeList:
   268  	typ
   269  |	typeList ',' typ
   270  
   271  nonExprType:
   272  	'*' nonExprType
   273  |	fnType
   274  |	otherType
   275  |	rxChanType
   276  
   277  convType:
   278  	fnType
   279  |	otherType
   280  
   281  qualifiedIdent:
   282  	IDENT %prec _NotParen
   283  |	IDENT '.' IDENT
   284  
   285  otherType:
   286  	"chan" "<-" typ
   287  |	"chan" '(' typ ')'
   288  |	"chan" qualifiedIdent
   289  |	"chan" fnType
   290  |	"chan" otherType
   291  |	"chan" ptrType
   292  |	"interface" lbrace '}'
   293  |	"interface" lbrace interfaceDeclList semiOpt '}'
   294  |	"map" '[' typ ']' typ
   295  |	"struct" lbrace '}'
   296  |	"struct" lbrace fieldDeclList semiOpt '}'
   297  |	'[' "..." ']' typ
   298  |	'[' exprOpt ']' typ
   299  
   300  ptrType:
   301  	'*' typ
   302  
   303  rxChanType:
   304  	"<-" "chan" typ
   305  
   306  fnType:
   307  	"func" '(' paramTypeListCommaOptOpt ')' result
   308  
   309  fnBody:
   310  |	'{' stmtList '}'
   311  
   312  result:
   313  	%prec _NotParen
   314  |	'(' paramTypeListCommaOptOpt ')'
   315  |	qualifiedIdent genericArgsOpt
   316  |	fnType
   317  |	otherType
   318  |	ptrType
   319  |	rxChanType
   320  
   321  varSpecList:
   322  	varSpec
   323  |	varSpecList ';' varSpec
   324  
   325  constSpecList:
   326  	constSpec
   327  |	constSpecList ';' constSpec
   328  
   329  typeSpecList:
   330  	typeSpec
   331  |	typeSpecList ';' typeSpec
   332  
   333  fieldDeclList:
   334  	fieldDecl
   335  |	fieldDeclList ';' fieldDecl
   336  
   337  interfaceDeclList:
   338  	interfaceDecl
   339  |	interfaceDeclList ';' interfaceDecl
   340  
   341  fieldDecl:
   342  	'*' embeddedName literalOpt
   343  |	identList typ literalOpt
   344  |	embeddedName literalOpt
   345  
   346  embeddedName:
   347  	IDENT
   348  |	IDENT '.' IDENT
   349  
   350  interfaceDecl:
   351  	IDENT '(' paramTypeListCommaOptOpt ')' result
   352  |	embeddedName
   353  
   354  paramType:
   355  	IDENT dddType
   356  |	IDENT typ
   357  |	dddType
   358  |	typ
   359  
   360  paramTypeList:
   361  	paramType
   362  |	paramTypeList ',' paramType
   363  
   364  paramTypeListCommaOptOpt:
   365  |	paramTypeList commaOpt
   366  
   367  stmt:
   368  |	"break" identOpt
   369  |	"continue" identOpt
   370  |	"defer" primaryExpr
   371  |	"fallthrough"
   372  |	"for" "range" expr loopBody
   373  |	"for" exprList ":=" "range" expr loopBody
   374  |	"for" exprList '=' "range" expr loopBody
   375  |	"for" simpleStmtOpt ';' simpleStmtOpt ';' simpleStmtOpt loopBody
   376  |	"for" simpleStmtOpt loopBody
   377  |	"go" primaryExpr
   378  |	"goto" IDENT
   379  |	"if" ifHeader loopBody elseIfList
   380  |	"if" ifHeader loopBody elseIfList "else" compoundStmt
   381  |	"return"
   382  |	"return" exprList
   383  |	"select" BODY caseBlockList '}'
   384  |	"switch" ifHeader BODY caseBlockList '}'
   385  |	IDENT ':' stmt
   386  |	commonDecl
   387  |	compoundStmt
   388  |	simpleStmt
   389  
   390  stmtList:
   391  	stmt
   392  |	stmtList ';' stmt
   393  
   394  identList:
   395  	IDENT
   396  |	identList ',' IDENT
   397  
   398  exprList:
   399  	expr
   400  |	exprList ',' expr
   401  
   402  exprOrTypeList:
   403  	exprOrType
   404  |	exprOrTypeList ',' exprOrType
   405  
   406  keyValList:
   407  	keyVal
   408  |	keyValList ',' keyVal
   409  
   410  bracedKeyValList:
   411  |	keyValList commaOpt
   412  
   413  semiOpt:
   414  |	';'
   415  
   416  commaOpt:
   417  |	','
   418  
   419  exprOpt:
   420  |	expr
   421  
   422  simpleStmtOpt:
   423  |	simpleStmt
   424  
   425  literalOpt:
   426  |	literal
   427  
   428  literal:
   429  	CHAR
   430  |	FLOAT
   431  |	IMAG
   432  |	INT
   433  |	STRING