File tree Expand file tree Collapse file tree 4 files changed +21
-3
lines changed Expand file tree Collapse file tree 4 files changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -110,7 +110,7 @@ Interaction with the C preprocessor
110
110
-----------------------------------
111
111
112
112
In order to be compilable, C code must be preprocessed by the C preprocessor -
113
- ``cpp ``. ``cpp `` handles preprocessing directives like ``#include `` and
113
+ ``cpp ``. A compatible ``cpp `` handles preprocessing directives like ``#include `` and
114
114
``#define ``, removes comments, and performs other minor tasks that prepare the C
115
115
code for compilation.
116
116
Original file line number Diff line number Diff line change @@ -209,6 +209,10 @@ def _make_tok_location(self, token):
209
209
210
210
bad_octal_constant = '0[0-7]*[89]'
211
211
212
+ # comments are not supported
213
+ unsupported_c_style_comment = r'\/\*'
214
+ unsupported_cxx_style_comment = r'\/\/'
215
+
212
216
# character constants (K&R2: A.2.5.2)
213
217
# Note: a-zA-Z and '.-~^_!=&;,' are allowed as escape chars to support #line
214
218
# directives with Windows paths as filenames (..\..\dir\file)
@@ -475,6 +479,16 @@ def t_BAD_CONST_OCT(self, t):
475
479
msg = "Invalid octal constant"
476
480
self ._error (msg , t )
477
481
482
+ @TOKEN (unsupported_c_style_comment )
483
+ def t_UNSUPPORTED_C_STYLE_COMMENT (self , t ):
484
+ msg = "Comments are not supported, see https://github.com/eliben/pycparser#3using."
485
+ self ._error (msg , t )
486
+
487
+ @TOKEN (unsupported_cxx_style_comment )
488
+ def t_UNSUPPORTED_CXX_STYLE_COMMENT (self , t ):
489
+ msg = "Comments are not supported, see https://github.com/eliben/pycparser#3using."
490
+ self ._error (msg , t )
491
+
478
492
@TOKEN (octal_constant )
479
493
def t_INT_CONST_OCT (self , t ):
480
494
return t
Original file line number Diff line number Diff line change 1
- Run 'python -m unittest discover' from the root pycparser directory
1
+ Run 'python3 -m unittest discover' from the root repository directory.
Original file line number Diff line number Diff line change @@ -418,11 +418,12 @@ def test_preprocessor_pragma(self):
418
418
ERR_FILENAME_BEFORE_LINE = 'filename before line'
419
419
ERR_LINENUM_MISSING = 'line number missing'
420
420
ERR_INVALID_LINE_DIRECTIVE = 'invalid #line directive'
421
+ ERR_COMMENT = 'Comments are not supported'
421
422
422
423
423
424
class TestCLexerErrors (unittest .TestCase ):
424
425
""" Test lexing of erroneous strings.
425
- Works by passing an error functions that saves the error
426
+ Works by passing an error function that saves the error
426
427
in an attribute for later perusal.
427
428
"""
428
429
def error_func (self , msg , line , column ):
@@ -496,6 +497,9 @@ def test_preprocessor(self):
496
497
self .assertLexerError ('#line "ka"' , ERR_FILENAME_BEFORE_LINE )
497
498
self .assertLexerError ('#line df' , ERR_INVALID_LINE_DIRECTIVE )
498
499
self .assertLexerError ('#line \n ' , ERR_LINENUM_MISSING )
500
+ # a compatible preprocessor must remove comments.
501
+ self .assertLexerError ('//' , ERR_COMMENT )
502
+ self .assertLexerError ('/*' , ERR_COMMENT )
499
503
500
504
501
505
if __name__ == '__main__' :
You can’t perform that action at this time.
0 commit comments