Skip to content

Commit 8afb579

Browse files
authored
refactor: tokenizer take chars (#1799)
1 parent dfd3663 commit 8afb579

File tree

1 file changed

+57
-42
lines changed

1 file changed

+57
-42
lines changed

packages/message-compiler/src/tokenizer.ts

+57-42
Original file line numberDiff line numberDiff line change
@@ -406,48 +406,57 @@ export function createTokenizer(
406406
return null
407407
}
408408

409+
function isIdentifier(ch: string): boolean {
410+
const cc = ch.charCodeAt(0)
411+
return (
412+
(cc >= 97 && cc <= 122) || // a-z
413+
(cc >= 65 && cc <= 90) || // A-Z
414+
(cc >= 48 && cc <= 57) || // 0-9
415+
cc === 95 || // _
416+
cc === 36 // $
417+
)
418+
}
419+
409420
function takeIdentifierChar(scnr: Scanner): string | undefined | null {
410-
const closure = (ch: string) => {
411-
const cc = ch.charCodeAt(0)
412-
return (
413-
(cc >= 97 && cc <= 122) || // a-z
414-
(cc >= 65 && cc <= 90) || // A-Z
415-
(cc >= 48 && cc <= 57) || // 0-9
416-
cc === 95 || // _
417-
cc === 36 // $
418-
)
419-
}
420-
return takeChar(scnr, closure)
421+
return takeChar(scnr, isIdentifier)
422+
}
423+
424+
function isNamedIdentifier(ch: string): boolean {
425+
const cc = ch.charCodeAt(0)
426+
return (
427+
(cc >= 97 && cc <= 122) || // a-z
428+
(cc >= 65 && cc <= 90) || // A-Z
429+
(cc >= 48 && cc <= 57) || // 0-9
430+
cc === 95 || // _
431+
cc === 36 || // $
432+
cc === 45 // -
433+
)
421434
}
422435

423-
// NOTE: It is assumed that this function is used in conjunction with `takeIdentifierChar` for named.
424-
// TODO: we need to refactor this function ...
425436
function takeNamedIdentifierChar(scnr: Scanner): string | undefined | null {
426-
const closure = (ch: string) => {
427-
const cc = ch.charCodeAt(0)
428-
return cc === 45 // -
429-
}
430-
return takeChar(scnr, closure)
437+
return takeChar(scnr, isNamedIdentifier)
438+
}
439+
440+
function isDigit(ch: string): boolean {
441+
const cc = ch.charCodeAt(0)
442+
return cc >= 48 && cc <= 57 // 0-9
431443
}
432444

433445
function takeDigit(scnr: Scanner): string | undefined | null {
434-
const closure = (ch: string) => {
435-
const cc = ch.charCodeAt(0)
436-
return cc >= 48 && cc <= 57 // 0-9
437-
}
438-
return takeChar(scnr, closure)
446+
return takeChar(scnr, isDigit)
447+
}
448+
449+
function isHexDigit(ch: string): boolean {
450+
const cc = ch.charCodeAt(0)
451+
return (
452+
(cc >= 48 && cc <= 57) || // 0-9
453+
(cc >= 65 && cc <= 70) || // A-F
454+
(cc >= 97 && cc <= 102)
455+
) // a-f
439456
}
440457

441458
function takeHexDigit(scnr: Scanner): string | undefined | null {
442-
const closure = (ch: string) => {
443-
const cc = ch.charCodeAt(0)
444-
return (
445-
(cc >= 48 && cc <= 57) || // 0-9
446-
(cc >= 65 && cc <= 70) || // A-F
447-
(cc >= 97 && cc <= 102)
448-
) // a-f
449-
}
450-
return takeChar(scnr, closure)
459+
return takeChar(scnr, isHexDigit)
451460
}
452461

453462
function getDigits(scnr: Scanner): string {
@@ -513,8 +522,7 @@ export function createTokenizer(
513522

514523
let ch: string | undefined | null = ''
515524
let name = ''
516-
// eslint-disable-next-line no-cond-assign
517-
while ((ch = takeIdentifierChar(scnr) || takeNamedIdentifierChar(scnr))) {
525+
while ((ch = takeNamedIdentifierChar(scnr))) {
518526
name += ch
519527
}
520528

@@ -551,6 +559,10 @@ export function createTokenizer(
551559
return value
552560
}
553561

562+
function isLiteral(ch: string): boolean {
563+
return ch !== LITERAL_DELIMITER && ch !== NEW_LINE
564+
}
565+
554566
function readLiteral(scnr: Scanner): string {
555567
skipSpaces(scnr)
556568

@@ -559,8 +571,7 @@ export function createTokenizer(
559571

560572
let ch: string | undefined | null = ''
561573
let literal = ''
562-
const fn = (x: string) => x !== LITERAL_DELIMITER && x !== NEW_LINE
563-
while ((ch = takeChar(scnr, fn))) {
574+
while ((ch = takeChar(scnr, isLiteral))) {
564575
if (ch === '\\') {
565576
literal += readEscapeSequence(scnr)
566577
} else {
@@ -637,17 +648,21 @@ export function createTokenizer(
637648
return `\\${unicode}${sequence}`
638649
}
639650

651+
function isInvalidIdentifier(ch: string): boolean {
652+
return (
653+
ch !== TokenChars.BraceLeft &&
654+
ch !== TokenChars.BraceRight &&
655+
ch !== SPACE &&
656+
ch !== NEW_LINE
657+
)
658+
}
659+
640660
function readInvalidIdentifier(scnr: Scanner): string {
641661
skipSpaces(scnr)
642662

643663
let ch: string | undefined | null = ''
644664
let identifiers = ''
645-
const closure = (ch: string) =>
646-
ch !== TokenChars.BraceLeft &&
647-
ch !== TokenChars.BraceRight &&
648-
ch !== SPACE &&
649-
ch !== NEW_LINE
650-
while ((ch = takeChar(scnr, closure))) {
665+
while ((ch = takeChar(scnr, isInvalidIdentifier))) {
651666
identifiers += ch
652667
}
653668

0 commit comments

Comments
 (0)