def raise_error(self, perr: ParseSyntaxException): """Parsing failed - try to be produce a helpful error messsage. :param pe ParseSyntaxException: the Exception that was caught :return: None :rtype: None """ msg = f"ERROR: {type(perr)}\n" message = self.banner_message(perr.lineno, perr.col, perr.line, msg) perr.msg = message raise perr
def postParse(self, instring, loc, token_list): """Create ConfigTree from tokens :param instring: :param loc: :param token_list: :return: """ config_tree = ConfigTree(root=self.root) for element in token_list: expanded_tokens = element.tokens if isinstance(element, ConfigInclude) else [element] for tokens in expanded_tokens: # key, value1 (optional), ... key = tokens[0].strip() if isinstance(tokens[0], (unicode, basestring)) else tokens[0] operator = '=' if len(tokens) == 3 and tokens[1].strip() in [':', '=', '+=']: operator = tokens[1].strip() values = tokens[2:] elif len(tokens) == 2: values = tokens[1:] else: raise ParseSyntaxException("Unknown tokens {tokens} received".format(tokens=tokens)) # empty string if len(values) == 0: config_tree.put(key, '') else: value = values[0] if isinstance(value, list) and operator == "+=": value = ConfigValues([ConfigSubstitution(key, True, '', False, loc), value], False, loc) config_tree.put(key, value, False) elif isinstance(value, unicode) and operator == "+=": value = ConfigValues([ConfigSubstitution(key, True, '', True, loc), ' ' + value], True, loc) config_tree.put(key, value, False) elif isinstance(value, list): config_tree.put(key, value, False) else: existing_value = config_tree.get(key, None) if isinstance(value, ConfigTree) and not isinstance(existing_value, list): # Only Tree has to be merged with tree config_tree.put(key, value, True) elif isinstance(value, ConfigValues): conf_value = value value.parent = config_tree value.key = key if isinstance(existing_value, list) or isinstance(existing_value, ConfigTree): config_tree.put(key, conf_value, True) else: config_tree.put(key, conf_value, False) else: config_tree.put(key, value, False) return config_tree