aboutsummaryrefslogtreecommitdiff
path: root/src/Language/SimpleShell/Parser.hs
blob: 32015f7275f3128a329b7408e51fcbd731f4b6de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}

module Language.SimpleShell.Parser
  ( MainParser
  , lexeme
  , symbol
  , commentFirstChars
  )
where


import Control.Monad (void)
import Data.Text (Text)
import Data.Void (Void)
import Text.Megaparsec (MonadParsec, Parsec)
import Text.Megaparsec.Char (space1)
import qualified Text.Megaparsec.Char.Lexer as L
  ( lexeme
  , symbol
  , space
  , skipLineComment
  , skipBlockComment
  )


type MainParser = Parsec Void Text


-- Must be kept in sync with 'commentFirstChars'.
sc :: (MonadParsec e Text m) => m ()
sc = L.space
  space1
  (L.skipLineComment "//")
  (L.skipBlockComment "/*" "*/")

-- | List of all characters that may start a comment.
commentFirstChars :: [Char]
commentFirstChars = "/"

lexeme :: (MonadParsec e Text m) => m a -> m a
lexeme = L.lexeme sc

symbol :: (MonadParsec e Text m) => Text -> m ()
symbol = void . L.symbol sc