0 | module Graphics.DOT
 1 |
 2 | import Data.List
 3 | import Data.List1
 4 |
 5 | import Graphics.DOT.Lexer
 6 |
 7 | import public Graphics.DOT.AST
 8 | import public Graphics.DOT.Utils
 9 | import public Graphics.DOT.Parser
10 | import public Graphics.DOT.Interfaces
11 |
12 | -- TODO: REMOVE ONCE READY
13 | import System.File
14 | import Libraries.Text.Lexer.Core
15 | import Libraries.Text.Parser.Core
16 |
17 | ------------------------------------------------------------------------
18 | -- UTIL
19 |
20 | ||| The types of errors that can occur when processing a DOT/.gv file, combined
21 | ||| with the respective error message.
22 | public export
23 | data DOTError : Type where
24 |    ||| An error occurred when trying to read the file.
25 |    FError : (errMsg : String) -> DOTError
26 |    ||| Something's wrong with the structure of the DOT in the file.
27 |    ParseError : (errMsg : String) -> DOTError
28 |
29 | export
30 | Show DOTError where
31 |    show (FError errMsg) = "DOTERROR from file: " ++ errMsg
32 |    show (ParseError errMsg) = "DOTERROR when parsing: " ++ errMsg
33 |
34 |
35 | ||| Given a file name, open it and lex and parse the DOT in it, returning the
36 | ||| `Graph` node which is the root of the AST.
37 | |||
38 | ||| @ fname the file name to read
39 | export
40 | readDOTFile : HasIO io => (fname : String) -> io (Either DOTError Graph)
41 | readDOTFile fname =
42 |   do (Right contents) <- readFile fname
43 |         | Left err => pure $ (Left . FError) $ show err
44 |      let (tokData, _) = lex contents
45 |      Right (warns, ast, rem) <- pure $ parse tokData
46 |         | Left pErrs => pure $ (Left . ParseError) $ show (map show pErrs)
47 |      if (not . isNil) rem
48 |         then pure $ (Left . ParseError) $ "Non-empty token remainder:\n\t\{show rem}"
49 |         else pure $ Right ast
50 |
51 |
52 | ------------------------------------------------------------------------
53 | -- TESTS
54 |
55 | lexTest : String -> IO ()
56 | lexTest fp =
57 |   do (Right contents) <- readFile fp
58 |         | Left err => putStrLn $ "FILE ERROR: " ++ show err
59 |      let (tokList, (line, col, rem)) = lex contents
60 |      putStrLn $ "Reached " ++ (show line) ++ ":" ++ (show col)
61 |      putStrLn $ "Remainder: " ++ rem
62 |      putStrLn "----------------\n-- TOKEN LIST --\n----------------\n"
63 |      putStrLn $ show tokList
64 |
65 | parseTest : String -> IO ()
66 | parseTest fp =
67 |   do (Right contents) <- readFile fp
68 |        | Left err => putStrLn $ "FILE ERROR: " ++ show err
69 |      let (tokData, _) = lex contents
70 |      let pRes = parse tokData
71 |      putStrLn $
72 |         the String $
73 |             case pRes of
74 |                  Left errs => show $ map show errs
75 |                  Right (pWarns, ast, pRem) =>
76 |                      show ast ++ "\n\tREM: " ++ show pRem
77 |      Right (warns, ast, rem) <- pure $ parse tokData
78 |         | Left errs => do putStrLn (show $ map show errs)
79 |      putStrLn $ "Remainder: " ++ show rem
80 |
81 |