1
1
mirror of https://github.com/github/semantic.git synced 2025-01-02 04:10:29 +03:00

Merge pull request #258 from github/select-grammar

Select a grammar based on file extension
This commit is contained in:
Rob Rix 2015-12-03 15:55:03 -05:00
commit 041eec6eff
6 changed files with 31 additions and 10 deletions

2
.gitmodules vendored
View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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);

@ -1 +1 @@
Subproject commit 74444328e7910ea0d42a4a735b336a963e4b1105
Subproject commit 486c784ab4cfff7839bfacc40543422c61ef9abb

View File

@ -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