@gulujs/toml
Version:
TOML parser and serializer
49 lines (48 loc) • 1.76 kB
JavaScript
import { RE_INTEGER } from '../constants.js';
import { PRETTY_ERROR_MESSAGE, SYNTAX_ERROR_MESSAGE } from '../../errors/index.js';
export function getInteger(source, offset, converters) {
RE_INTEGER.lastIndex = offset;
const matches = RE_INTEGER.exec(source.line);
if (!matches || matches.index !== offset) {
return null;
}
let value;
if (matches[7]) {
if (matches[6]) {
throw new SyntaxError(SYNTAX_ERROR_MESSAGE(source, offset));
}
value = convertInteger(10, matches[2], matches[7], converters, source, offset + matches[1].length);
}
else if (matches[3]) {
assertSignEmpty(matches[2], source, offset);
value = convertInteger(16, matches[2], matches[3].toLowerCase(), converters, source, offset + matches[1].length);
}
else if (matches[4]) {
assertSignEmpty(matches[2], source, offset);
value = convertInteger(8, matches[2], matches[4], converters, source, offset + matches[1].length);
}
else if (matches[5]) {
assertSignEmpty(matches[2], source, offset);
value = convertInteger(2, matches[2], matches[5], converters, source, offset + matches[1].length);
}
return {
value,
nextIndex: RE_INTEGER.lastIndex
};
}
function convertInteger(radix, sign, str, converters, source, offset) {
try {
return converters.integer.convertStringToJsValue(str.replace(/_/g, ''), { sign, radix });
}
catch (e) {
if (e instanceof Error) {
e.message = PRETTY_ERROR_MESSAGE(e.message, source, offset);
}
throw e;
}
}
function assertSignEmpty(sign, source, offset) {
if (sign) {
throw new SyntaxError(SYNTAX_ERROR_MESSAGE(source, offset));
}
}