Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCaml support (WIP) #623

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ packages: semantic
semantic-go
semantic-java
semantic-json
semantic-ocaml
semantic-parse
semantic-php
semantic-proto
Expand All @@ -21,6 +22,9 @@ packages: semantic
semantic-tsx
semantic-typescript

package semantic-ocaml
ghc-options: -freduction-depth=300

source-repository-package
type: git
location: https://github.com/antitypical/fused-syntax.git
Expand Down
4 changes: 4 additions & 0 deletions cabal.project.ci
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ packages: semantic
semantic-go
semantic-java
semantic-json
semantic-ocaml
semantic-parse
semantic-php
semantic-proto
Expand Down Expand Up @@ -51,6 +52,9 @@ package semantic-java
package semantic-json
ghc-options: -Werror

package semantic-ocaml
ghc-options: -Werror -freduction-depth=300

package semantic-parse
ghc-options: -Werror

Expand Down
3 changes: 3 additions & 0 deletions script/ghci-flags
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ function flags {
echo "-isemantic-java/src"
echo "-isemantic-json/src"
echo "-isemantic-json/test"
echo "-isemantic-ocaml/src"
echo "-isemantic-ocaml/test"
echo "-isemantic-parse/src"
echo "-isemantic-php/src"
echo "-isemantic-proto/src"
Expand Down Expand Up @@ -103,6 +105,7 @@ function flags {
[[ "$ghc_version" = 8.6.* ]] || [[ "$ghc_version" = 8.8.* ]] || [[ "$ghc_version" = 8.10.* ]] && echo "-Wno-star-is-type" || true
[[ "$ghc_version" = 8.8.* ]] || [[ "$ghc_version" = 8.10.* ]] && echo "-Wno-missing-deriving-strategies" || true
[[ "$ghc_version" = 8.10.* ]] && echo "-Wno-missing-safe-haskell-mode" && echo "-Wno-prepositive-qualified-module" && echo "-Wno-unused-packages"
echo "-freduction-depth=300"
}

flags > "$output_file"
3 changes: 2 additions & 1 deletion script/ghci-flags-dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ echo "semantic-tags/semantic-tags.cabal"
echo "semantic-go/semantic-go.cabal"
echo "semantic-java/semantic-java.cabal"
echo "semantic-json/semantic-json.cabal"
echo "semantic-python/semantic-php.cabal"
echo "semantic-ocaml/semantic-ocaml.cabal"
echo "semantic-php/semantic-php.cabal"
echo "semantic-python/semantic-python.cabal"
echo "semantic-ruby/semantic-ruby.cabal"
echo "semantic-tsx/semantic-tsx.cabal"
Expand Down
16 changes: 16 additions & 0 deletions semantic-ocaml/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package(default_visibility = ["//visibility:public"])

load(
"//:build/common.bzl",
"semantic_language_library",
"semantic_language_parsing_test",
)

semantic_language_library(
name = "semantic-ocaml",
srcs = glob(["src/**/*.hs"]),
language = "ocaml",
nodetypes = "@tree-sitter-ocaml//:vendor/tree-sitter-ocaml/ocaml/src/node-types.json",
)

semantic_language_parsing_test(language = "ocaml")
21 changes: 21 additions & 0 deletions semantic-ocaml/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 GitHub

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions semantic-ocaml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Semantic support for OCaml

This package implements `semantic` support for OCaml using the `semantic-core` intermediate language.
2 changes: 2 additions & 0 deletions semantic-ocaml/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
75 changes: 75 additions & 0 deletions semantic-ocaml/semantic-ocaml.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
cabal-version: 2.4

name: semantic-ocaml
version: 0.0.0.0
synopsis: Semantic support for OCaml.
description: Semantic support for OCaml using the semantic-core intermediate language.
homepage: https://github.com/github/semantic/tree/master/semantic-ocaml#readme
bug-reports: https://github.com/github/semantic/issues
license: MIT
license-file: LICENSE
author: The Semantic authors
maintainer: [email protected]
copyright: (c) 2019 GitHub, Inc.
category: Language
build-type: Simple
stability: alpha
extra-source-files: README.md

tested-with: GHC == 8.6.5

common haskell
default-language: Haskell2010
ghc-options:
-Weverything
-Wno-missing-local-signatures
-Wno-missing-import-lists
-Wno-implicit-prelude
-Wno-safe
-Wno-unsafe
-Wno-name-shadowing
-Wno-monomorphism-restriction
-Wno-missed-specialisations
-Wno-all-missed-specialisations
-Wno-star-is-type
if (impl(ghc >= 8.8))
ghc-options: -Wno-missing-deriving-strategies
if (impl(ghc >= 8.10))
ghc-options:
-Wno-missing-safe-haskell-mode
-Wno-prepositive-qualified-module
cpp-options:
-DBAZEL_BUILD=0

library
import: haskell
exposed-modules:
Language.OCaml
Language.OCaml.AST
Language.OCaml.Grammar
Language.OCaml.Tags
hs-source-dirs: src
build-depends:
, base >= 4.13 && < 5
, fused-effects ^>= 1.1
, semantic-ast
, semantic-proto ^>= 0
, semantic-source ^>= 0.1.0.1
, semantic-tags ^>= 0.0
, template-haskell >= 2.15 && < 2.17
, text ^>= 1.2.3
, tree-sitter ^>= 0.9.0.0
, tree-sitter-ocaml ^>= 0.1

test-suite test
import: haskell
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: PreciseTest.hs
build-depends:
, base
, pathtype ^>= 0.8.1
, semantic-ast
, semantic-ocaml
, tasty
, tree-sitter-ocaml ^>= 0.1
27 changes: 27 additions & 0 deletions semantic-ocaml/src/Language/OCaml.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
-- | Semantic functionality for OCaml programs.
module Language.OCaml
( Term(..)
, Language.OCaml.Grammar.tree_sitter_ocaml
) where

import AST.Marshal.JSON
import qualified AST.Unmarshal as TS
import Data.Proxy
import qualified Language.OCaml.AST as OCaml
import qualified Language.OCaml.Grammar (tree_sitter_ocaml)
import qualified Language.OCaml.Tags as OCamlTags
import qualified Tags.Tagging.Precise as Tags

newtype Term a = Term { getTerm :: OCaml.CompilationUnit a }
deriving MarshalJSON

instance TS.SymbolMatching Term where
matchedSymbols _ = TS.matchedSymbols (Proxy :: Proxy OCaml.CompilationUnit)
showFailure _ = TS.showFailure (Proxy :: Proxy OCaml.CompilationUnit)

instance TS.Unmarshal Term where
matchers = fmap (fmap (TS.hoist Term)) TS.matchers

instance Tags.ToTags Term where
tags src = Tags.runTagging src . OCamlTags.tags . getTerm
33 changes: 33 additions & 0 deletions semantic-ocaml/src/Language/OCaml/AST.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}

module Language.OCaml.AST
( module Language.OCaml.AST
, OCaml.getTestCorpusDir
) where

import Prelude hiding (False, Float, Integer, String, True, Functor)
import AST.GenerateSyntax
import Language.Haskell.TH.Syntax (runIO)
import qualified TreeSitter.OCaml as OCaml (getNodeTypesPath, getTestCorpusDir, tree_sitter_ocaml)

#ifdef NODE_TYPES_PATH
astDeclarationsForLanguage OCaml.tree_sitter_ocaml
#else
runIO OCaml.getNodeTypesPath >>= astDeclarationsForLanguage OCaml.tree_sitter_ocaml
#endif
12 changes: 12 additions & 0 deletions semantic-ocaml/src/Language/OCaml/Grammar.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{-# LANGUAGE TemplateHaskell #-}
module Language.OCaml.Grammar
( tree_sitter_ocaml
, Grammar(..)
) where

import AST.Grammar.TH
import Language.Haskell.TH
import TreeSitter.OCaml (tree_sitter_ocaml)

-- | Statically-known rules corresponding to symbols in the grammar.
mkStaticallyKnownRuleGrammarData (mkName "Grammar") tree_sitter_ocaml
Loading