-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetLangParser.y
76 lines (65 loc) · 1.62 KB
/
SetLangParser.y
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
{
module SetLangParser (module SetLang, applySetLang, parseSetLang) where
import SetLang
import SetLangLexer
import Char
}
%name slparse
%tokentype { Token }
%error { parseError }
%token
string { TString $$ }
'=' { TEq }
"!=" { TNEq }
'!' { TNot }
"||" { TOr }
"&&" { TAnd }
"^^" { TXor }
'(' { TOB }
')' { TCB }
':' { TColon }
';' { TSemicolon }
',' { TComma }
"true" { TTrue }
"false" { TFalse }
"nop" { TNop }
"in" { TIn }
"defined" { TDefined }
"declare" { TDeclare }
"unset" { TUnset }
%%
Statement : Condition ':' Actions { Expression $1 $3 }
Actions
: Action { [ $1 ] }
| Action ';' Actions { $1 : $3 }
Condition
: '!' Condition { Not $2 }
| Condition "&&" Condition { And $1 $3 }
| Condition "||" Condition { Or $1 $3 }
| "defined" '(' Identifier ')' { Defined $3 }
| Condition "^^" Condition { Xor $1 $3 }
| Identifier "in" '(' Domain ')' { In $1 $4 }
| Identifier '=' Value { Is $1 $3 }
| Identifier "!=" Value { Not (Is $1 $3) }
| "true" { CTrue }
| "false" { CFalse }
| '(' Condition ')' { $2 }
Action
: "nop" { Nop }
| "declare" Identifier '=' '(' Domain ')' { DeclareVar $2 $5 }
| "unset" Identifier { UnsetVar $2 }
| Identifier '=' Value { SetVar $1 $3 }
Identifier : string { $1 }
Value : string { $1 }
Domain : SetOfValues { domainFromStringList $1 }
SetOfValues
: Value { [ $1 ] }
| Value ',' SetOfValues { $1 : $3 }
{
parseError :: [Token] -> a
parseError x = error ("Parse error: " ++ show x)
parseSetLang :: String -> Expression
parseSetLang = slparse . alexScanTokens
applySetLang :: VarState -> String -> Maybe VarState
applySetLang s e = apply (parseSetLang e) s
}