-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathParser.y
143 lines (114 loc) · 6 KB
/
Parser.y
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
{
{-# LANGUAGE Trustworthy #-}
{-|
Module : TOML.Parser
Description : /Internal:/ Parser for TOML generated by Happy
Copyright : (c) Eric Mertens, 2017
License : ISC
Maintainer : emertens@gmail.com
Parser for TOML generated by Happy.
-}
module TOML.Parser (parseComponents) where
import Data.Text (Text,pack)
import TOML.Components
import TOML.Errors
import TOML.Located
import TOML.Tokens
import TOML.Value
}
%tokentype { Located Token }
%token
STRING { Located _ (StringToken $$) }
BAREKEY { Located _ (BareKeyToken $$) }
INTEGER { Located _ (IntegerToken $$) }
DOUBLE { Located _ (DoubleToken $$) }
'true' { Located _ TrueToken }
'false' { Located _ FalseToken }
'[' { $$@(Located _ LeftBracketToken)}
']' { Located _ RightBracketToken }
'{' { $$@(Located _ LeftBraceToken) }
'}' { Located _ RightBraceToken }
',' { Located _ CommaToken }
'.' { Located _ PeriodToken }
'=' { Located _ EqualToken }
ZONEDTIME { Located _ (ZonedTimeToken $$) }
LOCALTIME { Located _ (LocalTimeToken $$) }
TIMEOFDAY { Located _ (TimeOfDayToken $$) }
DAY { Located _ (DayToken $$) }
EOF { Located _ EofToken }
%monad { Either TOMLError }
%error { errorP }
-- | Attempt to parse a layout annotated token stream or
-- the token that caused the parse to fail.
%name components
%%
components :: { [Component] }
: componentsR EOF { reverse $1 }
componentsR :: { [Component] }
: keyvalues { [InitialEntry $1] }
| componentsR component keyvalues { $2 $3 : $1 }
component :: { [(Text,Value)] -> Component }
: '[' keys ']' { TableEntry $2 }
| '[' '[' keys ']' ']' { ArrayEntry $3 }
| '[' keys error {% unterminated $1 }
| '[' '[' keys error {% unterminated $2 }
| '[' '[' keys ']' error {% unterminated $1 }
keyvalues :: { [(Text,Value)] }
: keyvaluesR { reverse $1 }
keyvaluesR :: { [(Text,Value)] }
: { [] }
| keyvaluesR key '=' value { ($2,$4):$1 }
keys :: { [Text] }
: keysR { reverse $1 }
keysR :: { [Text] }
: key { [$1] }
| keysR '.' key { $3 : $1 }
key :: { Text }
: BAREKEY { $1 }
| STRING { $1 }
| INTEGER { pack (show $1) }
| 'true' { pack "true" }
| 'false' { pack "false" }
value :: { Value }
: INTEGER { Integer $1 }
| DOUBLE { Double $1 }
| STRING { String $1 }
| ZONEDTIME { ZonedTimeV $1 }
| TIMEOFDAY { TimeOfDayV $1 }
| DAY { DayV $1 }
| LOCALTIME { LocalTimeV $1 }
| 'true' { Bool True }
| 'false' { Bool False }
| '{' inlinetable '}' { Table $2 }
| '[' inlinearray ']' { List $2 }
| '{' inlinetable error {% unterminated $1 }
| '[' inlinearray error {% unterminated $1 }
inlinetable :: { [(Text,Value)] }
: { [] }
| inlinetableR { reverse $1 }
inlinetableR :: { [(Text,Value)] }
: key '=' value { [($1,$3)] }
| inlinetableR ',' key '=' value
{ ($3,$5):$1 }
inlinearray :: { [Value] }
: { [] }
| inlinearrayR { reverse $1 }
| inlinearrayR ',' { reverse $1 }
inlinearrayR :: { [Value] }
: value { [$1] }
| inlinearrayR ',' value { $3 : $1 }
{
-- | This operation is called by happy when no production matches the
-- current token list.
errorP :: [Located Token] {- ^ nonempty remainig tokens -} -> Either TOMLError a
errorP = Left . Unexpected . head
-- | Attempt to parse a layout annotated token stream or
-- the token that caused the parse to fail.
parseComponents ::
[Located Token] {- ^ layout annotated token stream -} ->
Either TOMLError [Component] {- ^ token at failure or result -}
parseComponents = components
-- | Abort the parse with an error indicating that the given token was unmatched.
unterminated :: Located Token -> Either TOMLError a
unterminated = Left . Unterminated
}