summaryrefslogtreecommitdiff
path: root/Library/Parse.hs
blob: 0e2aa2505f682a7fa3f854dfef8515ae02a9ee30 (plain)
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
module Library.Parse (
	getLines,
	stripReturn,
	removeEscChars,
	removeQuotes,
	separateBy,
	isComment,
	isNumber,
	isName,
	output,
	fst3,
	snd3,
	thd3
	) where

import Control.Monad( liftM )
import qualified Data.Char as Char


getLines :: FilePath -> IO [String]
getLines = liftM lines . readFile


stripReturn :: String -> String
stripReturn s = if (last s == '\r') then init s else s


removeEscChars :: String -> String
removeEscChars [] = []
removeEscChars (x:[]) = [x]
removeEscChars x = if (head x == '\\')
                   then (x!!1) : (removeEscChars . (drop 2) $ x)
                   else (head x) : (removeEscChars . tail $ x)


removeQuotes :: String -> String
removeQuotes = init . tail


separateBy :: Char -> String -> [String]
separateBy char list =
	let f = (\x -> if (x == char)
		           then ' '
		           else x)
	in words . (map f) $ list


isComment :: String -> Bool
isComment = (==) '#' . head


isNumber :: String -> Bool
isNumber ('0':[]) = True
isNumber ('-':ns)
         | (ns /= [] && head ns /= '0') = isNumber ns
isNumber n = all (Char.isNumber) n


isName :: String -> Bool
isName s = all ((==) '"') [head s, last s]


output :: [String] -> IO ()
output [] = return ()
output list = do
	putStrLn (head list)
	output (tail list)


fst3 :: (a,b,c) -> a
fst3 (a,_,_) = a

snd3 :: (a,b,c) -> b
snd3 (_,b,_) = b

thd3 :: (a,b,c) -> c
thd3 (_,_,c) = c