Skip to content

Commit 1a52e9c

Browse files
committed
refactor: minify precedence
1 parent f52b3b1 commit 1a52e9c

14 files changed

+33
-33
lines changed

src/constructs/anchors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@ import type { EncodedRegex } from '../types';
44
* Start of string anchor. Matches the start of of string. In `multiline` mode, also matches immediately following a newline.
55
*/
66
export const startOfString: EncodedRegex = {
7-
k: 'atom',
7+
k: 0,
88
p: '^',
99
};
1010

1111
/**
1212
* End of string anchor. Matches the end of a string. In `multiline` mode, also matches immediately preceding a newline.
1313
*/
1414
export const endOfString: EncodedRegex = {
15-
k: 'atom',
15+
k: 0,
1616
p: '$',
1717
};
1818

1919
/**
2020
* 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).
2121
*/
2222
export const wordBoundary: EncodedRegex = {
23-
k: 'atom',
23+
k: 0,
2424
p: '\\b',
2525
};
2626

2727
/**
2828
* Non-word boundary anchor. Matches the position where both sides are word characters.
2929
*/
3030
export const nonWordBoundary: EncodedRegex = {
31-
k: 'atom',
31+
k: 0,
3232
p: '\\B',
3333
};
3434

src/constructs/capture.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ export function capture(sequence: RegexSequence, options?: CaptureOptions): Enco
2121
const name = options?.name;
2222
if (name) {
2323
return {
24-
k: 'atom',
24+
k: 0,
2525
p: `(?<${name}>${encode(sequence).p})`,
2626
};
2727
}
2828

2929
return {
30-
k: 'atom',
30+
k: 0,
3131
p: `(${encode(sequence).p})`,
3232
};
3333
}
@@ -43,7 +43,7 @@ export function capture(sequence: RegexSequence, options?: CaptureOptions): Enco
4343
*/
4444
export function ref(name: string): Reference {
4545
return {
46-
k: 'atom',
46+
k: 0,
4747
p: `\\k<${name}>`,
4848
name,
4949
};

src/constructs/char-class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function encodeCharClass(
8181
isNegated?: boolean,
8282
): EncodedRegex {
8383
return {
84-
k: 'atom',
84+
k: 0,
8585
p: `[${isNegated ? '^' : ''}${this.e.join('')}]`,
8686
};
8787
}

src/constructs/char-escape.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import type { CharacterEscape, EncodedRegex } from '../types';
55
* Specifically this one is NOT a character escape.
66
*/
77
export const any: EncodedRegex = {
8-
k: 'atom',
8+
k: 0,
99
p: '.',
1010
};
1111

1212
/**
1313
* Matches any digit (0-9).
1414
*/
1515
export const digit: CharacterEscape = {
16-
k: 'atom',
16+
k: 0,
1717
p: '\\d',
1818
e: ['\\d'],
1919
};
@@ -22,7 +22,7 @@ export const digit: CharacterEscape = {
2222
* Matches any non-digit (0-9) character.
2323
*/
2424
export const nonDigit: CharacterEscape = {
25-
k: 'atom',
25+
k: 0,
2626
p: '\\D',
2727
e: ['\\D'],
2828
};
@@ -31,7 +31,7 @@ export const nonDigit: CharacterEscape = {
3131
* Matches any word character (alphanumeric or underscore).
3232
*/
3333
export const word: CharacterEscape = {
34-
k: 'atom',
34+
k: 0,
3535
p: '\\w',
3636
e: ['\\w'],
3737
};
@@ -40,7 +40,7 @@ export const word: CharacterEscape = {
4040
* Matches any non-word (alphanumeric or underscore) character.
4141
*/
4242
export const nonWord: CharacterEscape = {
43-
k: 'atom',
43+
k: 0,
4444
p: '\\W',
4545
e: ['\\W'],
4646
};
@@ -49,7 +49,7 @@ export const nonWord: CharacterEscape = {
4949
* Matches any whitespace character (space, tab, newline, etc.).
5050
*/
5151
export const whitespace: CharacterEscape = {
52-
k: 'atom',
52+
k: 0,
5353
p: '\\s',
5454
e: ['\\s'],
5555
};
@@ -58,7 +58,7 @@ export const whitespace: CharacterEscape = {
5858
* Matches any non-whitespace (space, tab, newline, etc.) character.
5959
*/
6060
export const nonWhitespace: CharacterEscape = {
61-
k: 'atom',
61+
k: 0,
6262
p: '\\S',
6363
e: ['\\S'],
6464
};

src/constructs/choice-of.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function choiceOf(...alternatives: RegexSequence[]): EncodedRegex {
1818
}
1919

2020
return {
21-
k: 'disjunction',
21+
k: 2,
2222
p: encodedAlternatives.map((n) => n.p).join('|'),
2323
};
2424
}

src/constructs/lookahead.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
1717
*/
1818
export function lookahead(sequence: RegexSequence): EncodedRegex {
1919
return {
20-
k: 'atom',
20+
k: 0,
2121
p: `(?=${encode(sequence).p})`,
2222
};
2323
}

src/constructs/lookbehind.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
1717
*/
1818
export function lookbehind(sequence: RegexSequence): EncodedRegex {
1919
return {
20-
k: 'atom',
20+
k: 0,
2121
p: `(?<=${encode(sequence).p})`,
2222
};
2323
}

src/constructs/negative-lookahead.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
1717
*/
1818
export function negativeLookahead(sequence: RegexSequence): EncodedRegex {
1919
return {
20-
k: 'atom',
20+
k: 0,
2121
p: `(?!${encode(sequence).p})`,
2222
};
2323
}

src/constructs/negative-lookbehind.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { EncodedRegex, RegexSequence } from '../types';
1717
*/
1818
export function negativeLookbehind(sequence: RegexSequence): EncodedRegex {
1919
return {
20-
k: 'atom',
20+
k: 0,
2121
p: `(?<!${encode(sequence).p})`,
2222
};
2323
}

src/constructs/quantifiers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface QuantifierOptions {
1515
export function zeroOrMore(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
1616
const elements = ensureElements(sequence);
1717
return {
18-
k: 'sequence',
18+
k: 1,
1919
p: `${encodeAtomic(elements)}*${options?.greedy === false ? '?' : ''}`,
2020
};
2121
}
@@ -29,7 +29,7 @@ export function zeroOrMore(sequence: RegexSequence, options?: QuantifierOptions)
2929
export function oneOrMore(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
3030
const elements = ensureElements(sequence);
3131
return {
32-
k: 'sequence',
32+
k: 1,
3333
p: `${encodeAtomic(elements)}+${options?.greedy === false ? '?' : ''}`,
3434
};
3535
}
@@ -43,7 +43,7 @@ export function oneOrMore(sequence: RegexSequence, options?: QuantifierOptions):
4343
export function optional(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
4444
const elements = ensureElements(sequence);
4545
return {
46-
k: 'sequence',
46+
k: 1,
4747
p: `${encodeAtomic(elements)}?${options?.greedy === false ? '?' : ''}`,
4848
};
4949
}

src/constructs/repeat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ export function repeat(sequence: RegexSequence, options: RepeatOptions): Encoded
2222

2323
if (typeof options === 'number') {
2424
return {
25-
k: 'sequence',
25+
k: 1,
2626
p: `${encodeAtomic(elements)}{${options}}`,
2727
};
2828
}
2929

3030
return {
31-
k: 'sequence',
31+
k: 1,
3232
p: `${encodeAtomic(elements)}{${options.min},${options?.max ?? ''}}${
3333
options.greedy === false ? '?' : ''
3434
}`,

src/constructs/unicode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function unicodeChar(codePoint: number): CharacterEscape {
2323
: `\\u{${codePoint.toString(16)}}`; // 1-6 digit hex (requires unicode-aware mode)
2424

2525
return {
26-
k: 'atom',
26+
k: 0,
2727
p: escape,
2828
e: [escape],
2929
};
@@ -50,7 +50,7 @@ export function unicodeProperty(property: string, value?: string): CharacterEsca
5050
const escape = `\\p{${property}${value ? `=${value}` : ''}}`;
5151

5252
return {
53-
k: 'atom',
53+
k: 0,
5454
p: escape,
5555
e: [escape],
5656
};

src/encoder.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ export function encode(sequence: RegexSequence): EncodedRegex {
1010
}
1111

1212
return {
13-
k: 'sequence',
13+
k: 1,
1414
p: encoded
15-
.map((n) => (n.k === 'disjunction' ? encodeAtomic(n) : n.p))
15+
.map((n) => (n.k === 2 ? encodeAtomic(n) : n.p))
1616
.join(''),
1717
};
1818
}
1919

2020
export function encodeAtomic(sequence: RegexSequence): string {
2121
const encoded = encode(sequence);
22-
return encoded.k === 'atom' ? encoded.p : `(?:${encoded.p})`;
22+
return encoded.k === 0 ? encoded.p : `(?:${encoded.p})`;
2323
}
2424

2525
function encodeElement(element: RegexElement): EncodedRegex {
@@ -51,7 +51,7 @@ function encodeText(text: string): EncodedRegex {
5151

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

6262
return {
6363
// Encode at safe precedence
64-
k: isAtomicPattern(p) ? 'atom' : 'disjunction',
64+
k: isAtomicPattern(p) ? 0 : 2,
6565
p,
6666
};
6767
}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface EncodedRegex {
2828
/**
2929
* Precedence of given regex pattern.
3030
*/
31-
export type EncodePrecedence = 'atom' | 'sequence' | 'disjunction';
31+
export type EncodePrecedence = 0 | 1 | 2;
3232

3333
/**
3434
* Regex patter that can be encoded by calling the `encode` method.

0 commit comments

Comments
 (0)