All files / src/compiler/parser parser.js

100% Statements 26/26
100% Branches 21/21
100% Functions 2/2
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69  13x   13x 4x   9x 9x       22x   22x 8x   8x 7x         2x         27x 27x 27x   27x 1x 26x 10x       7x           3x     16x         5x   5x 1x   4x             11x        
function parseElements(start, end, tokens) {
	const length = end - start;
 
	if (length === 0) {
		return [];
	} else {
		for (
			let elementEnd = start + 1;
			elementEnd <= end;
			elementEnd++
		) {
			const element = parse(start, elementEnd, tokens);
 
			if (element !== null) {
				const elements = parseElements(elementEnd, end, tokens);
 
				if (elements !== null) {
					return [element, ...elements];
				}
			}
		}
 
		return null;
	}
}
 
export function parse(start, end, tokens) {
	const firstToken = tokens[start];
	const lastToken = tokens[end - 1];
	const length = end - start;
 
	if (length === 0) {
		return null;
	} else if (length === 1) {
		if (
			firstToken.type === "tagOpen" &&
			firstToken.closed === true
		) {
			return {
				type: firstToken.value,
				attributes: firstToken.attributes,
				children: []
			};
		} else {
			return null;
		}
	} else {
		if (
			firstToken.type === "tagOpen" &&
			lastToken.type === "tagClose" &&
			firstToken.value === lastToken.value
		) {
			const children = parseElements(start + 1, end - 1, tokens);
 
			if (children === null) {
				return null;
			} else {
				return {
					type: firstToken.value,
					attributes: firstToken.attributes,
					children
				};
			}
		} else {
			return null;
		}
	}
}