Debian Codegraph @

importers/ding2tei/src/Language/Ding/Parser/Line.y

raw
1 {-
2 - Language/Ding/Parser/Line.y - parser for a single Ding line
3 -
4 - Copyright 2020-2022 Einhard Leichtfuß
5 -
6 - This file is part of ding2tei-haskell.
7 -
8 - ding2tei-haskell is free software: you can redistribute it and/or modify
9 - it under the terms of the GNU Affero General Public License as published
10 - by the Free Software Foundation, either version 3 of the License, or
11 - (at your option) any later version.
12 -
13 - ding2tei-haskell is distributed in the hope that it will be useful,
14 - but WITHOUT ANY WARRANTY; without even the implied warranty of
15 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 - GNU Affero General Public License for more details.
17 -
18 - You should have received a copy of the GNU Affero General Public License
19 - along with ding2tei-haskell. If not, see <https://www.gnu.org/licenses/>.
20 -}
21
22
23 -- Notes:
24 -- * This file is supposed to be processed by Happy. It contains both Happy
25 -- specific syntax and regular Haskell code. The former is in some ways
26 -- similar to the latter. The result of processing with Happy is a regular
27 -- Haskell source file.
28 -- * The Happy syntax should be very similar to the syntax of Yacc/Bison.
29 -- * There is an important relation between the lexer (produced with the help
30 -- of Alex) and the parser.
31 -- * Happy Documentation: https://www.haskell.org/happy/
32
33
34 {
35 -- Haskell module header and import statements.
36
37 {-# LANGUAGE FlexibleInstances #-}
38
39 {-|
40 - Parse single Ding lines from a list of tokens. Only accepts newline
41 - terminated lines.
42 -}
43 module Language.Ding.Parser.Line (parseLine, FailWriter) where
44
45 import Prelude hiding (fail)
46 import Control.Monad.Fail (MonadFail(fail))
47 import Control.Monad.Writer
48 (Writer, tell, writer, WriterT(WriterT), mapWriterT)
49 import Data.List.NonEmpty (NonEmpty(..), (<|))
50 import qualified Data.List.NonEmpty as NEList
51 import Safe.Exact (zipWithExactMay)
52
53 import Data.NatLang.Grammar
54 ( GrammarInfo(..)
55 , GramLexCategory(..)
56 , Case(..)
57 , Collocate(..)
58 )
59 import Data.NatLang.Usage (Usage)
60 import Data.NatLang.InflectedForms (InflectedForms(..), InflectedForm(..))
61 import Language.Ding.Partial.Unit (PartialUnit)
62 import qualified Language.Ding.Partial.Unit as PU
63 import Language.Ding.Partial.PseudoUnit (PartialPseudoUnit)
64 import qualified Language.Ding.Partial.PseudoUnit as PSU
65 import Language.Ding.Read.Usage (readUsage)
66 import Language.Ding.Syntax
67 import Language.Ding.Token
68 (Token(..), Atom(..), Position(..), tokenToString, tokenToLine)
69 }
70
71
72 %name parseLine -- name of the resulting function
73 %tokentype { Token }
74 %monad { FailWriter }
75 %error { parseError } -- name of the error function - must be defined later
76
77
78 -------------------------------------------------------------------------------
79 -- Link Happy tokens (left) to Haskell patterns (right).
80
81 -- Notes:
82 -- * If `$$' is specified on the right, the Happy token will evaluate to the
83 -- token's part matched by `$$'. Otherwise, the Happy token evaluates to
84 -- the whole token.
85 -- * It is common for simple tokens to use as name the string representing the
86 -- token, however not at all necessary.
87
88 %token '\n' { Token _ _ NL }
89 '::' { Token _ _ LangSep }
90 '|' { Token _ _ Vert }
91 ';' { Token _ _ Semi }
92 ',' { Token _ _ Comma }
93 '~' { Token _ _ Tilde }
94 '+' { Token _ _ Plus }
95 '<>' { Token _ _ Wordswitch }
96 '/' { Token _ _ StrongSlash }
97 '</>' { Token _ _ WeakSlash }
98 '//' { Token _ _ DoubleSlash }
99
100 '{' { Token _ _ OBrace }
101 '}' { Token _ _ CBrace }
102 '[' { Token _ _ OBracket }
103 ']' { Token _ _ CBracket }
104 '(' { Token _ _ OParen }
105 ')' { Token _ _ CParen }
106 '<' { Token _ _ OAngle }
107 '>' { Token _ _ CAngle }
108 '</' { Token _ _ OSlash }
109 '/>' { Token _ _ CSlash }
110
111 'to' { Token _ _ KW_to }
112
113 tok_slashSpecial { Token _ _ (SlashSpecial _) }
114 tok_abbrev { Token _ _ (Abbrev _) }
115 tok_abbrevPlural { Token _ _ (AbbrevPlural _) }
116
117 -- Note:
118 -- * The grammar annotations are separated because gramCase needs to be
119 -- separately available (and Happy tokens may not overlap).
120 tok_gramPOS { Token _ _ (GramKW (PartOfSpeech _)) }
121 tok_gramGender { Token _ _ (GramKW (Gender _)) }
122 tok_gramNumber { Token _ _ (GramKW (Number _)) }
123 tok_gramCase { Token _ _ (GramKW (Case _)) }
124
125 tok_interrogPron { Token _ _ (IntPronKW _) }
126 tok_text { Token _ _ (Text _) }
127
128
129
130 -- Just ensure that 'to' has lowest precedence.
131 -- * Giving it right associativity causes 'to to' to cause a shift.
132 %right 'to'
133 %right ',' '~' '+' '<>' '/' '</>' '//' tok_gramPOS tok_gramGender
134 tok_gramNumber tok_gramCase tok_interrogPron tok_text
135 '('
136
137 %%
138
139
140 -------------------------------------------------------------------------------
141 -- Grammar specification
142 -------------------------------------------------------------------------------
143
144 -- See doc/ding-grammar-naming for a brief syntax overview and naming
145 -- conventions.
146
147 -- Notes:
148 -- * Only a single line is parsed.
149 -- * Left recursion is recommended by the Happy developers, due to space
150 -- requirements.
151 -- * This causes lists to be reversed, which requires re-reversal (or
152 -- special alternative list structures), which makes the grammar often
153 -- less readable.
154 -- * Since all lists are expected to be small, space should not be an issue.
155 -- * Hence, ignore this.
156 -- - https://www.haskell.org/happy/doc/html/sec-sequences.html
157 -- - String-concatenation that uses left-recursion has the same problem.
158 -- - (<>), when applied to tokens does essentially perform string
159 -- concatentation.
160 -- - mconcat can be used instead.
161 -- * The parsing happens in the Writer monad to allow for logging.
162 -- * There does however not happen any logging as of now.
163 -- * The syntax would be the same without a monad; only the type of the
164 -- parseLine function would be simpler.
165
166
167 -- Note:
168 -- * The newline token serves as EOL token, such that there is never an error
169 -- on end of tokens.
170 -- * (One could also have given this parser a non-newline terminated stream
171 -- of tokens.)
172 -- * End-of-tokens errors do not report the position of the error, which
173 -- usually is not a problem, since it is just the end of input.
174 -- * However, since we only parse single lines, where each contained
175 -- token is annotated with a line number, this would be a problem.
176 line :: { Line }
177 line : groups '::' groups '\n' {% makeLine (tokenToLine $2) $1 $3 }
178
179 groups :: { [Group] }
180 groups : group '|' groups { $1 : $3 }
181 | group { $1 : [] }
182
183 -- Note:
184 -- * In some cases, there is exactly one unit in a group, which does not obey
185 -- to the rules of a proper unit - the headword is enclosed in <()>.
186 -- * It does however (in practice) obey the rules of a PartialPseudoUnit.
187 -- * Use these, to ignore the unit. (TODO: improve)
188 -- * See also: todo/parsing.paren-units
189 group :: { Group }
190 group : units { Group $ reverse $1 }
191 | {- epsilon -} { Group [] }
192 | pseudoUnit { Group [] }
193
194 -- Notes:
195 -- * Produces the list of units in reverse order.
196 -- * This is to ease the application of `addToUnit'.
197 -- * Only accepts a non-empty list of units. (because of the <;> seps)
198 units :: { [Unit] }
199 units : units ';' unit { $3 : $1 }
200 | units ';' pseudoUnit { map (PSU.addToUnit $3) $1 }
201 | unit { $1 : [] }
202
203
204 -------------------------------------------------------------------------------
205 -- Unit
206
207 unit :: { Unit }
208 unit : parenExps partialUnit { PU.toUnit $1 $2 }
209
210 -- TODO: Do not ignore <>.
211 pseudoUnit :: { PartialPseudoUnit }
212 pseudoUnit : pseudoUnit gramAnnot { $1 `PSU.plusGramAnnot` $2 }
213 | pseudoUnit usageAnnot { $1 `PSU.plusUsageAnnot` $2 }
214 | pseudoUnit angleExp { $1 }
215 | parenExp parenExps { PSU.fromSuffixes ($1 : $2) }
216
217 -- Notes:
218 -- * Any infix annotations are silently dropped (see plusToken). Prefix
219 -- annotations are considered invalid and not accepted by the grammar.
220 -- * Parenthesis expressions may occur as prefixes though, they are
221 -- recognized in the `unit' rule.
222 -- * Initial abbreviations are equated to regular text, iff they are "single",
223 -- that is contain no <,> or <;> separated list of abbeviations.
224 -- * If prefixed by 'to', ignore the 'to' and mark as verb.
225 -- * This only happens if 'to' is immediately followed by something
226 -- recognized as regular text (unitText).
227 -- * In particular things like 'to (have a) lisp' or
228 -- 'to (give one's) consent (to)' are not specially treated.
229 -- (They are quite infrequent though.)
230 -- * Also, allowing () would complicate the parsing.
231 -- * PU.fromVerbToken annotates the unit as a verb. This may be superfluous
232 -- if there is an explicit annotation (rare), but such a duplicate would
233 -- be resolved during enrichment.
234 -- * angleExp ("<...>") is ignored. (TODO?)
235 -- * Semantics unclear.
236 partialUnit :: { PartialUnit }
237 partialUnit : partialUnit gramAnnot { $1 `PU.plusGramAnnot` $2 }
238 | partialUnit usageAnnot { $1 `PU.plusUsageAnnot` $2 }
239 | partialUnit abbrevAnnot { $1 `PU.plusAbbrevAnnot` $2 }
240 | partialUnit inflAnnot { $1 `PU.plusInflAnnot` $2 }
241 | partialUnit parenExp { $1 `PU.plusSuffix` $2 }
242 | partialUnit angleExp { $1 {- ignore $2 -} }
243 | partialUnit reference { $1 `PU.plusRef` $2 }
244 | partialUnit unitText { $1 `PU.plusToken` $2 }
245 | partialUnit 'to' { $1 `PU.plusToken` $2 }
246 | partialUnit unitInterpct { $1 `PU.plusToken` $2 }
247 | unitText { PU.fromToken $1 }
248
249 -- 'to' has lowest precedence (and is right associative), so the
250 -- later rules will be preferred.
251 | 'to' { PU.fromToken $1 }
252 | 'to' unitInterpct { PU.fromToken ($1 <> $2) }
253 | 'to' unitText { PU.fromVerbToken $2 }
254
255 -- Some units start with an abbreviation (usually without further
256 -- unitText). In such, treat the abbreviation as plain text.
257 | singleAbbrevAnnot { PU.fromToken $1 }
258
259 -- Notes:
260 -- * As of now, keywords (except for 'to') have no special meaning in
261 -- top-level text. Hence, treat them as simple text.
262 -- * Cannot use gtok_anyKW, since it includes 'to'.
263 unitText :: { Token }
264 unitText : tok_text { $1 }
265 | gtok_gram { $1 }
266 | tok_interrogPron { $1 }
267
268 -- | Interpunctuation in units.
269 --
270 -- Note: "<>" is ignored.
271 unitInterpct :: { Token }
272 unitInterpct : ',' { $1 }
273 | '+' { $1 }
274 | '//' { $1 }
275 | '/' { $1 }
276 | '</>' { $1 }
277 | '<>' { mempty }
278
279
280 -------------------------------------------------------------------------------
281 -- Grammar annotation
282
283 gramAnnot :: { NonEmpty GrammarInfo }
284 gramAnnot : '{' gramAnnot1s '}' { $2 }
285
286 -- Note: <;> and <,> are used with different meanings in this context
287 -- (usually AND and OR, respectively), but to keep things simple, they
288 -- are considered equivalent as of now.
289 gramAnnot1s :: { NonEmpty GrammarInfo }
290 gramAnnot1s : gramAnnot1 ';' gramAnnot1s { $1 <> $3 }
291 | gramAnnot1 { $1 }
292
293 gramAnnot1 :: { NonEmpty GrammarInfo }
294 gramAnnot1 : gramAnnot2s { $1 }
295 | collocAnnot1 usageAnnots { Collocate $1 $2 :| [] }
296
297 -- The two rules for CollocCase are necessary to avoid a shift/reduce conflict.
298 -- The first rule only allows a non-empty list of interrogation pronouns.
299 collocAnnot1 :: { Collocate }
300 collocAnnot1 : interrogProns
301 '+' tok_gramCase { CollocCase $1 (tokenToCase $3) }
302 | '+' tok_gramCase { CollocCase [] (tokenToCase $2) }
303 | '+' tok_gramPOS { CollocPOS (tokenToPOS $2) }
304 | '+' tok_gramNumber { CollocNumber (tokenToNumber $2) }
305
306 -- TODO: Consider to add '/' as alternative separator.
307 gramAnnot2s :: { NonEmpty GrammarInfo }
308 gramAnnot2s : gramAnnot2 ',' gramAnnot2s { $1 <| $3 }
309 | gramAnnot2 { $1 :| [] }
310
311 gramAnnot2 :: { GrammarInfo }
312 gramAnnot2 : gramLexCat { GramLexCategory $1 }
313
314 gramLexCat :: { GramLexCategory }
315 gramLexCat : gtok_gram { tokenToGramLexCat $1 }
316
317 interrogProns :: { [String] }
318 interrogProns : tok_interrogPron ',' interrogProns { tokenToString $1 : $3 }
319 | tok_interrogPron { tokenToString $1 : [] }
320
321
322 -------------------------------------------------------------------------------
323 -- Inflected forms
324
325 -- Notes:
326 -- * Inflected forms are distinguished from grammar annotations by the
327 -- absence of grammar keywords.
328 -- * NonEmpty lists are used here. (:|) constructs a NonEmpty list from a
329 -- single head element and a regular list. (<|) joins a single element with
330 -- a NonEmpty list, just like (:) for normal lists.
331 -- * Considering the implementation of NonEmpty, using (<|) is not very
332 -- efficient (decons (:|), cons (:), cons (:|) - instead of just
333 -- cons (:)).
334 inflAnnot :: { InflectedForms }
335 inflAnnot : '{' inflAnnot1s ';' inflAnnot1s '}' { InflectedForms $2 $4 }
336
337 inflAnnot1s :: { NonEmpty InflectedForm }
338 inflAnnot1s : inflAnnot1 ',' inflAnnot1s { $1 <| $3 }
339 | inflAnnot1 { $1 :| [] }
340
341 inflAnnot1 :: { InflectedForm }
342 inflAnnot1 : inflAnnot2 usageAnnots { InflectedForm (tokenToString $1) $2 }
343
344 -- Notes:
345 -- * Most inflected forms consist of a single word. Not all
346 -- (e.g., "creeped out").
347 -- * Cannot use gtok_anyKW, as it includes gtok_gramKW.
348 inflAnnot2 :: { Token }
349 inflAnnot2 : tok_text inflAnnot2 { $1 <> $2 }
350 | tok_text { $1 }
351 | tok_interrogPron { $1 }
352 | 'to' { $1 }
353
354
355 -------------------------------------------------------------------------------
356 -- Abbreviation annotations
357
358 -- Note: This rule exists purely to provide a prefix for unit.
359 singleAbbrevAnnot :: { Token }
360 singleAbbrevAnnot : '</' abbrevAnnot1 '/>' { $2 }
361 | tok_abbrev { $1 }
362 | tok_abbrevPlural { $1 }
363 | tok_slashSpecial { $1 }
364
365 abbrevAnnot :: { NonEmpty String }
366 abbrevAnnot : '</' abbrevAnnot1s '/>' { $2 }
367 | tok_abbrev { tokenToString $1 :| [] }
368 | tok_abbrevPlural { (tokenToString $1 ++ "s") :| [] }
369 | tok_slashSpecial { tokenToString $1 :| [] }
370
371 -- Note: <;> is more frequent.
372 abbrevAnnot1s :: { NonEmpty String }
373 abbrevAnnot1s : abbrevAnnot1 ';' abbrevAnnot1s { tokenToString $1 <| $3 }
374 | abbrevAnnot1 ',' abbrevAnnot1s { tokenToString $1 <| $3 }
375 | abbrevAnnot1 { tokenToString $1 :| [] }
376
377 -- Consider content a literal (in particular do not drop any inner annotation).
378 abbrevAnnot1 :: { Token }
379 abbrevAnnot1 : abbrevAnnot2 abbrevAnnot1 { $1 <> $2 }
380 | abbrevAnnot2 { $1 }
381
382 -- Note:
383 -- * <()>, <[]> are rare here.
384 -- . Ex.: "/Hg(CNO)2/"
385 -- . Ex.: "section 15, subsection 3 /s.15[3]/"
386 -- * Nesting likely does not occur at all, but is considered valid.
387 abbrevAnnot2 :: { Token }
388 abbrevAnnot2 : tok_text { $1 }
389 | gtok_anyKW { $1 }
390 | '+' { $1 }
391 | '/' { $1 }
392 | '(' abbrevAnnot2 ')' { $1 <> $2 <> $3 }
393 | '[' abbrevAnnot2 ']' { $1 <> $2 <> $3 }
394
395
396 -------------------------------------------------------------------------------
397 -- Expressions enclosed in parentheses
398
399 -- Note:
400 -- * The below rules give (String, Token) for a parenthesis expression.
401 -- The string is the content, the token joins the content with the
402 -- parentheses (and retains any spacing).
403 -- * The token is required to allow for infix parenthesis expressions to be
404 -- merged back with its context.
405
406 parenExps :: { [(String, Token)] }
407 parenExps : parenExp parenExps { $1 : $2 }
408 | {- epsilon -} { [] }
409
410 parenExp :: { (String, Token) }
411 parenExp : '(' parenExp1 ')' { (tokenToString $2, $1 <> $2 <> $3) }
412
413 parenExp1 :: { Token }
414 parenExp1 : parenExp2 parenExp1 { $1 <> $2 }
415 parenExp1 : parenExp2 { $1 }
416
417 -- Notes:
418 -- * Several inner annotations are ignored (on purpose), such as references or
419 -- <{}>-annotations.
420 -- * Nonetheless, for example <{}>-annotations should not be simplified
421 -- (allowed to contain more or less anything) - the content should still
422 -- be a valid annotation. I.e., use the grammar rules for those
423 -- annotations.
424 -- * Nested <()>-annotations are included literally.
425 -- * They are rare though. (Hence the nested (<>) application is ok.)
426 -- * One might consider to include abbreviations literally. (TODO?)
427 -- * This would require an abbrevAnnot'-rule, producing a token instead of
428 -- a list of Strings.
429 -- * Alternatively, one might reconstruct an equivalent representation,
430 -- based on that list.
431 parenExp2 :: { Token }
432 parenExp2 : tok_text { $1 }
433 | gtok_anyKW { $1 }
434 | reference { mempty }
435 | gramAnnot { mempty }
436 | inflAnnot { mempty }
437 | usageAnnot { mempty }
438 | abbrevAnnot { mempty }
439 | '(' parenExp1 ')' { $1 <> $2 <> $3 }
440 | ';' { $1 }
441 | ',' { $1 }
442 | '+' { $1 }
443 | '<>' { mempty }
444 | '/' { $1 }
445 | '</>' { $1 }
446 | '//' { $1 }
447
448
449 -------------------------------------------------------------------------------
450 -- Expressions enclosed in angle brackets
451
452 -- Note: They are dropped as of now, since their semantics are ambiguous.
453
454 angleExp :: { NonEmpty String }
455 angleExp : '<' angleExp1s '>' { $2 }
456
457 -- Note:
458 -- * Separation by <;>, <,> is rare (two/one occurrences).
459 angleExp1s :: { NonEmpty String }
460 angleExp1s : angleExp1 ';' angleExp1s { tokenToString $1 <| $3 }
461 | angleExp1 ',' angleExp1s { tokenToString $1 <| $3 }
462 | angleExp1 { tokenToString $1 :| [] }
463
464 angleExp1 :: { Token }
465 angleExp1 : angleExp2 angleExp1 { $1 <> $2 }
466 | angleExp2 { $1 }
467
468 -- Notes:
469 -- * There are nested angle-expressions. It is unclear how to handle them.
470 -- Drop for now. (TODO?)
471 -- * They are rare (two occurences).
472 -- * Only one occurence of nested <()>. Dropped for now. (TODO?)
473 -- * Only 4 occurrences of nested abbreviations. Dropped for now. (TODO?)
474 angleExp2 :: { Token }
475 angleExp2 : tok_text { $1 }
476 | gtok_anyKW { $1 }
477 | angleExp { mempty }
478 | '(' angleExp1 ')' { mempty }
479 | abbrevAnnot { mempty }
480
481
482 -------------------------------------------------------------------------------
483 -- References ("~ref")
484
485 -- Note:
486 -- * Due to the simple current word-matching in the lexer, this might catch
487 -- unexpected references, such as "~word?". (TODO)
488 reference :: { String }
489 reference : '~' reference1 { tokenToString $2 }
490
491 reference1 :: { Token }
492 reference1 : tok_text { $1 }
493 | gtok_anyKW { $1 }
494
495
496 -------------------------------------------------------------------------------
497 -- Usage annotations
498
499 -- Zero or more <[]>-enclosed usage annotations.
500 -- Unified to a single list (e.g., "[a,b] [c]" -> [a,b,c]).
501 usageAnnots :: { [Usage] }
502 usageAnnots : usageAnnot usageAnnots { NEList.toList $1 ++ $2 }
503 | {- epsilon -} { [] }
504
505 -- A single usage annotation (e.g., "[a,b]").
506 usageAnnot :: { NonEmpty Usage }
507 usageAnnot : '[' usageAnnot1s ']' { $2 }
508
509 -- Note: <,> and </> are treated as synonyms. They possibly shouldn't.
510 usageAnnot1s :: { NonEmpty Usage }
511 usageAnnot1s : usageAnnot1 ',' usageAnnot1s { $1 <| $3 }
512 | usageAnnot1 '/' usageAnnot1s { $1 <| $3 }
513 | usageAnnot1 { $1 :| [] }
514
515 usageAnnot1 :: { Usage }
516 usageAnnot1 : usageAnnot2s { readUsage $ tokenToString $1 }
517
518 usageAnnot2s :: { Token }
519 usageAnnot2s : usageAnnot2 usageAnnot2s { $1 <> $2 }
520 | usageAnnot2 { $1 }
521
522 usageAnnot2 :: { Token }
523 usageAnnot2 : tok_text { $1 }
524 | gtok_anyKW { $1 }
525 | ';' { $1 }
526 | '+' { $1 }
527
528
529 -------------------------------------------------------------------------------
530 -- Grouped simple tokens
531
532 gtok_anyKW :: { Token }
533 gtok_anyKW : gtok_gram { $1 }
534 | tok_interrogPron { $1 }
535 | 'to' { $1 }
536
537 gtok_gram :: { Token }
538 gtok_gram : tok_gramPOS { $1 }
539 | tok_gramGender { $1 }
540 | tok_gramNumber { $1 }
541 | tok_gramCase { $1 }
542
543 {
544
545 -------------------------------------------------------------------------------
546 -- Auxiliary Haskell code
547
548
549 -- Logging:
550 -- * The FailWriter below allows for logging of parse notes and errors
551 -- (`fail').
552 -- - Errors indicate a failure and, so, no value is returned.
553 -- * `fail' should always be preferred over `error', unless the respective
554 -- condition indicates a programming error.
555 -- * The filename should not and cannot be given here; it is prepended at a
556 -- higher level.
557
558
559 -- | A writer that allows logging informative messages, but also to fail with
560 -- an error message.
561 -- The informative log is retained in case of a failure.
562 type FailWriter = WriterT [String] (Either String)
563
564 instance MonadFail (Either String) where
565 fail = Left
566
567
568 -- Note: The token list can only be empty if the token list given to
569 -- `parseLine' was not terminated by a newline token. Which should not
570 -- be possible (an error would occur on a higher level).
571 -- - The first error is thus deliberately handled by `error' and not `fail'.
572 parseError :: [Token] -> FailWriter a
573 parseError [] = error "Parse error at end of line (missing newline?)."
574 parseError ((Token _ (Position line col) _):_)
575 = fail
576 $ "Parse error at line " ++ show line ++ ", column " ++ show col ++ "."
577
578
579 -- | Create a line from the left and right side's groups. Fail, if the groups
580 -- are not equal in number of entries.
581 makeLine :: Int -> [Group] -> [Group] -> FailWriter Line
582 makeLine line gs hs =
583 case zipWithExactMay Entry gs hs of
584 Just es -> return $ Line es
585 Nothing -> fail
586 $ "Line " ++ show line ++ ": "
587 ++ "Number of groups does not match on the two sides."
588
589
590 tokenToGramLexCat :: Token -> GramLexCategory
591 tokenToGramLexCat (Token _ _ (GramKW gram)) = gram
592 tokenToGramLexCat _ =
593 error "Not a grammar keyword."
594
595 tokenToGramAnnot :: Token -> GrammarInfo
596 tokenToGramAnnot (Token _ _ (GramKW gram)) = GramLexCategory gram
597 tokenToGramAnnot _ =
598 error "Not a grammar annotation."
599
600 tokenToCase :: Token -> Case
601 tokenToCase (Token _ _ (GramKW (Case cas))) = cas
602 tokenToCase _ =
603 error "Not a case keyword."
604
605 tokenToPOS (Token _ _ (GramKW (PartOfSpeech pos))) = pos
606 tokenToPOS _ =
607 error "Not a part of speech keyword."
608
609 tokenToNumber (Token _ _ (GramKW (Number number))) = number
610 tokenToNumber _ =
611 error "Not a grammatical number keyword."
612
613 }
614
615
616 -- vi: ft=haskell ts=2 sw=2 et