Skip to content

Commit cecb9a1

Browse files
committed
refactor: rename precedency as type
1 parent 63385ca commit cecb9a1

14 files changed

+34
-36
lines changed

src/constructs/anchors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import type { EncodedRegex } from '../types';
22

33
export const startOfString: EncodedRegex = {
4-
precedence: 'atom',
4+
type: 'atom',
55
pattern: '^',
66
};
77

88
export const endOfString: EncodedRegex = {
9-
precedence: 'atom',
9+
type: 'atom',
1010
pattern: '$',
1111
};
1212

1313
export const wordBoundary: EncodedRegex = {
14-
precedence: 'atom',
14+
type: 'atom',
1515
pattern: '\\b',
1616
};
1717

1818
export const nonWordBoundary: EncodedRegex = {
19-
precedence: 'atom',
19+
type: 'atom',
2020
pattern: '\\B',
2121
};
2222

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-
precedence: 'atom',
24+
type: 'atom',
2525
pattern: `(?<${name}>${encode(sequence).pattern})`,
2626
};
2727
}
2828

2929
return {
30-
precedence: 'atom',
30+
type: 'atom',
3131
pattern: `(${encode(sequence).pattern})`,
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-
precedence: 'atom',
46+
type: 'atom',
4747
pattern: `\\k<${name}>`,
4848
name,
4949
};

src/constructs/char-class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function encodeCharClass(
6969
if (pattern === '[^-]') pattern = '[\\^-]';
7070

7171
return {
72-
precedence: 'atom',
72+
type: 'atom',
7373
pattern,
7474
};
7575
}

src/constructs/char-escape.ts

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

1212
export const digit: CharacterEscape = {
13-
precedence: 'atom',
13+
type: 'atom',
1414
pattern: '\\d',
1515
chars: ['\\d'],
1616
};
1717

1818
export const nonDigit: CharacterEscape = {
19-
precedence: 'atom',
19+
type: 'atom',
2020
pattern: '\\D',
2121
chars: ['\\D'],
2222
};
2323

2424
export const word: CharacterEscape = {
25-
precedence: 'atom',
25+
type: 'atom',
2626
pattern: '\\w',
2727
chars: ['\\w'],
2828
};
2929

3030
export const nonWord: CharacterEscape = {
31-
precedence: 'atom',
31+
type: 'atom',
3232
pattern: '\\W',
3333
chars: ['\\W'],
3434
};
3535

3636
export const whitespace: CharacterEscape = {
37-
precedence: 'atom',
37+
type: 'atom',
3838
pattern: '\\s',
3939
chars: ['\\s'],
4040
};
4141

4242
export const nonWhitespace: CharacterEscape = {
43-
precedence: 'atom',
43+
type: 'atom',
4444
pattern: '\\S',
4545
chars: ['\\S'],
4646
};

src/constructs/choice-of.ts

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

1414
return {
15-
precedence: 'disjunction',
15+
type: 'disjunction',
1616
pattern: encodedAlternatives.map((n) => n.pattern).join('|'),
1717
};
1818
}

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-
precedence: 'atom',
20+
type: 'atom',
2121
pattern: `(?=${encode(sequence).pattern})`,
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-
precedence: 'atom',
20+
type: 'atom',
2121
pattern: `(?<=${encode(sequence).pattern})`,
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-
precedence: 'atom',
20+
type: 'atom',
2121
pattern: `(?!${encode(sequence).pattern})`,
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-
precedence: 'atom',
20+
type: 'atom',
2121
pattern: `(?<!${encode(sequence).pattern})`,
2222
};
2323
}

src/constructs/quantifiers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ export interface QuantifierOptions {
99
export function zeroOrMore(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
1010
const elements = ensureElements(sequence);
1111
return {
12-
precedence: 'sequence',
12+
type: 'sequence',
1313
pattern: `${encodeAtomic(elements)}*${options?.greedy === false ? '?' : ''}`,
1414
};
1515
}
1616

1717
export function oneOrMore(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
1818
const elements = ensureElements(sequence);
1919
return {
20-
precedence: 'sequence',
20+
type: 'sequence',
2121
pattern: `${encodeAtomic(elements)}+${options?.greedy === false ? '?' : ''}`,
2222
};
2323
}
2424

2525
export function optional(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
2626
const elements = ensureElements(sequence);
2727
return {
28-
precedence: 'sequence',
28+
type: 'sequence',
2929
pattern: `${encodeAtomic(elements)}?${options?.greedy === false ? '?' : ''}`,
3030
};
3131
}

src/constructs/repeat.ts

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

1010
if (typeof options === 'number') {
1111
return {
12-
precedence: 'sequence',
12+
type: 'sequence',
1313
pattern: `${encodeAtomic(elements)}{${options}}`,
1414
};
1515
}
1616

1717
return {
18-
precedence: 'sequence',
18+
type: 'sequence',
1919
pattern: `${encodeAtomic(elements)}{${options.min},${options?.max ?? ''}}${
2020
options.greedy === false ? '?' : ''
2121
}`,

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-
precedence: 'atom',
26+
type: 'atom',
2727
pattern: escape,
2828
chars: [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-
precedence: 'atom',
53+
type: 'atom',
5454
pattern: escape,
5555
chars: [escape],
5656
};

src/encoder.ts

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

1212
return {
13-
precedence: 'sequence',
14-
pattern: encoded
15-
.map((n) => (n.precedence === 'disjunction' ? encodeAtomic(n) : n.pattern))
16-
.join(''),
13+
type: 'sequence',
14+
pattern: encoded.map((n) => (n.type === 'disjunction' ? encodeAtomic(n) : n.pattern)).join(''),
1715
};
1816
}
1917

2018
export function encodeAtomic(sequence: RegexSequence): string {
2119
const encoded = encode(sequence);
22-
return encoded.precedence === 'atom' ? encoded.pattern : `(?:${encoded.pattern})`;
20+
return encoded.type === 'atom' ? encoded.pattern : `(?:${encoded.pattern})`;
2321
}
2422

2523
function encodeElement(element: RegexElement): EncodedRegex {
@@ -51,7 +49,7 @@ function encodeText(text: string): EncodedRegex {
5149

5250
return {
5351
// Optimize for single character case
54-
precedence: text.length === 1 ? 'atom' : 'sequence',
52+
type: text.length === 1 ? 'atom' : 'sequence',
5553
pattern: escapeText(text),
5654
};
5755
}
@@ -61,7 +59,7 @@ function encodeRegExp(regexp: RegExp): EncodedRegex {
6159

6260
return {
6361
// Encode at safe precedence
64-
precedence: isAtomicPattern(pattern) ? 'atom' : 'disjunction',
62+
type: isAtomicPattern(pattern) ? 'atom' : 'disjunction',
6563
pattern,
6664
};
6765
}

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ export type RegexConstruct = EncodedRegex | LazyEncodableRegex;
2121
* Encoded regex pattern with information about its type (atom, sequence)
2222
*/
2323
export interface EncodedRegex {
24-
precedence: EncodePrecedence;
24+
type: RegexType;
2525
pattern: string;
2626
}
2727

28-
export type EncodePrecedence = 'atom' | 'sequence' | 'disjunction';
28+
export type RegexType = 'atom' | 'sequence' | 'disjunction';
2929

3030
export interface CharacterEscape extends EncodedRegex {
3131
// `CharacterClass` compatibility

0 commit comments

Comments
 (0)