예제 #1
0
def _conditional_construct(content):
  return (
      _conditional_blocks(content)
      + pyparsing.Optional(_else_block(content))
      + _ENDIF
      + _LINE_END
  ).setParseAction(util.action(pre_ast.If))
예제 #2
0
def _type_name_with_fields():
  return (
      _type_reference()
      + _maybe_attributes()
      + pyparsing.delimitedList(_field())
      + _SEMICOLON
  ).setParseAction(util.action(_insert_type_reference_into_type_instances))
예제 #3
0
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
예제 #4
0
def _function_call(expression):
  return (
      ~_TYPE_PROPERTY_KEYWORD
      + _identifier()
      + _OPEN_PARENTHESIS
      + _arguments(expression)
      + _CLOSE_PARENTHESIS
  ).setParseAction(util.action(c_ast.CFunctionCall))
예제 #5
0
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))
예제 #6
0
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))
예제 #7
0
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))
예제 #8
0
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
예제 #9
0
    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)
예제 #10
0
  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)
예제 #11
0
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))
예제 #12
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))
예제 #13
0
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))
예제 #14
0
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
예제 #15
0
def _semicolon():
  return pyparsing.Literal(';').setParseAction(util.action(c_ast.CLiteral))
예제 #16
0
def _text_block():
  block_end = _PREPROCESSOR_KEYWORD | pyparsing.stringEnd
  return (
      ~block_end
      + pyparsing.SkipTo(block_end)
  ).setParseAction(util.action(pre_ast.TextBlock))
예제 #17
0
def _initial_conditional_block(content):
  return (
      (_if_expression() | _ifdef_expression() | _ifndef_expression())
      + content
  ).setParseAction(util.action(pre_ast.ConditionalBlock))
예제 #18
0
def _string_literal():
  return (
      pyparsing.dblQuotedString.copy()
  ).setParseAction(util.action(c_ast.CLiteral))
예제 #19
0
def _simple_type_instance(type_instance_constructor):
  return _identifier().setParseAction(util.action(type_instance_constructor))
예제 #20
0
def _multiword_argument():
  return pyparsing.Group(
      _variable()
      + pyparsing.OneOrMore(_variable())
  ).setParseAction(util.action(pre_ast.CompositeBlock))
예제 #21
0
def _program():
  return _top_level_elements().copy().setParseAction(
      util.action(c_ast.CProgram)
  )
예제 #22
0
def _error():
  return (
      _ERROR
      + pyparsing.SkipTo(pyparsing.lineEnd)
  ).setParseAction(util.action(pre_ast.Error))
예제 #23
0
def _variable():
  return (
      _identifier()
  ).addParseAction(util.action(c_ast.CVariable))
예제 #24
0
def _pragma():
  arguments = pyparsing.Group(pyparsing.ZeroOrMore(_pragma_argument()))
  return (
      _PRAGMA
      + _parse_to_line_end(arguments)
  ).setParseAction(util.action(pre_ast.Pragma))
예제 #25
0
def _argument_with_dots():
  return (
      _identifier_with_dots()
  ).setParseAction(util.action(c_ast.CLiteral))
예제 #26
0
def _natural():
  return pyparsing.Word(pyparsing.nums).setParseAction(util.action(int))
예제 #27
0
def _type_reference():
  return _type_identifier().addParseAction(util.action(c_ast.CTypeReference))
예제 #28
0
def _program():
  return _composite_block().copy().setParseAction(util.action(pre_ast.File))
예제 #29
0
def _undef():
  return (
      _UNDEF
      + _identifier()
  ).setParseAction(util.action(pre_ast.Undef))
예제 #30
0
def _nested_expression(expression):
  return (
      pyparsing.Literal('(')
      + expression
      + pyparsing.Literal(')')
  ).setParseAction(util.action(c_ast.CNestedExpression))
예제 #31
0
def _number():
  return _integer().addParseAction(util.action(c_ast.CNumber))