Skip to content

Commit

Permalink
refactor: minify precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski committed Sep 22, 2024
1 parent f52b3b1 commit 1a52e9c
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/constructs/anchors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@ import type { EncodedRegex } from '../types';
* Start of string anchor. Matches the start of of string. In `multiline` mode, also matches immediately following a newline.
*/
export const startOfString: EncodedRegex = {
k: 'atom',
k: 0,
p: '^',
};

/**
* End of string anchor. Matches the end of a string. In `multiline` mode, also matches immediately preceding a newline.
*/
export const endOfString: EncodedRegex = {
k: 'atom',
k: 0,
p: '$',
};

/**
* Word boundary anchor. Matches the position where one side is a word character (alphanumeric or underscore) and the other side is a non-word character (anything else).
*/
export const wordBoundary: EncodedRegex = {
k: 'atom',
k: 0,
p: '\\b',
};

/**
* Non-word boundary anchor. Matches the position where both sides are word characters.
*/
export const nonWordBoundary: EncodedRegex = {
k: 'atom',
k: 0,
p: '\\B',
};

Expand Down
6 changes: 3 additions & 3 deletions src/constructs/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export function capture(sequence: RegexSequence, options?: CaptureOptions): Enco
const name = options?.name;
if (name) {
return {
k: 'atom',
k: 0,
p: `(?<${name}>${encode(sequence).p})`,
};
}

return {
k: 'atom',
k: 0,
p: `(${encode(sequence).p})`,
};
}
Expand All @@ -43,7 +43,7 @@ export function capture(sequence: RegexSequence, options?: CaptureOptions): Enco
*/
export function ref(name: string): Reference {
return {
k: 'atom',
k: 0,
p: `\\k<${name}>`,
name,
};
Expand Down
2 changes: 1 addition & 1 deletion src/constructs/char-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function encodeCharClass(
isNegated?: boolean,
): EncodedRegex {
return {
k: 'atom',
k: 0,
p: `[${isNegated ? '^' : ''}${this.e.join('')}]`,
};
}
14 changes: 7 additions & 7 deletions src/constructs/char-escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import type { CharacterEscape, EncodedRegex } from '../types';
* Specifically this one is NOT a character escape.
*/
export const any: EncodedRegex = {
k: 'atom',
k: 0,
p: '.',
};

/**
* Matches any digit (0-9).
*/
export const digit: CharacterEscape = {
k: 'atom',
k: 0,
p: '\\d',
e: ['\\d'],
};
Expand All @@ -22,7 +22,7 @@ export const digit: CharacterEscape = {
* Matches any non-digit (0-9) character.
*/
export const nonDigit: CharacterEscape = {
k: 'atom',
k: 0,
p: '\\D',
e: ['\\D'],
};
Expand All @@ -31,7 +31,7 @@ export const nonDigit: CharacterEscape = {
* Matches any word character (alphanumeric or underscore).
*/
export const word: CharacterEscape = {
k: 'atom',
k: 0,
p: '\\w',
e: ['\\w'],
};
Expand All @@ -40,7 +40,7 @@ export const word: CharacterEscape = {
* Matches any non-word (alphanumeric or underscore) character.
*/
export const nonWord: CharacterEscape = {
k: 'atom',
k: 0,
p: '\\W',
e: ['\\W'],
};
Expand All @@ -49,7 +49,7 @@ export const nonWord: CharacterEscape = {
* Matches any whitespace character (space, tab, newline, etc.).
*/
export const whitespace: CharacterEscape = {
k: 'atom',
k: 0,
p: '\\s',
e: ['\\s'],
};
Expand All @@ -58,7 +58,7 @@ export const whitespace: CharacterEscape = {
* Matches any non-whitespace (space, tab, newline, etc.) character.
*/
export const nonWhitespace: CharacterEscape = {
k: 'atom',
k: 0,
p: '\\S',
e: ['\\S'],
};
Expand Down
2 changes: 1 addition & 1 deletion src/constructs/choice-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function choiceOf(...alternatives: RegexSequence[]): EncodedRegex {
}

return {
k: 'disjunction',
k: 2,
p: encodedAlternatives.map((n) => n.p).join('|'),
};
}
2 changes: 1 addition & 1 deletion src/constructs/lookahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
*/
export function lookahead(sequence: RegexSequence): EncodedRegex {
return {
k: 'atom',
k: 0,
p: `(?=${encode(sequence).p})`,
};
}
2 changes: 1 addition & 1 deletion src/constructs/lookbehind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
*/
export function lookbehind(sequence: RegexSequence): EncodedRegex {
return {
k: 'atom',
k: 0,
p: `(?<=${encode(sequence).p})`,
};
}
2 changes: 1 addition & 1 deletion src/constructs/negative-lookahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
*/
export function negativeLookahead(sequence: RegexSequence): EncodedRegex {
return {
k: 'atom',
k: 0,
p: `(?!${encode(sequence).p})`,
};
}
2 changes: 1 addition & 1 deletion src/constructs/negative-lookbehind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
*/
export function negativeLookbehind(sequence: RegexSequence): EncodedRegex {
return {
k: 'atom',
k: 0,
p: `(?<!${encode(sequence).p})`,
};
}
6 changes: 3 additions & 3 deletions src/constructs/quantifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface QuantifierOptions {
export function zeroOrMore(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
const elements = ensureElements(sequence);
return {
k: 'sequence',
k: 1,
p: `${encodeAtomic(elements)}*${options?.greedy === false ? '?' : ''}`,
};
}
Expand All @@ -29,7 +29,7 @@ export function zeroOrMore(sequence: RegexSequence, options?: QuantifierOptions)
export function oneOrMore(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
const elements = ensureElements(sequence);
return {
k: 'sequence',
k: 1,
p: `${encodeAtomic(elements)}+${options?.greedy === false ? '?' : ''}`,
};
}
Expand All @@ -43,7 +43,7 @@ export function oneOrMore(sequence: RegexSequence, options?: QuantifierOptions):
export function optional(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
const elements = ensureElements(sequence);
return {
k: 'sequence',
k: 1,
p: `${encodeAtomic(elements)}?${options?.greedy === false ? '?' : ''}`,
};
}
4 changes: 2 additions & 2 deletions src/constructs/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export function repeat(sequence: RegexSequence, options: RepeatOptions): Encoded

if (typeof options === 'number') {
return {
k: 'sequence',
k: 1,
p: `${encodeAtomic(elements)}{${options}}`,
};
}

return {
k: 'sequence',
k: 1,
p: `${encodeAtomic(elements)}{${options.min},${options?.max ?? ''}}${
options.greedy === false ? '?' : ''
}`,
Expand Down
4 changes: 2 additions & 2 deletions src/constructs/unicode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function unicodeChar(codePoint: number): CharacterEscape {
: `\\u{${codePoint.toString(16)}}`; // 1-6 digit hex (requires unicode-aware mode)

return {
k: 'atom',
k: 0,
p: escape,
e: [escape],
};
Expand All @@ -50,7 +50,7 @@ export function unicodeProperty(property: string, value?: string): CharacterEsca
const escape = `\\p{${property}${value ? `=${value}` : ''}}`;

return {
k: 'atom',
k: 0,
p: escape,
e: [escape],
};
Expand Down
10 changes: 5 additions & 5 deletions src/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ export function encode(sequence: RegexSequence): EncodedRegex {
}

return {
k: 'sequence',
k: 1,
p: encoded

Check failure on line 14 in src/encoder.ts

View workflow job for this annotation

GitHub Actions / Lint and Typecheck

Replace `⏎······.map((n)·=>·(n.k·===·2·?·encodeAtomic(n)·:·n.p))⏎······` with `.map((n)·=>·(n.k·===·2·?·encodeAtomic(n)·:·n.p))`
.map((n) => (n.k === 'disjunction' ? encodeAtomic(n) : n.p))
.map((n) => (n.k === 2 ? encodeAtomic(n) : n.p))
.join(''),
};
}

export function encodeAtomic(sequence: RegexSequence): string {
const encoded = encode(sequence);
return encoded.k === 'atom' ? encoded.p : `(?:${encoded.p})`;
return encoded.k === 0 ? encoded.p : `(?:${encoded.p})`;
}

function encodeElement(element: RegexElement): EncodedRegex {
Expand Down Expand Up @@ -51,7 +51,7 @@ function encodeText(text: string): EncodedRegex {

return {
// Optimize for single character case
k: text.length === 1 ? 'atom' : 'sequence',
k: text.length === 1 ? 0 : 1,
p: escapeText(text),
};
}
Expand All @@ -61,7 +61,7 @@ function encodeRegExp(regexp: RegExp): EncodedRegex {

return {
// Encode at safe precedence
k: isAtomicPattern(p) ? 'atom' : 'disjunction',
k: isAtomicPattern(p) ? 0 : 2,
p,
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface EncodedRegex {
/**
* Precedence of given regex pattern.
*/
export type EncodePrecedence = 'atom' | 'sequence' | 'disjunction';
export type EncodePrecedence = 0 | 1 | 2;

/**
* Regex patter that can be encoded by calling the `encode` method.
Expand Down

0 comments on commit 1a52e9c

Please sign in to comment.