@amplitude/ampli
Version:
Amplitude CLI
42 lines (41 loc) • 1.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
function parse(codeFactory) {
const segments = [];
let currentState = codeFactory.createState();
let fromIndex = 0;
while (fromIndex < codeFactory.source.length) {
const lastSegment = segments.length > 0 ? segments[segments.length - 1] : undefined;
const startIndex = lastSegment !== undefined ? lastSegment.startIndex + lastSegment.length : 0;
fromIndex = startIndex + currentState.length;
const nextState = currentState.visit(fromIndex);
if (currentState !== nextState) {
if (currentState.length > 0) {
segments.push({
segmentType: currentState.segmentType,
startIndex,
length: currentState.length,
});
}
if (nextState === undefined) {
currentState = codeFactory.createState();
}
else {
currentState = nextState;
}
}
}
const lastSegment = segments.length > 0 ? segments[segments.length - 1] : undefined;
if (currentState.length > 0) {
segments.push({
segmentType: currentState.segmentType,
startIndex: lastSegment !== undefined ? lastSegment.startIndex + lastSegment.length : 0,
length: currentState.length,
});
}
return {
source: codeFactory.source,
segments,
};
}
exports.default = parse;