parse diagnosis
MODIFY
packages/@romejs/codec-js-regexp/index.ts
packages/@romejs/codec-js-regexp/index.ts
@@ -213,8 +213,6 @@ export const createRegExpParser = createParser(
let namedBackReferenceIndex = get0(index) + 2;
let namedBackRefenenceChar = input[namedBackReferenceIndex];
if (namedBackRefenenceChar === '<') {
- // skip the opening <
- namedBackReferenceIndex++;
namedBackRefenenceChar = input[namedBackReferenceIndex];
while (namedBackRefenenceChar !== '>' &&
namedBackReferenceIndex < input.length) {
@@ -223,9 +221,10 @@ export const createRegExpParser = createParser(
namedBackRefenenceChar = input[namedBackReferenceIndex];
}
if (namedBackRefenenceChar === '>') {
- // skip the closing >
+ namedBackReference += namedBackRefenenceChar;
namedBackReferenceIndex++;
}
+ console.log('name: ', namedBackReference);
return this.finishComplexToken('NamedBackReferenceCharacter', {
value: namedBackReference,
escaped: true,
@@ -617,11 +616,27 @@ export const createRegExpParser = createParser(
}
if (token.type === 'NamedBackReferenceCharacter') {
+ const start = this.input.slice(0, get0(token.start));
this.nextToken();
+ if (token.value[token.value.length - 1] != '>') {
+ this.addDiagnostic({
+ description: descriptions.REGEX_PARSER.UNCLOSED_NAMED_CAPTURE,
+ loc: this.finishLocFromToken(token),
+ });
+ }
+
+ if (!start.includes(token.value)) {
+ this.addDiagnostic({
+ description: descriptions.REGEX_PARSER.INVALID_NAMED_CAPTURE,
+ loc: this.finishLocFromToken(token),
+ });
+ }
+
+ const name = token.value.slice(1, token.value.length - 1);
return {
type: 'RegExpNamedBackReference',
- name: token.value,
+ name,
loc: this.finishLocFromToken(token),
};
}

MODIFY
packages/@romejs/diagnostics/descriptions.ts
packages/@romejs/diagnostics/descriptions.ts
@@ -158,6 +158,8 @@ export const descriptions = createMessages(
DUPLICATE_FLAG: 'Duplicate regular expression flag',
INVALID_FLAG: 'Invalid regular expression flag',
NO_TARGET_QUANTIFIER: 'Nothing to repeat',
+ INVALID_NAMED_CAPTURE: 'Invalid named capture referenced',
+ UNCLOSED_NAMED_CAPTURE: 'Unclosed named capture',
},
// @romejs/codec-json

MODIFY
packages/@romejs/js-parser/parser.ts
packages/@romejs/js-parser/parser.ts
@@ -26,6 +26,7 @@ import {
DiagnosticDescription,
descriptions,
DiagnosticsProcessor,
+ DiagnosticLocation,
} from '@romejs/diagnostics';
import ParserBranchFinder from './ParserBranchFinder';
import {Token, nextToken} from './tokenizer/index';
@@ -340,6 +341,7 @@ const createJSParser = createParser(
end?: Position;
loc?: SourceLocation;
index?: Number0;
+ location?: DiagnosticLocation,
}): void {
if (this.isLookahead) {
return;
@@ -366,7 +368,12 @@ const createJSParser = createParser(
end = start;
}
- if (opts.loc !== undefined) {
+ if (opts.location !== undefined) {
+ start = opts.location.start;
+ end = opts.location.end;
+ }
+
+ if ((start === undefined && end === undefined) && opts.loc !== undefined) {
start = opts.loc.start;
end = opts.loc.end;
}

parse diagnosis