2015-12-17 00:21:49 +03:00
|
|
|
module Parsers where
|
2015-12-09 17:49:24 +03:00
|
|
|
|
|
|
|
import Diff
|
2015-12-09 18:32:22 +03:00
|
|
|
import Range
|
2015-12-17 00:24:23 +03:00
|
|
|
import Parser
|
2015-12-24 08:20:47 +03:00
|
|
|
import Source hiding ((++))
|
2015-12-09 18:32:22 +03:00
|
|
|
import Syntax
|
2015-12-15 22:35:11 +03:00
|
|
|
import TreeSitter
|
2015-12-09 18:32:22 +03:00
|
|
|
import Control.Comonad.Cofree
|
2015-12-24 02:01:31 +03:00
|
|
|
import Data.Foldable
|
2015-12-09 17:49:24 +03:00
|
|
|
|
2015-12-15 22:36:25 +03:00
|
|
|
parserForType :: String -> Parser
|
2015-12-15 22:40:49 +03:00
|
|
|
parserForType mediaType = maybe lineByLineParser parseTreeSitterFile $ languageForType mediaType
|
2015-12-15 22:36:25 +03:00
|
|
|
|
2015-12-09 18:32:22 +03:00
|
|
|
lineByLineParser :: Parser
|
2015-12-24 02:01:31 +03:00
|
|
|
lineByLineParser input = return . root . Indexed $ case foldl' annotateLeaves ([], 0) lines of
|
2015-12-14 23:52:39 +03:00
|
|
|
(leaves, _) -> leaves
|
2015-12-09 18:32:22 +03:00
|
|
|
where
|
2015-12-24 08:20:47 +03:00
|
|
|
lines = actualLines input
|
2015-12-15 22:35:37 +03:00
|
|
|
root syntax = Info (Range 0 $ length input) mempty :< syntax
|
2015-12-24 08:20:47 +03:00
|
|
|
leaf charIndex line = Info (Range charIndex $ charIndex + length line) mempty :< Leaf (Source.toList line)
|
2015-12-14 23:52:39 +03:00
|
|
|
annotateLeaves (accum, charIndex) line =
|
|
|
|
(accum ++ [ leaf charIndex line ]
|
|
|
|
, charIndex + length line + 1)
|