-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
411 lines (407 loc) · 28.6 KB
/
index.html
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Parser</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/github.min.css" media="screen" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/github-dark.min.css" media="screen and (prefers-color-scheme: dark)" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-n8MVd4RsNIU0tAv4ct0nTaAbDJwPJzDEaqSD1odI+WdtXRGWt2kTvGFasHpSy3SV" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.5.1/github-markdown.min.css">
<style>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
.snippet pre {
margin-bottom: .1em;
}
.snippet-path {
font-size: 0.8em;
font-style: italic;
text-align: right;
}
</style>
</head>
<body class="markdown-body">
<div id="container">
<main>
<p class="updated">Last updated on <time datetime=2024-03-11T13:48:40>Monday, March 11, 2024</time></p>
<h1><a href="#lab-02---parser" id="lab-02---parser">Lab 02 - Parser</a></h1>
<p>In this second lab, you will implement the parser for the Alpine compiler.</p>
<h2><a href="#obtaining-the-lab-files" id="obtaining-the-lab-files">Obtaining the lab files</a></h2>
<p>To get the lab files, you have 2 options:</p>
<ul>
<li>
<p>pull this repository if you already cloned it last week. Otherwise, you can clone it by running the following command:</p>
<pre><code class="language-console">$ git pull
</code></pre>
<p>or</p>
<pre><code class="language-console">$ git clone https://github.com/epfl-lara/compiler2024-labs-public.git
</code></pre>
</li>
<li>
<p>Download the zip file on Moodle</p>
</li>
</ul>
<p>Then take your current <code>alpine</code> project, i.e., where you implemented the interpreter, and:</p>
<ul>
<li>copy the <code>parsing/</code> directory from this week (either from zip or repo) into your <code>alpine</code> project at this place: <code>src/main/scala/alpine/parsing</code></li>
<li>copy the <code>util/</code> directory from this week (either from zip or repo) into your <code>alpine</code> project at this place: <code>src/main/scala/alpine/util</code></li>
<li>copy the <code>Main.scala</code> file from this week (either from zip or repo) into your <code>alpine</code> project at this place: <code>src/main/scala/Main.scala</code></li>
<li>copy the <code>driver/</code> directory from this week (either from zip or repo) into your <code>alpine</code> project at this place: <code>src/main/scala/alpine/driver</code></li>
<li>copy the new test files by copying the <code>test/parsing</code> directory from this week (either from zip or repo) into your <code>alpine</code> project at this place: <code>src/test/scala/alpine/parsing</code></li>
<li>copy the <code>test/util/</code> directory from this week (either from zip or repo) into your <code>alpine</code> project at this place: <code>src/test/scala/alpine/util</code></li>
<li>remove the <code>lib/</code> directory from your <code>alpine</code> project</li>
<li>move the interpreter tests from <code>src/test/scala/alpine/evaluation</code> to <code>archive/test/evaluation</code>. This is because these tests rely on the typechecking phase of the compiler, that we will implement later in the semester. So we keep them here to add them back later.</li>
</ul>
<p>Your project directory structure should look like something like this:</p>
<pre><code class="language-console">alpine/
├── archive/
│ ├── test/
│ │ ├── evaluation/ <----- MOVE THE INTERPRETER TESTS HERE
│ │ │ ├── InterpreterTest.scala
├── lib/ <----- DELETE THIS DIRECTORY
├── src/
│ ├── main/
│ │ ├── scala/
│ │ │ ├── alpine/
│ │ │ │ ├── driver/ <----- COPY FROM THIS WEEK FILES
│ │ │ │ ├── evaluation/
│ │ │ │ │ ├── Interpreter.scala
│ │ │ │ ├── parsing/ <----- COPY FROM THIS WEEK FILES
│ │ │ │ │ ├── Parser.scala
│ │ │ │ │ ├── ...
│ │ │ │ ├── util/ <----- COPY FROM THIS WEEK FILES
│ │ │ │ │ ├── ...
│ │ │ ├── Main.scala <----- COPY FROM THIS WEEK FILES (replace the current one)
├── test/
│ ├── scala/
│ │ ├── alpine/
│ │ │ ├── evaluation/ <----- MOVED TO ARCHIVE
│ │ │ ├── parsing/ <----- COPY FROM THIS WEEK FILES
│ │ │ │ ├── ...
│ │ │ ├── util/ <----- COPY FROM THIS WEEK FILES
</code></pre>
<h2><a href="#submit-your-work" id="submit-your-work">Submit your work</a></h2>
<p>To submit your work, go to this week assignment on Moodle and upload the following file:</p>
<ul>
<li><code>src/main/scala/alpine/parser/Parser.scala</code></li>
</ul>
<h2><a href="#general-idea-of-the-project" id="general-idea-of-the-project">General idea of the project</a></h2>
<p>Let’s recall the global idea of a simple compiler’s pipeline:</p>
<pre><code>Source code -> Lexer -> Parser -> Type checking -> Assembly generation
</code></pre>
<p>The lexer generates a sequence of tokens from the source code.
The parser generates an AST from the sequence of tokens.</p>
<p>For example, consider the following program:</p>
<pre><code class="language-swift">let main = exit(1)
</code></pre>
<p>The lexer generates the following sequence of tokens:</p>
<pre><code class="language-scala">List(
Let(0: 3), // let
Identifier(4: 8), // main
Eq(9: 10), // =
Identifier(11: 15), // exit
LParen(15: 16), // (
Integer(16: 17), // 1
RParen(17: 18) // )
)
</code></pre>
<p><code>Let</code>, <code>Identifier</code>, <code>Eq</code>, <code>LParen</code>, <code>Integer</code>, <code>RParen</code> are tokens.
The number in parentheses denote the positions in the source text from which the token has been parsed, as 0-based indices in an array of code points.</p>
<p>Given this token stream, the parser generates the following AST:</p>
<pre><code class="language-scala">List(
Binding(
main, // identifier
None, // type
Some( // initializer
Application( // function call
Identifier(exit, hello.al: 1: 12 - 1: 16),
List( // arguments
Labeled(
None,
IntegerLiteral(1, hello.al: 1: 17 - 1: 18),
hello.al: 1: 17 - 1: 18
)
),
hello.al: 1: 12 - 1: 19
)
),
hello.al: 1: 1 - 1: 19
)
)
</code></pre>
<p>The AST is more expressive than the sequence of tokens as it represents the structure of the source code.</p>
<h2><a href="#general-structure-of-the-parser-and-the-codebase" id="general-structure-of-the-parser-and-the-codebase">General structure of the parser and the codebase</a></h2>
<p>Parsing is decomposed into multiple functions, each of them responsible for parsing a specific part of the grammar.
For example, the method <code>binding</code> is responsible for parsing binding trees, like the one shown in the previous section.
All of these parsing functions use the following core API:</p>
<ul>
<li><code>peek</code>: looks at the next token without consuming it.
<ul>
<li>it returns either <code>Some(token)</code> or <code>None</code> if the stream is empty (i.e. we reach an EOF).</li>
</ul>
</li>
<li><code>take()</code>: consumes the next token
<ul>
<li>it returns either <code>Some(token)</code> or <code>None</code> if the stream is empty (i.e. we reach an EOF), and consumes a token from the stream.</li>
</ul>
</li>
<li><code>takeIf(pred: Token => Boolean)</code>: takes a predicate, and consumes and returns the next token <strong>if it satisfies the predicate</strong></li>
<li><code>take(k: Token.Kind)</code>: shorthand for <code>takeIf(_.kind == k)</code></li>
<li><code>expect(k: Token.Kind)</code>: shorthand for <code>take(k)).getOrElse(throw FatalError(…))</code>
<ul>
<li>i.e. it takes the next token and throws an error if it is not the expected kind of token.</li>
</ul>
</li>
<li><code>expect(construct: String, pred: Token => Boolean)</code>: same as <code>expect</code> but takes a construct to include in the error message.</li>
<li><code>report</code>: reports an error while parsing.</li>
<li><code>snapshot</code>: returns the current state of the parser</li>
<li><code>restore</code> : restores the state of the parser from a backup returned by <code>snapshot</code></li>
</ul>
<p>Observer how these methods are used in the parts of the code that have been provided to get a sense of how they can be used.
In particular, pay attention to the way <code>peek</code> and <code>take</code> (and its variants) are used.</p>
<h3><a href="#new-elements-of-the-language" id="new-elements-of-the-language">New elements of the language</a></h3>
<p>Throughout the lab, we will see new elements of the language.</p>
<h4><a href="#types" id="types">Types</a></h4>
<p>In <em>Alpine</em>, you can create type declarations:</p>
<pre><code class="language-swift">type Vector2 =
#vector2(x: Float, y: Float)
type Circle =
#circle(origin: Vector2, radius: Float)
type Rectangle =
#rectangle(origin: Vector2, dimension: Vector2)
</code></pre>
<p>There is also closed union types (also known as a sum type):</p>
<pre><code class="language-swift">type OptionInt = #none | #some(Int)
</code></pre>
<p>In this case, a value of type <code>OptionInt</code> can be either a <code>#none</code> or a <code>#some(Int)</code>.</p>
<p>You can also define recursive types:</p>
<pre><code class="language-swift">type List = #empty | #list(head: Any, tail: List)
</code></pre>
<p>For reference, the grammar is provided inside the <a href="./grammar.md"><code>grammar.md</code></a>/<a href="./grammar.html"><code>grammar.html</code></a> file.</p>
<h2><a href="#implementation" id="implementation">Implementation</a></h2>
<h3><a href="#some-hints" id="some-hints">Some hints</a></h3>
<p>The parser is written in composing functions, that each parses a part of the grammar.</p>
<p>For example, let’s have a look at the <code>primaryExpression()</code> function. This function <code>peek</code> at the next token, and depending on its nature, calls the appropriate function to parse the corresponding producing rule of the grammar.</p>
<div class='snippet'>
<pre><code class="language-scala">private[parsing] def primaryExpression(): Expression =
peek match
case Some(Token(K.Identifier, s)) =>
identifier()
case Some(Token(K.True, _)) =>
booleanLiteral()
case Some(Token(K.False, _)) =>
booleanLiteral()
case Some(Token(K.Integer, _)) =>
integerLiteral()
case Some(Token(K.Float, _)) =>
floatLiteral()
case Some(Token(K.String, _)) =>
stringLiteral()
case Some(Token(K.Label, _)) =>
recordExpression()
case Some(Token(K.If, _)) =>
conditional()
case Some(Token(K.Match, _)) =>
mtch()
case Some(Token(K.Let, _)) =>
let()
case Some(Token(K.LParen, _)) =>
lambdaOrParenthesizedExpression()
case Some(t) if t.kind.isOperatorPart =>
operator()
case _ =>
recover(ExpectedTree("expression", emptySiteAtLastBoundary), ErrorTree.apply)
</code></pre>
<p class='snippet-path'>src/main/scala/alpine/parsing/Parser.scala</p>
</div>
<p>In general, each function parses a producing rule of the grammar (see <a href="./grammar.md"><code>grammar.md</code></a>).</p>
<p>An important thing to understand and be careful with is when a token is peeked and when it is taken. This is an important aspect of the parser, so think about it before starting to implement.</p>
<p>Some of the functions in the parser are high order parsers: this means that they take another parser as argument. For example, <code>commaSeparatedList</code> takes a parser as argument and returns a parser that parses a list of elements separated by commas.</p>
<p>One last interesting aspect about our pipeline is that some operators are not tokenized by the tokenizer. For example, <code>>=</code> is tokenized as two tokens <code>></code> and <code>=</code>. This is because the <code>></code> can be used in other contexts (s.t. <code>List<Int></code>). Therefore, the parser is responsible from handling this case and create the operator or not depending on the context. This is an important technique in the world of compilers.</p>
<h3><a href="#your-task-implement-the-parser" id="your-task-implement-the-parser">Your Task: Implement the Parser</a></h3>
<p>We suggest to start implementing by <code>conditional()</code>.</p>
<h4><a href="#some-hints-about-how-to-parse-the-grammar" id="some-hints-about-how-to-parse-the-grammar">Some hints about how to parse the grammar</a></h4>
<p>This section contains a non-exhaustive list of hints to help you parse and understand the grammar.</p>
<h5><a href="#compound-expressions-compoundexpression" id="compound-expressions-compoundexpression">Compound expressions <code>compoundExpression()</code></a></h5>
<p>Look at the grammar and we can see that a compound expression is primary expression followed by a ‘.’, a ‘(’, or nothing. Here are some examples and the corresponding type of the node produced by the parser when the primary expression is followed by ‘.’:</p>
<ul>
<li><code>#record(a: 1).1</code>: <code>Selection(Record(…), IntegerLiteral(1, …))</code></li>
<li><code>#record(a: 1).a</code>: <code>Selection(Record(…), Identifier("a", …))</code></li>
<li><code>#record(a: 1).+</code>: <code>Selection(Record(…), Identifier("+", …))</code></li>
<li><code>a.b</code>: <code>Selection(Identifier("a", …), Identifier("b", …)</code></li>
</ul>
<h5><a href="#prefix-expressions-prefixexpression" id="prefix-expressions-prefixexpression">Prefix expressions <code>prefixExpression()</code></a></h5>
<pre><code class="language-grammar">PrefixExpression -> InfixOp | InfixOp CompoundExpression | CompoundExpression
</code></pre>
<p>A prefix expression checks if the next token is an operator. <strong>If there is no space between the operator and the next token</strong> (see <code>noWhitespaceBeforeNextToken</code>), parse the prefix operator and the compound expression that follows. It returns a <code>PrefixApplication</code> AST node. The fact we have to check for white space presence shows us that this gammar is not a context-free grammar.</p>
<p>If there is a whitespace, then it returns directly the operator (recall: it’s an <code>Identifier</code>.)</p>
<p>In the case where it is not an operator, it will parse the compound expression (so call the <code>compoundExpression()</code> function.)</p>
<h5><a href="#ascribed" id="ascribed"><code>ascribed()</code></a></h5>
<p>An ascribed expression is a prefix expression followed by an optional type cast. It returns a <code>AscribedExpression</code> AST node if there is a type cast, otherwise just a prefix expression. You can use the <code>typecast</code> function.</p>
<p>Example:</p>
<ul>
<li><code>a @ Int</code>: <code>AscribedExpression(Identifier("a", …), Typecast.Widen, TypeIdentifier("Int", _), _)</code></li>
<li><code>1 @ Int</code>: <code>AscribedExpression(IntegerLiteral(1, …), Typecast.Widen, TypeIdentifier("Int", _), _)</code></li>
<li><code>1</code>: <code>IntegerLiteral(1, …)</code> (returned by the <code>prefixExpression</code> function)</li>
</ul>
<h5><a href="#infixexpression-and-expression" id="infixexpression-and-expression"><code>infixExpression</code> and <code>expression()</code></a></h5>
<p>As we saw in the lecture, parsing expressions requires care because of the ambiguity introduced by precedence.</p>
<p>Notice that infixEpression takes a precendence as input: you may use it this parameter to factor out the parsing of all possible infix expressions with different precedence levels.</p>
<p>You may take inspiration from the precedence climbing algorithm to parse infix expressions. See <a href="https://en.wikipedia.org/wiki/Operator-precedence_parser#Precedence_climbing_method">the Wikipedia article</a> for more information.</p>
<h5><a href="#literals" id="literals">Literals</a></h5>
<p>To get the text a token contain, you can use the <code>.site.text</code> method.</p>
<h5><a href="#labeled" id="labeled"><code>labeled(…)</code></a></h5>
<p>A <code>Labeled[T]</code> is a value of type <code>T</code> (as in Scala) with an optional <code>String</code> denoting its label.</p>
<p>In <em>Alpine</em>, a <code>Labeled[T]</code> can be:</p>
<ul>
<li><code><value></code></li>
<li><code><label>: <value></code></li>
</ul>
<p><em>Hint</em>: you may find the <code>snapshot</code> and <code>restore</code> methods useful.</p>
<p><em>Note</em>: as stated in the grammar, a <code><label></code> can be an <code><identifier></code> or a <code><keyword></code>.</p>
<p><em>Examples</em>:</p>
<ul>
<li><code>label: 1</code> → <code>Labeled(Some("label"), IntegerLiteral(1, …))</code></li>
<li><code>match: 1</code> → <code>Labeled(Some("match"), IntegerLiteral(1, …))</code></li>
<li><code>1</code> → <code>Labeled(None, IntegerLiteral(1, …))</code></li>
</ul>
<h5><a href="#inparentheses-inbraces-inangles" id="inparentheses-inbraces-inangles"><code>inParentheses</code>, <code>inBraces</code>, <code>inAngles</code></a></h5>
<p>Complete the three different functions that parses an <code>element</code> delimited by parentheses (<code>(<element>)</code>), braces (<code>{<element>}</code>) and angles (<code><></code>)</p>
<h5><a href="#parenthesizedlabeledlistvalue---t-listlabeledt" id="parenthesizedlabeledlistvalue---t-listlabeledt"><code>parenthesizedLabeledList(value: () => T): List[Labeled[T]]</code></a></h5>
<p>This function parses a list of labeled values delimited by parentheses. The list can be empty.</p>
<p>For example, the above function is responsible to parse the following code:</p>
<pre><code class="language-swift">(1, 2, label: 3)
</code></pre>
<p><em>Hint</em>: <code>inParentheses</code>, <code>labeled</code> and <code>commaSeparatedList</code> are useful to implement this function.</p>
<h5><a href="#records" id="records">Records</a></h5>
<p>In this part, we will break down the record parsing. You should implement <code>recordExpression()</code>, <code>recordExpressionFields()</code> and <code>record(fields: () => List[Field], make: (String, List[Field], …) => T)</code></p>
<ul>
<li>
<p>The <code>record</code> function is responsible for parsing a record. It returns a <code>T</code> AST node. In the case of parsing record expressions, <code>T</code> is <code>ast.Record</code>. It is general and will be used as well for <code>recordType</code>s</p>
</li>
<li>
<p><code>recordExpression()</code> is a function used to parse a record expression. It returns a <code>Record</code> AST node.</p>
</li>
<li>
<p>The <code>recordExpressionFields()</code> function is responsible for parsing the fields of a record expression. It returns a <code>List[Labeled[Expression]]</code> AST node.</p>
</li>
</ul>
<div class="hint">
<p>Do forget that you can reuse parser functions you already implemented so far.</p>
</div>
<p>It should parse the following sub grammar:</p>
<pre><code>Record -> '#' Identifier ['(' LabeledExpressionList ')']
</code></pre>
<div class="note">
<p>An identifier with a <code>#</code> prefix is a special token called <code>Label</code>.</p>
<p>Note as well that <code>Field</code> in <code>record(…)</code> is a generic type! It should be of subtype <code>Labeled[Tree]</code> and <code>Labeled</code> is covariant. It will come handy when we will parse record types.</p>
</div>
<h5><a href="#conditionals-conditional" id="conditionals-conditional">Conditionals <code>conditional()</code></a></h5>
<p><code>if <expression> then <expression> else <expression></code></p>
<p>which correspond to:</p>
<p><code>IfExpression -> 'if' Expression 'then' Expression 'else' Expression</code> in the grammar.</p>
<p>The <code>if</code> function has necessarily to have an <code>else</code> branch. The <code>else</code> branch is mandatory in <em>Alpine</em>.</p>
<h5><a href="#tpe" id="tpe"><code>tpe()</code></a></h5>
<p>The <code>tpe</code> function is responsible for parsing a type. It returns a <code>Type</code> AST node. For the fact, <code>tpe</code> is called <code>tpe</code> because <code>type</code> is a reserved keyword in Scala.</p>
<p><code>tpe</code> should parse the <code>Type</code> given the grammar above. You can call the <code>primaryType</code> function to parse a <code>PrimaryType</code>.</p>
<h5><a href="#recordtype-and-recordtypefields" id="recordtype-and-recordtypefields"><code>recordType()</code> and <code>recordTypeFields()</code></a></h5>
<p>Now, implement the <code>recordType</code> and <code>recordTypeFields</code> functions. They are responsible for parsing a record type and its fields. They should return a <code>RecordType</code> and a <code>List[Labeled[Type]]</code> respectively.</p>
<p>In the same manner as <code>recordExpression</code> and <code>recordExpressionFields</code>, <code>recordType</code> should call the <code>record</code> function.</p>
<h5><a href="#arroworparenthesizedtype" id="arroworparenthesizedtype"><code>arrowOrParenthesizedType()</code></a></h5>
<p>When encountering a <code>(</code> token, it can be either a function’s type or a parenthesized type (a parenthesized type is a type between parentheses). This function should parse the two cases and return the corresponding AST node.</p>
<p>It should parse both cases:</p>
<ul>
<li>Parenthesized type (<code>ParenthesizedType</code>):</li>
</ul>
<pre><code>(<type>)
</code></pre>
<p>and</p>
<ul>
<li>Arrow/Lambda type:</li>
</ul>
<pre><code>(<type1>, <type2>, …, <typeN>) -> <type>
</code></pre>
<h5><a href="#bindings--let" id="bindings--let">Bindings & let</a></h5>
<h6><a href="#binding" id="binding"><code>binding()</code></a></h6>
<p>A binding is a top-level declaration that binds an identifier to a value. It has the following form:</p>
<pre><code>Binding -> 'let' Identifier [':' Type] ['=' Expression]
</code></pre>
<p>where <code>[':' Type]</code> and <code>['=' Expression]</code> is not always optional: the argument <code>initializerIsExpected</code> is <code>true</code> if the initializer is expected (i.e. not optional) and <code>false</code> otherwise (i.e. optional): it may come handy for later! Implement the <code>binding()</code> function that parses a binding.</p>
<h5><a href="#functions-function-valueparameterlist-parameter" id="functions-function-valueparameterlist-parameter">Functions: <code>function()</code>, <code>valueParameterList()</code>, <code>parameter()</code></a></h5>
<h5><a href="#parameter" id="parameter"><code>parameter()</code></a></h5>
<p>A parameter is of the form:</p>
<pre><code><identifier> <identifier> [: <type>] // labeled
'_' <identifier> [: <type>] // unlabeled
<keyword> <identifier> [: <type>] // labeled by keyword
</code></pre>
<p>In the first case, the first element is the label of the parameter (can be an identifier, <code>_</code> or a keyword) and the second element is the name of the argument. When labeled by a keyword, the keyword is the label. When labeled, the parameter’s name (i.e. its identifier inside the function) is the second identifier. When unlabeled (i.e. <code>_</code> is the label), the parameter’s name is the first and only identifier.</p>
<p>Implement the <code>parameter</code> function that parses a parameter. It returns a <code>Parameter</code> AST node.</p>
<p><em>Note</em>: here the label is before the identifier without any separator token. It is not the case for <code>labeled</code> where the label is separated by a colon with the identifier.</p>
<ul>
<li>Examples:
<ul>
<li><code>_ x: Int</code>: <code>Parameter(None, "x", Some(TypeIdentifier("Int", _)), _)</code></li>
<li><code>label x: Int</code>: <code>Parameter(Some("label"), "x", Some(TypeIdentifier("Int", _)), _)</code></li>
<li><code>label x</code>: <code>Parameter(Some("label"), "x", None, _)</code></li>
</ul>
</li>
</ul>
<h6><a href="#valueparameterlist" id="valueparameterlist"><code>valueParameterList()</code></a></h6>
<p>A value parameter list is a list of parameters. It has the following form:</p>
<pre><code>( <parameter1>, … )
</code></pre>
<p>Implement the <code>valueParameterList</code> function that parses a value parameter list. It returns a <code>List[Parameter]</code> AST node.</p>
<p><em>Hint</em>: you may find the <code>commaSeparatedList</code> function and <code>parameter()</code> useful.</p>
<h5><a href="#function" id="function"><code>function()</code></a></h5>
<p>A function is of the form:</p>
<pre><code>fun <identifier> (<type parameters>) [-> <type>] { <expression> }
</code></pre>
<p>where <code>[-> <type>]</code> is optional. Implement the <code>function</code> function that parses a function. It returns a <code>Function</code> AST node.</p>
<h3><a href="#lambdaorparenthesizedexpression" id="lambdaorparenthesizedexpression"><code>lambdaOrParenthesizedExpression()</code></a></h3>
<p>When encountering a <code>(</code> token, it can be either a lambda or a parenthesized expression. This function should parse the two cases and return the corresponding AST node.</p>
<p>It should parse both cases:</p>
<pre><code>(<expression>)
</code></pre>
<p>and</p>
<pre><code>(<value parameter list>) [-> type] { <expression> }
</code></pre>
<p>where <code>[-> type]</code> is optional.</p>
<p><em>Hint</em>: <code>snapshot</code> and <code>restore</code> may come handy.</p>
<h5><a href="#match-expressions" id="match-expressions">Match expressions</a></h5>
<p>A match expression is a conditional expression that matches a value against a set of patterns. It has the following form:</p>
<pre><code class="language-swift">match <expression> {
case <pattern> then <expression>
}
</code></pre>
<p>Let’s decompose this pattern into smaller parts as we did for the other elements of the language.</p>
<h6><a href="#mtch" id="mtch"><code>mtch()</code></a></h6>
<p>The <code>mtch</code> function is responsible for parsing a match expression. It returns a <code>Match</code> AST node. It expects a <code>match</code> token, an expression and then calls <code>matchBody</code> to parse the body of the match expression.</p>
<h6><a href="#pattern-with-wildcard-recordpattern-bindingpattern-and-valuepattern" id="pattern-with-wildcard-recordpattern-bindingpattern-and-valuepattern"><code>pattern()</code> with <code>wildcard()</code>, <code>recordPattern()</code>, <code>bindingPattern()</code> and <code>valuePattern()</code></a></h6>
<p>The four functions are responsible for parsing a pattern. They return a <code>Pattern</code> AST node.</p>
<ul>
<li><code>wildcard</code> should parse the <code>_</code> token.</li>
<li><code>valuePattern</code> should parse an <code>expression</code> and return a <code>ValuePattern</code> AST node.</li>
<li><code>bindingPattern</code> should parse a <code>binding()</code> without an initializer!</li>
<li><code>recordPattern</code> should call the <code>record</code> function and return a <code>RecordPattern</code> AST node. Don’t forget that you’ve made a function to parse records! However, it is required to fill the <code>recordPatternFields()</code> function.</li>
</ul>
</main>
</div>
<script src="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/languages/scala.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/languages/swift.min.js"></script>
<script>hljs.highlightAll();</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" integrity="sha384-XjKyOOlGwcjNTAIQHIpgOno0Hl1YQqzUOEleOLALmuqehneUG+vnGctmUb0ZY0l8" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js" integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05" crossorigin="anonymous" onload="renderMathInElement(document.body, {delimiters:[{left:'$$', right:'$$', display: true}, {left:'$', right:'$', display: false}]});"></script>
</body>
</html>