def test_parse_pragma(self): source = '#pragma foo' actual = self.parser.parse(source) expected = pre_ast.CompositeBlock([ pre_ast.Pragma('foo'), ]) self.assertASTEqual(actual, expected)
def test_parse_pragma_with_string_argument(self): source = '#pragma "-foo"' actual = self.parser.parse(source) expected = pre_ast.CompositeBlock([ pre_ast.Pragma('"-foo"'), ]) self.assertASTEqual(actual, expected)
def test_parse_pragma_and_text_block(self): source = '\n'.join(( '#pragma foo bar', 'int x;', )) actual = self.parser.parse(source) expected = pre_ast.CompositeBlock([ pre_ast.Pragma("foo bar"), pre_ast.TextBlock('int x;'), ]) self.assertASTEqual(actual, expected)
def parse(self, source): line_continuation = pyparsing.Literal('\\\n') ignorable = ( line_continuation | pyparsing.cppStyleComment.addParseAction( pyparsing.ParserElement.resetCache) # Removed for now because it is too expensive at this point. We can # eliminate those later in the trimming phase. # | parser.extern_field() | self.static_function()).suppress() source = ignorable.transformString(source) source = Sanitizer().transform(source) # Start parsing: Top level node will be a pre_ast.File self.push_node(pre_ast.CompositeBlock(content=[])) last_block_end = 0 scanner = self._preprocessor_directive().parseWithTabs() for tokens, start, end in scanner.scanString(source): text = source[last_block_end:start].strip() if text: # We skipped over a text block - push it on the current node. self.current_node.content.append(pre_ast.TextBlock(text)) last_block_end = end # Now process the different directives. directive, rest_of_line = tokens if directive == "include": self._process_include(rest_of_line) # Im not really sure what #pragma is supposed to do here. elif directive == "pragma": self.current_node.content.append(pre_ast.Pragma(rest_of_line)) elif directive == "error": self.current_node.content.append(pre_ast.Error(rest_of_line)) elif directive == "define": self._define_parser.parseString(rest_of_line) elif directive == "undef": self.current_node.content.append(pre_ast.Undef(rest_of_line)) elif directive == "if": self._add_conditional_block(rest_of_line) elif directive == "ifdef": self._add_conditional_block("defined(%s)" % rest_of_line) elif directive == "ifndef": self._add_conditional_block("!defined(%s)" % rest_of_line) elif directive == "else": self._add_elif_block("1") elif directive == "elif": self._add_elif_block(rest_of_line) elif directive == "endif": # Pop the stack. self.pop_node() # ConditionalBlock self.pop_node() # If block. # Last text node. text = source[last_block_end:].strip() if text: self.current_node.content.append(pre_ast.TextBlock(text)) return self.pop_node()
def test_collect_includes_with_pragma(self): node = pre_ast.Pragma('some_arguments') actual = self.include_collector.collect_includes(node) expected = [] self.assertEqual(actual, expected)