Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

Commit

Permalink
fix zeroturnaround#162, add handling for prefix/suffix only specialWo…
Browse files Browse the repository at this point in the history
…rdChars
  • Loading branch information
inferrinizzard committed Nov 17, 2021
1 parent ad3beae commit c48e7de
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/core/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface TokenizerOptions {
indexedPlaceholderTypes?: string[];
namedPlaceholderTypes: string[];
lineCommentTypes: string[];
specialWordChars?: string[];
specialWordChars?: { prefix?: string; suffix?: string; any: string };
operators?: string[];
}

Expand Down Expand Up @@ -52,23 +52,23 @@ export default class Tokenizer {
[TokenType.STRING]: regexFactory.createStringRegex(cfg.stringTypes),
[TokenType.RESERVED_KEYWORD]: regexFactory.createReservedWordRegex(
cfg.reservedKeywords,
cfg.specialWordChars
(cfg.specialWordChars?.any ?? '') + (cfg.specialWordChars?.suffix ?? '')
),
[TokenType.RESERVED_DEPENDENT_CLAUSE]: regexFactory.createReservedWordRegex(
cfg.reservedDependentClauses ?? [],
cfg.specialWordChars
(cfg.specialWordChars?.any ?? '') + (cfg.specialWordChars?.suffix ?? '')
),
[TokenType.RESERVED_LOGICAL_OPERATOR]: regexFactory.createReservedWordRegex(
cfg.reservedLogicalOperators,
cfg.specialWordChars
(cfg.specialWordChars?.any ?? '') + (cfg.specialWordChars?.suffix ?? '')
),
[TokenType.RESERVED_COMMAND]: regexFactory.createReservedWordRegex(
cfg.reservedCommands,
cfg.specialWordChars
(cfg.specialWordChars?.any ?? '') + (cfg.specialWordChars?.suffix ?? '')
),
[TokenType.RESERVED_BINARY_COMMAND]: regexFactory.createReservedWordRegex(
cfg.reservedBinaryCommands,
cfg.specialWordChars
(cfg.specialWordChars?.any ?? '') + (cfg.specialWordChars?.suffix ?? '')
),
[TokenType.OPERATOR]: regexFactory.createOperatorRegex('+-/*%&|^><=.,;[]{}`:$', [
'<>',
Expand Down
23 changes: 14 additions & 9 deletions src/core/regexFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,29 @@ export function createLineCommentRegex(lineCommentTypes: string[]) {
);
}

export function createReservedWordRegex(
reservedKeywords: string[],
specialWordChars: string[] = []
) {
export function createReservedWordRegex(reservedKeywords: string[], specialWordChars = '') {
if (reservedKeywords.length === 0) {
return new RegExp(`^\b$`, 'u');
}
const reservedKeywordsPattern = sortByLengthDesc(reservedKeywords)
.join('|')
.replace(/ /gu, '\\s+');
return new RegExp(`^(${reservedKeywordsPattern})(?![${specialWordChars.join('')}]+)\\b`, 'iu');
return new RegExp(
`^(${reservedKeywordsPattern})(?![${escapeRegExp(specialWordChars)}]+)\\b`,
'iu'
);
}

export function createWordRegex(specialChars: string[] = []) {
export function createWordRegex(
specialChars: { any?: string; suffix?: string; prefix?: string } = {}
) {
const prefixLookBehind = `(?<=[${escapeRegExp(specialChars.prefix ?? '')}]?)`;
const suffixLookAhead = `(?=[${escapeRegExp(specialChars.suffix ?? '')}]?)`;
const unicodeWordChar =
'\\p{Alphabetic}\\p{Mark}\\p{Decimal_Number}\\p{Connector_Punctuation}\\p{Join_Control}';
const specialWordChars = `${escapeRegExp(specialChars.any ?? '')}`;
return new RegExp(
`^([\\p{Alphabetic}\\p{Mark}\\p{Decimal_Number}\\p{Connector_Punctuation}\\p{Join_Control}${specialChars.join(
''
)}]+)`,
`^(${prefixLookBehind}([${unicodeWordChar}${specialWordChars}]+)${suffixLookAhead})`,
'u'
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/languages/Db2Formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ export default class Db2Formatter extends Formatter {
static indexedPlaceholderTypes = ['?'];
static namedPlaceholderTypes = [':'];
static lineCommentTypes = ['--'];
static specialWordChars = ['#', '@'];
static specialWordChars = { any: '#@' };
static operators = ['**', '!>', '!<', '||'];

tokenizer() {
Expand Down
2 changes: 1 addition & 1 deletion src/languages/MariaDbFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ export default class MariaDbFormatter extends Formatter {
static indexedPlaceholderTypes = ['?'];
static namedPlaceholderTypes = [];
static lineCommentTypes = ['--', '#'];
static specialWordChars = ['@'];
static specialWordChars = { any: '@' };
static operators = [':=', '<<', '>>', '<=>', '&&', '||'];

tokenizer() {
Expand Down
2 changes: 1 addition & 1 deletion src/languages/MySqlFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ export default class MySqlFormatter extends Formatter {
static indexedPlaceholderTypes = ['?'];
static namedPlaceholderTypes = [];
static lineCommentTypes = ['--', '#'];
static specialWordChars = ['@'];
static specialWordChars = { any: '@', prefix: ':' };
static operators = [':=', '<<', '>>', '<=>', '&&', '||', '->', '->>'];

tokenizer() {
Expand Down
2 changes: 1 addition & 1 deletion src/languages/PlSqlFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ export default class PlSqlFormatter extends Formatter {
static indexedPlaceholderTypes = ['?'];
static namedPlaceholderTypes = [':'];
static lineCommentTypes = ['--'];
static specialWordChars = ['_', '$', '#', '.', '@'];
static specialWordChars = { any: '_$#.@' };
static operators = [
'||',
'**',
Expand Down
2 changes: 1 addition & 1 deletion src/languages/TSqlFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ export default class TSqlFormatter extends Formatter {
static indexedPlaceholderTypes = [];
static namedPlaceholderTypes = ['@'];
static lineCommentTypes = ['--'];
static specialWordChars = ['#', '@'];
static specialWordChars = { any: '#@' };
static operators = ['!<', '!>', '+=', '-=', '*=', '/=', '%=', '|=', '&=', '^=', '::'];

tokenizer() {
Expand Down

0 comments on commit c48e7de

Please sign in to comment.