Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 18 additions & 30 deletions packages/idea-parser/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,37 +305,25 @@ export function reader(
code: string,
index: number
): UnknownToken | undefined {
let value = '';
let matched = false;
const start = index;
while (index < code.length) {
//get the character (and increment index afterwards)
const char = code.charAt(index++);
if (!regexp.test(value + char)) {
//if we never had a match
if (!matched) {
//add character to value anyways
value += char;
//let it keep parsing
continue;
}
//if we do not have a value
if (value.length === 0) {
return undefined;
}
//return where we ended
return { type, start, end: index - 1, value, raw: value };
}
//add character to value
value += char;
//remember last match
matched = true;
//slice the code from the index so it only works with a substring
const slice = code.slice(index);

//ensures regex matches from the beginning
const anchored = new RegExp(regexp.source, regexp.flags.replace('g', ''));

const match = anchored.exec(slice);
if (!match || match.index !== 0) {
return undefined;
}
//no more code...
//did it end with a match?
return matched && value.length
? { type, start, end: index, value, raw: value }
: undefined;

const value = match[0];
return {
type,
start: index,
end: index + value.length,
value,
raw: value
};
}

export function identifier(
Expand Down