Skip to content

Commit e94ea5a

Browse files
committed
Comments
1 parent accafa0 commit e94ea5a

File tree

13 files changed

+251
-33
lines changed

13 files changed

+251
-33
lines changed

docs/Text/Parsing/Parser.md

+19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ data ParseError
77
= ParseError { message :: String, position :: Position }
88
```
99

10+
A parsing error, consisting of a message and position information.
11+
1012
##### Instances
1113
``` purescript
1214
instance errorParseError :: Error ParseError
@@ -29,6 +31,10 @@ newtype ParserT s m a
2931
= ParserT (PState s -> m { input :: s, result :: Either ParseError a, consumed :: Boolean, position :: Position })
3032
```
3133

34+
The Parser monad transformer.
35+
36+
The first type argument is the stream type. Typically, this is either `String`, or some sort of token stream.
37+
3238
##### Instances
3339
``` purescript
3440
instance functorParserT :: (Functor m) => Functor (ParserT s m)
@@ -51,36 +57,48 @@ instance lazyParserT :: Lazy (ParserT s m a)
5157
unParserT :: forall m s a. ParserT s m a -> PState s -> m { input :: s, result :: Either ParseError a, consumed :: Boolean, position :: Position }
5258
```
5359

60+
Apply a parser by providing an initial state.
61+
5462
#### `runParserT`
5563

5664
``` purescript
5765
runParserT :: forall m s a. (Monad m) => PState s -> ParserT s m a -> m (Either ParseError a)
5866
```
5967

68+
Apply a parser, keeping only the parsed result.
69+
6070
#### `Parser`
6171

6272
``` purescript
6373
type Parser s a = ParserT s Identity a
6474
```
6575

76+
The `Parser` monad is a synonym for the parser monad transformer applied to the `Identity` monad.
77+
6678
#### `runParser`
6779

6880
``` purescript
6981
runParser :: forall s a. s -> Parser s a -> Either ParseError a
7082
```
7183

84+
Apply a parser, keeping only the parsed result.
85+
7286
#### `consume`
7387

7488
``` purescript
7589
consume :: forall s m. (Monad m) => ParserT s m Unit
7690
```
7791

92+
Set the consumed flag.
93+
7894
#### `fail`
7995

8096
``` purescript
8197
fail :: forall m s a. (Monad m) => String -> ParserT s m a
8298
```
8399

100+
Fail with a message.
101+
84102
#### `parseFailed`
85103

86104
``` purescript
@@ -89,6 +107,7 @@ parseFailed :: forall s a. s -> Position -> String -> { input :: s, result :: Ei
89107

90108
Creates a failed parser state for the remaining input `s` and current position
91109
with an error message.
110+
92111
Most of the time, `fail` should be used instead.
93112

94113

docs/Text/Parsing/Parser/Combinators.md

+67-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Module Text.Parsing.Parser.Combinators
22

3+
Combinators for creating parsers.
4+
35
#### `(<?>)`
46

57
``` purescript
@@ -8,90 +10,138 @@
810

911
_left-associative / precedence -1_
1012

13+
Provide an error message in the case of failure.
14+
1115
#### `between`
1216

1317
``` purescript
1418
between :: forall m s a open close. (Monad m) => ParserT s m open -> ParserT s m close -> ParserT s m a -> ParserT s m a
1519
```
1620

21+
Wrap a parser with opening and closing markers.
22+
23+
For example:
24+
25+
```purescript
26+
parens = between (string "(") (string ")")
27+
```
28+
1729
#### `option`
1830

1931
``` purescript
2032
option :: forall m s a. (Monad m) => a -> ParserT s m a -> ParserT s m a
2133
```
2234

35+
Provide a default result in the case where a parser fails without consuming input.
36+
2337
#### `optional`
2438

2539
``` purescript
2640
optional :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m Unit
2741
```
2842

43+
Optionally parse something, failing quietly.
44+
2945
#### `optionMaybe`
3046

3147
``` purescript
32-
optionMaybe :: forall m s a. (Functor m, Monad m) => ParserT s m a -> ParserT s m (Maybe a)
48+
optionMaybe :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m (Maybe a)
3349
```
3450

51+
Return `Nothing` in the case where a parser fails without consuming input.
52+
3553
#### `try`
3654

3755
``` purescript
3856
try :: forall m s a. (Functor m) => ParserT s m a -> ParserT s m a
3957
```
4058

59+
In case of failure, reset the stream to the unconsumed state.
60+
4161
#### `sepBy`
4262

4363
``` purescript
4464
sepBy :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
4565
```
4666

67+
Parse phrases delimited by a separator.
68+
69+
For example:
70+
71+
```purescript
72+
digit `sepBy` string ","
73+
```
74+
4775
#### `sepBy1`
4876

4977
``` purescript
5078
sepBy1 :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
5179
```
5280

81+
Parse phrases delimited by a separator, requiring at least one match.
82+
5383
#### `sepEndBy`
5484

5585
``` purescript
5686
sepEndBy :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
5787
```
5888

89+
Parse phrases delimited and optionally terminated by a separator.
90+
5991
#### `sepEndBy1`
6092

6193
``` purescript
6294
sepEndBy1 :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
6395
```
6496

97+
Parse phrases delimited and optionally terminated by a separator, requiring at least one match.
98+
6599
#### `endBy1`
66100

67101
``` purescript
68102
endBy1 :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
69103
```
70104

105+
Parse phrases delimited and terminated by a separator, requiring at least one match.
106+
71107
#### `endBy`
72108

73109
``` purescript
74110
endBy :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
75111
```
76112

113+
Parse phrases delimited and terminated by a separator.
114+
77115
#### `chainr`
78116

79117
``` purescript
80118
chainr :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a
81119
```
82120

121+
Parse phrases delimited by a right-associative operator.
122+
123+
For example:
124+
125+
```purescript
126+
chainr digit (string "+" *> add) 0
127+
```
128+
83129
#### `chainl`
84130

85131
``` purescript
86132
chainl :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m (a -> a -> a) -> a -> ParserT s m a
87133
```
88134

135+
Parse phrases delimited by a left-associative operator.
136+
89137
#### `chainl1`
90138

91139
``` purescript
92140
chainl1 :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m (a -> a -> a) -> ParserT s m a
93141
```
94142

143+
Parse phrases delimited by a left-associative operator, requiring at least one match.
144+
95145
#### `chainl1'`
96146

97147
``` purescript
@@ -104,6 +154,8 @@ chainl1' :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m (a -> a -> a
104154
chainr1 :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m (a -> a -> a) -> ParserT s m a
105155
```
106156

157+
Parse phrases delimited by a right-associative operator, requiring at least one match.
158+
107159
#### `chainr1'`
108160

109161
``` purescript
@@ -116,40 +168,54 @@ chainr1' :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m (a -> a -> a
116168
choice :: forall f m s a. (Foldable f, Monad m) => f (ParserT s m a) -> ParserT s m a
117169
```
118170

171+
Parse one of a set of alternatives.
172+
119173
#### `skipMany`
120174

121175
``` purescript
122176
skipMany :: forall s a m. (Monad m) => ParserT s m a -> ParserT s m Unit
123177
```
124178

179+
Skip many instances of a phrase.
180+
125181
#### `skipMany1`
126182

127183
``` purescript
128184
skipMany1 :: forall s a m. (Monad m) => ParserT s m a -> ParserT s m Unit
129185
```
130186

187+
Skip at least one instance of a phrase.
188+
131189
#### `lookAhead`
132190

133191
``` purescript
134192
lookAhead :: forall s a m. (Monad m) => ParserT s m a -> ParserT s m a
135193
```
136194

195+
Parse a phrase, without modifying the consumed state or stream position.
196+
137197
#### `notFollowedBy`
138198

139199
``` purescript
140200
notFollowedBy :: forall s a m. (Monad m) => ParserT s m a -> ParserT s m Unit
141201
```
142202

203+
Fail if the specified parser matches.
204+
143205
#### `manyTill`
144206

145207
``` purescript
146208
manyTill :: forall s a m e. (Monad m) => ParserT s m a -> ParserT s m e -> ParserT s m (List a)
147209
```
148210

211+
Parse several phrases until the specified terminator matches.
212+
149213
#### `many1Till`
150214

151215
``` purescript
152216
many1Till :: forall s a m e. (Monad m) => ParserT s m a -> ParserT s m e -> ParserT s m (List a)
153217
```
154218

219+
Parse several phrases until the specified terminator matches, requiring at least one match.
220+
155221

docs/Text/Parsing/Parser/Expr.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,22 @@ data Operator m s a
2424
type OperatorTable m s a = Array (Array (Operator m s a))
2525
```
2626

27-
#### `SplitAccum`
27+
#### `buildExprParser`
2828

2929
``` purescript
30-
type SplitAccum m s a = { rassoc :: List (ParserT s m (a -> a -> a)), lassoc :: List (ParserT s m (a -> a -> a)), nassoc :: List (ParserT s m (a -> a -> a)), prefix :: List (ParserT s m (a -> a)), postfix :: List (ParserT s m (a -> a)) }
30+
buildExprParser :: forall m s a. (Monad m) => OperatorTable m s a -> ParserT s m a -> ParserT s m a
3131
```
3232

33-
#### `buildExprParser`
33+
Build a parser from an `OperatorTable`.
3434

35-
``` purescript
36-
buildExprParser :: forall m s a. (Monad m) => OperatorTable m s a -> ParserT s m a -> ParserT s m a
35+
For example:
36+
37+
```purescript
38+
buildExprParser [ [ Infix (string "/" $> div) AssocRight ]
39+
, [ Infix (string "*" $> mul) AssocRight ]
40+
, [ Infix (string "-" $> sub) AssocRight ]
41+
, [ Infix (string "+" $> add) AssocRight ]
42+
] digit
3743
```
3844

3945

docs/Text/Parsing/Parser/Pos.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ data Position
88
```
99

1010
`Position` represents the position of the parser in the input.
11+
1112
- `line` is the current line in the input
1213
- `column` is the column of the next character in the current line that will be parsed
1314

0 commit comments

Comments
 (0)