@@ -25,7 +25,7 @@ import Distribution.Parsec.Lexer
25
25
import Distribution.Parsec.LexerMonad (unLex , LexState (.. ), LexResult (.. ), Position (.. ), LexWarning )
26
26
27
27
import Text.Parsec.Prim
28
- import Text.Parsec.Combinator hiding (eof )
28
+ import Text.Parsec.Combinator hiding (eof , notFollowedBy )
29
29
import Text.Parsec.Pos
30
30
import Text.Parsec.Error
31
31
@@ -90,15 +90,13 @@ describeToken t = case t of
90
90
EOF -> " end of file"
91
91
LexicalError is -> " character in input " ++ show (B. head is)
92
92
93
- -- tokName, tokStr, tokNum, tokOther, tokFieldLine :: Parser String
94
93
tokName :: Parser (Name Position )
95
- -- tokNum, tokOther
96
- -- tokStr :: Parser String
97
- tokIndent :: Parser Int
98
- tokColon, tokOpenBrace,
99
- tokCloseBrace :: Parser ()
94
+ tokName' , tokStr , tokNum , tokOther :: Parser (SectionArg Position )
95
+ tokIndent :: Parser Int
96
+ tokColon , tokOpenBrace , tokCloseBrace :: Parser ()
97
+ tokFieldLine :: Parser (FieldLine Position )
100
98
101
- tokName = getTokenWithPos $ \ t -> case t of L pos (TokSym x) -> Just (name pos x); _ -> Nothing
99
+ tokName = getTokenWithPos $ \ t -> case t of L pos (TokSym x) -> Just (mkName pos x); _ -> Nothing
102
100
tokName' = getTokenWithPos $ \ t -> case t of L pos (TokSym x) -> Just (SecArgName pos x); _ -> Nothing
103
101
tokStr = getTokenWithPos $ \ t -> case t of L pos (TokStr x) -> Just (SecArgStr pos x); _ -> Nothing
104
102
tokNum = getTokenWithPos $ \ t -> case t of L pos (TokNum x) -> Just (SecArgNum pos x); _ -> Nothing
@@ -109,16 +107,20 @@ tokOpenBrace = getToken $ \t -> case t of OpenBrace -> Just (); _ -> Nothing
109
107
tokCloseBrace = getToken $ \ t -> case t of CloseBrace -> Just () ; _ -> Nothing
110
108
tokFieldLine = getTokenWithPos $ \ t -> case t of L pos (TokFieldLine s) -> Just (FieldLine pos s); _ -> Nothing
111
109
112
- -- sectionName, sectionArg, fieldSecName, fieldContent :: Parser String
113
110
colon , openBrace , closeBrace :: Parser ()
114
111
115
- sectionName = tokName <?> " section name "
112
+ sectionArg :: Parser ( SectionArg Position )
116
113
sectionArg = tokName' <|> tokStr
117
114
<|> tokNum <|> tokOther <?> " section parameter"
115
+
116
+ fieldSecName :: Parser (Name Position )
118
117
fieldSecName = tokName <?> " field or section name"
118
+
119
119
colon = tokColon <?> " \" :\" "
120
120
openBrace = tokOpenBrace <?> " \" {\" "
121
121
closeBrace = tokCloseBrace <?> " \" }\" "
122
+
123
+ fieldContent :: Parser (FieldLine Position )
122
124
fieldContent = tokFieldLine <?> " field contents"
123
125
124
126
newtype IndentLevel = IndentLevel Int
@@ -150,8 +152,8 @@ data Field ann = Field !(Name ann) [FieldLine ann]
150
152
data Name ann = Name ! ann ! ByteString
151
153
deriving (Eq , Show , Functor )
152
154
153
- name :: ann -> ByteString -> Name ann
154
- name ann bs = Name ann (B. map Char. toLower bs)
155
+ mkName :: ann -> ByteString -> Name ann
156
+ mkName ann bs = Name ann (B. map Char. toLower bs)
155
157
156
158
getName :: Name a -> ByteString
157
159
getName (Name _ bs) = bs
@@ -240,9 +242,9 @@ elements ilevel = many (element ilevel)
240
242
element :: IndentLevel -> Parser (Field Position )
241
243
element ilevel =
242
244
(do ilevel' <- indentOfAtLeast ilevel
243
- name <- tokName
245
+ name <- fieldSecName
244
246
elementInLayoutContext (incIndentLevel ilevel') name)
245
- <|> (do name <- tokName
247
+ <|> (do name <- fieldSecName
246
248
elementInNonLayoutContext name)
247
249
248
250
-- An element (field or section) that is valid in a layout context.
@@ -382,6 +384,8 @@ eof = notFollowedBy anyToken <?> "end of file"
382
384
elaborate :: Show a => [Field a ] -> [Field a ]
383
385
elaborate [] = []
384
386
elaborate (field@ Field {} : rest) = field : elaborate rest
387
+ elaborate (IfElseBlock args t e : rest) =
388
+ IfElseBlock args (elaborate t) (elaborate e) : elaborate rest
385
389
elaborate (Section name args fields : Section ename [] efields : rest)
386
390
| getName name == " if" && getName ename == " else" =
387
391
IfElseBlock args (elaborate fields) (elaborate efields) : elaborate rest
0 commit comments