summaryrefslogtreecommitdiff
path: root/src/lib/Codec/Pesto/Parse.lhs
blob: 762fff4a6991125d7ab320768fe5f8b7fd422fd7 (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
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
412
.. _language-syntax:

Language syntax
---------------

.. class:: nodoc

> module Codec.Pesto.Parse (
> 	  parse
> 	, test
> 	, Instruction(..)
> 	, Quantity(..)
> 	, Unit
> 	, Object
> 	, Approximately(..)
> 	, Amount(..)
> 	, isResult
> 	, isReference
> 	, isAlternative
> 	, isAnnotation
> 	, isAction
> 	, isDirective
> 	, isUnknown
> 	, spaces1
> 	, notspace
> 	) where
> import Data.Char (isSpace)
> import Data.Ratio ((%))
> import Text.Parsec hiding (parse)
> import Text.ParserCombinators.Parsec.Pos (newPos)
> import Text.ParserCombinators.Parsec.Error (newErrorUnknown)
> import Test.HUnit hiding (test)
>
> import Codec.Pesto.Serialize (serialize)

Pesto parses `UTF-8 <https://tools.ietf.org/html/rfc3629>`_ encoded input data
consisting of space-delimited token. Every character within the Unicode
whitespace class is considered a space.

.. _spaces1:

> stream = ((,) <$> getPosition <*> instruction) `sepEndBy` spaces1
> 	<?> "stream"
> spaces1 = many1 space

The following instructions are supported:

> data Instruction =
> 	  Annotation String
> 	| Ingredient Quantity
> 	| Tool Quantity
> 	| Action String
> 	| Reference Quantity
> 	| Result Quantity
> 	| Alternative Quantity
> 	| Directive String
> 	| Unknown String
> 	deriving (Show, Eq)
>
> instruction =
> 	    try annotation
> 	<|> try ingredient
> 	<|> try tool
> 	<|> try action
> 	<|> try result
> 	<|> try alternative
> 	<|> try reference
> 	<|> try directive
> 	<|> try unknown
> 	<?> "instruction"

The pesto grammar has two instruction types: The first one begins with a
start symbol (``start``) and consumes any character up to and including a
terminating symbol (``end``), which can be escaped with a backslash (``\``).

> betweenEscaped :: Char -> Char -> Parsec String () String
> betweenEscaped start end =
> 	   char start
> 	*> many (try (char '\\' *> char end) <|> satisfy (/= end))
> 	<* char end

Annotations and actions both are of this kind:

> annotation = Annotation <$> betweenEscaped '(' ')'
> action = Action <$> betweenEscaped '[' ']'

Here are examples for both:

> testOpterm = [cmpInstruction "(skinless\nboneless)" (Right (Annotation "skinless\nboneless"))
> 	, cmpInstruction "[stir together]" (Right (Action "stir together"))
> 	, cmpInstruction "[stir\\]together]" (Right (Action "stir]together"))
> 	, cmpInstruction "[stir [together]" (Right (Action "stir [together"))]

The second one starts with one identifying character, ignores the following
whitespace characters, and then consumes a ``Quantity``.

> oparg :: Char -> Parsec String () Instruction -> Parsec String () Instruction
> oparg ident cont = char ident *> spaces *> cont
>
> ingredient = oparg '+' (Ingredient <$> quantity)
> tool = oparg '&' (Tool <$> quantity)
> result = oparg '>' (Result <$> quantity)
> alternative = oparg '|' (Alternative <$> quantity)
> reference = oparg '*' (Reference <$> quantity)

Additionally, there are two special instructions. Directives are similar to the
previous instructions but consume a quoted string (``qstr``).

> directive = oparg '%' (Directive <$> qstr)

Unknown instructions are the fallthrough-case and accept anything. They must
not be discarded at this point. The point of accepting anything is to fail as
late as possible while processing input. This gives the parser a chance to
print helpful messages that provide additional aid to the user, who can then fix
the problem.

> unknown = Unknown <$> many1 notspace

Below are examples for these instructions:

> testOparg = [
> 	  cmpInstruction "+100 g flour"
> 	      (Right (Ingredient (Quantity (Exact (AmountRatio (100%1))) "g" "flour")))
> 	, cmpInstruction "&oven"
> 	      (Right (Tool (strQuantity "oven")))
> 	, cmpInstruction ">dough" (Right (Result (strQuantity "dough")))
> 	, cmpInstruction "|trimmings" (Right (Alternative (strQuantity "trimmings")))
> 	, cmpInstruction "*fish"
> 	      (Right (Reference (strQuantity "fish")))
> 	, cmpInstruction3 "% invalid" (Right (Directive "invalid")) "%invalid"
> 	, cmpInstruction3 "* \t\n 1 _ cheese"
> 	      (Right (Reference (Quantity (Exact (AmountRatio (1%1))) "" "cheese")))
> 	      "*1 _ cheese"
> 	, cmpInstruction3 "!invalid" (Right (Unknown "!invalid")) "!invalid"
> 	]

Qstr
++++

Before introducing quantities we need to have a look at qstr, which is used by
them. A qstr, short for quoted string, can be a string enclosed in double
quotes, a single word or the underscore character that represents the
empty string.

> qstr = try (betweenEscaped '"' '"')
> 	<|> word
> 	<|> char '_' *> return ""
 
A word always starts with a letter, followed by any number of non-space
characters.

> word = (:) <$> letter <*> many notspace
> notspace = satisfy (not . isSpace)

The empty string can be represented by two double quotes or the underscore, but
not the empty string itself.

> testQstr = [
> 	  cmpQstr3 "\"\"" (Right "") "_"
> 	, cmpQstr "_" (Right "")
> 	, cmpQstr "" parseError

Any Unicode character with a General_Category major class L (i.e., a letter, see
`Unicode standard section 4.5
<http://www.unicode.org/versions/Unicode7.0.0/ch04.pdf>`_ for example) is
accepted as first character of a word. That includes german umlauts as well as
greek or arabic script. Numbers, separators, punctuation, and others are not
permitted.

> 	, cmpQstr "water" (Right "water")
> 	, cmpQstr "Äpfel" (Right "Äpfel")
> 	, cmpQstr "τυρί" (Right "τυρί")
> 	, cmpQstr "جبن" (Right "جبن")
> 	, cmpQstr "1sugar" parseError
> 	, cmpQstr "+milk" parseError
> 	, cmpQstr "∀onion" parseError

The remaining letters of a word can be any character, including symbols,
numbers, …

> 	, cmpQstr "rump-roast" (Right "rump-roast")
> 	, cmpQstr "v1negar" (Right "v1negar")
> 	, cmpQstr "mush\"rooms" (Right "mush\"rooms")

…but not spaces.

> 	, cmpQstr " tomatoes" parseError
> 	, cmpQstr "tomatoes " parseError
> 	, cmpQstr "lemon juice" parseError
> 	, cmpQstr "sour\tcream" parseError
> 	, cmpQstr "white\nwine" parseError

If a string contains spaces or starts with a special character, it must be
enclosed in double quotes.

> 	, cmpQstr3 "\"salt\"" (Right "salt") "salt"
> 	, cmpQstr "\"+milk\"" (Right "+milk")
> 	, cmpQstr "\"soy sauce\"" (Right "soy sauce")
> 	, cmpQstr "\"1sugar\"" (Right "1sugar")
> 	, cmpQstr "\"chicken\tbreast\nmeat\"" (Right "chicken\tbreast\nmeat")

Doublequotes within a string can be quoted by prepending a backslash. However,
the usual escape codes like \\n, \\t, … will *not* be expanded.

> 	, cmpQstr "\"vine\"gar\"" parseError
> 	, cmpQstr3 "\"vine\\\"gar\"" (Right "vine\"gar") "vine\"gar"
> 	, cmpQstr "\"oli\\ve oil\"" (Right "oli\\ve oil")
> 	, cmpQstr "\"oli\\\\\"ve oil\"" (Right "oli\\\"ve oil")
> 	, cmpQstr3 "\"sal\\tmon\"" (Right "sal\\tmon") "sal\\tmon"
> 	]

Quantity
++++++++

A ``Quantity`` is a triple of ``Approximately``, ``Unit`` and ``Object`` as parameter.

> data Quantity = Quantity Approximately Unit Object deriving (Show, Eq)

The syntactic construct is overloaded and accepts one to three
arguments. If just one is given, it is assumed to be the ``Object``
and ``Approximately`` and ``Unit`` are empty. Two arguments set
``Approximately`` and ``Unit``, which is convenient when the unit implies
the object (minutes usually refer to the object time, for example).

> quantity = try quantityA <|> quantityB

> quantityA = Quantity
> 	<$> approximately
> 	<*  spaces1
> 	<*> unit
> 	<*> (try (spaces1 *> object) <|> return "")

> quantityB = Quantity
> 	<$> return (Exact (AmountStr ""))
> 	<*> return ""
> 	<*> object

> testQuantityOverloaded = [
> 	  cmpQuantity "oven" (exactQuantity (AmountStr "") "" "oven")
> 	, cmpQuantity "10 min" (exactQuantity (AmountRatio (10%1)) "min" "")
> 	, cmpQuantity "100 g flour" (exactQuantity (AmountRatio (100%1)) "g" "flour")

The first two are equivalent to

> 	, cmpQuantity3 "_ _ oven" (exactQuantity (AmountStr "") "" "oven") "oven"
> 	, cmpQuantity3 "10 min _" (exactQuantity (AmountRatio (10%1)) "min" "") "10 min"

Missing units must not be omitted. The version with underscore should be preferred.

> 	, cmpQuantity3 "1 \"\" meal" (exactQuantity (AmountRatio (1%1)) "" "meal") "1 _ meal"
> 	, cmpQuantity "1 _ meal" (exactQuantity (AmountRatio (1%1)) "" "meal")
> 	]

Units and objects are just strings. However, units should be limited to
`well-known metric units <#well-known-units>`_.

> type Unit = String
> unit = qstr
>
> type Object = String
> object = qstr

``Approximately`` is a wrapper for ranges, that is, two amounts separated by a dash,
approximate amounts, prepended with a tilde, and exact amounts without a modifier.

> data Approximately =
> 	  Range Amount Amount
> 	| Approx Amount
> 	| Exact Amount
> 	deriving (Show, Eq)
> 
> approximately = try range <|> try approx <|> exact
> range = Range <$> amount <*> (char '-' *> amount)
> approx = Approx <$> (char '~' *> amount)
> exact = Exact <$> amount

> testQuantityApprox = [
> 	  cmpQuantity "1-2 _ bananas" (Right (Quantity (Range (AmountRatio (1%1)) (AmountRatio (2%1))) "" "bananas"))
> 	, cmpQuantity "1 - 2 _ bananas" parseError
> 	, cmpQuantity "1- 2 _ bananas" parseError
> 	, cmpQuantity "1 -2 _ bananas" parseError
> 	, cmpQuantity "~2 _ bananas" (Right (Quantity (Approx (AmountRatio (2%1))) "" "bananas"))
> 	, cmpQuantity "~ 2 _ bananas" parseError
> 	]

Amounts are limited to rational numbers and strings. There are no real numbers
by design, and implementations should avoid representing rational numbers as
floating point numbers. They are not required and introduce ugly corner cases when
rounding while converting units, for example.

> data Amount =
> 	  AmountRatio Rational
> 	| AmountStr String
> 	deriving (Show, Eq)
>
> amount = try ratio <|> AmountStr <$> qstr

> testQuantityAmount = [
> 	  cmpQuantity "some _ pepper" (exactQuantity (AmountStr "some") "" "pepper")
> 	, cmpQuantity3 "\"some\"-\"a few\" _ bananas" (Right (Quantity (Range (AmountStr "some") (AmountStr "a few")) "" "bananas")) "some-\"a few\" _ bananas"
> 	, cmpQuantity "~\"the stars in your eyes\" _ bananas" (Right (Quantity (Approx (AmountStr "the stars in your eyes")) "" "bananas"))
> 	]

Rational numbers can be an integral, numerator, and denominator, each separated
by a forward slash, just the numerator and denominator, again separated by a
forward slash, or just a numerator with the default denominator 1 (i.e., ordinary
integral number).

> ratio = let toRatio i num denom = AmountRatio ((i*denom+num)%denom) in
> 	    try (toRatio <$> int <*> (char '/' *> int) <*> (char '/' *> int))
> 	<|> try (toRatio <$> return 0 <*> int <*> (char '/' *> int))
> 	<|> try (toRatio <$> return 0 <*> int <*> return 1)

The following representations are all equal with the first one being
the preferred one:

> testQuantityRatio = [
> 	  cmpQuantity "3 _ bananas" (exactQuantity (AmountRatio (3%1)) "" "bananas")
> 	, cmpQuantity3 "3/1 _ bananas" (exactQuantity (AmountRatio (3%1)) "" "bananas")
> 	               "3 _ bananas"
> 	, cmpQuantity3 "3/0/1 _ bananas" (exactQuantity (AmountRatio (3%1)) "" "bananas")
> 	               "3 _ bananas"

Two numbers are numerator and denominator:

> 	, cmpQuantity "3/5 _ bananas" (exactQuantity (AmountRatio (3%5)) "" "bananas")

Three numbers add an integral part:

> 	, cmpQuantity "3/5/7 _ bananas" (exactQuantity (AmountRatio ((3*7+5)%7)) "" "bananas")
> 	, cmpQuantity3 "10/3 _ bananas" (exactQuantity (AmountRatio (10%3)) "" "bananas")
> 	               "3/1/3 _ bananas"

Rational numbers can be used in ranges and mixed with strings too.

> 	, cmpQuantity "1-\"a few\" _ bananas" (Right (Quantity
> 			(Range (AmountRatio (1%1)) (AmountStr "a few")) "" "bananas"))
> 	, cmpQuantity "1/1/2-2 _ bananas" (Right (Quantity
> 			(Range (AmountRatio (3%2)) (AmountRatio (4%2))) "" "bananas"))
> 	, cmpQuantity "~1/1/2 _ bananas" (Right (Quantity
> 			(Approx (AmountRatio (3%2))) "" "bananas"))
> 	]

Appendix
++++++++

Parser main entry point.

> parse = runParser stream () ""
> int = read <$> many1 digit

Test helpers:

> isLeft (Left _) = True
> isLeft _ = False

A generic parser error:

> parseError = Left (newErrorUnknown (newPos "" 0 0))

Compare output of parser ``f`` for string ``str`` with ``expected``. The
expected result can be a parser error, which matches any actual parse error
(first case).

> cmpParser f str (Left _) = TestCase $ assertBool str $ isLeft $ runParser (f <* eof) () "" str
> cmpParser f str expected = str ~: runParser (f <* eof) () "" str ~?= expected

> cmpParseSerialize f str expectp@(Left _) _ = [cmpParser f str expectp]
> cmpParseSerialize f str expectp@(Right expectpval) expects = [
> 	  cmpParser f str expectp
> 	, serialize expectpval ~?= expects]

Wrap qstr test in AmountStr to aid serialization test

> cmpQstr input expectp = cmpQstr3 input expectp input
> cmpQstr3 input (Left expect) _ = [cmpParser (AmountStr <$> qstr) input (Left expect)]
> cmpQstr3 input (Right expect) expects = cmpParseSerialize (AmountStr <$> qstr) input (Right (AmountStr expect)) expects

> cmpQuantity a b = cmpQuantity3 a b a
> cmpQuantity3 = cmpParseSerialize quantity

> cmpInstruction a b = cmpInstruction3 a b a
> cmpInstruction3 = cmpParseSerialize instruction

> exactQuantity a b c = Right (Quantity (Exact a) b c)
> strQuantity = Quantity (Exact (AmountStr "")) ""

> test = [
> 	  "quantity" ~: testQuantityOverloaded
> 			++ testQuantityApprox
> 			++ testQuantityAmount
> 			++ testQuantityRatio
> 	, "qstr" ~: testQstr
> 	, "oparg" ~: testOparg
> 	, "opterm" ~: testOpterm
> 	]

> isResult (Result _) = True
> isResult _ = False
> isReference (Reference _) = True
> isReference _ = False
> isAlternative (Alternative _) = True
> isAlternative _ = False
> isAnnotation (Annotation _) = True
> isAnnotation _ = False
> isAction (Action _) = True
> isAction _ = False
> isDirective (Directive _) = True
> isDirective _ = False
> isUnknown (Unknown _) = True
> isUnknown _ = False