1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 10:15:55 +03:00

Add CodeQL; bump semantic-source

This commit is contained in:
Rick Winfrey 2020-03-20 14:26:42 -07:00
parent d4e6951e16
commit d0fca24ef5
4 changed files with 27 additions and 13 deletions

View File

@ -1,7 +1,7 @@
cabal-version: 2.4
name: semantic-source
version: 0.1.0.0
version: 0.0.3.0
synopsis: Types and functionality for working with source code
description: Types and functionality for working with source code (program text).
homepage: https://github.com/github/semantic/tree/master/semantic-source#readme
@ -55,7 +55,7 @@ library
, containers ^>= 0.6.2
, generic-monoid ^>= 0.1.0.0
, hashable >= 1.2.7 && < 1.4
, lingo ^>= 0.3
, lingo ^>= 0.3.2.0
, pathtype ^>= 0.8.1
, semilattices ^>= 0.0.0.3
, text ^>= 1.2.3.1

View File

@ -26,6 +26,7 @@ import qualified System.Path.PartClass as Path.PartClass
-- | The various languages we support.
data Language
= Unknown
| CodeQL
| Go
| Haskell
| Java
@ -33,10 +34,10 @@ data Language
| JSON
| JSX
| Markdown
| PHP
| Python
| Ruby
| TypeScript
| PHP
| TSX
deriving (Eq, Generic, Ord, Read, Show, Bounded, Hashable, ToJSON, Enum)
@ -47,6 +48,9 @@ class SLanguage (lang :: Language) where
instance SLanguage 'Unknown where
reflect _ = Unknown
instance SLanguage 'CodeQL where
reflect _ = CodeQL
instance SLanguage 'Go where
reflect _ = Go
@ -68,6 +72,9 @@ instance SLanguage 'JSX where
instance SLanguage 'Markdown where
reflect _ = Markdown
instance SLanguage 'PHP where
reflect _ = PHP
instance SLanguage 'Python where
reflect _ = Python
@ -77,9 +84,6 @@ instance SLanguage 'Ruby where
instance SLanguage 'TypeScript where
reflect _ = TypeScript
instance SLanguage 'PHP where
reflect _ = PHP
instance FromJSON Language where
parseJSON = withText "Language" $ \l ->
pure $ textToLanguage l
@ -106,6 +110,7 @@ forPath path =
languageToText :: Language -> T.Text
languageToText = \case
Unknown -> "Unknown"
CodeQL -> "CodeQL"
Go -> "Go"
Haskell -> "Haskell"
Java -> "Java"
@ -113,14 +118,15 @@ languageToText = \case
JSON -> "JSON"
JSX -> "JSX"
Markdown -> "Markdown"
PHP -> "PHP"
Python -> "Python"
Ruby -> "Ruby"
TypeScript -> "TypeScript"
TSX -> "TSX"
PHP -> "PHP"
textToLanguage :: T.Text -> Language
textToLanguage = \case
"CodeQL" -> CodeQL
"Go" -> Go
"Haskell" -> Haskell
"Java" -> Java
@ -128,9 +134,9 @@ textToLanguage = \case
"JSON" -> JSON
"JSX" -> JSX
"Markdown" -> Markdown
"PHP" -> PHP
"Python" -> Python
"Ruby" -> Ruby
"TypeScript" -> TypeScript
"TSX" -> TSX
"PHP" -> PHP
_ -> Unknown

View File

@ -1,5 +1,4 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveGeneric, GeneralizedNewtypeDeriving #-}
{-|
'Source' models source code, represented as a thin wrapper around a 'B.ByteString' with conveniences for splitting by line, slicing, etc.
@ -38,7 +37,7 @@ import Data.Aeson (FromJSON (..), withText)
import qualified Data.ByteString as B
import Data.Char (ord)
import Data.Maybe (fromMaybe)
import Data.Monoid (Last (..))
import Data.Monoid (Last(..))
import Data.Semilattice.Lower
import Data.String (IsString (..))
import qualified Data.Text as T
@ -46,7 +45,7 @@ import qualified Data.Text.Encoding as T
import Data.Text.Encoding.Error (lenientDecode)
import GHC.Generics (Generic)
import Source.Range
import Source.Span (Pos (..), Span (Span))
import Source.Span (Span(Span), Pos(..))
-- | The contents of a source file. This is represented as a UTF-8
@ -76,7 +75,7 @@ totalRange = Range 0 . B.length . bytes
-- | Return a 'Span' that covers the entire text.
totalSpan :: Source -> Span
totalSpan source = Span (Pos 1 1) (Pos (Prelude.length ranges) (succ (end lastRange - start lastRange))) where
totalSpan source = Span lowerBound (Pos (Prelude.length ranges) (succ (end lastRange - start lastRange))) where
ranges = lineRanges source
lastRange = fromMaybe lowerBound (getLast (foldMap (Last . Just) ranges))

View File

@ -18,6 +18,7 @@ import Control.DeepSeq (NFData)
import Data.Aeson ((.:), (.=))
import qualified Data.Aeson as A
import Data.Hashable (Hashable)
import Data.Semilattice.Lower (Lower (..))
import GHC.Generics (Generic)
import GHC.Stack (SrcLoc (..))
@ -45,6 +46,10 @@ instance A.FromJSON Span where
<$> o .: "start"
<*> o .: "end"
instance Lower Span where
lowerBound = Span lowerBound lowerBound
-- | Construct a Span with a given value for both its start and end positions.
point :: Pos -> Span
point p = Span p p
@ -78,6 +83,10 @@ instance A.FromJSON Pos where
[ line, col ] <- A.parseJSON arr
pure $ Pos line col
instance Lower Pos where
lowerBound = Pos 1 1
line_, column_ :: Lens' Pos Int
line_ = lens line (\p l -> p { line = l })
column_ = lens column (\p l -> p { column = l })