monk/monkEnum.py

95 lines
2.2 KiB
Python
Raw Normal View History

2013-12-22 18:55:48 +01:00
#!/usr/bin/python
import monkDebug as debug
import monkNode as Node
class Enum(Node.Node):
def __init__(self, stack=[], file="", lineNumber=0, documentation=[]):
self.base_value = 0;
2013-12-22 18:55:48 +01:00
# check input :
2014-04-24 21:31:22 +02:00
if len(stack) < 1:
debug.error("Can not parse enum : " + str(stack))
2013-12-22 18:55:48 +01:00
return
self.typedef = False
if len(stack) > 0 \
and stack[0] == 'typedef':
2013-12-22 18:55:48 +01:00
self.typedef = True
stack[1:]
if len(stack) > 0 \
and stack[0] == 'enum':
stack[1:]
else:
2014-04-24 21:31:22 +02:00
debug.error("Can not parse enum : " + str(stack))
return
self.is_class = False
if len(stack) > 0 \
and stack[0] == 'class':
self.is_class = True
stack[1:]
if len(stack) >= 1:
2014-04-24 21:31:22 +02:00
localEnumName = stack[1]
else:
debug.error("Can not parse enum : " + str(stack))
return
2013-12-22 18:55:48 +01:00
2014-04-24 21:31:22 +02:00
Node.Node.__init__(self, 'enum', localEnumName, file, lineNumber, documentation)
2013-12-22 18:55:48 +01:00
self.list_element = []
2013-12-22 18:55:48 +01:00
def to_str(self) :
if self.is_class == True:
return "enum class " + self.name + " { ... };"
2013-12-22 18:55:48 +01:00
return "enum " + self.name + " { ... };"
def enum_append(self, stack):
2013-12-27 07:57:16 +01:00
debug.verbose("enum : " + str(stack))
2013-12-22 18:55:48 +01:00
subList = []
tmp = []
for element in stack:
if element == ',':
subList.append(tmp)
tmp = []
else:
tmp.append(element)
if len(tmp) != 0:
subList.append(tmp)
#debug.verbose(" TODO : Need to append enum : " + str(subList))
for element in subList:
2013-12-27 07:57:16 +01:00
# extract comment:
filtered = []
comments = ""
for subs in element:
if subs[:5] == "//!< ":
if comments != "":
comments += "\n"
comments += subs[5:]
else:
filtered.append(subs)
element = filtered
2013-12-22 18:55:48 +01:00
value = ""
if len(element) > 2:
if element[1] == '=':
for tmp in element[2:]:
2013-12-25 23:56:09 +01:00
value = tmp
if value == "":
if self.base_value == None:
2013-12-25 23:56:09 +01:00
value = "???"
else:
value = str(self.base_value)
self.base_value += 1
2013-12-25 23:56:09 +01:00
else:
try:
tmpVal = int(value)
self.base_value = tmpVal + 1
2013-12-25 23:56:09 +01:00
except:
debug.debug("can not parse enum value : '" + value + "'")
self.base_value = None
self.list_element.append({'name' : element[0], 'value' : value, 'doc' : comments})
2013-12-22 18:55:48 +01:00
debug.verbose("enum list : " + str(self.list_element))
2013-12-25 23:56:09 +01:00
def get_enum_list(self):
return self.list_element
2013-12-25 23:56:09 +01:00