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'}); return fs.readFileSync(STRUCTURE_FILES[name]!, {encoding: 'utf8'});
} }
export function parseTypeAndName(parts: string[]) { export function syntaxError(message?: string) {
let type = parts[0]!; throw new Error('Syntax Error' + (message ? `: ${message}` : ''));
let name = parts[1]!; }
const index = name.lastIndexOf('*'); export function parseTypeAndName(piece: string) {
if (index !== -1) { // Split On Last Space
type += ' ' + name.substring(0, index + 1); const index = piece.lastIndexOf(' ');
name = name.substring(index + 1); 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}; return {type, name};
} }
export const MIN_SIZE = 1; 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 { isCppAllowed } from './map';
import { Method } from './method'; import { Method } from './method';
import { SimpleProperty, StaticProperty } from './property'; import { SimpleProperty, StaticProperty } from './property';
@ -12,9 +12,6 @@ function safeParseInt(str: string) {
} }
return x; return x;
} }
function syntaxError(message?: string) {
throw new Error('Syntax Error' + (message ? `: ${message}` : ''));
}
export class ErrorOnLine { export class ErrorOnLine {
readonly error: unknown; readonly error: unknown;
readonly file: string; readonly file: string;
@ -28,15 +25,12 @@ export class ErrorOnLine {
// Parse Property // Parse Property
function parseProperty(args: string) { function parseProperty(args: string) {
const parts = args.split(' '); const parts = args.split(' = ');
if (parts.length !== 4) { if (parts.length !== 2) {
syntaxError('Invalid Piece Count'); syntaxError('Invalid Piece Count');
} }
if (parts[2] !== '=') { const {type, name} = parseTypeAndName(parts[0]!);
syntaxError(); const offset = safeParseInt(parts[1]!);
}
const {type, name} = parseTypeAndName([parts[0]!, parts[1]!]);
const offset = safeParseInt(parts[3]!);
return {type, name, offset}; return {type, name, offset};
} }
@ -46,11 +40,8 @@ function parseMethod(args: string, self: string, insertSelfArg: boolean) {
if (argsStart === -1) { if (argsStart === -1) {
syntaxError('Cannot Find Arguments'); syntaxError('Cannot Find Arguments');
} }
const start = args.substring(0, argsStart).trim().split(' '); const start = args.substring(0, argsStart).trim();
if (start.length !== 2) { const {type, name} = parseTypeAndName(start);
syntaxError('Invalid Piece Count');
}
const {type, name} = parseTypeAndName([start[0]!, start[1]!]);
const end = args.substring(argsStart).trim().split(' = '); const end = args.substring(argsStart).trim().split(' = ');
if (end.length !== 2) { if (end.length !== 2) {
syntaxError('Invalid Piece Count'); syntaxError('Invalid Piece Count');