forked from bjpop/language-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoundTrip.hs
More file actions
80 lines (71 loc) · 2.53 KB
/
Copy pathRoundTrip.hs
File metadata and controls
80 lines (71 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import Language.Python.Common
import Language.Python.Version2 as V2
import Language.Python.Version3 as V3
import System.Exit
import System.Environment
data PythonVersion = Two | Three | Both
deriving (Eq, Show)
data Comparison = ParseFailed String | Equal | NotEqual String String
type Parser = String -> String -> Either ParseError (ModuleSpan, [Token])
main :: IO ()
main = do
args <- getArgs
case args of
(versionStr:inFile:_rest) ->
case parseVersion versionStr of
Nothing -> do
putStrLn $ "Unknown Python version: " ++ versionStr
exitFailure
Just version -> do
contents <- readFile inFile
let parsers = pickParsers version
comparisons = [parseAndCompare p inFile contents | p <- parsers]
test <- check comparisons
if test then exitWith ExitSuccess else exitSuccess
_other -> putStrLn "Incorrect command line. Expected: <2|3|n> inputFileName"
check :: [Comparison] -> IO Bool
check [] = return True -- must have all been equal
check (Equal:rest) = check rest
check (NotEqual s1 s2:_rest) = do
doubleLine
putStrLn "Round trip parse failed"
doubleLine
putStrLn "pretty1"
line
putStrLn s1
doubleLine
putStrLn "pretty2"
line
putStrLn s2
return False
check (ParseFailed e:_rest) = do
putStrLn "Parse failed with error: "
putStrLn e
return False
pickParsers :: PythonVersion -> [Parser]
pickParsers Two = [V2.parseModule]
pickParsers Three = [V3.parseModule]
pickParsers Both = [V2.parseModule, V3.parseModule]
parseAndCompare :: Parser -> FilePath -> String -> Comparison
parseAndCompare parser inFile contents =
case parseAndPretty parser inFile contents of
Left e -> ParseFailed $ prettyText e
Right pretty1 ->
case parseAndPretty parser "<pretty printed>" pretty1 of
Left e -> ParseFailed $ prettyText e
Right pretty2
| pretty1 == pretty2 -> Equal
| otherwise -> NotEqual pretty1 pretty2
line, doubleLine :: IO ()
line = putStrLn $ replicate 80 '-'
doubleLine = putStrLn $ replicate 80 '='
parseAndPretty :: Parser -> FilePath -> String -> Either ParseError String
parseAndPretty parser fileName contents =
case parser contents fileName of
Left e -> Left e
Right (ast, _comments) -> Right (prettyText ast ++ "\n")
parseVersion :: String -> Maybe PythonVersion
parseVersion "2" = Just Two
parseVersion "3" = Just Three
parseVersion "n" = Just Both
parseVersion _other = Nothing