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
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
ifdeftoken = return () <* string "#IFDEF" <* whitespaces
elsetoken = return () <* newline <* string "#ELSE" <* whitespaces
endtoken = return () <* newline <* string "#ENDIF" <* whitespaces
iftoken = () <$ try (string "#IF" <* whitespaces)
ifdeftoken = () <$ try (string "#IFDEF" <* whitespaces)
elsetoken = () <$ try (newline <* string "#ELSE" <* whitespaces)
endtoken = () <$ try (newline <* string "#ENDIF" <* whitespaces)
reservedTokens = [iftoken, ifdeftoken, elsetoken, endtoken]
whitespaces :: Parser ()
whitespaces = skipMany (() <$ space)
test = either putStrLn print (simpleParse example)
simpleParse :: String -> Either String [Stmt]
simpleParse = either (Left . show) Right . runParser program () ""
......@@ -37,7 +39,7 @@ statement :: Parser Stmt
statement = ifdef <|> ifstmt <|> code
ifstmt :: Parser Stmt
ifstmt = try ifelse <|> try ifstmt'
ifstmt = try ifstmt' <|> try ifelse
ifelse :: Parser Stmt
ifelse = IF <$> (iftoken *> condition <* newline)
......@@ -62,8 +64,8 @@ ifdef' = IFDEF <$> (ifdeftoken *> identifier <* newline)
<*> many statement
<*> (return [] <* endtoken)
newline :: Parser Char
newline = char '\n'
newline :: Parser String
newline = string "\n" <|> string "\r\n"
code :: Parser Stmt
code = Code <$> many1 (notFollowedBy (choice reservedTokens) *> anyChar)
......@@ -72,16 +74,15 @@ condition :: Parser Condition
condition = do ident <- identifier
op <- operator
condVal <- many digit
whitespaces
return (ident, op, condVal)
operator :: Parser CompOperator
operator = choice [ EQUAL <$ string "="
operator = choice [ LEQ <$ string "<="
, LESS <$ string "<"
, GREATER <$ string ">"
, LEQ <$ string "<="
, GEQ <$ string ">="
, GREATER <$ string ">"
, NEQ <$ string "!="
, EQUAL <$ 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