diff options
author | Joe Crayne <joe@jerkface.net> | 2019-03-17 14:41:18 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-03-17 14:41:18 -0400 |
commit | 16d4397e2cbe550c473117dae7ec1ef577fb7937 (patch) | |
tree | 8295ff50f0f06eed9f6e1c7032b08e642d2f77c8 | |
parent | aacea6a3294a7e27d60f49352cbe54139c76dce0 (diff) |
Seperate module for comment parsing.
-rw-r--r-- | Comments.hs | 31 |
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 #-} | ||
3 | module Comments where | ||
4 | |||
5 | import Control.Arrow | ||
6 | |||
7 | parseComments :: (Num col, Num lin) => lin -> col -> [Char] -> [(lin, col, [Char])] | ||
8 | parseComments !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 | |||
21 | findCloser :: (Num a4, Num a3, Num a2, Num a1, Eq a1) => | ||
22 | a1 -> (a4, a2, a3) -> [Char] -> (a4, a2, a3) | ||
23 | findCloser !1 (l,c,b) ('*':'/':_) = (l,c+2,b+2) | ||
24 | findCloser !d (l,c,b) ('*':'/':xs) = findCloser (d - 1) (l,c+2,b+2) xs | ||
25 | findCloser !d (l,c,b) ('/':'*':xs) = findCloser (d + 1) (l,c+2,b+2) xs | ||
26 | findCloser !d (l,c,b) ('\n':xs) = findCloser d (l+1,1,b+1) xs | ||
27 | findCloser !d (l,c,b) (_:xs) = findCloser d (l,c+1,b+1) xs | ||
28 | findCloser !d (l,c,b) [] = (l,c,b) | ||
29 | |||
30 | mkComment :: a -> b -> c -> (a, b, c) | ||
31 | mkComment lin no str = (lin,no,str) | ||