Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 80e294d

Browse files
committed
Add semantic-c
Just basic parsing support, no tags.
1 parent ab9cdc0 commit 80e294d

File tree

19 files changed

+5657
-1
lines changed

19 files changed

+5657
-1
lines changed

.bazelignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
semantic/dist-newstyle
22
semantic-analysis/dist-newstyle
33
semantic-ast/dist-newstyle
4+
semantic-c/dist-newstyle
45
semantic-codeql/dist-newstyle
56
semantic-core/dist-newstyle
67
semantic-go/dist-newstyle

.github/workflows/haskell.yml

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ jobs:
5757
cabal v2-build --project-file=cabal.project.ci semantic:exe:semantic
5858
cabal v2-run --project-file=cabal.project.ci semantic:test
5959
cabal v2-run --project-file=cabal.project.ci semantic-tags:test
60+
cabal v2-run --project-file=cabal.project.ci semantic-c:test
6061
cabal v2-run --project-file=cabal.project.ci semantic-codeql:test
6162
cabal v2-run --project-file=cabal.project.ci semantic-go:test
6263
cabal v2-run --project-file=cabal.project.ci semantic-java:test

cabal.project

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
packages: semantic
55
semantic-analysis
66
semantic-ast
7+
semantic-c
78
semantic-codeql
89
semantic-core
910
semantic-go

cabal.project.ci

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
packages: semantic
55
semantic-analysis
66
semantic-ast
7+
semantic-c
78
semantic-codeql
89
semantic-core
910
semantic-go
@@ -31,6 +32,9 @@ package semantic-analysis
3132
package semantic-ast
3233
ghc-options: -Werror
3334

35+
package semantic-c
36+
ghc-options: -Werror
37+
3438
package semantic-codeql
3539
ghc-options: -Werror
3640

script/ghci-flags

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ function flags {
5656
echo "-isemantic/test"
5757
echo "-isemantic-analysis/src"
5858
echo "-isemantic-ast/src"
59+
echo "-isemantic-c/src"
60+
echo "-isemantic-c/test"
5961
echo "-isemantic-codeql/src"
6062
echo "-isemantic-codeql/test"
6163
echo "-isemantic-core/src"

script/ghci-flags-dependencies

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ echo "semantic-analysis/semantic-analysis.cabal"
1212
echo "semantic-ast/semantic-ast.cabal"
1313
echo "semantic-core/semantic-core.cabal"
1414
echo "semantic-tags/semantic-tags.cabal"
15+
echo "semantic-c/semantic-c.cabal"
1516
echo "semantic-go/semantic-go.cabal"
1617
echo "semantic-java/semantic-java.cabal"
1718
echo "semantic-json/semantic-json.cabal"

semantic-ast/BUILD.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ haskell_library(
4949
)
5050

5151
all_ts_deps = ["@tree-sitter-{name}".format(name = name) for name in [
52+
"c",
5253
"go",
5354
"java",
5455
"json",
@@ -62,6 +63,7 @@ all_ts_deps = ["@tree-sitter-{name}".format(name = name) for name in [
6263
]]
6364

6465
all_file_deps = ["@tree-sitter-{name}//:src/node-types.json".format(name = name) for name in [
66+
"c",
6567
"go",
6668
"java",
6769
"json",

semantic-ast/app/Main.hs

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import System.Exit
3535
import System.IO
3636
import System.Process
3737
import Text.Printf
38+
import qualified TreeSitter.C as C (tree_sitter_c)
3839
import qualified TreeSitter.Go as Go (tree_sitter_go)
3940
import qualified TreeSitter.JSON as JSON (tree_sitter_json)
4041
import qualified TreeSitter.Java as Java (tree_sitter_java)
@@ -72,6 +73,7 @@ pathForLanguage :: Bazel.Runfiles -> Language -> FilePath
7273
pathForLanguage rf =
7374
let loc = Bazel.rlocation rf
7475
in \case
76+
C -> loc "tree-sitter-c/vendor/tree-sitter-c/src/node-types.json"
7577
CodeQL -> loc "tree-sitter-ql/vendor/tree-sitter-ql/src/node-types.json"
7678
Go -> loc "tree-sitter-go/vendor/tree-sitter-go/src/node-types.json"
7779
PHP -> loc "tree-sitter-php/vendor/tree-sitter-php/src/node-types.json"
@@ -88,6 +90,7 @@ targetForLanguage :: Language -> FilePath
8890
targetForLanguage x =
8991
let go lc = printf "semantic-%s/src/Language/%s/AST.hs" (lc :: String) (show x)
9092
in case x of
93+
C -> go "c"
9194
CodeQL -> go "codeql"
9295
Go -> go "go"
9396
PHP -> go "php"
@@ -102,6 +105,7 @@ targetForLanguage x =
102105
parserForLanguage :: Language -> Ptr TreeSitter.Language.Language
103106
parserForLanguage = \case
104107
Unknown -> error "Unknown language encountered"
108+
C -> C.tree_sitter_c
105109
CodeQL -> (CodeQL.tree_sitter_ql)
106110
Go -> Go.tree_sitter_go
107111
Haskell -> error "Haskell backend not implemented yet"
@@ -121,7 +125,7 @@ parserForLanguage = \case
121125
-- CodeQL -> r
122126

123127
validLanguages :: [Language]
124-
validLanguages = [CodeQL, Go, Java, PHP, Python, Ruby, TypeScript, TSX]
128+
validLanguages = [C, CodeQL, Go, Java, PHP, Python, Ruby, TypeScript, TSX]
125129

126130
emit :: FilePath -> Language -> IO ()
127131
emit root lang = do

semantic-c/BUILD.bazel

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load(
4+
"//:build/common.bzl",
5+
"semantic_language_library",
6+
"semantic_language_parsing_test",
7+
)
8+
9+
semantic_language_library(
10+
name = "semantic-c",
11+
srcs = glob(["src/**/*.hs"]),
12+
language = "c",
13+
)
14+
15+
semantic_language_parsing_test(
16+
language = "c",
17+
semantic_package = "c",
18+
ts_package = "c",
19+
)

semantic-c/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 GitHub
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

semantic-c/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Semantic support for C
2+
3+
This package implements `semantic` support for C using the `semantic-core` intermediate language.

semantic-c/Setup.hs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

semantic-c/semantic-c.cabal

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
cabal-version: 2.4
2+
3+
name: semantic-c
4+
version: 0.0.0.0
5+
synopsis: Semantic support for C.
6+
description: Semantic support for C using the semantic-core intermediate language.
7+
homepage: https://github.com/github/semantic/tree/master/semantic-c#readme
8+
bug-reports: https://github.com/github/semantic/issues
9+
license: MIT
10+
license-file: LICENSE
11+
author: The Semantic authors
12+
maintainer: [email protected]
13+
copyright: (c) 2019 GitHub, Inc.
14+
category: Language
15+
build-type: Simple
16+
stability: alpha
17+
extra-source-files: README.md
18+
19+
tested-with: GHC == 8.6.5
20+
21+
common haskell
22+
default-language: Haskell2010
23+
ghc-options:
24+
-Weverything
25+
-Wno-missing-local-signatures
26+
-Wno-missing-import-lists
27+
-Wno-implicit-prelude
28+
-Wno-safe
29+
-Wno-unsafe
30+
-Wno-name-shadowing
31+
-Wno-monomorphism-restriction
32+
-Wno-missed-specialisations
33+
-Wno-all-missed-specialisations
34+
-Wno-star-is-type
35+
if (impl(ghc >= 8.8))
36+
ghc-options: -Wno-missing-deriving-strategies
37+
if (impl(ghc >= 8.10))
38+
ghc-options:
39+
-Wno-missing-safe-haskell-mode
40+
-Wno-prepositive-qualified-module
41+
cpp-options:
42+
-DBAZEL_BUILD=0
43+
44+
library
45+
import: haskell
46+
exposed-modules:
47+
Language.C
48+
Language.C.AST
49+
Language.C.Grammar
50+
Language.C.Tags
51+
hs-source-dirs: src
52+
build-depends:
53+
, base >= 4.13 && < 5
54+
, fused-effects ^>= 1.1
55+
, semantic-ast
56+
, semantic-source ^>= 0.1.0.1
57+
, semantic-tags ^>= 0.0
58+
, template-haskell >= 2.15 && < 2.17
59+
, text ^>= 1.2.3
60+
, tree-sitter ^>= 0.9
61+
, tree-sitter-c ^>= 0.1.0.0
62+
63+
test-suite test
64+
import: haskell
65+
type: exitcode-stdio-1.0
66+
hs-source-dirs: test
67+
main-is: PreciseTest.hs
68+
build-depends:
69+
, base
70+
, pathtype ^>= 0.8.1
71+
, semantic-ast
72+
, semantic-c
73+
, tasty
74+
, tree-sitter-c ^>= 0.1.0.0

semantic-c/src/Language/C.hs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2+
{-# OPTIONS_GHC -freduction-depth=0 #-}
3+
-- | Semantic functionality for C programs.
4+
module Language.C
5+
( Term(..)
6+
, TreeSitter.C.tree_sitter_c
7+
) where
8+
9+
import AST.Marshal.JSON
10+
import qualified AST.Unmarshal as TS
11+
import Data.Proxy
12+
import qualified Language.C.AST as C
13+
import qualified Language.C.Tags as CTags
14+
import qualified Tags.Tagging.Precise as Tags
15+
import qualified TreeSitter.C (tree_sitter_c)
16+
17+
newtype Term a = Term { getTerm :: C.TranslationUnit a }
18+
deriving MarshalJSON
19+
20+
instance TS.SymbolMatching Term where
21+
matchedSymbols _ = TS.matchedSymbols (Proxy :: Proxy C.TranslationUnit)
22+
showFailure _ = TS.showFailure (Proxy :: Proxy C.TranslationUnit)
23+
24+
instance TS.Unmarshal Term where
25+
matchers = fmap (fmap (TS.hoist Term)) TS.matchers
26+
27+
instance Tags.ToTags Term where
28+
tags src = Tags.runTagging src . CTags.tags . getTerm

0 commit comments

Comments
 (0)