summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-03-17 14:41:18 -0400
committerJoe Crayne <joe@jerkface.net>2019-03-17 14:41:18 -0400
commit16d4397e2cbe550c473117dae7ec1ef577fb7937 (patch)
tree8295ff50f0f06eed9f6e1c7032b08e642d2f77c8
parentaacea6a3294a7e27d60f49352cbe54139c76dce0 (diff)
Seperate module for comment parsing.
-rw-r--r--Comments.hs31
1 files changed, 31 insertions, 0 deletions
diff --git a/Comments.hs b/Comments.hs
new file mode 100644
index 0000000..25cd7de
--- /dev/null
+++ b/Comments.hs
@@ -0,0 +1,31 @@
1{-# LANGUAGE BangPatterns #-}
2{-# LANGUAGE LambdaCase #-}
3module Comments where
4
5import Control.Arrow
6
7parseComments :: (Num col, Num lin) => lin -> col -> [Char] -> [(lin, col, [Char])]
8parseComments !lin !col = \case
9 ('/':'*':cs) -> let (lcnt,col',bcnt) = findCloser 1 (0,col,0) cs
10 (xs,cs') = case drop (bcnt-2) cs of
11 '*':'/':_ -> second (drop 2) $ splitAt (bcnt-2) cs
12 _ -> splitAt bcnt cs
13 in mkComment lin col xs : parseComments (lin + lcnt) col' cs'
14 ('/':'/':cs) -> let (comment,ds) = break (=='\n') cs
15 in mkComment lin col comment : parseComments (lin + 1) 1 cs
16 ('\n' : cs) -> parseComments (lin+1) 1 cs
17 ( x : cs) -> parseComments lin (col+1) cs
18 [] -> []
19
20
21findCloser :: (Num a4, Num a3, Num a2, Num a1, Eq a1) =>
22 a1 -> (a4, a2, a3) -> [Char] -> (a4, a2, a3)
23findCloser !1 (l,c,b) ('*':'/':_) = (l,c+2,b+2)
24findCloser !d (l,c,b) ('*':'/':xs) = findCloser (d - 1) (l,c+2,b+2) xs
25findCloser !d (l,c,b) ('/':'*':xs) = findCloser (d + 1) (l,c+2,b+2) xs
26findCloser !d (l,c,b) ('\n':xs) = findCloser d (l+1,1,b+1) xs
27findCloser !d (l,c,b) (_:xs) = findCloser d (l,c+1,b+1) xs
28findCloser !d (l,c,b) [] = (l,c,b)
29
30mkComment :: a -> b -> c -> (a, b, c)
31mkComment lin no str = (lin,no,str)