def _make_attribute(self, tok):
"""Compose a c_ast.CAttribute() object for each attribute."""
result = []
for attr_specifier in tok:
expression = []
if attr_specifier.args:
# Try to parse the expression if possible.
try:
expression = [self.expression_parser.parse(
attr_specifier.args)]
except pyparsing.ParseException:
pass
result.append(c_ast.CAttribute(
attr_specifier.name.first,
*expression))
return result
python类ParseException()的实例源码
def FromString(cls, desc):
"""Parse this stop condition from a string representation.
The string needs to match:
run_time number [seconds|minutes|hours|days|months|years]
Args:
desc (str): The description
Returns:
TimeBasedStopCondition
"""
parse_exp = Literal(u'run_time').suppress() + time_interval(u'interval')
try:
data = parse_exp.parseString(desc)
return TimeBasedStopCondition(data[u'interval'][0])
except ParseException:
raise ArgumentError(u"Could not parse time based stop condition")
def _parse_line(self, line_no, line):
"""Parse a line in a TileBus file
Args:
lineno (int): The line number for printing useful error messages
line (string): The line that we are trying to parse
"""
try:
matched = statement.parseString(line)
except ParseException, exc:
raise DataError("Error parsing line in TileBus file", line_number=line_no, column=exc.col, contents=line)
if 'symbol' in matched:
self._parse_cmd(matched)
elif 'filename' in matched:
self._parse_include(matched)
elif 'variable' in matched:
self._parse_assignment(matched)
elif 'interface' in matched:
self._parse_interface(matched)
elif 'configvar' in matched:
self._parse_configvar(matched)
def __init__(self, requirement_string):
try:
req = REQUIREMENT.parseString(requirement_string)
except ParseException as e:
raise InvalidRequirement(
"Invalid requirement, parse error at \"{0!r}\"".format(
requirement_string[e.loc:e.loc + 8]))
self.name = req.name
if req.url:
parsed_url = urlparse.urlparse(req.url)
if not (parsed_url.scheme and parsed_url.netloc) or (
not parsed_url.scheme and not parsed_url.netloc):
raise InvalidRequirement("Invalid URL given")
self.url = req.url
else:
self.url = None
self.extras = set(req.extras.asList() if req.extras else [])
self.specifier = SpecifierSet(req.specifier)
self.marker = req.marker if req.marker else None
def __init__(self, requirement_string):
try:
req = REQUIREMENT.parseString(requirement_string)
except ParseException as e:
raise InvalidRequirement(
"Invalid requirement, parse error at \"{0!r}\"".format(
requirement_string[e.loc:e.loc + 8]))
self.name = req.name
if req.url:
parsed_url = urlparse.urlparse(req.url)
if not (parsed_url.scheme and parsed_url.netloc) or (
not parsed_url.scheme and not parsed_url.netloc):
raise InvalidRequirement("Invalid URL given")
self.url = req.url
else:
self.url = None
self.extras = set(req.extras.asList() if req.extras else [])
self.specifier = SpecifierSet(req.specifier)
self.marker = req.marker if req.marker else None
def prepare(self, fp):
def _read_and_parse():
for i, line in enumerate((x.strip() for x in fp.readlines())):
if i == 0:
continue
m = None
for p in patterns:
try:
m = p.parseString(line, parseAll=True)
fields = m['group'], m['host'], m['graph'], m.get('subgraph', None), m.get('datarow', None), \
m['attribute'], m['value']
#logging.debug("%-30s%-30s%-30s%-30s%-30s%-30s%s", *fields)
yield Row(*fields)
break
except ParseException:
continue
if not m:
logger.error("No pattern matched line: %s", line)
for f in _read_and_parse():
yield f
def __init__(self, requirement_string):
try:
req = REQUIREMENT.parseString(requirement_string)
except ParseException as e:
raise InvalidRequirement(
"Invalid requirement, parse error at \"{0!r}\"".format(
requirement_string[e.loc:e.loc + 8]))
self.name = req.name
if req.url:
parsed_url = urlparse.urlparse(req.url)
if not (parsed_url.scheme and parsed_url.netloc) or (
not parsed_url.scheme and not parsed_url.netloc):
raise InvalidRequirement("Invalid URL given")
self.url = req.url
else:
self.url = None
self.extras = set(req.extras.asList() if req.extras else [])
self.specifier = SpecifierSet(req.specifier)
self.marker = req.marker if req.marker else None
def dice(self, *, input : str = '6'):
'''
Roll dice
Inputs: Examples:
S | S - number of sides (default is 6) [6 | 12]
AdS | A - amount (default is 1) [5d6 | 2d10]
AdSt | t - return total [2d6t | 20d5t]
AdSs | s - return sorted [4d6s | 5d8s]
AdS^H | ^H - return highest H rolls [10d6^4 | 2d7^1]
AdSvL | vL - return lowest L rolls [15d7v2 | 8d9v2]
'''
# TODO: Add documentation on arithmetic/basic integer operations
if 'd' not in input:
input = 'd' + input
with multiprocessing.Pool(1) as pool:
async_result = pool.apply_async(dice.roll, (input,))
future = self.bot.loop.run_in_executor(None, async_result.get, 10.0)
try:
result = await asyncio.wait_for(future, 10.0, loop = self.bot.loop)
if type(result) is int:
await self.bot.embed_reply(result)
else:
await self.bot.embed_reply(", ".join(str(roll) for roll in result))
except discord.errors.HTTPException:
await self.bot.embed_reply(":no_entry: Output too long")
except pyparsing.ParseException:
await self.bot.embed_reply(":no_entry: Invalid input")
except (concurrent.futures.TimeoutError, multiprocessing.context.TimeoutError):
await self.bot.embed_reply(":no_entry: Execution exceeded time limit")
def parseAssert(self, text):
try:
parsed = ASSERT.parseString(text, parseAll=True).asList()
except pp.ParseException as e:
raise ParseException('Check for the appropriate syntax!', e.lineno, e.line)
# Returns the AST built by the parser.
return parsed
def parseRetract(self, text):
try:
parsed = RETRACT.parseString(text, parseAll=True).asList()
except pp.ParseException as e:
raise ParseException('Check for the appropriate syntax!', e.lineno, e.line)
# Returns the AST built by the parser.
return parsed
def parseProgram(self, text):
try:
parsed = PROGRAM.parseString(text, parseAll=True).asList()
except pp.ParseException as e:
raise ParseException('Check for the appropriate syntax!', e.lineno, e.line)
# Returns the AST built by the parser.
return parsed
def getkerneldescriptors(self, ast, var_name='meta_args'):
descs = ast.get_variable(var_name)
if descs is None:
raise ParseError(
"kernel call does not contain a {0} type".format(var_name))
try:
nargs = int(descs.shape[0])
except AttributeError:
raise ParseError(
"kernel metadata {0}: {1} variable must be an array".
format(self._name, var_name))
if len(descs.shape) is not 1:
raise ParseError(
"kernel metadata {0}: {1} variable must be a 1 dimensional "
"array".format(self._name, var_name))
if descs.init.find("[") is not -1 and descs.init.find("]") is not -1:
# there is a bug in f2py
raise ParseError(
"Parser does not currently support [...] initialisation for "
"{0}, please use (/.../) instead".format(var_name))
try:
inits = expr.FORT_EXPRESSION.parseString(descs.init)[0]
except ParseException:
raise ParseError("kernel metadata has an invalid format {0}".
format(descs.init))
nargs = int(descs.shape[0])
if len(inits) != nargs:
raise ParseError(
"Error, in {0} specification, the number of args {1} and "
"number of dimensions {2} do not match".
format(var_name, nargs, len(inits)))
return inits
def test_abort_on_parse_failure(self):
with open(util.get_data_filename('broken.conf')) as handle:
self.assertRaises(ParseException, load, handle)
def _parse_files(self, filepath, override=False):
"""Parse files from a glob
:param str filepath: Nginx config file path
:param bool override: Whether to parse a file that has been parsed
:returns: list of parsed tree structures
:rtype: list
"""
files = glob.glob(filepath) # nginx on unix calls glob(3) for this
# XXX Windows nginx uses FindFirstFile, and
# should have a narrower call here
trees = []
for item in files:
if item in self.parsed and not override:
continue
try:
with open(item) as _file:
parsed = nginxparser.load(_file)
self.parsed[item] = parsed
trees.append(parsed)
except IOError:
logger.warning("Could not open file: %s", item)
except pyparsing.ParseException as err:
logger.debug("Could not parse file: %s due to %s", item, err)
return trees
def test_predicate_failure(self):
"""Checks that if there's a problem with the relation/object, that an error gets thrown"""
statement = 'composite(p(HGNC:CASP8),p(HGNC:FADD),a(ADO:"Abeta_42")) -> nope(GOBP:"neuron apoptotic process")'
with self.assertRaises(ParseException):
self.parser.relation.parseString(statement)
def parse_statements(graph, statements, bel_parser):
"""Parses a list of statements from a BEL Script.
:param BELGraph graph: A BEL graph
:param iter[str] statements: An enumerated iterable over the lines in the statements section of a BEL script
:param BelParser bel_parser: A BEL parser
"""
t = time.time()
for line_number, line in statements:
try:
bel_parser.parseString(line, line_number=line_number)
except ParseException as e:
parse_log.error('Line %07d - General Parser Failure: %s', line_number, line)
graph.add_warning(line_number, line, BelSyntaxError(line_number, line, e.loc),
bel_parser.get_annotations())
except PyBelWarning as e:
parse_log.warning('Line %07d - %s: %s', line_number, e.__class__.__name__, e)
graph.add_warning(line_number, line, e, bel_parser.get_annotations())
except Exception as e:
parse_log.exception('Line %07d - General Failure: %s', line_number, line)
graph.add_warning(line_number, line, e, bel_parser.get_annotations())
log.info('Parsed statements section in %.02f seconds with %d warnings', time.time() - t, len(graph.warnings))
for k, v in sorted(Counter(e.__class__.__name__ for _, _, e, _ in graph.warnings).items(), reverse=True):
log.debug(' %s: %d', k, v)
def match(cmp_value, spec):
"""Match a given value to a given spec DSL."""
expr = make_grammar()
try:
ast = expr.parseString(spec)
except pyparsing.ParseException:
ast = [spec]
if len(ast) == 1:
return ast[0] == cmp_value
op = op_methods[ast[0]]
return op(cmp_value, *ast[1:])
def split_by_commas(value):
"""Split values by commas and quotes according to api-wg
:param value: value to be split
.. versionadded:: 3.17
"""
word = (pp.QuotedString(quoteChar='"', escChar='\\')
| pp.Word(pp.printables, excludeChars='",'))
grammar = pp.stringStart + pp.delimitedList(word) + pp.stringEnd
try:
return list(grammar.parseString(value))
except pp.ParseException:
raise ValueError("Invalid value: %s" % value)
def __init__(self, marker):
try:
self._markers = _coerce_parse_result(MARKER.parseString(marker))
except ParseException as e:
err_str = "Invalid marker: {0!r}, parse error at {1!r}".format(
marker, marker[e.loc:e.loc + 8])
raise InvalidMarker(err_str)