forked from DamienCassou/Functional-DiaSpec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.hs
98 lines (76 loc) · 3.15 KB
/
main.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
module Main where
import Text.ParserCombinators.Parsec
import qualified Text.ParserCombinators.Parsec.Token as P
import Text.ParserCombinators.Parsec.Language (haskellDef)
import Control.Applicative hiding ((<|>),many)
-- AST
data Document = Document [Context]
deriving Show
data DataType = String | Integer | Boolean | UserDT String
deriving Show
data Context = Context { ctName :: String
, ctType :: DataType
, ctBehavioralContracts :: [BehavioralContract] }
deriving Show
data BehavioralContract = Pull { bcDataRequirement :: String
, bcEmission :: Emission
}
| Push { bcActivation :: String
, bcDataRequirement :: String
, bcEmission :: Emission
}
deriving Show
data Emission = EmitEmpty | EmitSelf | EmitSelfOpt
deriving Show
-- Lexer
lexer = P.makeTokenParser haskellDef
whiteSpace= P.whiteSpace lexer
lexeme = P.lexeme lexer
symbol = P.symbol lexer
natural = P.natural lexer
parens = P.parens lexer
angles = P.angles lexer
braces = P.braces lexer
semi = P.semi lexer
identifier= P.identifier lexer
reserved = P.reserved lexer
reservedOp= P.reservedOp lexer
-- Parser
document :: Parser Document
document = Document <$> many context
dataTypeRef :: Parser DataType
dataTypeRef = (reserved "Boolean" *> return Boolean)
<|> (reserved "Integer" *> return Integer)
<?> "data type"
emission :: Parser Emission
emission = (reserved "empty" *> return EmitEmpty)
<|> (reserved "self" *> (option EmitSelf (char '?' *> return EmitSelfOpt)))
behavioralContract :: Parser BehavioralContract
behavioralContract = angles contract
where contract = Pull <$> (reserved "pull" *> reserved "self" *> semi *> identifier)
<*> (semi *> emission)
<|> Push <$> (reserved "push" *> identifier)
<*> (semi *> identifier)
<*> (semi *> emission)
context :: Parser Context
context = Context <$> (reserved "context" *> identifier)
<*> (reserved "as" *> dataTypeRef)
<*> braces (sepEndBy behavioralContract semi)
parseDocument :: String -> Either ParseError Document
parseDocument s = runLex document s
runLex :: Show a => Parser a -> String -> Either ParseError a
runLex p = parse (whiteSpace *> p) ""
-- Sample expressions
mainParse = parseDocument "\
\context Tre as Boolean {<push D; val; empty>}\
\context Tre as Boolean {<pull self; val; empty>}"
-- Generator
data CompilationUnit = CompilationUnit { cuName :: String
, cuContent :: String
}
generateCompilationUnits :: Document -> [CompilationUnit]
generateCompilationUnits doc@(Document ctxts) = map (transform doc) ctxts
transform :: Document -> Context -> CompilationUnit
transform doc ctxt = CompilationUnit "" ""
-- main function : make it compile
main = print mainParse