Skip to content

Instantly share code, notes, and snippets.

@rblaze
Created June 7, 2012 16:36
Show Gist options
  • Save rblaze/2889923 to your computer and use it in GitHub Desktop.
Save rblaze/2889923 to your computer and use it in GitHub Desktop.
module LangParser(parseGrammar, Token(..)) where
import Control.Applicative
import Control.Monad
data Token =
Return |
Simple Char |
OctalConst String |
DecimalConst String |
HexConst String |
NamedParam String |
Identifier String |
LexError String
deriving (Eq, Show)
tReturn :: [Token] -> Maybe [Token]
tReturn [] = Nothing
tReturn (x:xs) = f x xs
where f Return xs = Just xs
f _ _ = Nothing
tSimple :: Char -> [Token] -> Maybe [Token]
tSimple _ [] = Nothing
tSimple c (x:xs) = f x xs
where f (Simple c) xs = Just xs
f _ _ = Nothing
parseGrammar :: [Token] -> Maybe [Token]
parseGrammar = retexpr
expr :: [Token] -> Maybe [Token]
expr = tSimple ':'
retexprs :: [Token] -> Maybe [Token]
retexprs = multiexpr <|> expr
where multiexpr = expr >=> tSimple ',' >=> retexprs
retexpr :: [Token] -> Maybe [Token]
retexpr = tReturn >=> retexprs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment