Improve Parsing

This commit is contained in:
TheBrokenRail 2024-01-11 22:09:34 -05:00
parent 00b223a6ad
commit 63ad20456c
2 changed files with 26 additions and 23 deletions

View File

@ -11,14 +11,26 @@ export function readDefinition(name: string) {
}
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);
export function syntaxError(message?: string) {
throw new Error('Syntax Error' + (message ? `: ${message}` : ''));
}
export function parseTypeAndName(piece: string) {
// Split On Last Space
const index = piece.lastIndexOf(' ');
if (index === -1) {
syntaxError('Unable To Find Name/Type Divider');
}
let name = piece.substring(index + 1)!;
let type = piece.substring(0, index)!;
// Move Asterisks From Name To Type
while (name.startsWith('*')) {
name = name.substring(1);
if (!type.endsWith('*')) {
type += ' ';
}
type += '*';
}
// Return
return {type, name};
}
export const MIN_SIZE = 1;

View File

@ -1,4 +1,4 @@
import { COMMENT, EXTENSION, parseTypeAndName, readDefinition } from './common';
import { COMMENT, EXTENSION, parseTypeAndName, readDefinition, syntaxError } from './common';
import { isCppAllowed } from './map';
import { Method } from './method';
import { SimpleProperty, StaticProperty } from './property';
@ -12,9 +12,6 @@ function safeParseInt(str: string) {
}
return x;
}
function syntaxError(message?: string) {
throw new Error('Syntax Error' + (message ? `: ${message}` : ''));
}
export class ErrorOnLine {
readonly error: unknown;
readonly file: string;
@ -28,15 +25,12 @@ export class ErrorOnLine {
// Parse Property
function parseProperty(args: string) {
const parts = args.split(' ');
if (parts.length !== 4) {
const parts = args.split(' = ');
if (parts.length !== 2) {
syntaxError('Invalid Piece Count');
}
if (parts[2] !== '=') {
syntaxError();
}
const {type, name} = parseTypeAndName([parts[0]!, parts[1]!]);
const offset = safeParseInt(parts[3]!);
const {type, name} = parseTypeAndName(parts[0]!);
const offset = safeParseInt(parts[1]!);
return {type, name, offset};
}
@ -46,11 +40,8 @@ function parseMethod(args: string, self: string, insertSelfArg: boolean) {
if (argsStart === -1) {
syntaxError('Cannot Find Arguments');
}
const start = args.substring(0, argsStart).trim().split(' ');
if (start.length !== 2) {
syntaxError('Invalid Piece Count');
}
const {type, name} = parseTypeAndName([start[0]!, start[1]!]);
const start = args.substring(0, argsStart).trim();
const {type, name} = parseTypeAndName(start);
const end = args.substring(argsStart).trim().split(' = ');
if (end.length !== 2) {
syntaxError('Invalid Piece Count');