mirror of
https://github.com/github/semantic.git
synced 2024-11-24 00:42:33 +03:00
25 lines
767 B
Haskell
25 lines
767 B
Haskell
module Parser where
|
|
|
|
import Diff
|
|
import Range
|
|
import Syntax
|
|
import Term
|
|
import TreeSitter
|
|
import Control.Comonad.Cofree
|
|
|
|
type Parser = String -> IO (Term String Info)
|
|
|
|
parserForType :: String -> Parser
|
|
parserForType mediaType = maybe lineByLineParser parseTreeSitterFile $ languageForType mediaType
|
|
|
|
lineByLineParser :: Parser
|
|
lineByLineParser input = return . root . Indexed $ case foldl annotateLeaves ([], 0) lines of
|
|
(leaves, _) -> leaves
|
|
where
|
|
lines = Prelude.lines input
|
|
root syntax = Info (Range 0 $ length input) mempty :< syntax
|
|
leaf charIndex line = Info (Range charIndex $ charIndex + length line) mempty :< Leaf line
|
|
annotateLeaves (accum, charIndex) line =
|
|
(accum ++ [ leaf charIndex line ]
|
|
, charIndex + length line + 1)
|