1 {-# LANGUAGE NoMonomorphismRestriction #-}
2 {-# LANGUAGE Rank2Types #-}
3 {-# LANGUAGE TemplateHaskell #-}
4 {-# LANGUAGE UnboxedTuples #-}
5 module Parsers.Tiny where
7 import Control.Monad (Monad(..))
8 import Data.Char (Char)
9 import Data.Either (Either(..))
10 import Data.Function (($))
11 import Data.Semigroup (Semigroup(..))
12 import Data.String (String, IsString(..))
13 import Data.Text (Text)
14 import Data.Text.IO (readFile)
15 import System.IO (IO, FilePath)
17 import Test.Tasty.Golden
18 import Text.Show (Show(..))
19 import qualified Data.ByteString.Lazy as BSL
20 import qualified Data.IORef as IORef
21 import qualified Data.Text.Lazy as TL
22 import qualified Data.Text.Lazy.Encoding as TL
23 import qualified Language.Haskell.TH.Syntax as TH
25 import qualified Symantic.Parser as P
26 import qualified Symantic.Parser.Haskell as H
30 p_Tiny = p_CmdSeq P.<* P.eof -- (!. / %{eof})
31 p_CmdSeq = P.some (p_Cmd p_SEMICOLON{-^cmdSeq-})
32 p_Cmd = p_IfCmd <|> p_RepeatCmd / p_ReadCmd / p_WriteCmd / p_AssignCmd
34 -- conditional :: Eq a => [H.Haskell (a -> Bool)] -> [repr b] -> repr a -> repr b -> repr b
37 [p_IfCmd, p_RepeatCmd, p_ReadCmd, p_WriteCmd, p_AssignCmd]
41 ((\c -> haskell c [||c||]) Prelude.<$> ["", ])
42 (look anyChar) op empty
45 p_IfCmd = p_IF p_Exp^ifExp p_THEN^ifThen p_CmdSeq^ifThenCmdSeq (p_ELSE p_CmdSeq^ifElseCmdSeq / '') p_END^ifEnd
46 p_RepeatCmd = p_REPEAT p_CmdSeq^repeatCmdSeq p_UNTIL^repeatUntil p_Exp^repeatExp
47 p_AssignCmd = p_NAME p_ASSIGNMENT^assignOp p_Exp^assignExp
48 p_ReadCmd = p_READ p_NAME^readName
49 p_WriteCmd = p_WRITE p_Exp^writeExp
50 p_Exp = p_SimpleExp ((p_LESS / p_EQUAL) p_SimpleExp^simpleExp / '')
51 p_SimpleExp = p_Term ((p_ADD / p_SUB) p_Term^term)*
52 p_Term = p_Factor ((p_MUL / p_DIV) p_Factor^factor)*
53 p_Factor = p_OPENPAR p_Exp^openParExp p_CLOSEPAR^closePar / p_NUMBER / p_NAME
55 p_ASSIGNMENT = lex ":="
64 p_NAME = !p_RESERVED lex (some (oneOf ['a' .. 'z']))
65 p_NUMBER = lex (some (oneOf ['0' .. '9']))
68 p_REPEAT = lex "repeat"
74 p_RESERVED = (p_IF / p_ELSE / p_END / p_READ / p_REPEAT / p_THEN / p_UNTIL / p_WRITE) ![a-z]+
75 p_Sp = (" " P.<|> "\n")*