def check_syntax(entity, parser, uid = 0):
print entity
print parser
print uid
# Handle injections
if type(entity) == tuple: entity = list(entity) # To guarantee we can make insertions
if type(entity) == list: entity = handle_list_injections(entity)
# Process entity
if type(parser) == tuple:
if type(uid) == int:
if not isinstance(entity, list): raise MSException('illegal top-level entity %r, tuple or list expected' % entity)
try:
possible_uid = entity[uid]
except IndexError:
raise MSException('failed to retrieve identifier at position #%d in %s' % (uid, compressed_tuple(entity)))
if type(possible_uid) != str:
raise MSException('%r is not a legal identifier at position #%d in %s' % (possible_uid, uid, compressed_tuple(entity)))
uid = internal_identifier(possible_uid)
index = 0
repeating = None
output = []
try:
for subparser in parser:
if subparser == SCRIPT:
if type(entity[index]) != list:
raise MSException('expected script but found %r at position #%d in %s' % (entity[index], index, compressed_tuple(entity)))
output.append(entity[index])
index += 1
elif subparser == AGGREGATE:
if type(entity[index]) not in (int, long, AGGREGATE):
raise MSException('expected aggregate value but found %r at position #%d in %s' % (entity[index], index, compressed_tuple(entity)))
output.append(entity[index])
index += 1
elif subparser == REPEATABLE:
repeating = []
output.append(repeating)
elif type(subparser) == dict:
try:
output.append(check_syntax(entity[index], subparser['check'], uid))
index += 1
except IndexError:
output.append(deepcopy(subparser['default']))
except MSException:
output.append(deepcopy(subparser['default']))
else:
if repeating is None:
output.append(check_syntax(entity[index], subparser, uid))
index += 1
else:
try:
while True:
repeating.append(check_syntax(entity[index], subparser, uid))
index += 1
except IndexError:
break
except MSException, e:
raise MSException('incorrect syntax at position #%d in %s' % (index, compressed_tuple(entity)), *e.args)
except IndexError:
print formatted_exception()
raise MSException('not enough elements in module `%s` entity `%s` (%d total): %s' % (WRECK.current_module, uid, len(entity), compressed_tuple(entity)))
if index < len(entity):
WRECK.errors.append('too many elements in module `%s` entity `%s` (%d parsed out of total %d): %s' % (WRECK.current_module, uid, index, len(entity), compressed_tuple(entity)))
return output
if type(parser) == list:
output = []
for index in xrange(len(entity)):
try:
output.append(check_syntax(entity[index], parser[0], uid))
except MSException, e:
raise MSException('failed to parse element #%d' % (index, ), *e.args)
return output
if parser == troop_item:
if type(entity) == list:
return check_syntax(entity, (int, int), uid)
else:
return [check_syntax(entity, int, uid), 0]
elif parser == int:
if type(entity) == str: entity = convert_string_id_to_variable(entity)
if isinstance(entity, VARIABLE):
if not entity.is_static: raise MSException('value of %r is undefined at compile time' % entity)
elif type(entity) not in (int, long):
raise MSException('cannot convert value %r to integer' % (entity, ))
elif isinstance(parser, UID):
if type(entity) == str: entity = convert_string_id_to_variable(entity, parser)
if isinstance(entity, VARIABLE):
if not entity.is_static: raise MSException('value of %r is undefined at compile time' % entity)
elif type(entity) not in (int, long):
raise MSException('cannot convert value %r to integer' % (entity, ))
elif parser == float:
if type(entity) == str: entity = convert_string_id_to_variable(entity)
if isinstance(entity, VARIABLE):
if not entity.is_static: raise MSException('value of %r is undefined at compile time' % entity)
elif type(entity) not in (int, long, float):
raise MSException('cannot convert value %r to float' % (entity, ))
elif parser == str:
if entity == 0: entity = '0' # DIRTY HACK
elif entity == '': entity = '_' # Fix for CTD
elif type(entity) != str:
raise MSException('value %r must be a string' % (entity, ))
elif parser == id:
# TODO: identifier validity check
if (type(entity) != str) and (entity not in set([0])): raise MSException('value %r is not a valid identifier' % (entity, ))
elif parser == file:
# TODO: filename validity check
if type(entity) != str: raise MSException('value %r is not a valid filename' % (entity, ))
elif type(parser) == str:
if entity != parser: raise MSException('value %r must always be a string constant %r' % (entity, parser))
else:
raise MSException('unknown validator type %r' % (parser, ))
return entity