def _conditional_construct(content): return ( _conditional_blocks(content) + pyparsing.Optional(_else_block(content)) + _ENDIF + _LINE_END ).setParseAction(util.action(pre_ast.If))
def _type_name_with_fields(): return ( _type_reference() + _maybe_attributes() + pyparsing.delimitedList(_field()) + _SEMICOLON ).setParseAction(util.action(_insert_type_reference_into_type_instances))
def _make_attribute(name, parameters_parser=None): """Creates an attribute parser (pyparsing). Args: name: a string representing the name of the attribute. parameters_parser: a (pyparsing) parser for parsing the parameters of the attribute. Returns: A (pyparsing) parser for the attribute. """ if parameters_parser: maybe_parameters = ( _OPEN_PARENTHESIS + parameters_parser + _CLOSE_PARENTHESIS ) else: maybe_parameters = pyparsing.empty attribute = ( _attribute_name(name) + maybe_parameters ) attribute.setParseAction(util.action(c_ast.CAttribute)) return attribute
def _function_call(expression): return ( ~_TYPE_PROPERTY_KEYWORD + _identifier() + _OPEN_PARENTHESIS + _arguments(expression) + _CLOSE_PARENTHESIS ).setParseAction(util.action(c_ast.CFunctionCall))
def _include_with_angle_brackets(): quotes_type = pre_ast.Include.QuotesType.ANGLE_BRACKETS return ( _INCLUDE + _OPEN_ANGLE_BRACKETS + _path() + _CLOSE_ANGLE_BRACKETS ).setParseAction(util.action(pre_ast.Include, quotes_type=quotes_type))
def _elif_block(content): expression = expression_parser.expression_parser() expression.ignore(pyparsing.cppStyleComment) return ( _ELIF + _parse_to_line_end(expression) + content ).setParseAction(util.action(pre_ast.ConditionalBlock))
def _include_with_double_quotes(): quotes_type = pre_ast.Include.QuotesType.DOUBLE_QUOTES return ( _INCLUDE + _DOUBLE_QUOTE + _path() + _DOUBLE_QUOTE ).setParseAction(util.action(pre_ast.Include, quotes_type=quotes_type))
def _type_instance(type_instance_constructor): type_instance = ( (_concrete_type_instance(type_instance_constructor) + (~_OPEN_BRACKET)) | _pointer_type_instance(type_instance_constructor) ) attributed_type_instance = type_instance + _maybe_attributes() attributed_type_instance.setParseAction( util.action(_insert_attributes_into_type_instance) ) return attributed_type_instance
def test_action_with_args_and_kwargs(self): def function(*args, **kwargs): self.assertEqual(args, (51, 42, 33)) self.assertEqual(kwargs, {'a': 3, 'b': 2, 'c': 1}) return -1 self.tokens.asList.return_value = [51, 42, 33] wrapped_function = util.action(function, a=3, b=2, c=1) actual = wrapped_function('string1', 0, self.tokens) expected = -1 self.assertEqual(actual, expected)
def _integer(): integer = _hexadecimal_as_string() | _decimal_as_string() unsigned_suffix = pyparsing.Literal('u') | 'U' size_suffix = pyparsing.Literal('ll') | 'LL' | 'l' | 'L' maybe_suffix = ( pyparsing.Optional(unsigned_suffix) + pyparsing.Optional(size_suffix) ).suppress() return ( integer + maybe_suffix ).setParseAction(util.action(int, base=0))
def _defined(): maybe_nested_variable = pyparsing.Forward() nested_variable = ( _OPEN_PARENTHESIS + maybe_nested_variable + _CLOSE_PARENTHESIS ) # pylint: disable=expression-not-assigned maybe_nested_variable << (_variable() | nested_variable) return ( _DEFINED + pyparsing.Group(maybe_nested_variable) ).setParseAction(util.action(c_ast.CFunctionCall))
def _pragma_argument(): expression = expression_parser.expression_parser() expression.ignore(pyparsing.cppStyleComment) arguments = pyparsing.Group( _OPEN_PARENTHESES + _maybe_empty_delimited_list(expression) + _CLOSE_PARENTHESES ) value_assignment = _EQUALS + expression return ( (_identifier() | pyparsing.dblQuotedString) + pyparsing.Optional(arguments, None) + pyparsing.Optional(value_assignment, None) ).setParseAction(util.action(pre_ast.PragmaArgument))
def _composite_block(): """Creates a (pyparsing) parser that parses a CompositeBlock object.""" composite_block = pyparsing.Forward() element = ( _include() | _pragma() | _error() | _define() | _undef() | _conditional_construct(composite_block) | _text_block() ) # pylint: disable=expression-not-assigned composite_block << pyparsing.Group( pyparsing.ZeroOrMore(element) ).setParseAction(util.action(pre_ast.CompositeBlock)) return composite_block
def _semicolon(): return pyparsing.Literal(';').setParseAction(util.action(c_ast.CLiteral))
def _text_block(): block_end = _PREPROCESSOR_KEYWORD | pyparsing.stringEnd return ( ~block_end + pyparsing.SkipTo(block_end) ).setParseAction(util.action(pre_ast.TextBlock))
def _initial_conditional_block(content): return ( (_if_expression() | _ifdef_expression() | _ifndef_expression()) + content ).setParseAction(util.action(pre_ast.ConditionalBlock))
def _string_literal(): return ( pyparsing.dblQuotedString.copy() ).setParseAction(util.action(c_ast.CLiteral))
def _simple_type_instance(type_instance_constructor): return _identifier().setParseAction(util.action(type_instance_constructor))
def _multiword_argument(): return pyparsing.Group( _variable() + pyparsing.OneOrMore(_variable()) ).setParseAction(util.action(pre_ast.CompositeBlock))
def _program(): return _top_level_elements().copy().setParseAction( util.action(c_ast.CProgram) )
def _error(): return ( _ERROR + pyparsing.SkipTo(pyparsing.lineEnd) ).setParseAction(util.action(pre_ast.Error))
def _variable(): return ( _identifier() ).addParseAction(util.action(c_ast.CVariable))
def _pragma(): arguments = pyparsing.Group(pyparsing.ZeroOrMore(_pragma_argument())) return ( _PRAGMA + _parse_to_line_end(arguments) ).setParseAction(util.action(pre_ast.Pragma))
def _argument_with_dots(): return ( _identifier_with_dots() ).setParseAction(util.action(c_ast.CLiteral))
def _natural(): return pyparsing.Word(pyparsing.nums).setParseAction(util.action(int))
def _type_reference(): return _type_identifier().addParseAction(util.action(c_ast.CTypeReference))
def _program(): return _composite_block().copy().setParseAction(util.action(pre_ast.File))
def _undef(): return ( _UNDEF + _identifier() ).setParseAction(util.action(pre_ast.Undef))
def _nested_expression(expression): return ( pyparsing.Literal('(') + expression + pyparsing.Literal(')') ).setParseAction(util.action(c_ast.CNestedExpression))
def _number(): return _integer().addParseAction(util.action(c_ast.CNumber))