diff --git a/.gitmodules b/.gitmodules index df9c8f83d..f0c6ba8db 100644 --- a/.gitmodules +++ b/.gitmodules @@ -15,7 +15,7 @@ url = https://github.com/maxbrunsfeld/tree-sitter.git [submodule "prototype/External/tree-sitter-javascript"] path = prototype/External/tree-sitter-javascript - url = https://github.com/maxbrunsfeld/tree-sitter-javascript.git + url = https://github.com/joshvera/tree-sitter-javascript.git [submodule "prototype/External/tree-sitter-c"] path = prototype/External/tree-sitter-c url = https://github.com/robrix/tree-sitter-c.git diff --git a/app/Main.hs b/app/Main.hs index c115f906a..fc58dd39f 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -13,6 +13,7 @@ import qualified Data.Map as Map import qualified Data.ByteString.Char8 as ByteString import Data.Set hiding (split) import Options.Applicative +import System.FilePath import Foreign import Foreign.C @@ -20,6 +21,7 @@ import Foreign.C.Types data TSLanguage = TsLanguage deriving (Show, Eq) foreign import ccall "prototype/doubt-difftool/doubt-difftool-Bridging-Header.h ts_language_c" ts_language_c :: IO (Ptr TSLanguage) +foreign import ccall "prototype/doubt-difftool/doubt-difftool-Bridging-Header.h ts_language_javascript" ts_language_javascript :: IO (Ptr TSLanguage) data TSDocument = TsDocument deriving (Show, Eq) foreign import ccall "prototype/External/tree-sitter/include/tree_sitter/runtime.h ts_document_make" ts_document_make :: IO (Ptr TSDocument) @@ -64,10 +66,15 @@ main :: IO () main = do arguments <- execParser opts output <- do - aContents <- readFile $ sourceA arguments - bContents <- readFile $ sourceB arguments - aTerm <- parseTreeSitterFile aContents - bTerm <- parseTreeSitterFile bContents + let (sourceAPath, sourceBPath) = (sourceA arguments, sourceB arguments) + aContents <- readFile sourceAPath + bContents <- readFile sourceBPath + language <- (parserForType . takeExtension) sourceAPath + (aTerm, bTerm) <- case language of + Just lang -> do aTerm <- parseTreeSitterFile lang aContents + bTerm <- parseTreeSitterFile lang bContents + return (aTerm, bTerm) + Nothing -> error ("Unsupported language extension in path: " ++ sourceAPath) let diff = interpret comparable aTerm bTerm in case output arguments of Unified -> unified diff aContents bContents @@ -76,10 +83,15 @@ main = do opts = info (helper <*> arguments) (fullDesc <> progDesc "Diff some things" <> header "semantic-diff - diff semantically") -parseTreeSitterFile :: String -> IO (Term String Info) -parseTreeSitterFile contents = do +parserForType mediaType = sequence $ case mediaType of + "h" -> Just ts_language_c + "c" -> Just ts_language_c + "js" -> Just ts_language_javascript + _ -> Nothing + +parseTreeSitterFile :: Ptr TSLanguage -> String -> IO (Term String Info) +parseTreeSitterFile language contents = do document <- ts_document_make - language <- ts_language_c ts_document_set_language document language withCString contents (\source -> do ts_document_set_input_string document source diff --git a/app/bridge.c b/app/bridge.c index 088803a70..22d8bb428 100644 --- a/app/bridge.c +++ b/app/bridge.c @@ -12,6 +12,10 @@ TSLanguage *ts_language_c_use() { return ts_language_c(); } +TSLanguage *ts_language_javascript_use() { + return ts_language_javascript(); +} + const char *ts_node_p_name(const TSNode *node, const TSDocument *document) { assert(node != NULL); assert(node->data != NULL); diff --git a/app/bridge.h b/app/bridge.h index 63a502968..b33103ee2 100644 --- a/app/bridge.h +++ b/app/bridge.h @@ -1,6 +1,7 @@ #include "tree_sitter/runtime.h" extern TSLanguage *ts_language_c(); +extern TSLanguage *ts_language_javascript(); void ts_document_root_node_p(TSDocument *document, TSNode *outNode); diff --git a/prototype/External/tree-sitter-javascript b/prototype/External/tree-sitter-javascript index 74444328e..486c784ab 160000 --- a/prototype/External/tree-sitter-javascript +++ b/prototype/External/tree-sitter-javascript @@ -1 +1 @@ -Subproject commit 74444328e7910ea0d42a4a735b336a963e4b1105 +Subproject commit 486c784ab4cfff7839bfacc40543422c61ef9abb diff --git a/semantic-diff.cabal b/semantic-diff.cabal index b4a5c6bd6..3b94d017a 100644 --- a/semantic-diff.cabal +++ b/semantic-diff.cabal @@ -45,9 +45,13 @@ executable semantic-diff-exe , semantic-diff , bytestring , optparse-applicative + , filepath default-language: Haskell2010 extra-libraries: bridge - extra-lib-dirs: prototype/External/tree-sitter/out/Release, prototype/External/tree-sitter-c, . + extra-lib-dirs: prototype/External/tree-sitter/out/Release + , prototype/External/tree-sitter-c + , prototype/External/tree-sitter-javascript + , . test-suite semantic-diff-test type: exitcode-stdio-1.0