Sebastian Kreisel 2 years ago
parent
commit
aa36fac9d0
4 changed files with 88 additions and 8 deletions
  1. 1 1
      .gitignore
  2. 4 4
      src/Main.hs
  3. 43 0
      src/Parseck/Latex.hs
  4. 40 3
      src/Parser/Parseck.hs

+ 1 - 1
.gitignore

@@ -1,2 +1,2 @@
-**/stack-work
+**/.stack-work
 **/test

+ 4 - 4
src/Main.hs

@@ -3,12 +3,12 @@ module Main where
 import Control.Monad
 import Control.Applicative
 
-import Parser.Parseck
+import Parseck.Parseck
+import Parseck.Latex
 
 
 main :: IO ()
 main = do
-  let test = "a b"
+  test <- readFile "./test/test1.txt"
+  putStrLn $ show (parse blocks test)
   putStrLn $ "\n >>> See you Space Cowboy <<<"
-readTest :: String -> IO String
-readTest = readFile

+ 43 - 0
src/Parseck/Latex.hs

@@ -0,0 +1,43 @@
+module Parseck.Latex where
+
+import Text.Read (readMaybe)
+import Control.Applicative
+
+import Parseck.Parseck
+
+
+data Block = Section Int String | Math String | Paragraph String | Space Int
+           deriving Show
+
+blocks :: Parser [Block]
+blocks = many (section <|> math <|> space <|> paragraph)
+
+section :: Parser Block
+section = do
+  string "\\"
+  sc <- subs
+  string "section{"
+  s <- untilStop "}"
+  string "}"
+  if sc > 3 then failure else return (Section sc s)
+  where subs = many (string "sub") >>= return . length
+
+math :: Parser Block
+math = enclosed ("$$", "$$") (untilStop "$$") >>= return . Math
+
+space :: Parser Block
+space = do
+  s <- enclosed ("\\vspace{", "px}") (untilStop "px}")
+  case readMaybe s of
+    Just i -> return (Space i)
+    Nothing -> failure
+
+paragraph :: Parser Block
+paragraph = (untilStop "\\vecspace" <|>
+             untilStop "\\section" <|>
+             untilStop "\\subsection" <|>
+             untilStop "\\subsubsection" <|>
+             untilStop "\\subsubsubsection" <|>
+             untilStop "$$" <|>
+             untilIncluded "\n\n")
+            >>= return . Paragraph

+ 40 - 3
src/Parser/Parseck.hs

@@ -1,4 +1,4 @@
-module Parser.Parseck where
+module Parseck.Parseck where
 
 import Control.Monad
 import Control.Applicative
@@ -58,6 +58,9 @@ item = Parser $ \s -> case s of
   [] -> []
   (c : cs) -> [(c, cs)]
 
+itemS :: Parser String
+itemS = item >>= \i -> return [i]
+
 satisfy :: (Char -> Bool) -> Parser Char
 satisfy f = item >>= \c -> if f c then pure c else failure
 
@@ -68,12 +71,15 @@ char :: Char -> Parser Char
 char c = satisfy (== c)
 
 string :: String -> Parser String
-string "" = failure
-string (c : cs) = do { char c; string cs; pure (c : cs) }
+string [] = return []
+string (c : cs) = do { char c; string cs; return (c : cs) }
 
 spaces :: Parser String
 spaces = many $ oneOf " \n\r"
 
+everything :: Parser String
+everything = Parser $ \s -> [(s, "")]
+
 token :: Parser a -> Parser a
 token p = do { a <- p; spaces; pure a }
 
@@ -83,3 +89,34 @@ enclosed (pre, post) p = do
   a <- p
   string post
   pure a
+
+-- Parses anything until stop. Consumes stop
+untilIncluded :: String -> Parser String
+untilIncluded stop = do
+  (string stop >> return "") <|> do
+    i <- item
+    s <- untilIncluded stop
+    return (i : s)
+
+-- Fails if it parses s. Does not consume anything otherwise and returns ""
+notstring :: String -> Parser String
+notstring s = do
+  res <- string s <|> return ""
+  if res == ""
+    then return ""
+    else failure
+
+-- Parses anything until stop. Does not consume stop
+untilExcluded :: String -> Parser String
+untilExcluded stop = do
+  res <- notstring stop <|> return "x"
+  if res == "x"
+    then return ""
+    else do a <- itemS
+            b <- untilExcluded stop
+            return $ a ++ b
+
+untilStop = untilExcluded
+
+untilAny :: [String] -> Parser String
+untilAny stops = undefined