github.com/k14s/starlark-go@v0.0.0-20200720175618-3a5c849cc368/syntax/testdata/errors.star (about)

     1  # Tests of parse errors.
     2  # This is a "chunked" file; each "---" line demarcates a new parser input.
     3  #
     4  # TODO(adonovan): lots more tests.
     5  
     6  x = 1 +
     7  2 ### "got newline, want primary expression"
     8  
     9  ---
    10  
    11  _ = *x ### `got '\*', want primary`
    12  
    13  ---
    14  
    15  def f(a, ):  # trailing comma is ok
    16    pass
    17  
    18  ---
    19  
    20  def f(*args, ):  ### `got '\)', want parameter`
    21    pass
    22  
    23  ---
    24  
    25  def f(**kwargs, ): ### `got '\)', want parameter`
    26    pass
    27  
    28  ---
    29  
    30  # Parameters are validated later.
    31  def f(**kwargs, *args, *, b=1, a, **kwargs, *args, *, b=1, a):
    32    pass
    33  
    34  ---
    35  
    36  def f(a, *-b, c): # ### `got '-', want ','`
    37    pass
    38  
    39  ---
    40  
    41  def f(**kwargs, *args, b=1, a, **kwargs, *args, b=1, a):
    42    pass
    43  
    44  ---
    45  
    46  def pass(): ### "not an identifier"
    47    pass
    48  
    49  ---
    50  
    51  def f : ### `got ':', want '\('`
    52  
    53  ---
    54  
    55  f(a, ) # trailing comma is ok
    56  
    57  ---
    58  
    59  f(*args, ) ### `got '\)', want argument`
    60  
    61  ---
    62  
    63  f(**kwargs, ) ### `got '\)', want argument`
    64  
    65  ---
    66  
    67  f(a=1, *, b=2) ### `got ',', want primary`
    68  
    69  ---
    70  
    71  _ = {x:y for y in z} # ok
    72  _ = {x for y in z}   ### `got for, want ':'`
    73  
    74  ---
    75  
    76  def f():
    77    pass
    78   pass ### `unindent does not match any outer indentation level`
    79  
    80  ---
    81  def f(): pass
    82  ---
    83  # Blank line after pass => outdent.
    84  def f():
    85  	pass
    86  
    87  ---
    88  # No blank line after pass; EOF acts like a newline.
    89  def f():
    90  	pass
    91  ---
    92  # This is a well known parsing ambiguity in Python.
    93  # Python 2.7 accepts it but Python3 and Starlark reject it.
    94  _ = [x for x in lambda: True, lambda: False if x()] ### "got lambda, want primary"
    95  
    96  _ = [x for x in (lambda: True, lambda: False) if x()] # ok in all dialects
    97  
    98  ---
    99  # Starlark, following Python 3, allows an unparenthesized
   100  # tuple after 'in' only in a for statement but not in a comprehension.
   101  # (Python 2.7 allows both.)
   102  for x in 1, 2, 3:
   103        print(x)
   104  
   105  _ = [x for x in 1, 2, 3] ### `got ',', want ']', for, or if`
   106  ---
   107  # Unparenthesized tuple is not allowed as operand of 'if' in comprehension.
   108  _ = [a for b in c if 1, 2] ### `got ',', want ']', for, or if`
   109  
   110  ---
   111  # Lambda is ok though.
   112  _ = [a for b in c if lambda: d] # ok
   113  
   114  # But the body of such a lambda may not be a conditional:
   115  _ = [a for b in c if (lambda: d if e else f)] # ok
   116  _ = [a for b in c if lambda: d if e else f]   ### "got else, want ']'"
   117  
   118  ---
   119  # A lambda is not allowed as the operand of a 'for' clause.
   120  _ = [a for b in lambda: c] ### `got lambda, want primary`
   121  
   122  ---
   123  # Comparison operations are not associative.
   124  
   125  _ = (0 == 1) == 2 # ok
   126  _ = 0 == (1 == 2) # ok
   127  _ = 0 == 1 == 2 ### "== does not associate with =="
   128  
   129  ---
   130  
   131  _ = (0 <= i) < n   # ok
   132  _ = 0 <= (i < n) # ok
   133  _ = 0 <= i < n ### "<= does not associate with <"
   134  
   135  ---
   136  
   137  _ = (a in b) not in c  # ok
   138  _ = a in (b not in c)  # ok
   139  _ = a in b not in c    ### "in does not associate with not in"
   140  
   141  ---
   142  # shift/reduce ambiguity is reduced
   143  _ = [x for x in a if b else c] ### `got else, want ']', for, or if`
   144  ---
   145  [a for b in c else d] ### `got else, want ']', for, or if`
   146  ---
   147  _ = a + b not c ### "got identifier, want in"
   148  ---
   149  f(1+2 = 3) ### "keyword argument must have form name=expr"
   150  ---
   151  print(1, 2, 3
   152  ### `got end of file, want '\)'`
   153  ---
   154  _ = a if b ### "conditional expression without else clause"
   155  ---
   156  load("") ### "load statement must import at least 1 symbol"
   157  ---
   158  load("", 1) ### `load operand must be "name" or localname="name" \(got int literal\)`
   159  ---
   160  load("a", "x") # ok
   161  ---
   162  load(1, 2) ### "first operand of load statement must be a string literal"
   163  ---
   164  load("a", x) ### `load operand must be "x" or x="originalname"`
   165  ---
   166  load("a", x2=x) ### `original name of loaded symbol must be quoted: x2="originalname"`
   167  ---
   168  # All of these parse.
   169  load("a", "x")
   170  load("a", "x", y2="y")
   171  load("a", x2="x", "y") # => positional-before-named arg check happens later (!)
   172  ---
   173  # 'load' is not an identifier
   174  load = 1 ### `got '=', want '\('`
   175  ---
   176  # 'load' is not an identifier
   177  f(load()) ### `got load, want primary`
   178  ---
   179  # 'load' is not an identifier
   180  def load(): ### `not an identifier`
   181    pass
   182  ---
   183  # 'load' is not an identifier
   184  def f(load): ### `not an identifier`
   185    pass
   186  ---
   187  # A load statement allows a trailing comma.
   188  load("module", "x",)
   189  ---
   190  x = 1 +
   191  2 ### "got newline, want primary expression"
   192  ---
   193  def f():
   194      pass
   195  # this used to cause a spurious indentation error
   196  ---
   197  print 1 2 ### `got int literal, want newline`
   198  
   199  ---
   200  # newlines are not allowed in raw string literals
   201  raw = r'a ### `unexpected newline in string`
   202  b'
   203  
   204  ---
   205  # The parser permits an unparenthesized tuple expression for the first index.
   206  x[1, 2:] # ok
   207  ---
   208  # But not if it has a trailing comma.
   209  x[1, 2,:] ### `got ':', want primary`
   210  ---
   211  # Trailing tuple commas are permitted only within parens; see b/28867036.
   212  (a, b,) = 1, 2 # ok
   213  c, d = 1, 2 # ok
   214  ---
   215  a, b, = 1, 2 ### `unparenthesized tuple with trailing comma`
   216  ---
   217  a, b = 1, 2, ### `unparenthesized tuple with trailing comma`
   218  
   219  ---
   220  # See github.com/google/starlark-go/issues/48
   221  a = max(range(10))) ### `unexpected '\)'`
   222  
   223  ---
   224  # github.com/google/starlark-go/issues/85
   225  s = "\x-0" ### `invalid escape sequence`