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(fortran) support intrinsic data types & better 0 width match error detection #2379

Merged
merged 3 commits into from Feb 6, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -14,6 +14,7 @@ Core Changes:

Language Improvements:

- (fortran) enh(fortran) support intrinsic data types (#2379) [Josh Goebel][]
- enh(java) annotations can include numbers (#2377) [Josh Goebel][]
- enh(java) annotations can take params (#2377) [Josh Goebel][]
- enh(java) allow annotations inside function call params (#2377) [Josh Goebel][]
Expand Down
7 changes: 7 additions & 0 deletions src/highlight.js
Expand Up @@ -694,6 +694,12 @@ https://highlightjs.org/
if (lastMatch.type=="begin" && match.type=="end" && lastMatch.index == match.index && lexeme === "") {
// spit the "skipped" character that our regex choked on back into the output sequence
mode_buffer += codeToHighlight.slice(match.index, match.index + 1);
if (!SAFE_MODE) {
var err = new Error('0 width match regex');
err.languageName = languageName;
err.badRule = lastMatch.rule;
throw(err);
}
return 1;
}
lastMatch = match;
Expand Down Expand Up @@ -1018,6 +1024,7 @@ https://highlightjs.org/
hljs.inherit = inherit;
hljs.addPlugin = addPlugin;
hljs.debugMode = function() { SAFE_MODE = false; }
hljs.safeMode = function() { SAFE_MODE = true; }

// Common regexps
hljs.IDENT_RE = '[a-zA-Z]\\w*';
Expand Down
3 changes: 2 additions & 1 deletion src/languages/fortran.js
Expand Up @@ -69,7 +69,8 @@ function(hljs) {
hljs.COMMENT('!', '$', {relevance: 0}),
{
className: 'number',
begin: '(?=\\b|\\+|\\-|\\.)(?=\\.\\d|\\d)(?:\\d+)?(?:\\.?\\d*)(?:[de][+-]?\\d+)?\\b\\.?',
// regex in both fortran and irpf90 should match
begin: '(?=\\b|\\+|\\-|\\.)(?:\\.|\\d+\\.?)\\d*([de][+-]?\\d+)?(_[a-z_\\d]+)?',
relevance: 0
}
]
Expand Down
3 changes: 2 additions & 1 deletion src/languages/irpf90.js
Expand Up @@ -75,7 +75,8 @@ function(hljs) {
hljs.COMMENT('begin_doc', 'end_doc', {relevance: 10}),
{
className: 'number',
begin: '(?=\\b|\\+|\\-|\\.)(?=\\.\\d|\\d)(?:\\d+)?(?:\\.?\\d*)(?:[de][+-]?\\d+)?\\b\\.?',
// regex in both fortran and irpf90 should match
begin: '(?=\\b|\\+|\\-|\\.)(?:\\.|\\d+\\.?)\\d*([de][+-]?\\d+)?(_[a-z_\\d]+)?',
relevance: 0
}
]
Expand Down
1 change: 1 addition & 0 deletions test/detect/index.js
Expand Up @@ -5,6 +5,7 @@ delete require.cache[require.resolve('../../build/lib/highlight')]

const fs = require('fs').promises;
const hljs = require('../../build');
hljs.debugMode(); // tests run in debug mode so errors are raised
const path = require('path');
const utility = require('../utility');

Expand Down
3 changes: 3 additions & 0 deletions test/index.js
@@ -1,5 +1,8 @@
'use strict';

const hljs = require('../build');
hljs.debugMode(); // tests run in debug mode so errors are raised

// Tests specific to the API exposed inside the hljs object.
// Right now, that only includes tests for several common regular expressions.
require('./api');
Expand Down
6 changes: 3 additions & 3 deletions test/markup/fortran/numbers.expect.txt
Expand Up @@ -14,6 +14,6 @@ var1
va1r
mo_tot_8 = <span class="hljs-number">1.</span>/(<span class="hljs-number">0.4</span>*<span class="hljs-built_in">log</span>(<span class="hljs-built_in">float</span>(elec_num_tot_8+<span class="hljs-number">0.4</span>)))

<span class="hljs-number">6</span>_ikind
<span class="hljs-number">1</span>_c_short
<span class="hljs-number">6.</span><span class="hljs-number">6</span>66666666666666_DBL
<span class="hljs-number">6_ikind</span>
<span class="hljs-number">1_c_short</span>
<span class="hljs-number">6.666666666666666_DBL</span>
7 changes: 7 additions & 0 deletions test/parser/should-not-destroyData.js
Expand Up @@ -5,6 +5,7 @@ describe("bugs", function () {
// CONTEXT: https://github.com/highlightjs/highlight.js/pull/2219
describe("a grammar with a mode that makes a 0 width match", () => {
it("should instead count it as a 1 character match", () => {
hljs.safeMode();
hljs.registerLanguage('test-language', (hljs) => {

// broken regex from old Fortran ruleset
Expand All @@ -28,6 +29,12 @@ describe("bugs", function () {
// Incorrect prior output:
// 'The number is <span class="hljs-number"></span>23_longint yes.'
)
hljs.debugMode();
should(() => {
hljs.highlight('test-language', 'The number is 123_longint yes.').value
}).throw(Error, {
message: "0 width match regex",
languageName: "test-language"})
})
})
})