Skip to content

Commit b355e01

Browse files
committed
Added function equivalents of |. and |= operators
1 parent dccde05 commit b355e01

3 files changed

Lines changed: 52 additions & 7 deletions

File tree

src/Parser.gren

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Parser exposing
22
( Parser, run
33
, int, float, number, symbol, keyword, variable, end
4-
, succeed, (|=), (|.), lazy, andThen, problem
4+
, succeed, (|=), (|.), keep, skip, lazy, andThen, problem
55
, oneOf, map, backtrackable, commit, token
66
, sequence, Trailing(..), loop, Step(..)
77
, spaces, lineComment, multiComment, Nestable(..)
@@ -291,6 +291,16 @@ ignorer =
291291
(|.)
292292

293293

294+
keep : Parser keep -> Parser (keep -> b) -> Parser b
295+
keep =
296+
A.keep
297+
298+
299+
skip : Parser ignore -> Parser kept -> Parser kept
300+
skip =
301+
A.skip
302+
303+
294304
{-| Helper to define recursive parsers. Say we want a parser for simple
295305
boolean expressions:
296306

src/Parser/Advanced.gren

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Parser.Advanced exposing
22
( Parser, run, DeadEnd, inContext, Token(..)
33
, int, float, number, symbol, keyword, variable, end
4-
, succeed, (|=), (|.), lazy, andThen, problem
4+
, succeed, (|=), (|.), keep, skip, lazy, andThen, problem
55
, oneOf, map, backtrackable, commit, token
66
, sequence, Trailing(..), loop, Step(..)
77
, spaces, lineComment, multiComment, Nestable(..)
@@ -322,6 +322,19 @@ ignorer keepParser ignoreParser =
322322
map2 (\val _ -> val) keepParser ignoreParser
323323

324324

325+
{-| Just like the [`(|=)`](Parser#|=) from the `Parser` module.
326+
-}
327+
keep : Parser c x a -> Parser c x (a -> b) -> Parser c x b
328+
keep parseArg parseFunc =
329+
map2 (<|) parseFunc parseArg
330+
331+
332+
{-| Just like the [`(|.)`](Parser#|.) from the `Parser` module.
333+
-}
334+
skip : Parser c x ignore -> Parser c x keep -> Parser c x keep
335+
skip ignoreParser keepParser =
336+
map2 (\val _ -> val) keepParser ignoreParser
337+
325338

326339
-- AND THEN
327340

@@ -1254,8 +1267,8 @@ sequence
12541267
}
12551268
-> Parser c x (Array a)
12561269
sequence i =
1257-
skip (token i.start) <|
1258-
skip i.spaces <|
1270+
seqSkip (token i.start) <|
1271+
seqSkip i.spaces <|
12591272
sequenceEnd (token i.end) i.spaces i.item (token i.separator) i.trailing
12601273

12611274

@@ -1266,8 +1279,8 @@ club](https://poorlydrawnlines.com/comic/shapes-club/)!
12661279
type Trailing = Forbidden | Optional | Mandatory
12671280

12681281

1269-
skip : Parser c x ignore -> Parser c x keep -> Parser c x keep
1270-
skip iParser kParser =
1282+
seqSkip : Parser c x ignore -> Parser c x keep -> Parser c x keep
1283+
seqSkip iParser kParser =
12711284
map2 revAlways iParser kParser
12721285

12731286

tests/src/Main.gren

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Main exposing (main)
33
import Expect
44
import Test exposing (describe, test)
55
import Test.Runner.Node exposing (Program, run)
6-
import Parser as P
6+
import Parser as P exposing ((|.), (|=))
77

88

99

@@ -19,6 +19,28 @@ main =
1919
P.run P.int "3.1415"
2020
|> expectErr
2121
]
22+
, describe "combinators"
23+
[ test "functions" <| \{} ->
24+
let
25+
parser =
26+
P.succeed (\a b -> a + b)
27+
|> P.keep P.int
28+
|> P.skip (P.symbol "+")
29+
|> P.keep P.int
30+
in
31+
P.run parser "3+5"
32+
|> Expect.equal (Ok 8)
33+
, test "operators" <| \{} ->
34+
let
35+
parser =
36+
P.succeed (\a b -> a + b)
37+
|= P.int
38+
|. P.symbol "+"
39+
|= P.int
40+
in
41+
P.run parser "2+2"
42+
|> Expect.equal (Ok 4)
43+
]
2244
]
2345

2446

0 commit comments

Comments
 (0)