public
Description: An Actionscript 3 to haXe source converter written in Haskell
Homepage: http://geekrelief.wordpress.com
Clone URL: git://github.com/geekrelief/as3tohaxe.git
Click here to lend your support to: as3tohaxe and make a donation at www.pledgie.com !
as3tohaxe / Lexitall.hs
100644 94 lines (76 sloc) 3.48 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{-
as3tohaxe - An Actionscript 3 to haXe source file translator
Copyright (C) 2008 Don-Duong Quach
 
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-}
module Main where
 
import ActionhaXe.Lexer
import Text.Parsec.Pos
import System.Environment (getArgs)
import Text.PrettyPrint
import System.Directory
import Data.Char
import Data.List
import Control.Monad
 
format s a = parens (hcat [text (sourceName s), space, int (sourceLine s), colon, int (sourceColumn s) ]) <+> text a
 
{-
unknowns ts = [ format s a | t@(s, TokenUnknown a) <- ts]
comments ts = [ format s a | t@(s, TokenComment a) <- ts]
strings ts = [ format s a | t@(s, TokenString a) <- ts]
xmls ts = [ format s a | t@(s, TokenXml a) <- ts]
regexs ts = [ format s (r1++"/"++r2) | t@(s, TokenRegex (r1,r2)) <- ts]
-}
 
unknowns ts = [ t | t@(s, TokenUnknown a) <- ts]
comments ts = [ t | t@(s, TokenComment a) <- ts]
strings ts = [ t | t@(s, TokenString a) <- ts]
xmls ts = [ t | t@(s, TokenXml a) <- ts]
regexs ts = [ t | t@(s, TokenRegex (r1,r2)) <- ts]
 
inRange (s, _) (t, _) = linet > lines - 3 && linet < lines + 3
    where
           linet = sourceLine t
           lines = sourceLine s
 
getContext ftoken tokens = filter (\t -> inRange ftoken t) tokens
 
displayToken tokens t = do putStrLn "Detected Unknown: "
                           print t
                           putStrLn "Context --"
                           mapM_ print $ getContext t tokens
 
renderTokens lfilter filename =
    do contents <- readFile filename
       putStrLn $ "Checking: "++filename
       let tokens = runLexer filename contents
       let ftokens = lfilter tokens
       if null ftokens == False
           then do putStrLn $ "Detected: "++filename
                  -- putStrLn $ render $ hcat $ punctuate (text "\n") $ ftokens
                   mapM_ (displayToken tokens) ftokens
           else return ()
 
isFile f = do t <- doesFileExist f
              return $ t && ("as" == (map toLower $ reverse $ take 2 $ reverse f))
 
isDir d = do t <- doesDirectoryExist d
             return $ t && d /= "." && d /= ".." && d /= ".svn"
 
getASFiles dir = do contents <- getDirectoryContents dir
                    let c = map (\e -> dir++"/"++e) (filter (\d-> d /= "." && d /="..") contents)
                    asfiles <- filterM isFile c
                    asdirs <- filterM isDir c
                    childAsFiles <- mapM getASFiles asdirs
                    if null asfiles
                        then return $ concat childAsFiles
                        else return $ concat (asfiles: childAsFiles)
 
 
main = do args <- getArgs
          if isSuffixOf ".as" (args!!0)
              then renderTokens unknowns $ args!!0
              else do asFiles <- getASFiles (args!!0)
                      mapM_ (renderTokens unknowns) asFiles
 
{-
main = do args <- getArgs
renderTokens unknowns $ args!!0
-}