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
29 p_Tiny = p_CmdSeq P.<* P.eof -- (!. / %{eof})
30 p_CmdSeq = P.some (p_Cmd p_SEMICOLON{-^cmdSeq-})
31 p_Cmd = p_IfCmd <|> p_RepeatCmd / p_ReadCmd / p_WriteCmd / p_AssignCmd
33 -- conditional :: Eq a => [Production (a -> Bool)] -> [repr b] -> repr a -> repr b -> repr b
36 [p_IfCmd, p_RepeatCmd, p_ReadCmd, p_WriteCmd, p_AssignCmd]
40 ((\c -> haskell c [||c||]) Prelude.<$> ["", ])
41 (look anyChar) op empty
44 p_IfCmd = p_IF p_Exp^ifExp p_THEN^ifThen p_CmdSeq^ifThenCmdSeq (p_ELSE p_CmdSeq^ifElseCmdSeq / '') p_END^ifEnd
45 p_RepeatCmd = p_REPEAT p_CmdSeq^repeatCmdSeq p_UNTIL^repeatUntil p_Exp^repeatExp
46 p_AssignCmd = p_NAME p_ASSIGNMENT^assignOp p_Exp^assignExp
47 p_ReadCmd = p_READ p_NAME^readName
48 p_WriteCmd = p_WRITE p_Exp^writeExp
49 p_Exp = p_SimpleExp ((p_LESS / p_EQUAL) p_SimpleExp^simpleExp / '')
50 p_SimpleExp = p_Term ((p_ADD / p_SUB) p_Term^term)*
51 p_Term = p_Factor ((p_MUL / p_DIV) p_Factor^factor)*
52 p_Factor = p_OPENPAR p_Exp^openParExp p_CLOSEPAR^closePar / p_NUMBER / p_NAME
54 p_ASSIGNMENT = lex ":="
63 p_NAME = !p_RESERVED lex (some (oneOf ['a' .. 'z']))
64 p_NUMBER = lex (some (oneOf ['0' .. '9']))
67 p_REPEAT = lex "repeat"
73 p_RESERVED = (p_IF / p_ELSE / p_END / p_READ / p_REPEAT / p_THEN / p_UNTIL / p_WRITE) ![a-z]+
74 p_Sp = (" " P.<|> "\n")*