Skip to content

Commit

Permalink
(objectivec) Sync preprocessor highlighting with cpp (#2265)
Browse files Browse the repository at this point in the history
The objectivec highlighting had a few bugs and was missing a few
features:

- Expressions with < were misidentified as a string like <stdio.h>
  (fixes #1964).
- Keywords like if, else, endif, etc. were not highlighted.
- The escape sequences in string literals were not parsed.
- Line continuations were not parsed.
- Comments were not parsed.

Fix all these by adapting cpp's preprocessor definition. Pull in the
preprocessor test from cpp. (This is actually objectivec's first markup
test. It probably could do with tests beyond just this feature.)
  • Loading branch information
davidben authored and joshgoebel committed Nov 12, 2019
1 parent 5ef8249 commit 980ac21
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -20,6 +20,7 @@ Improvements:
- fix(parser): Look-ahead regex now work for end matches also (#2237) [Josh Goebel][]
- fix(parser): Better errors when a language is missing (#2236) [Josh Goebel][]
- fix(go): Fix escaped character literals (#2266) [David Benjamin][]
- fix(objectivec): Fix various preprocessor highlighting issues (#2265) [David Benjamin][]
- fix(objectivec): Handle multibyte character literals (#2268) [David Benjamin][]

[Josh Goebel]: https://github.com/yyyc514
Expand Down
2 changes: 1 addition & 1 deletion src/languages/cpp.js
Expand Up @@ -56,7 +56,7 @@ function(hljs) {
hljs.inherit(STRINGS, {className: 'meta-string'}),
{
className: 'meta-string',
begin: /<[^\n>]*>/, end: /$/,
begin: /<.*?>/, end: /$/,
illegal: '\\n',
},
hljs.C_LINE_COMMENT_MODE,
Expand Down
22 changes: 15 additions & 7 deletions src/languages/objectivec.js
Expand Up @@ -66,16 +66,24 @@ function(hljs) {
},
{
className: 'meta',
begin: '#',
end: '$',
begin: /#\s*[a-z]+\b/, end: /$/,
keywords: {
'meta-keyword':
'if else elif endif define undef warning error line ' +
'pragma ifdef ifndef include'
},
contains: [
{
begin: /\\\n/, relevance: 0
},
hljs.inherit(hljs.QUOTE_STRING_MODE, {className: 'meta-string'}),
{
className: 'meta-string',
variants: [
{ begin: '\"', end: '\"' },
{ begin: '<', end: '>' }
]
}
begin: /<.*?>/, end: /$/,
illegal: '\\n',
},
hljs.C_LINE_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE
]
},
{
Expand Down
6 changes: 6 additions & 0 deletions test/markup/cpp/preprocessor.expect.txt
Expand Up @@ -11,3 +11,9 @@
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> x(v) ((v))</span>
<span class="hljs-meta"># <span class="hljs-meta-keyword">define</span> x(v) ((v))</span>
<span class="hljs-meta"># <span class="hljs-meta-keyword">define</span> x(v) ((v))</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> MACRO_WITH_STRING_ARG(<span class="hljs-meta-string">"hello \"world\""</span>)</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">elif</span> MULTI_LINE <span class="hljs-comment">/* comment */</span> &lt; \
EXPRESSION</span>
<span class="hljs-keyword">int</span> bar;
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span> <span class="hljs-comment">// comment</span></span>
6 changes: 6 additions & 0 deletions test/markup/cpp/preprocessor.txt
Expand Up @@ -11,3 +11,9 @@ int foo(void)
#define x(v) ((v))
# define x(v) ((v))
# define x(v) ((v))

#if MACRO_WITH_STRING_ARG("hello \"world\"")
#elif MULTI_LINE /* comment */ < \
EXPRESSION
int bar;
#endif // comment
19 changes: 19 additions & 0 deletions test/markup/objectivec/preprocessor.expect.txt
@@ -0,0 +1,19 @@
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> foo 1&lt;&lt;16</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">ifdef</span> DEBUG</span>
TYPE1 foo(<span class="hljs-keyword">void</span>)
<span class="hljs-meta">#<span class="hljs-meta-keyword">else</span></span>
<span class="hljs-keyword">int</span> foo(<span class="hljs-keyword">void</span>)
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>
{ }

<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> x(v) ((v))</span>
<span class="hljs-meta"># <span class="hljs-meta-keyword">define</span> x(v) ((v))</span>
<span class="hljs-meta"># <span class="hljs-meta-keyword">define</span> x(v) ((v))</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> MACRO_WITH_STRING_ARG(<span class="hljs-meta-string">"hello \"world\""</span>)</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">elif</span> MULTI_LINE <span class="hljs-comment">/* comment */</span> &lt; \
EXPRESSION</span>
<span class="hljs-keyword">int</span> bar;
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span> <span class="hljs-comment">// comment</span></span>
19 changes: 19 additions & 0 deletions test/markup/objectivec/preprocessor.txt
@@ -0,0 +1,19 @@
#include <iostream>
#define foo 1<<16

#ifdef DEBUG
TYPE1 foo(void)
#else
int foo(void)
#endif
{ }

#define x(v) ((v))
# define x(v) ((v))
# define x(v) ((v))

#if MACRO_WITH_STRING_ARG("hello \"world\"")
#elif MULTI_LINE /* comment */ < \
EXPRESSION
int bar;
#endif // comment

0 comments on commit 980ac21

Please sign in to comment.