-
Notifications
You must be signed in to change notification settings - Fork 4
/
base.py
90 lines (64 loc) · 2.21 KB
/
base.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
from pyparsing import Forward, Or, Literal, Suppress
class NonImplemented(Exception):
pass
class IncorrectValue(Exception):
pass
class ParseError(Exception):
pass
class ModelicaBase(object):
"""
Modelica Base Class
===================
This class is the base of all modelica entities. It has two methods:
* **load(cls, string)** It creates an element based on the string passed.
* **dump(self)** It return the element in string modelica format.
"""
@classmethod
def load(cls, string):
# This reads a string and return the element created.
try:
return cls.ebnf().parseString(string)[0]
except:
raise ParseError()
def dump(self, indent = 0):
raise NonImplemented
def __str__(self):
return self.dump()
def __repr__(self):
return "%s: %s"%(str(self.__class__.__name__), str(self))
@classmethod
def ebnf(self, syntax = None, action = None):
# Try to set the __ebnf__
if not hasattr(self, '__ebnf__'):
self.__ebnf__ = Forward()
if syntax is not None:
if action is None:
self.__ebnf__ << syntax
self.__ebnf__ = self.__ebnf__.setParseAction(lambda s,l,t: self(**dict(t)))
else:
self.__ebnf__ << syntax
if action is not None:
self.__ebnf__ = self.__ebnf__.setParseAction(action)
return self.__ebnf__
@classmethod
def name(cls, name):
return Or(cls.ebnf()).setResultsName(name)
@classmethod
def names(cls, name):
return Or(cls.ebnf()).setResultsName(name, listAllMatches=True)
@classmethod
def __call__(cls, name=None):
if name is None:
return cls.ebnf()
else:
return cls.name(name)
def presenceBool(syntax):
return syntax.setParseAction(lambda s,l,t:True)
def hasLiteral(name, variable=None):
if variable is None:
variable = name
return presenceBool(Suppress(name))(variable)
def hasLiterals(names, variable=None):
if variable is None:
variable = "_".join(names)
return presenceBool(names.reduce(lambda x,y: Suppress(x) + Suppress(y)))(variable)