forked from VeraBE/VeriMan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
134 lines (102 loc) · 3.98 KB
/
parser.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import tatsu
import re
import random, string
import os
from tatsu.ast import AST
from iteration_utilities import deepflatten
TRUE = 'true'
FALSE = 'false'
AND = '&&'
OR = '||'
EQUAL = '=='
NOTEQUAL = '!='
IMPLICATION = '->'
NOT = '!'
SINCE = 'since'
PREVIOUSLY = 'previously'
ONCE = 'once'
ALWAYS = 'always'
GRAMMAR_FILE = 'grammar.txt'
VARIABLE_NAME_LENGTH = 8
class Parser:
def __init__(self):
with open(os.path.dirname(os.path.abspath(__file__)) + '/' + GRAMMAR_FILE, 'r') as grammar_file:
self.grammar = tatsu.compile(grammar_file.read())
def parse(self, text):
predicate = self.grammar.parse(text, semantics=Semantics())
if not isinstance(predicate, Predicate):
predicate = Term(predicate)
return predicate
@staticmethod
def create_variable_name(base):
return 'VERIMAN_' + base + '_' + ''.join(
random.choice(string.ascii_lowercase) for _ in range(VARIABLE_NAME_LENGTH))
class Semantics(object):
def expression(self, ast):
if not isinstance(ast, AST):
return ast
elif ast.op in [AND, OR, EQUAL, NOTEQUAL]:
return Predicate(ast.op, [ast.left, ast.right])
elif ast.op == IMPLICATION:
# p -> q === !p || q
return Predicate(OR, [Predicate(NOT, [ast.left]), ast.right])
else:
raise Exception('Unknown operator', ast.op)
def term(self, ast):
if not isinstance(ast, AST):
return ast
elif ast.op == SINCE:
return Predicate(ast.op, [ast.left, ast.right])
elif ast.op == ONCE:
# once(p) === since(true, p)
return Predicate(SINCE, [TRUE, ast.value])
elif ast.op == ALWAYS:
# always(p) === !once(!p) === !since(true, !p)
return Predicate(NOT, [Predicate(SINCE, [TRUE, Predicate(NOT, [ast.value])])])
elif ast.op in [PREVIOUSLY, NOT]:
return Predicate(ast.op, [ast.value])
else:
raise Exception('Unknown operator', ast.op)
class Predicate:
def __init__(self, operator, values):
self.operator = operator
self.values = []
self.related_vars = []
for value in values:
term = value
if isinstance(value, list) and value[0] == '(' and value[2] == ')' and isinstance(value[1], Predicate):
term = value[1]
elif not isinstance(value, Predicate):
term = Term(value)
self.related_vars += term.related_vars
self.values.append(term)
self.related_vars = list(set(self.related_vars))
if operator == PREVIOUSLY:
new_var = Parser.create_variable_name(operator)
self.solidity_repr = new_var
self.solidity_vars = [new_var]
elif operator == SINCE:
q = Parser.create_variable_name('q')
p_since_q = Parser.create_variable_name('p_since_q')
self.solidity_repr = f'({q}{AND}{p_since_q})'
self.solidity_vars = [q, p_since_q]
else:
self.solidity_vars = []
if self.operator == NOT:
self.solidity_repr = f'!({self.values[0].solidity_repr})'
elif self.operator in [EQUAL, NOTEQUAL, AND, OR]:
self.solidity_repr = f'({self.values[0].solidity_repr}){self.operator}({self.values[1].solidity_repr})'
class Term(Predicate):
def __init__(self, value):
super().__init__('', [])
if isinstance(value, list):
self.solidity_repr = ' '.join(list(deepflatten(value, types=list)))
elif isinstance(value, str):
self.solidity_repr = value
else:
raise Exception('Unexpected term')
self.solidity_repr = self.solidity_repr.replace(' ', '')
vars = re.findall('[a-zA-Z0-9_.]*?[a-zA-Z][a-zA-Z0-9_.]*', self.solidity_repr)
# TODO improve:
vars = list(filter(lambda elem: elem != TRUE and elem != FALSE, vars))
self.related_vars = list(set(vars))