1
1
mirror of https://github.com/github/semantic.git synced 2024-12-18 04:11:48 +03:00
semantic/app/Parser.hs

25 lines
767 B
Haskell
Raw Normal View History

2015-12-09 17:49:24 +03:00
module Parser where
import Diff
import Range
import Syntax
2015-12-09 17:49:24 +03:00
import Term
2015-12-15 22:35:11 +03:00
import TreeSitter
import Control.Comonad.Cofree
2015-12-09 17:49:24 +03:00
type Parser = String -> IO (Term String Info)
2015-12-15 22:36:25 +03:00
parserForType :: String -> Parser
parserForType mediaType = maybe lineByLineParser parseTreeSitterFile $ languageForType mediaType
2015-12-15 22:36:25 +03:00
lineByLineParser :: Parser
2015-12-14 23:52:39 +03:00
lineByLineParser input = return . root . Indexed $ case foldl annotateLeaves ([], 0) lines of
(leaves, _) -> leaves
where
lines = Prelude.lines input
2015-12-15 22:35:37 +03:00
root syntax = Info (Range 0 $ length input) mempty :< syntax
leaf charIndex line = Info (Range charIndex $ charIndex + length line) mempty :< Leaf line
2015-12-14 23:52:39 +03:00
annotateLeaves (accum, charIndex) line =
(accum ++ [ leaf charIndex line ]
, charIndex + length line + 1)