github.com/google/skylark@v0.0.0-20181101142754-a5f7082aabed/syntax/testdata/errors.sky (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 def pass(): ### "not an identifier" 31 pass 32 33 --- 34 35 def f : ### `got ':', want '\('` 36 37 --- 38 39 f(a, ) # trailing comma is ok 40 41 --- 42 43 f(*args, ) ### `got '\)', want argument` 44 45 --- 46 47 f(**kwargs, ) ### `got '\)', want argument` 48 49 --- 50 51 _ = {x:y for y in z} # ok 52 _ = {x for y in z} ### `got for, want ':'` 53 54 --- 55 56 def f(): 57 pass 58 pass ### `unindent does not match any outer indentation level` 59 60 --- 61 def f(): pass 62 --- 63 # Blank line after pass => outdent. 64 def f(): 65 pass 66 67 --- 68 # No blank line after pass; EOF acts like a newline. 69 def f(): 70 pass 71 --- 72 # This is a well known parsing ambiguity in Python. 73 # Python 2.7 accepts it but Python3 and Skylark reject it. 74 _ = [x for x in lambda: True, lambda: False if x()] ### "got lambda, want primary" 75 76 _ = [x for x in (lambda: True, lambda: False) if x()] # ok in all dialects 77 78 --- 79 # Skylark, following Python 3, allows an unparenthesized 80 # tuple after 'in' only in a for statement but not in a comprehension. 81 # (Python 2.7 allows both.) 82 for x in 1, 2, 3: 83 print(x) 84 85 _ = [x for x in 1, 2, 3] ### `got ',', want ']', for, or if` 86 --- 87 # Unparenthesized tuple is not allowed as operand of 'if' in comprehension. 88 _ = [a for b in c if 1, 2] ### `got ',', want ']', for, or if` 89 90 --- 91 # Lambda is ok though. 92 _ = [a for b in c if lambda: d] # ok 93 94 # But the body of such a lambda may not be a conditional: 95 _ = [a for b in c if (lambda: d if e else f)] # ok 96 _ = [a for b in c if lambda: d if e else f] ### "got else, want ']'" 97 98 --- 99 # A lambda is not allowed as the operand of a 'for' clause. 100 _ = [a for b in lambda: c] ### `got lambda, want primary` 101 102 --- 103 # Comparison operations are not associative. 104 105 _ = (0 == 1) == 2 # ok 106 _ = 0 == (1 == 2) # ok 107 _ = 0 == 1 == 2 ### "== does not associate with ==" 108 109 --- 110 111 _ = (0 <= i) < n # ok 112 _ = 0 <= (i < n) # ok 113 _ = 0 <= i < n ### "<= does not associate with <" 114 115 --- 116 117 _ = (a in b) not in c # ok 118 _ = a in (b not in c) # ok 119 _ = a in b not in c ### "in does not associate with not in" 120 121 --- 122 # shift/reduce ambiguity is reduced 123 _ = [x for x in a if b else c] ### `got else, want ']', for, or if` 124 --- 125 [a for b in c else d] ### `got else, want ']', for, or if` 126 --- 127 _ = a + b not c ### "got identifier, want in" 128 --- 129 f(1+2 = 3) ### "keyword argument must have form name=expr" 130 --- 131 print(1, 2, 3 ### `got end of file, want '\)'` 132 --- 133 _ = a if b ### "conditional expression without else clause" 134 --- 135 load("") ### "load statement must import at least 1 symbol" 136 --- 137 load("", 1) ### `load operand must be "name" or localname="name" \(got int literal\)` 138 --- 139 load("a", "x") # ok 140 --- 141 load(1, 2) ### "first operand of load statement must be a string literal" 142 --- 143 load("a", x) ### `load operand must be "x" or x="originalname"` 144 --- 145 load("a", x2=x) ### `original name of loaded symbol must be quoted: x2="originalname"` 146 --- 147 # All of these parse. 148 load("a", "x") 149 load("a", "x", y2="y") 150 load("a", x2="x", "y") # => positional-before-named arg check happens later (!) 151 --- 152 # 'load' is not an identifier 153 load = 1 ### `got '=', want '\('` 154 --- 155 # 'load' is not an identifier 156 f(load()) ### `got load, want primary` 157 --- 158 # 'load' is not an identifier 159 def load(): ### `not an identifier` 160 pass 161 --- 162 # 'load' is not an identifier 163 def f(load): ### `not an identifier` 164 pass 165 --- 166 # A load statement allows a trailing comma. 167 load("module", "x",) 168 --- 169 x = 1 + 170 2 ### "got newline, want primary expression" 171 --- 172 def f(): 173 pass 174 # this used to cause a spurious indentation error 175 --- 176 print 1 2 ### `got int literal, want newline` 177 178 --- 179 # newlines are not allowed in raw string literals 180 raw = r'a ### `unexpected newline in string` 181 b' 182 183 --- 184 # The parser permits an unparenthesized tuple expression for the first index. 185 x[1, 2:] # ok 186 --- 187 # But not if it has a trailing comma. 188 x[1, 2,:] ### `got ':', want primary` 189 --- 190 # Trailing tuple commas are permitted only within parens; see b/28867036. 191 (a, b,) = 1, 2 # ok 192 c, d = 1, 2 # ok 193 --- 194 a, b, = 1, 2 ### `unparenthesized tuple with trailing comma` 195 --- 196 a, b = 1, 2, ### `unparenthesized tuple with trailing comma`