-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseStim.hs
104 lines (77 loc) · 3.04 KB
/
ParseStim.hs
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module ParseStim where
import GenerateStim hiding (main)
import Text.ParserCombinators.Parsec hiding (space, spaces)
import Data.Char
import Numeric
import Control.Monad
type ErrMsg = String
--fromStim "asd" = 1
-- Converts a Stim file to a bunch of function calls
--fromStim :: String -> Either ErrMsg String
--fromStim s = case s of
-- TODO: use tagless final for the above too?
data Statement
= IntAssignment String Int
| Delay Int
deriving Show
--This will reset the state back if parsing fails for either parser a or b. For the normal choice operator: if the parser consumed input it will not reset the state back!
a <||> b = try a <|> b
--End of Line
eol = char '\n'
-- *> is then (sequencing).
-- a comment can be many of anything exceot \n, then a newline.
commentParser = symb "//" *> many (noneOf "\n") *> eol
whitespace = oneOf " \n\r\t" <|> commentParser <?> "whitespace"
space :: Parser () -- () for unit because we don't want it to return anything.
space = skipMany whitespace
-- Takes a String and makes a parser that parses that string and then whitespace after it, but only returns the parsed string and not the whitespace that came after [via tok].
symb :: String -> Parser String
symb = tok . string
-- https://stackoverflow.com/questions/7215967/adding-readhex-to-a-parsec-parser
parseHex = do
char '0'
char 'x'
digits <- many1 hexDigit -- hexDigit is defined by Parsec.
return (fst (readHex digits !! 0))
-- Parse decimal numbers (base 10)
parseDec :: Parser Int
parseDec = read <$> many1 digit
-- Parse hex or regular base 10 digits
nat :: Parser Int
nat = parseHex <||> parseDec
-- Like "nat" but consumes trailing whitespace.
natural = tok nat
digitp :: Char -> Bool
digitp x = x `elem` ['0' .. '9']
-- Transforms a parser into one that also consumes trailing whitespace. runs parser p, then runs the space parser but returns only the result returned from p, ignoring the spaces.
tok p = p <* space
-- The initial character of an indentifier must satisfy this predicate.
initp x = isAlpha x || elem x "_"
-- The subsequent characters of an indentifier must satisfy this predicate.
-- These subsequent characters can be what the initial characters can be, or also digits.
subseqp c = initp c || digitp c
-- An identifier without considering trailing whitespace.
ident = do
x <- satisfy initp
xs <- many (satisfy subseqp)
return (x:xs)
identifier = tok ident
intAssignment = do
--lhs = Left Hand Side
lhs <- identifier
symb "="
rhs <- natural
return (IntAssignment lhs rhs)
delayParser = do
symb "#"
delay_ <- natural
return (Delay delay_)
-- "<*" == can be pronounced "before". << is for monads but this is for applicatives.
parseStim = space *> (intAssignment <||> delayParser) `sepBy` space <* eof
runStimParser str = parse parseStim "" str
main = do
contents <- readFile "LogFile.stim"
let result = runStimParser contents
case result of
Right res -> putStrLn (show res)
Left err -> print err