1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 22:31:36 +03:00

Stub in a function to produce AST for Ruby.

This commit is contained in:
Rob Rix 2017-04-12 14:31:55 -04:00
parent 9d6a8b438d
commit ce075494cf

View File

@ -7,12 +7,14 @@ module TreeSitter
import Prologue hiding (Constructor) import Prologue hiding (Constructor)
import Category import Category
import Data.Record import Data.Record
import qualified Data.Syntax.Assignment as A
import Language import Language
import qualified Language.C as C import qualified Language.C as C
import qualified Language.Go as Go import qualified Language.Go as Go
import qualified Language.JavaScript as JS import qualified Language.JavaScript as JS
import qualified Language.TypeScript as TS import qualified Language.TypeScript as TS
import qualified Language.Ruby as Ruby import qualified Language.Ruby as Ruby
import qualified Language.Ruby.Syntax as Ruby
import Parser import Parser
import Range import Range
import Source import Source
@ -25,6 +27,7 @@ import qualified Syntax as S
import Term import Term
import Text.Parser.TreeSitter hiding (Language(..)) import Text.Parser.TreeSitter hiding (Language(..))
import qualified Text.Parser.TreeSitter as TS import qualified Text.Parser.TreeSitter as TS
import qualified Text.Parser.TreeSitter.Ruby as Ruby
import SourceSpan import SourceSpan
import Info import Info
@ -41,6 +44,25 @@ treeSitterParser language grammar blob = do
pure term pure term
parseRubyToAST :: Source -> IO (A.AST Ruby.Grammar)
parseRubyToAST source = do
document <- ts_document_new
ts_document_set_language document Ruby.tree_sitter_ruby
root <- withCStringLen (toText source) $ \ (source, len) -> do
ts_document_set_input_string_with_length document source len
ts_document_parse document
alloca (\ rootPtr -> do
ts_document_root_node_p document rootPtr
peek rootPtr)
ast <- toAST root source
ts_document_free document
pure ast
where toAST Node{..} source = do
pure $ A.Rose (A.Node (toEnum (fromIntegral nodeSymbol)) "") []
-- | Return a parser for a tree sitter language & document. -- | Return a parser for a tree sitter language & document.
documentToTerm :: Language -> Ptr Document -> Parser (Syntax.Syntax Text) (Record DefaultFields) documentToTerm :: Language -> Ptr Document -> Parser (Syntax.Syntax Text) (Record DefaultFields)
documentToTerm language document SourceBlob{..} = do documentToTerm language document SourceBlob{..} = do