Skip to content

Commit 7593b01

Browse files
committed
Add a ModuleParser
1 parent e92258b commit 7593b01

File tree

4 files changed

+103
-7
lines changed

4 files changed

+103
-7
lines changed

src/Docs/Search/ModuleParser.purs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module Docs.Search.ModuleParser where
2+
3+
import Prelude
4+
5+
import Control.Alt ((<|>))
6+
import Data.Either (hush)
7+
import Data.Foldable (intercalate)
8+
import Data.Maybe (Maybe)
9+
import Data.Newtype (wrap)
10+
import Docs.Search.Types (ModuleName)
11+
import StringParser (Parser, char, choice, fix, many, manyTill, noneOf, regex, runParser, sepBy, sepBy1, string, try, whiteSpace)
12+
13+
parseModuleName :: String -> Maybe ModuleName
14+
parseModuleName = map wrap <<< hush <<< runParser do
15+
void $ many whiteSpaceOrComment
16+
moduleHeader
17+
18+
whiteSpaceOrComment :: Parser Unit
19+
whiteSpaceOrComment =
20+
choice
21+
[ try $ multiLineComment
22+
, try $ singleLineComment
23+
, try $ void whiteSpace
24+
]
25+
26+
multiLineComment :: Parser Unit
27+
multiLineComment = do
28+
void $ string "{-"
29+
void $ manyTill (void $ noneOf []) (string "-}")
30+
31+
singleLineComment :: Parser Unit
32+
singleLineComment = do
33+
void $ string "--"
34+
void $ manyTill (void $ noneOf ['\n']) (char '\n')
35+
36+
37+
moduleHeader :: Parser String
38+
moduleHeader = do
39+
void $ string "module"
40+
void $ many whiteSpaceOrComment
41+
moduleName
42+
43+
moduleName :: Parser String
44+
moduleName = sepBy1 moduleNameWord (string ".") <#> intercalate "."
45+
46+
moduleNameWord :: Parser String
47+
moduleNameWord = do
48+
first <- regex "[A-Z]"
49+
rest <- regex "[a-z0-9]*"
50+
pure $ first <> rest

src/Docs/Search/Types.purs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ derive newtype instance ordModuleName :: Ord ModuleName
3131
derive newtype instance decodeJsonModuleName :: DecodeJson ModuleName
3232
derive newtype instance encodeJsonModuleName :: EncodeJson ModuleName
3333

34+
instance Show ModuleName where
35+
show = genericShow
36+
3437

3538
-- | Non-normalized package name, e.g. `purescript-prelude` or just `prelude`.
3639
newtype RawPackageName = RawPackageName String
@@ -55,14 +58,14 @@ data PackageInfo = LocalPackage | Builtin | Package PackageName | UnknownPackage
5558
derive instance eqPackageInfo :: Eq PackageInfo
5659
derive instance ordPackageInfo :: Ord PackageInfo
5760
derive instance genericPackageInfo :: Generic PackageInfo _
58-
instance showPackageInfo :: Show PackageInfo where
59-
show = genericShow
60-
6161
instance decodeJsonPackageInfo :: DecodeJson PackageInfo where
6262
decodeJson = genericDecodeJson
6363
instance encodeJsonPackageInfo :: EncodeJson PackageInfo where
6464
encodeJson = genericEncodeJson
6565

66+
instance showPackageInfo :: Show PackageInfo where
67+
show = genericShow
68+
6669

6770
newtype PackageScore = PackageScore Int
6871

test/Main.purs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ module Test.Main where
33
import Prelude
44

55
import Effect (Effect)
6+
import Effect.Aff (launchAff_)
67
import Test.Declarations as Declarations
78
import Test.IndexBuilder as IndexBuilder
89
import Test.ModuleIndex as ModuleIndex
9-
import Test.TypeQuery as TypeQuery
10-
import Test.TypeJson as TypeJson
11-
-- import Test.UI as UI
10+
import Test.ModuleParser as ModuleParser
1211
import Test.Spec (Spec)
1312
import Test.Spec.Reporter.Console (consoleReporter)
1413
import Test.Spec.Runner (runSpec)
15-
import Effect.Aff (launchAff_)
14+
import Test.TypeJson as TypeJson
15+
import Test.TypeQuery as TypeQuery
1616

1717
main :: Effect Unit
1818
main = do
@@ -21,6 +21,7 @@ main = do
2121

2222
mainTest :: Spec Unit
2323
mainTest = do
24+
ModuleParser.tests
2425
TypeQuery.tests
2526
TypeJson.tests
2627
IndexBuilder.tests

test/Test/ModuleParser.purs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module Test.ModuleParser where
2+
3+
import Prelude
4+
5+
import Data.Either (Either(..))
6+
import Data.Maybe (Maybe(..))
7+
import Docs.Search.ModuleParser (multiLineComment, parseModuleName, singleLineComment)
8+
import Docs.Search.Types (ModuleName(..))
9+
import StringParser (runParser)
10+
import Test.Spec (Spec, describe, it)
11+
import Test.Spec.Assertions (shouldEqual)
12+
13+
14+
tests :: Spec Unit
15+
tests = do
16+
describe "ModuleParser" do
17+
it "test #0" do
18+
parseModuleName "module Foo" `shouldEqual` Just (ModuleName "Foo")
19+
it "test #1" do
20+
parseModuleName "module Foo.Bar.B" `shouldEqual` Just (ModuleName "Foo.Bar.B")
21+
it "test #2" do
22+
parseModuleName " module Foo" `shouldEqual` Just (ModuleName "Foo")
23+
it "test #3" do
24+
parseModuleName " {- asdas -} module Foo" `shouldEqual` Just (ModuleName "Foo")
25+
it "test #4" do
26+
parseModuleName "{--}module Foo" `shouldEqual` Just (ModuleName "Foo")
27+
it "test #5" do
28+
parseModuleName "--\nmodule Foo" `shouldEqual` Just (ModuleName "Foo")
29+
it "test #6" do
30+
parseModuleName "--\n-- foo\nmodule Foo" `shouldEqual` Just (ModuleName "Foo")
31+
it "test #7" do
32+
parseModuleName "-- \n -- foo\n {- bar -} --baz\n module Foo" `shouldEqual` Just (ModuleName "Foo")
33+
it "multiline comment #1" do
34+
runParser multiLineComment "{--}" `shouldEqual` Right unit
35+
it "multiline comment #1" do
36+
runParser multiLineComment "{- foo -}" `shouldEqual` Right unit
37+
it "multiline comment #1" do
38+
runParser multiLineComment "{- foo\nbar\nbar\n -}" `shouldEqual` Right unit
39+
it "single line comment #1" do
40+
runParser singleLineComment "-- asd\n" `shouldEqual` Right unit
41+
it "single line comment #1" do
42+
runParser singleLineComment "--\n" `shouldEqual` Right unit

0 commit comments

Comments
 (0)