Skip to content

Commit

Permalink
Allow one-line grouping using "." in idents
Browse files Browse the repository at this point in the history
Fixes bos#19
  • Loading branch information
dylex committed May 22, 2015
1 parent ff6d476 commit 93e0e3e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist/
25 changes: 19 additions & 6 deletions Data/Configurator/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ topLevel = directives <* skipLWS <* endOfInput

directive :: Parser Directive
directive =
mconcat [
string "import" *> skipLWS *> (Import <$> string_)
, Bind <$> try (ident <* skipLWS <* char '=' <* skipLWS) <*> value
, Group <$> try (ident <* skipLWS <* char '{' <* skipLWS)
<*> directives <* skipLWS <* char '}'
]
string "import" *> skipLWS *> (Import <$> string_)
<|> do
ids <- idents
skipLWS
groupIdents (\n -> bind n <|> group n) ids

directives :: Parser [Directive]
directives = (skipLWS *> directive <* skipHWS) `sepBy`
Expand Down Expand Up @@ -77,6 +76,14 @@ ident = do
where
isCont c = isAlphaNum c || c == '_' || c == '-'

idents :: Parser [Name]
idents = sepBy1 ident (char '.')

groupIdents :: (Name -> Parser Directive) -> [Name] -> Parser Directive
groupIdents p [n] = p n
groupIdents p (g:n) = Group g . return <$> groupIdents p n
groupIdents p [] = p (error "empty ident")

value :: Parser Value
value = mconcat [
string "on" *> pure (Bool True)
Expand All @@ -89,6 +96,12 @@ value = mconcat [
((value <* skipLWS) `sepBy` (char ',' <* skipLWS))
]

bind :: Name -> Parser Directive
bind n = char '=' >> skipLWS >> Bind n <$> value

group :: Name -> Parser Directive
group n = brackets '{' '}' $ Group n <$> directives <* skipLWS

string_ :: Parser Text
string_ = do
s <- char '"' *> scan False isChar <* char '"'
Expand Down
6 changes: 2 additions & 4 deletions tests/resources/interp.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services = "$(HOME)/services"
root = "can be overwritten by inner block."
myprogram.name="myprogram"
myprogram {
name = "myprogram"
root = "$(services)/$(name)"
exec = "$(root)/$(name)"
stdout = "$(root)/stdout"
Expand All @@ -13,8 +13,6 @@ top {
dir = "$(dir)/top"
layer1 {
dir = "$(dir)/layer1"
layer2 {
dir = "$(dir)/layer2"
}
layer2.dir ="$(dir)/layer2"
}
}
4 changes: 3 additions & 1 deletion tests/resources/pathological.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ af
#baz
]#quux

ag { q-e { i_u9 { a=false}}}
ag { q-e { i_u9 { a=true}}}

ba = "$(HOME)"

xs = [1,2,3]

c = "x"

ag.q-e.i_u9.a=false

0 comments on commit 93e0e3e

Please sign in to comment.