summaryrefslogtreecommitdiff
path: root/tests/parser-tests.dx
blob: c1a014a4dbefe690a5a74e7e508867d54c665752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'For now, arithmetic is not sensitive to whitespace:

:p 1.0+1.0
> 2.

:p 1.0 +1.0
> 2.

:p 1.0+ 1.0
> 2.

:p 1.0 + 1.0
> 2.

:p 1.0-1.0
> 0.

:p 1.0 -1.0
> 0.

:p 1.0- 1.0
> 0.

:p 1.0 - 1.0
> 0.

'Applying a function to a negative literal thus requires parentheses.

f = \x. x + 10.

:p f -1.0   -- parses as (-) f (-1.0)
> Type error:
> Expected: ((x:Float32) -> Float32)
>   Actual: Float32
>
> :p f -1.0   -- parses as (-) f (-1.0)
>       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

:p f (-1.0)
> 9.

:p (
    1
    +
    2
   )
> 3

:p
  xs = [1,2,3]
  for i.
    if xs[i] > 1
      then 0
      else 1
> [1, 0, 0]

:p
  run_state 5 \ref.
    n = get ref
    for_ i:(Fin n).
      ref := get ref + 1
> ((), 10)

-- Check that a syntax error in a funDefLet doesn't try to reparse the
-- whole definition as something it's not.
def frob(x:Int, y:Int) -> + =

> Parse error:66:29:
>    |
> 66 | def frob(x:Int, y:Int) -> + =
>    |                             ^
> unexpected '='
> expecting name or symbol name

-- Check that indented blocks have to actually be indented.
for i.
i

> Parse error:77:1:
>    |
> 77 | i
>    | ^
> expecting end of line or space

def (foo + bar) : Int = 6

> Parse error:85:6:
>    |
> 85 | def (foo + bar) : Int = 6
>    |      ^
> unexpected 'f'
> expecting symbol name

'Data definitions allow but do not require type / kind annotations

data MyPair1(a, b) = MkPair1(a, b)

data MyPair2(a:Type, b:Type) = MkPair2(x:a, y:b)

'Data definitions allow interleaving arguments and class constraints
(regression test for Issue 1015)

data TableInType(n|Ix, a, table:(n=>a)) =
  MkTableInType              -- Doesn't store any data except in the type!

'Left arrow <- desugars to a continuation lambda
(feature test for Issue 1137)

:p
  x <- with_state 0
  x := 4
  get x
> 4

'Check that we get reasonably helpful error messages if we try to write param lists
without parens or with whitespace before the parens.


data MyMaybe a =
  MyNothing

> Parse error:119:14:
>     |
> 119 | data MyMaybe a =
>     |              ^
> unexpected 'a'
> expecting '=' or parameter list in parentheses (without preceding whitespace)
data MyMaybe (a) =
  MyNothing

> Parse error:128:14:
>     |
> 128 | data MyMaybe (a) =
>     |              ^
> unexpected '('
> expecting '=' or parameter list in parentheses (without preceding whitespace)
data MyMaybe(a) =
  MyNothing
  MyJust a

> Parse error:139:10:
>     |
> 139 |   MyJust a
>     |          ^^
> unexpected "a<newline>"
> expecting end of input, end of line, or optional parameter list
data MyMaybe(a) =
  MyNothing
  MyJust (a)

> Parse error:149:10:
>     |
> 149 |   MyJust (a)
>     |          ^^
> unexpected "(a"
> expecting end of input, end of line, or optional parameter list
interface MyClass a
  pass

> Parse error:157:19:
>     |
> 157 | interface MyClass a
>     |                   ^
> expecting parameter list in parentheses (without preceding whitespace)
instance MyClass Int
  pass

> Parse error:165:18:
>     |
> 165 | instance MyClass Int
>     |                  ^
> expecting '(' (without preceding whitespace)
instance MyClass (Int)
  pass

> Parse error:173:18:
>     |
> 173 | instance MyClass (Int)
>     |                  ^
> expecting '(' (without preceding whitespace)
def myFunction (x) = ()

> Parse error:181:16:
>     |
> 181 | def myFunction (x) = ()
>     |                ^
> expecting parameter list in parentheses (without preceding whitespace)

def myFunction x = ()

> Parse error:189:16:
>     |
> 189 | def myFunction x = ()
>     |                ^
> expecting parameter list in parentheses (without preceding whitespace)