Skip to content

Commit e5f51b4

Browse files
committed
Clean up Program parsers. #148
1 parent 0e87dc5 commit e5f51b4

File tree

2 files changed

+40
-62
lines changed

2 files changed

+40
-62
lines changed

src/Language/CQL/Parser.hs

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -34,68 +34,46 @@ import Language.CQL.Parser.Typeside as T'
3434
import Language.CQL.Program as P
3535
import Text.Megaparsec
3636

37-
parseCqlProgram' :: Parser (String, Exp)
38-
parseCqlProgram' = do
39-
_ <- constant "typeside"
40-
x <- identifier
41-
_ <- constant "="
42-
y <- typesideExpParser
43-
return (x, ExpTy y)
44-
<|>
45-
do
46-
_ <- constant "schema"
47-
x <- identifier
48-
_ <- constant "="
49-
y <- schemaExpParser
50-
return (x, ExpS y)
51-
<|>
52-
do
53-
_ <- constant "instance"
54-
x <- identifier
55-
_ <- constant "="
56-
y <- instExpParser
57-
return (x, ExpI y)
58-
<|>
59-
do
60-
_ <- constant "mapping"
61-
x <- identifier
62-
_ <- constant "="
63-
y <- mapExpParser
64-
return (x, ExpM y)
65-
<|>
66-
do
67-
_ <- constant "transform"
68-
x <- identifier
69-
_ <- constant "="
70-
y <- transExpParser
71-
return (x, ExpT y)
37+
parseCqlProgram :: String -> Err Prog
38+
parseCqlProgram s = case runParser parseCqlProgram' "" s of
39+
Left err -> Left $ "Parse error: " ++ parseErrorPretty err
40+
Right (o, x) -> if length (fst $ unzip x) == length (nub $ fst $ unzip x)
41+
then pure $ toProg o x
42+
else Left $ "Duplicate definition: " ++ show (nub (fmap fst x \\ nub (fmap fst x)))
7243

73-
parseCqlProgram'' :: Parser ([(String,String)],[(String, Exp)])
74-
parseCqlProgram'' = between spaceConsumer eof g
44+
-- | Returns a list of config options and programs.
45+
parseCqlProgram' :: Parser ([(String, String)], [(String, Exp)])
46+
parseCqlProgram' =
47+
between spaceConsumer eof configsAndProgs
7548
where
76-
f = do
77-
_ <- constant "options"
78-
many optionParser
79-
g = do
80-
x <- optional f
81-
y <- many parseCqlProgram'
82-
return (fromMaybe [] x, y)
49+
configsAndProgs = do
50+
opts <- optional (constant "options" *> many optionParser)
51+
progs <- many parseSection
52+
return (fromMaybe [] opts, progs)
8353

54+
toProg :: [(String, String)] -> [(String, Exp)] -> Prog
55+
toProg _ [] = newProg
56+
toProg opts ((v,e):p) = case e of
57+
ExpTy ty' -> KindCtx (Map.insert v ty' t) s i m q tr opts
58+
ExpS s' -> KindCtx t (Map.insert v s' s) i m q tr opts
59+
ExpI i' -> KindCtx t s (Map.insert v i' i) m q tr opts
60+
ExpM m' -> KindCtx t s i (Map.insert v m' m) q tr opts
61+
ExpQ q' -> KindCtx t s i m (Map.insert v q' q) tr opts
62+
ExpT t' -> KindCtx t s i m q (Map.insert v t' tr) opts
63+
where
64+
KindCtx t s i m q tr _ = toProg opts p
8465

85-
toProg' :: [(String, String)] -> [(String, Exp)] -> Prog
86-
toProg' _ [] = newProg
87-
toProg' o ((v,e):p) = case e of
88-
ExpTy ty' -> KindCtx (Map.insert v ty' t) s i m q tr o
89-
ExpS s' -> KindCtx t (Map.insert v s' s) i m q tr o
90-
ExpI i' -> KindCtx t s (Map.insert v i' i) m q tr o
91-
ExpM m' -> KindCtx t s i (Map.insert v m' m) q tr o
92-
ExpQ q' -> KindCtx t s i m (Map.insert v q' q) tr o
93-
ExpT t' -> KindCtx t s i m q (Map.insert v t' tr) o
94-
where KindCtx t s i m q tr _ = toProg' o p
95-
96-
parseCqlProgram :: String -> Err Prog
97-
parseCqlProgram s = case runParser parseCqlProgram'' "" s of
98-
Left err -> Left $ "Parse error: " ++ (parseErrorPretty err)
99-
Right (o, x) -> if length (fst $ unzip x) == length (nub $ fst $ unzip x)
100-
then pure $ toProg' o x
101-
else Left $ "Duplicate definition: " ++ show (nub (fmap fst x \\ nub (fmap fst x)))
66+
parseSection :: Parser (String, Exp)
67+
parseSection =
68+
section "typeside" typesideExpParser ExpTy <|>
69+
section "schema" schemaExpParser ExpS <|>
70+
section "instance" instExpParser ExpI <|>
71+
section "mapping" mapExpParser ExpM <|>
72+
section "transform" transExpParser ExpT
73+
where
74+
section sectionKindName bodyParser ctor = do
75+
_ <- constant sectionKindName
76+
sectionName <- identifier
77+
_ <- constant "="
78+
body <- bodyParser
79+
return (sectionName, ctor body)

src/Language/CQL/Parser/Parser.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ boolParser
9999
textParser :: Parser String
100100
textParser = do
101101
_ <- constant "\""
102-
text <- many (escapeSeq <|> show <$> noneOf ['"', '\r', '\n', '\\']) -- TODO: check if the escping is correct
102+
text <- many (escapeSeq <|> show <$> noneOf ['"', '\r', '\n', '\\']) -- TODO: check if the escaping is correct
103103
_ <- constant "\""
104104
pure $ unwords text
105105

0 commit comments

Comments
 (0)