summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2010-11-26 10:46:37 +0100
committerGeorg Brandl <georg@python.org>2010-11-26 10:46:37 +0100
commitd584af251c63006a42d316bd8fac177e998ca402 (patch)
treed7125e5319f0b46cd749d7b6852d1c7f492da058
parent5350e14fb69d59a55b31f6009208d84d4026ac54 (diff)
downloadpygments-d584af251c63006a42d316bd8fac177e998ca402.tar.gz
Support comments before preprocessor directives in C/C++ (#616).
-rw-r--r--CHANGES3
-rw-r--r--pygments/lexers/compiled.py27
2 files changed, 23 insertions, 7 deletions
diff --git a/CHANGES b/CHANGES
index fe1471e3..68663a3c 100644
--- a/CHANGES
+++ b/CHANGES
@@ -55,7 +55,8 @@ Version 1.4
- Small PHP lexer string escaping fix (#515).
-- Support unsigned/long long literals in C/C++ (#613).
+- Support comments before preprocessor directives, and unsigned/
+ long long literals in C/C++ (#613, #616).
Version 1.3.1
diff --git a/pygments/lexers/compiled.py b/pygments/lexers/compiled.py
index a3f85adb..8cbde6b7 100644
--- a/pygments/lexers/compiled.py
+++ b/pygments/lexers/compiled.py
@@ -43,8 +43,12 @@ class CLexer(RegexLexer):
tokens = {
'whitespace': [
- (r'^\s*#if\s+0', Comment.Preproc, 'if0'),
- (r'^\s*#', Comment.Preproc, 'macro'),
+ # preprocessor directives: without whitespace
+ ('^#if\s+0', Comment.Preproc, 'if0'),
+ ('^#', Comment.Preproc, 'macro'),
+ # or with whitespace
+ ('^' + _ws + r'#if\s+0', Comment.Preproc, 'if0'),
+ ('^' + _ws + '#', Comment.Preproc, 'macro'),
(r'^(\s*)([a-zA-Z_][a-zA-Z0-9_]*:(?!:))', bygroups(Text, Name.Label)),
(r'\n', Text),
(r'\s+', Text),
@@ -168,10 +172,17 @@ class CppLexer(RegexLexer):
filenames = ['*.cpp', '*.hpp', '*.c++', '*.h++', '*.cc', '*.hh', '*.cxx', '*.hxx']
mimetypes = ['text/x-c++hdr', 'text/x-c++src']
+ #: optional Comment or Whitespace
+ _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
+
tokens = {
'root': [
- (r'^\s*#if\s+0', Comment.Preproc, 'if0'),
- (r'^\s*#', Comment.Preproc, 'macro'),
+ # preprocessor directives: without whitespace
+ ('^#if\s+0', Comment.Preproc, 'if0'),
+ ('^#', Comment.Preproc, 'macro'),
+ # or with whitespace
+ ('^' + _ws + r'#if\s+0', Comment.Preproc, 'if0'),
+ ('^' + _ws + '#', Comment.Preproc, 'macro'),
(r'\n', Text),
(r'\s+', Text),
(r'\\\n', Text), # line continuation
@@ -1092,8 +1103,12 @@ class ObjectiveCLexer(RegexLexer):
tokens = {
'whitespace': [
- (r'^(\s*)(#if\s+0)', bygroups(Text, Comment.Preproc), 'if0'),
- (r'^(\s*)(#)', bygroups(Text, Comment.Preproc), 'macro'),
+ # preprocessor directives: without whitespace
+ ('^#if\s+0', Comment.Preproc, 'if0'),
+ ('^#', Comment.Preproc, 'macro'),
+ # or with whitespace
+ ('^' + _ws + r'#if\s+0', Comment.Preproc, 'if0'),
+ ('^' + _ws + '#', Comment.Preproc, 'macro'),
(r'\n', Text),
(r'\s+', Text),
(r'\\\n', Text), # line continuation