Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Interaction with the C preprocessor
-----------------------------------

In order to be compilable, C code must be preprocessed by the C preprocessor -
``cpp``. ``cpp`` handles preprocessing directives like ``#include`` and
``cpp``. A compatible ``cpp`` handles preprocessing directives like ``#include`` and
``#define``, removes comments, and performs other minor tasks that prepare the C
code for compilation.

Expand Down
14 changes: 14 additions & 0 deletions pycparser/c_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ def _make_tok_location(self, token):

bad_octal_constant = '0[0-7]*[89]'

# comments are not supported
unsupported_c_style_comment = r'\/\*'
unsupported_cxx_style_comment = r'\/\/'

# character constants (K&R2: A.2.5.2)
# Note: a-zA-Z and '.-~^_!=&;,' are allowed as escape chars to support #line
# directives with Windows paths as filenames (..\..\dir\file)
Expand Down Expand Up @@ -475,6 +479,16 @@ def t_BAD_CONST_OCT(self, t):
msg = "Invalid octal constant"
self._error(msg, t)

@TOKEN(unsupported_c_style_comment)
def t_UNSUPPORTED_C_STYLE_COMMENT(self, t):
msg = "Comments are not supported, see https://github.com/eliben/pycparser#3using."
self._error(msg, t)

@TOKEN(unsupported_cxx_style_comment)
def t_UNSUPPORTED_CXX_STYLE_COMMENT(self, t):
msg = "Comments are not supported, see https://github.com/eliben/pycparser#3using."
self._error(msg, t)

@TOKEN(octal_constant)
def t_INT_CONST_OCT(self, t):
return t
Expand Down
2 changes: 1 addition & 1 deletion tests/README.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Run 'python -m unittest discover' from the root pycparser directory
Run 'python3 -m unittest discover' from the root repository directory.
6 changes: 5 additions & 1 deletion tests/test_c_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,12 @@ def test_preprocessor_pragma(self):
ERR_FILENAME_BEFORE_LINE = 'filename before line'
ERR_LINENUM_MISSING = 'line number missing'
ERR_INVALID_LINE_DIRECTIVE = 'invalid #line directive'
ERR_COMMENT = 'Comments are not supported'


class TestCLexerErrors(unittest.TestCase):
""" Test lexing of erroneous strings.
Works by passing an error functions that saves the error
Works by passing an error function that saves the error
in an attribute for later perusal.
"""
def error_func(self, msg, line, column):
Expand Down Expand Up @@ -496,6 +497,9 @@ def test_preprocessor(self):
self.assertLexerError('#line "ka"', ERR_FILENAME_BEFORE_LINE)
self.assertLexerError('#line df', ERR_INVALID_LINE_DIRECTIVE)
self.assertLexerError('#line \n', ERR_LINENUM_MISSING)
# a compatible preprocessor must remove comments.
self.assertLexerError('//', ERR_COMMENT)
self.assertLexerError('/*', ERR_COMMENT)


if __name__ == '__main__':
Expand Down