Thanks to visit codestin.com
Credit goes to github.com

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

Commit e96832f

Browse files
authored
Merge pull request #269 from github/semantic-source
Factor source code-related facilities into a new package
2 parents af35b22 + 77ff50b commit e96832f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+999
-1015
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ script:
3939
- cabal new-run semantic-core:spec
4040
- cabal new-run semantic-core:doctest
4141
- cabal new-run semantic-python:test
42+
- cabal new-run semantic-source:test
43+
- cabal new-run semantic-source:doctest
4244
# parse-examples is disabled because it slaughters our CI
4345
# - cabal new-run semantic:parse-examples
4446

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
packages: . semantic-core semantic-python
1+
packages: . semantic-core semantic-python semantic-source
22

33
jobs: $ncpus
44

docs/assignment.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ someLanguageConstruct :: Assignment
2929
someLanguageConstruct = makeTerm <$> symbol NodeNameOfSymbolToMatch <*> children (SyntaxDataType <$> field1 <*> field2)
3030
```
3131

32-
The building blocks that compose this DSL come from: `Assigning.Assignment`, explained below.
32+
The building blocks that compose this DSL come from: `Assigning.Assignment`, explained below.
3333

3434
### The underlying machinery of `Assigning.Assignment`
3535

@@ -73,7 +73,7 @@ TODO: explain how traversal works in terms of matching/advancing -->
7373

7474
#### Ways to combine assignments
7575

76-
1. The `Functor` instance maps values from the AST (`Location`, `ByteString`, etc.) onto another structure.
76+
1. The `Functor` instance maps values from the AST (`Loc`, `ByteString`, etc.) onto another structure.
7777

7878
2. The `Applicative` instance assigns sequences of (sibling) AST nodes in order, as well as providing `pure` assignments.
7979

semantic-core/test/Generators.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ module Generators
1212
, expr
1313
) where
1414

15-
import Prelude hiding (span)
16-
1715
import Hedgehog hiding (Var)
1816
import qualified Hedgehog.Gen as Gen
1917
import qualified Hedgehog.Range as Range

semantic-source/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2015-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-source/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# semantic-source
2+
3+
Types and functionality for working with source code (program text).
4+
5+
6+
## Development
7+
8+
This project consists of a Haskell package named `semantic-source`. The library’s sources are in [`src`][].
9+
10+
Development of `semantic-source` is typically done using `cabal v2-build`:
11+
12+
```shell
13+
cabal v2-build # build the library
14+
cabal v2-repl # load the package into ghci
15+
cabal v2-test # build and run the doctests
16+
```
17+
18+
[`src`]: https://github.com/github/semantic/tree/master/semantic-source/src

semantic-source/Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
cabal-version: 2.4
2+
3+
name: semantic-source
4+
version: 0.0.0.0
5+
synopsis: Types and functionality for working with source code
6+
description: Types and functionality for working with source code (program text).
7+
homepage: https://github.com/github/semantic/tree/master/semantic-source#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: Data
15+
build-type: Simple
16+
stability: alpha
17+
extra-source-files:
18+
README.md
19+
20+
21+
tested-with:
22+
GHC == 8.6.5
23+
24+
common common
25+
default-language: Haskell2010
26+
ghc-options:
27+
-Weverything
28+
-Wno-missing-local-signatures
29+
-Wno-missing-import-lists
30+
-Wno-implicit-prelude
31+
-Wno-safe
32+
-Wno-unsafe
33+
-Wno-name-shadowing
34+
-Wno-monomorphism-restriction
35+
-Wno-missed-specialisations
36+
-Wno-all-missed-specialisations
37+
-Wno-star-is-type
38+
39+
library
40+
import: common
41+
exposed-modules:
42+
Source.Loc
43+
Source.Range
44+
Source.Source
45+
Source.Span
46+
-- other-modules:
47+
-- other-extensions:
48+
build-depends:
49+
aeson ^>= 1.4.2.0
50+
, base >= 4.12 && < 5
51+
, bytestring ^>= 0.10.8.2
52+
, deepseq ^>= 1.4.4.0
53+
, generic-monoid ^>= 0.1.0.0
54+
, hashable ^>= 1.2.7.0
55+
, semilattices ^>= 0.0.0.3
56+
, text ^>= 1.2.3.1
57+
hs-source-dirs: src
58+
59+
test-suite doctest
60+
import: common
61+
type: exitcode-stdio-1.0
62+
main-is: Doctest.hs
63+
build-depends:
64+
base
65+
, doctest >= 0.7 && <1.0
66+
, QuickCheck
67+
, semantic-source
68+
hs-source-dirs: test
69+
70+
test-suite test
71+
import: common
72+
type: exitcode-stdio-1.0
73+
main-is: Test.hs
74+
other-modules:
75+
Source.Test
76+
build-depends:
77+
base
78+
, hedgehog ^>= 1
79+
, semantic-source
80+
, tasty >= 1.2 && <2
81+
, tasty-hedgehog ^>= 1.0.0.1
82+
, tasty-hunit >= 0.10 && <1
83+
, text
84+
hs-source-dirs: test
85+
86+
source-repository head
87+
type: git
88+
location: https://github.com/github/semantic

semantic-source/src/Source/Loc.hs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{-# LANGUAGE DeriveGeneric, DerivingVia, RankNTypes #-}
2+
module Source.Loc
3+
( Loc(..)
4+
, byteRange_
5+
, Span(Span)
6+
, Range(Range)
7+
) where
8+
9+
import Control.DeepSeq (NFData)
10+
import Data.Hashable (Hashable)
11+
import Data.Monoid.Generic
12+
import GHC.Generics (Generic)
13+
import Prelude hiding (span)
14+
import Source.Range
15+
import Source.Span
16+
17+
data Loc = Loc
18+
{ byteRange :: {-# UNPACK #-} !Range
19+
, span :: {-# UNPACK #-} !Span
20+
}
21+
deriving (Eq, Ord, Show, Generic)
22+
deriving Semigroup via GenericSemigroup Loc
23+
24+
instance Hashable Loc
25+
instance NFData Loc
26+
27+
instance HasSpan Loc where
28+
span_ = lens span (\l s -> l { span = s })
29+
{-# INLINE span_ #-}
30+
31+
32+
byteRange_ :: Lens' Loc Range
33+
byteRange_ = lens byteRange (\l r -> l { byteRange = r })
34+
35+
36+
type Lens' s a = forall f . Functor f => (a -> f a) -> (s -> f s)
37+
38+
lens :: (s -> a) -> (s -> a -> s) -> Lens' s a
39+
lens get put afa s = fmap (put s) (afa (get s))
40+
{-# INLINE lens #-}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{-# LANGUAGE DeriveGeneric, RankNTypes #-}
2+
module Source.Range
3+
( Range(..)
4+
, point
5+
, rangeLength
6+
, subtractRange
7+
-- * Lenses
8+
, start_
9+
, end_
10+
) where
11+
12+
import Control.DeepSeq (NFData)
13+
import Data.Hashable (Hashable)
14+
import Data.Semilattice.Lower (Lower(..))
15+
import GHC.Generics (Generic)
16+
17+
-- | A 0-indexed, half-open interval of integers, defined by start & end indices.
18+
data Range = Range
19+
{ start :: {-# UNPACK #-} !Int
20+
, end :: {-# UNPACK #-} !Int
21+
}
22+
deriving (Eq, Generic, Ord, Show)
23+
24+
instance Hashable Range
25+
instance NFData Range
26+
27+
-- $
28+
-- prop> a <> (b <> c) === (a <> b) <> (c :: Range)
29+
instance Semigroup Range where
30+
Range start1 end1 <> Range start2 end2 = Range (min start1 start2) (max end1 end2)
31+
32+
instance Lower Range where
33+
lowerBound = Range 0 0
34+
35+
36+
-- | Construct a 'Range' with a given value for both its start and end indices.
37+
point :: Int -> Range
38+
point i = Range i i
39+
40+
-- | Return the length of the range.
41+
rangeLength :: Range -> Int
42+
rangeLength range = end range - start range
43+
44+
subtractRange :: Range -> Range -> Range
45+
subtractRange range1 range2 = Range (start range1) (end range1 - rangeLength (Range (start range2) (max (end range1) (end range2))))
46+
47+
48+
start_, end_ :: Lens' Range Int
49+
start_ = lens start (\r s -> r { start = s })
50+
end_ = lens end (\r e -> r { end = e })
51+
52+
53+
type Lens' s a = forall f . Functor f => (a -> f a) -> (s -> f s)
54+
55+
lens :: (s -> a) -> (s -> a -> s) -> Lens' s a
56+
lens get put afa s = fmap (put s) (afa (get s))
57+
{-# INLINE lens #-}
58+
59+
60+
-- $setup
61+
-- >>> import Test.QuickCheck
62+
-- >>> instance Arbitrary Range where arbitrary = Range <$> arbitrary <*> arbitrary ; shrink (Range s e) = Range <$> shrink s <*> shrink e

0 commit comments

Comments
 (0)