Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(cpp) simpler struct matching #3078

Merged
merged 16 commits into from
Apr 13, 2021
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
11 changes: 11 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ Parser:

- enh(parser) support multi-class matchers (#3081) [Josh Goebel][]
- enh(parser) Detect comments based on english like text, rather than keyword list [Josh Goebel][]
- adds `title.class` sub-scope support (#3078) [Josh Goebel][]
- adds `title.function` sub-scope support (#3078) [Josh Goebel][]
- adds `beforeMatch` compiler extension (#3078) [Josh Goebel][]

Grammars:

- enh(java) Simplified class-like matcher (#3078) [Josh Goebel][]
- enh(cpp) Simplified class-like matcher (#3078) [Josh Goebel][]
- enh(rust) Simplified class-like matcher (#3078) [Josh Goebel][]
- enh(actionscript) Simplified class-like matcher (#3078) [Josh Goebel][]
- enh(arcade) `function.title` => `title.function` (#3078) [Josh Goebel][]
- enh(autoit) `function.title` => `title.function` (#3078) [Josh Goebel][]
- enh(c) `function.title` => `title.function` (#3078) [Josh Goebel][]
- enh(rust) support function invoke and `impl` (#3078) [Josh Goebel][]
- chore(properties) disable auto-detection #3102 [Josh Goebel][]
- fix(properties) fix incorrect handling of non-alphanumeric keys #3102 [Egor Rogov][]
- enh(java) support functions with nested template types (#2641) [Josh Goebel][]
Expand Down
261 changes: 142 additions & 119 deletions docs/css-classes-reference.rst

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions docs/mode-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,29 @@ This callback is triggered the moment an end match is detected. ``matchData`` in
For an example of usage see ``END_SAME_AS_BEGIN`` in ``modes.js``.


beforeMatch
^^^^^^^^^^^

- **type**: string

Used to qualify a match by the content that immediately precedes it. This is syntactic sugar that generates a much more complex mode that first matches the entire sequence (using look-ahead), then glosses over the ``beforeMatch`` portion and ``starts`` a new rule to handle the actual match.

Notes:

- Any ``keywords`` specified are applied to the ``beforeMatch`` text as well (as shown in the example below)
- Do not get this confused with any type of look-behind. We are always looking forward.
- This is incompatible with ``starts``.

::

{
beforeMatch: /\b(enum|class|struct|union)\s+/,
keywords: "enum class struct union",
begin: /\w+/,
className: "title.class"
}


beginKeywords
^^^^^^^^^^^^^

Expand Down
5 changes: 3 additions & 2 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ const HLJS = function(hljs) {
*/
function emitMultiClass(mode, match) {
let i = 1;
while (match[i]) {
// eslint-disable-next-line no-undefined
while (match[i] !== undefined) {
const klass = language.classNameAliases[mode.className[i]] || mode.className[i];
const text = match[i];
if (klass) { emitter.addKeyword(text, klass); } else {
Expand Down Expand Up @@ -452,7 +453,7 @@ const HLJS = function(hljs) {
modeBuffer += codeToHighlight.slice(match.index, match.index + 1);
if (!SAFE_MODE) {
/** @type {AnnotatedError} */
const err = new Error('0 width match regex');
const err = new Error(`0 width match regex (${languageName})`);
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
err.languageName = languageName;
err.badRule = lastMatch.rule;
throw err;
Expand Down
27 changes: 13 additions & 14 deletions src/languages/actionscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import * as regex from '../lib/regex.js';
/** @type LanguageFn */
export default function(hljs) {
const IDENT_RE = /[a-zA-Z_$][a-zA-Z0-9_$]*/;
const PKG_NAME_RE = regex.concat(
IDENT_RE,
regex.concat("(\\.", IDENT_RE, ")*")
);
const IDENT_FUNC_RETURN_TYPE_RE = /([*]|[a-zA-Z_$][a-zA-Z0-9_$]*)/;

const AS3_REST_ARG_MODE = {
Expand Down Expand Up @@ -92,20 +96,16 @@ export default function(hljs) {
hljs.C_BLOCK_COMMENT_MODE,
hljs.C_NUMBER_MODE,
{
className: 'class',
beginKeywords: 'package',
end: /\{/,
contains: [ hljs.TITLE_MODE ]
beforeMatch: /\b(package)\s+/,
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
keywords: "package",
match: PKG_NAME_RE,
className: "title.class"
},
{
className: 'class',
beginKeywords: 'class interface',
end: /\{/,
excludeEnd: true,
contains: [
{ beginKeywords: 'extends implements' },
hljs.TITLE_MODE
]
beforeMatch: /\b(class|interface|extends|implements)\s+/,
keywords: "class interface extends implements",
match: IDENT_RE,
className: "title.class"
},
{
className: 'meta',
Expand All @@ -114,13 +114,12 @@ export default function(hljs) {
keywords: { 'meta-keyword': 'import include' }
},
{
className: 'function',
beginKeywords: 'function',
end: /[{;]/,
excludeEnd: true,
illegal: /\S/,
contains: [
hljs.TITLE_MODE,
hljs.inherit(hljs.TITLE_MODE, { className: "title.function" }),
{
className: 'params',
begin: /\(/,
Expand Down
2 changes: 1 addition & 1 deletion src/languages/arcade.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ export default function(hljs) {
relevance: 0
},
{
className: 'function',
beginKeywords: 'function',
end: /\{/,
excludeEnd: true,
contains: [
hljs.inherit(hljs.TITLE_MODE, {
className: "title.function",
begin: IDENT_RE
}),
{
Expand Down
5 changes: 2 additions & 3 deletions src/languages/autoit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function(hljs) {
"Tidy_On",
"Tidy_Parameters"
]

const LITERAL = 'True False And Null Not Or Default';

const BUILT_IN
Expand Down Expand Up @@ -140,12 +140,11 @@ export default function(hljs) {
};

const FUNCTION = {
className: 'function',
beginKeywords: 'Func',
end: '$',
illegal: '\\$|\\[|%',
contains: [
hljs.UNDERSCORE_TITLE_MODE,
hljs.inherit(hljs.UNDERSCORE_TITLE_MODE, { className: "title.function" }),
{
className: 'params',
begin: '\\(',
Expand Down
5 changes: 3 additions & 2 deletions src/languages/c.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ export default function(hljs) {
};

const FUNCTION_DECLARATION = {
className: 'function',
begin: '(' + FUNCTION_TYPE_RE + '[\\*&\\s]+)+' + FUNCTION_TITLE,
returnBegin: true,
end: /[{;=]/,
Expand All @@ -251,7 +250,9 @@ export default function(hljs) {
{
begin: FUNCTION_TITLE,
returnBegin: true,
contains: [ TITLE_MODE ],
contains: [
hljs.inherit(TITLE_MODE, { className: "title.function" })
],
relevance: 0
},
{
Expand Down
17 changes: 6 additions & 11 deletions src/languages/cpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function(hljs) {
const DECLTYPE_AUTO_RE = 'decltype\\(auto\\)';
const NAMESPACE_RE = '[a-zA-Z_]\\w*::';
const TEMPLATE_ARGUMENT_RE = '<[^<>]+>';
const FUNCTION_TYPE_RE = '(' +
const FUNCTION_TYPE_RE = '(?!struct)(' +
DECLTYPE_AUTO_RE + '|' +
regex.optional(NAMESPACE_RE) +
'[a-zA-Z_]\\w*' + regex.optional(TEMPLATE_ARGUMENT_RE) +
Expand Down Expand Up @@ -233,7 +233,7 @@ export default function(hljs) {
'atomic_bool atomic_char atomic_schar ' +
'atomic_uchar atomic_short atomic_ushort atomic_int atomic_uint atomic_long atomic_ulong atomic_llong ' +
'atomic_ullong new throw return ' +
'and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq',
'and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq struct',
built_in: '_Bool _Complex _Imaginary',
_relevance_hints: COMMON_CPP_HINTS,
literal: 'true false nullptr NULL'
Expand Down Expand Up @@ -403,15 +403,10 @@ export default function(hljs) {
keywords: CPP_KEYWORDS
},
{
className: 'class',
beginKeywords: 'enum class struct union',
end: /[{;:<>=]/,
contains: [
{
beginKeywords: "final class struct"
},
hljs.TITLE_MODE
]
beforeMatch: /\b(enum|class|struct|union)\s+/,
keywords: "enum class struct union",
match: /\w+/,
className: "title.class"
}
])
};
Expand Down
Loading