@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
44 lines (38 loc) • 1.48 kB
JavaScript
import { assert } from "../../assert.js";
import DataType from "./DataType.js";
import ParserError from "./ParserError.js";
import Token from "./Token.js";
import TokenType from "./TokenType.js";
/**
*
* @param {string} text
* @param {number} cursor
* @param {number} length
* @returns {Token}
*/
export function readBooleanToken(text, cursor, length) {
assert.isString(text, 'text');
assert.isNonNegativeInteger(cursor, 'cursor');
assert.isNonNegativeInteger(length, 'length');
const firstChar = text.charAt(cursor);
let value;
let end = cursor;
if (firstChar === 't' && length - cursor >= 4) {
if (text.substring(cursor + 1, cursor + 4) === 'rue') {
value = true;
end = cursor + 4;
} else {
throw new ParserError(cursor, `expected 'true', instead got '${text.substring(cursor, cursor + 4)}'`, text);
}
} else if (firstChar === 'f' && length - cursor >= 5) {
if (text.substring(cursor + 1, cursor + 5) === 'alse') {
value = false;
end = cursor + 5;
} else {
throw new ParserError(cursor, `expected 'false', instead got '${text.substring(cursor, cursor + 5)}'`, text);
}
} else {
throw new ParserError(cursor, `expected 't' or 'f', instead got '${firstChar}'`, text);
}
return new Token(value, cursor, end, TokenType.LiteralBoolean, DataType.Boolean);
}