Skip to content

Instantly share code, notes, and snippets.

@alok

alok/lex.hs Secret

Created January 13, 2017 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alok/90bd979dc6496dfe0ddb72cdd799c58b to your computer and use it in GitHub Desktop.
Save alok/90bd979dc6496dfe0ddb72cdd799c58b to your computer and use it in GitHub Desktop.
import Control.Monad
import System.IO
import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Expr
import Text.ParserCombinators.Parsec.Language
import qualified Text.ParserCombinators.Parsec.Token as Token
languageDef =
emptyDef
{ Token.commentLine = "%"
, Token.identStart = letter
, Token.identLetter = alphaNum
, Token.reservedNames =
["\\", "$", "\\(", "\\)", "\\[", "\\]", "$$", "{", "}"]
, Token.reservedOpNames = ["\\", "$", "\\(", "\\)", "\\[", "\\]", "$$"]
}
lexer = Token.makeTokenParser languageDef
parseString :: String -> Stmt
parseString str =
case parse whileParser "" str of
Left e -> error $ show e
Right r -> r
parseFile :: String -> IO Stmt
parseFile file = do
program <- readFile file
case parse whileParser "" program of
Left e -> print e >> fail "parse error"
Right r -> return r
main :: IO ()
main = do
error "undefined: `main' in lex.hs"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment