Skip to content

Commit 985e2f3

Browse files
committed
Expression parser now parses function calls
1 parent 97e10ea commit 985e2f3

File tree

3 files changed

+76
-13
lines changed

3 files changed

+76
-13
lines changed

src/Parse/Expression.gren

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Error
2626
| WildcardAttempt
2727
| ExpectedLowerVariable AST.Expression
2828
| NameMismatch { first : String, second : String }
29+
| IndentError
2930

3031

3132
parser : Parser c Error AST.Expression
@@ -36,6 +37,39 @@ parser =
3637
, letParser
3738
, function
3839
, possiblyNegativeTerm
40+
|> Parser.andThen
41+
(\firstTerm ->
42+
Parser.succeed
43+
(\args ->
44+
if Array.isEmpty args then
45+
firstTerm
46+
47+
else
48+
let
49+
end =
50+
Array.last args
51+
|> Maybe.map .end
52+
|> Maybe.withDefault firstTerm.end
53+
in
54+
SourcePosition.at
55+
firstTerm.start
56+
end
57+
(AST.Call { fn = firstTerm, args = args })
58+
)
59+
|> Parser.skip Space.parser
60+
|> Parser.keep (Parser.withIndent firstTerm.start.col (Parser.loop [] argOrOperatorLoop))
61+
)
62+
]
63+
64+
65+
argOrOperatorLoop : Array AST.Expression -> Parser c Error (Parser.Step (Array AST.Expression) (Array AST.Expression))
66+
argOrOperatorLoop args =
67+
Parser.oneOf
68+
[ Parser.succeed (\arg -> Parser.Loop <| Array.pushLast arg args)
69+
|> Parser.skip (Parser.mapError (\_ -> IndentError) Space.checkIndent)
70+
|> Parser.keep possiblyNegativeTerm
71+
|> Parser.skip Space.parser
72+
, Parser.succeed (Parser.Done args)
3973
]
4074

4175

src/Parse/Variable.gren

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ lowerCase =
3535
else
3636
Parser.succeed word
3737
)
38+
-- TODO: uncertain if this `backtrackable` work the way I intend
39+
-- ideally, we'd only backtrack on reserved words
40+
|> Parser.backtrackable
3841

3942

4043
reservedWords : Set String

tests/src/Test/Parse/Expression.gren

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,27 @@ tests =
7070
, accessor = "expression"
7171
}
7272
)
73+
, test "function calls" <| \_ ->
74+
Parser.run PE.parser "func a b"
75+
|> expectExpression
76+
(AST.Call
77+
{ fn =
78+
SourcePosition.at
79+
{ row = 1, col = 1 }
80+
{ row = 1, col = 5 }
81+
(AST.Var { name = "func", varType = AST.LowVar })
82+
, args =
83+
[ SourcePosition.at
84+
{ row = 1, col = 6 }
85+
{ row = 1, col = 7 }
86+
(AST.Var { name = "a", varType = AST.LowVar })
87+
, SourcePosition.at
88+
{ row = 1, col = 8 }
89+
{ row = 1, col = 9 }
90+
(AST.Var { name = "b", varType = AST.LowVar })
91+
]
92+
}
93+
)
7394
]
7495
, describe "Array"
7596
[ test "Empty array" <| \_ ->
@@ -278,7 +299,12 @@ tests =
278299
}
279300
)
280301
, test "multiple branches" <| \_ ->
281-
Parser.run PE.parser "when myVar is A -> 'a' B -> 'b'"
302+
Parser.run PE.parser
303+
"""
304+
when myVar is
305+
A -> 'a'
306+
B -> 'b'
307+
"""
282308
|> expectExpression
283309
(AST.When
284310
{ expression =
@@ -289,40 +315,40 @@ tests =
289315
, branches =
290316
[ { pattern =
291317
SourcePosition.at
292-
{ row = 1, col = 15 }
293-
{ row = 1, col = 17 }
318+
{ row = 2, col = 5 }
319+
{ row = 2, col = 7 }
294320
(AST.PCtor
295321
{ name =
296322
SourcePosition.at
297-
{ row = 1, col = 15 }
298-
{ row = 1, col = 16 }
323+
{ row = 2, col = 5 }
324+
{ row = 2, col = 6 }
299325
"A"
300326
, arg = Nothing
301327
}
302328
)
303329
, body =
304330
SourcePosition.at
305-
{ row = 1, col = 20 }
306-
{ row = 1, col = 23 }
331+
{ row = 2, col = 10 }
332+
{ row = 2, col = 13 }
307333
(AST.CharLiteral 'a')
308334
}
309335
, { pattern =
310336
SourcePosition.at
311-
{ row = 1, col = 24 }
312-
{ row = 1, col = 26 }
337+
{ row = 3, col = 5 }
338+
{ row = 3, col = 7 }
313339
(AST.PCtor
314340
{ name =
315341
SourcePosition.at
316-
{ row = 1, col = 24 }
317-
{ row = 1, col = 25 }
342+
{ row = 3, col = 5 }
343+
{ row = 3, col = 6 }
318344
"B"
319345
, arg = Nothing
320346
}
321347
)
322348
, body =
323349
SourcePosition.at
324-
{ row = 1, col = 29 }
325-
{ row = 1, col = 32 }
350+
{ row = 3, col = 10 }
351+
{ row = 3, col = 13 }
326352
(AST.CharLiteral 'b')
327353
}
328354
]

0 commit comments

Comments
 (0)