Skip to content

Commit

Permalink
Remove spaces from regexps in anticipation of identical change on scr…
Browse files Browse the repository at this point in the history
  • Loading branch information
wkeese committed Oct 3, 2023
1 parent 8cabde8 commit b73a745
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-regex-spaces */
'use strict'

const keywords = require('./keywords')
Expand All @@ -23,37 +22,32 @@ const DEFAULT_OPTIONS = {
const DEFAULT_KEYWORD = 'default'

const highlighters = [
/\b(?<number> \d+ (?:\.\d+)? )\b/,
/\b(?<number>\d+(?:\.\d+)?)\b/,

// Note: Repeating string escapes like 'sql''server' will also work as they are just repeating strings
/(?<string> '(?: [^'\\] | \\. )*' | "(?: [^"\\] | \\. )*" )/,
/(?<string>'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")/,

/(?<comment> --[^\n\r]* | #[^\n\r]* | \/\* (?: [^*] | \* (?!\/) )* \*\/ )/,
/(?<comment>--[^\n\r]*|#[^\n\r]*|\/\*(?:[^*]|\*(?!\/))*\*\/)/,

// Future improvement: Comments should be allowed between the function name and the opening parenthesis
/\b(?<function> \w+ ) (?= \s*\( )/,
/\b(?<function>\w+)(?=\s*\()/,

/(?<bracket> [()] )/,
/(?<bracket>[()])/,

/(?<special> != | [=%*/\-+,;:<>.] )/,
/(?<special>!=|[=%*/\-+,;:<>.])/,

/(?<identifier> \b\w+\b | `(?: [^`\\] | \\. )*`)/,
/(?<identifier>\b\w+\b|`(?:[^`\\]|\\.)*`)/,

/(?<whitespace> \s+ )/,
/(?<whitespace>\s+)/,

/(?<unknown> \.+? )/
/(?<unknown>\.+?)/
]

function getRegexString (regex) {
const str = regex.toString()
return str.replace(/^\/|\/\w*$|[\t ]+/g, '')
}

// Regex of the shape /((?<token1>...)|(?<token2>...)|...|$)/g
const tokenizer = new RegExp(
'(' +
'\\b(?<keyword>' + keywords.join('|') + ')\\b|' +
highlighters.map(getRegexString).join('|') +
highlighters.map(regex => regex.source).join('|') +
')',
'gis'
)
Expand Down

0 comments on commit b73a745

Please sign in to comment.