UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

73 lines (54 loc) 1.96 kB
import DataType from "./DataType.js"; import ParserError from "./ParserError.js"; import { readLiteralToken } from "./readLiteralToken.js"; import { skipWhitespace } from "./skipWhitespace.js"; import Token from "./Token.js"; const CHAR_ARRAY_TERMINATOR = ']'; /** * * @param {string} text * @param {number} cursor * @param {number} limit * @return {Token} */ export function readArrayLiteral(text, cursor, limit) { let i = cursor; const firstChar = text.charAt(i); if (firstChar !== '[') { throw new ParserError(cursor, `expected array start: '[', got '${firstChar}' instead`, text); } i = skipWhitespace(text, i + 1, limit); const values = []; // check for empty array const char = text.charAt(i); if (char === CHAR_ARRAY_TERMINATOR) { // empty array i++; } else { while (i < limit) { i = skipWhitespace(text, i, limit); const token = readLiteralToken(text, i, limit); i = token.end; values.push(token); //try to find separator i = skipWhitespace(text, i, limit); if (i < limit) { const char = text.charAt(i); if (char === ',') { //separator i++; } else if (char === CHAR_ARRAY_TERMINATOR) { //end of sequence i++; break; } else { //unexpected input throw new ParserError(i, `Unexpected input '${char}', expected either separator ',' or end of sequence '${CHAR_ARRAY_TERMINATOR}'`, text); } } else { throw new ParserError(i, `Unterminated array literal`, text); } } } return new Token(values, cursor, i, 'array', DataType.Array); }