From 39ff973e811f159b5236155b4af62dce2ccfa9a6 Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Thu, 6 Jan 2011 18:03:56 -0800 Subject: [PATCH] Simple lexer/parser fixes. --- hsrc/Main.hs | 31 +++++++++++++++++++++++++++---- hsrc/Syntax/Lexer.x | 2 +- hsrc/Syntax/Parser.y | 1 - 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/hsrc/Main.hs b/hsrc/Main.hs index 5a6a69c..dcd3a19 100644 --- a/hsrc/Main.hs +++ b/hsrc/Main.hs @@ -6,15 +6,38 @@ import System.Exit import System.IO.Error import Syntax.AST +import Syntax.Lexer import Syntax.Parser import Syntax.ParserCore main :: IO () main = do - [file] <- getArgs - ast <- loadModule file - putStrLn "Successful parse!" - putStrLn (show ast) + args <- getArgs + case args of + [file] -> do + ast <- loadModule file + putStrLn "Successful parse!" + putStrLn (show ast) + ["-lex",path] -> do + mtxt <- tryJust (guard . isDoesNotExistError) $ S.readFile path + case mtxt of + Left _ -> fail $ "Unable to open file: " ++ path + Right txt -> do + case runParser path txt pullTokens of + Left err -> printError err >> exitWith (ExitFailure 1) + Right ress -> do + mapM_ putStrLn ress + putStrLn "Successful lex." + +pullTokens :: Parser [String] +pullTokens = do + tok <- scan + case tok of + Lexeme pos tok -> do + let res = show pos ++ " " ++ show tok + if tok == TokEOF + then return [res] + else return (res :) `ap` pullTokens loadModule :: FilePath -> IO (Module ()) loadModule path = do diff --git a/hsrc/Syntax/Lexer.x b/hsrc/Syntax/Lexer.x index 0eecc9b..1b1e399 100644 --- a/hsrc/Syntax/Lexer.x +++ b/hsrc/Syntax/Lexer.x @@ -20,7 +20,7 @@ $bindigit = [01] -- Identifier Characters $typestart = [A-Z\_] $valstart = [a-z\_] -$identrest = [a-zA-Z0-9\_] +$identrest = [a-zA-Z0-9\_\.] $opident = [\~\!\@\#\$\%\^\&\*\+\-\=\.\:\<\>\?\/\_] $escape_char = [abfnrtv'\"\\] diff --git a/hsrc/Syntax/Parser.y b/hsrc/Syntax/Parser.y index 61affd5..67a74aa 100644 --- a/hsrc/Syntax/Parser.y +++ b/hsrc/Syntax/Parser.y @@ -175,7 +175,6 @@ bangtype2 :: { Type } bangtype3 :: { Type } : TYPE_IDENT { TVar (makeQualified $1) Star } - | VAL_IDENT { TVar (makeQualified $1) Star } | '(' bangtype ')' { $2 } -- Expressions --------------------------------------------------------------