symbol-processor/src/common.ts

49 lines
1.4 KiB
TypeScript

import * as fs from 'node:fs';
export const INDENT = ' ';
export const POINTER_SIZE = 4;
export const EXTENSION = '.def';
export function readDefinition(name: string) {
const file = `${__dirname}/../symbols/${name}${EXTENSION}`;
return fs.readFileSync(file, {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 = '//';