1
1
mirror of https://github.com/github/semantic.git synced 2024-12-24 23:42:31 +03:00

Pass Text to tree-sitter

This commit is contained in:
Timothy Clem 2017-02-09 21:31:29 -08:00
parent 905fdea0a2
commit d6796afd6f
2 changed files with 12 additions and 8 deletions

View File

@ -17,7 +17,8 @@ import Range
import Source
import qualified Syntax
import Foreign
import Foreign.C.String
import Foreign.C.String as FString (peekCString, withCStringLen)
import Data.Text.Foreign as Foreign
import qualified Syntax as S
import Term
import Text.Parser.TreeSitter hiding (Language(..))
@ -30,12 +31,15 @@ treeSitterParser :: Language -> Ptr TS.Language -> Parser (Syntax.Syntax Text) (
treeSitterParser language grammar blob = do
document <- ts_document_new
ts_document_set_language document grammar
withCString (toString $ source blob) (\source -> do
ts_document_set_input_string document source
ts_document_parse document
term <- documentToTerm language document blob
ts_document_free document
pure term)
let termWithCString (source, len) = do
ts_document_set_input_string2 document source len
ts_document_parse document
term <- documentToTerm language document blob
ts_document_free document
pure term
if Source.null (source blob)
then FString.withCStringLen "" termWithCString
else Foreign.withCStringLen (toText $ source blob) termWithCString
-- | Return a parser for a tree sitter language & document.
documentToTerm :: Language -> Ptr Document -> Parser (Syntax.Syntax Text) (Record '[Range, Category, SourceSpan])

@ -1 +1 @@
Subproject commit 8116ff5ec37cae106ed78771a9676057ad702b80
Subproject commit cec2607f906245315e0fcea8be9754c563846860