Commit 715cb8c7 authored by Kai Prott's avatar Kai Prott
Browse files

Fixed some problems, nested parsing still does not work

parent afce504f
...@@ -15,18 +15,20 @@ data CompOperator = EQUAL | LESS | LEQ | GREATER | GEQ | NEQ ...@@ -15,18 +15,20 @@ data CompOperator = EQUAL | LESS | LEQ | GREATER | GEQ | NEQ
type Parser a = Parsec String () a type Parser a = Parsec String () a
example = "#IFDEF value\nlol\n#ENDIF" example = "#IF value<=599\nLOL\n#ENDIF\n#IFDEF anotha\nONE\n#ENDIF\n"
iftoken = return () <* string "#IF" <* whitespaces iftoken = () <$ try (string "#IF" <* whitespaces)
ifdeftoken = return () <* string "#IFDEF" <* whitespaces ifdeftoken = () <$ try (string "#IFDEF" <* whitespaces)
elsetoken = return () <* newline <* string "#ELSE" <* whitespaces elsetoken = () <$ try (newline <* string "#ELSE" <* whitespaces)
endtoken = return () <* newline <* string "#ENDIF" <* whitespaces endtoken = () <$ try (newline <* string "#ENDIF" <* whitespaces)
reservedTokens = [iftoken, ifdeftoken, elsetoken, endtoken] reservedTokens = [iftoken, ifdeftoken, elsetoken, endtoken]
whitespaces :: Parser () whitespaces :: Parser ()
whitespaces = skipMany (() <$ space) whitespaces = skipMany (() <$ space)
test = either putStrLn print (simpleParse example)
simpleParse :: String -> Either String [Stmt] simpleParse :: String -> Either String [Stmt]
simpleParse = either (Left . show) Right . runParser program () "" simpleParse = either (Left . show) Right . runParser program () ""
...@@ -37,7 +39,7 @@ statement :: Parser Stmt ...@@ -37,7 +39,7 @@ statement :: Parser Stmt
statement = ifdef <|> ifstmt <|> code statement = ifdef <|> ifstmt <|> code
ifstmt :: Parser Stmt ifstmt :: Parser Stmt
ifstmt = try ifelse <|> try ifstmt' ifstmt = try ifstmt' <|> try ifelse
ifelse :: Parser Stmt ifelse :: Parser Stmt
ifelse = IF <$> (iftoken *> condition <* newline) ifelse = IF <$> (iftoken *> condition <* newline)
...@@ -62,8 +64,8 @@ ifdef' = IFDEF <$> (ifdeftoken *> identifier <* newline) ...@@ -62,8 +64,8 @@ ifdef' = IFDEF <$> (ifdeftoken *> identifier <* newline)
<*> many statement <*> many statement
<*> (return [] <* endtoken) <*> (return [] <* endtoken)
newline :: Parser Char newline :: Parser String
newline = char '\n' newline = string "\n" <|> string "\r\n"
code :: Parser Stmt code :: Parser Stmt
code = Code <$> many1 (notFollowedBy (choice reservedTokens) *> anyChar) code = Code <$> many1 (notFollowedBy (choice reservedTokens) *> anyChar)
...@@ -72,16 +74,15 @@ condition :: Parser Condition ...@@ -72,16 +74,15 @@ condition :: Parser Condition
condition = do ident <- identifier condition = do ident <- identifier
op <- operator op <- operator
condVal <- many digit condVal <- many digit
whitespaces
return (ident, op, condVal) return (ident, op, condVal)
operator :: Parser CompOperator operator :: Parser CompOperator
operator = choice [ EQUAL <$ string "=" operator = choice [ LEQ <$ string "<="
, LESS <$ string "<" , LESS <$ string "<"
, GREATER <$ string ">"
, LEQ <$ string "<="
, GEQ <$ string ">=" , GEQ <$ string ">="
, GREATER <$ string ">"
, NEQ <$ string "!=" , NEQ <$ string "!="
, EQUAL <$ string "="
] ]
identifier :: Parser String identifier :: Parser String
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment