symbol-processor/src/common.ts
2024-01-04 17:00:19 -05:00

52 lines
1.5 KiB
TypeScript

import * as fs from 'node:fs';
export const INDENT = ' ';
export const POINTER_SIZE = 4;
export const EXTENSION = '.def';
export const STRUCTURE_FILES: {[id: string]: string} = {};
export function readDefinition(name: string) {
if (!STRUCTURE_FILES[name]) {
throw new Error(`Missing Definition File: ${name}${EXTENSION}`);
}
return fs.readFileSync(STRUCTURE_FILES[name]!, {encoding: 'utf8'});
}
export function parseTypeAndName(parts: string[]) {
let type = parts[0]!;
let name = parts[1]!;
const index = name.lastIndexOf('*');
if (index !== -1) {
type += ' ' + name.substring(0, index + 1);
name = name.substring(index + 1);
}
return {type, name};
}
export const MIN_SIZE = 1;
export function toUpperSnakeCase(str: string) {
let wasUpper = false;
let nextIsUpper = false;
let out = '';
for (let i = 0; i < str.length; i++) {
let character = str.charAt(i);
if (character === '_') {
wasUpper = false;
nextIsUpper = true;
continue;
}
const isUpper = character === character.toUpperCase() || nextIsUpper;
nextIsUpper = false;
character = character.toUpperCase();
if (isUpper && i > 0 && !wasUpper) {
out += '_';
}
out += character;
wasUpper = isUpper;
}
return out;
}
export function formatType(type: string) {
if (!type.endsWith('*')) {
type += ' ';
}
return type;
}
export const COMMENT = '//';