Skip to content

Commit

Permalink
Refactor parser inner-functions to remove the use of the pieces variable
Browse files Browse the repository at this point in the history
Signed-off-by: worksofliam <[email protected]>
  • Loading branch information
worksofliam committed Dec 9, 2024
1 parent eaf0f30 commit e4ff871
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
40 changes: 22 additions & 18 deletions language/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,6 @@ export default class Parser {
let lineNumber = -1;
let lineIndex = 0;

let parts: string[];
let partsLower: string[];
let pieces: string[];

let isFullyFree = lines[0].toUpperCase().startsWith(`**FREE`);
let lineIsFree = false;

Expand All @@ -350,6 +346,16 @@ export default class Parser {
return directIfScope.length === 0 || directIfScope.every(scope => scope.condition);
}

const stripComment = (inputLine: string) => {
const comment = inputLine.indexOf(`//`);
return (comment >= 0 ? inputLine.substring(0, comment).trimEnd() : inputLine);
}

const getValidStatement = (inputLine: string, withSep?: boolean) => {
const sep = inputLine.indexOf(`;`);
return (sep >= 0 ? inputLine.substring(0, sep + (withSep ? 1 : 0)) : inputLine);
}

const expandDs = async (file: string, ds: Declaration): Promise<void> => {
const tags = [`LIKEDS`, `LIKEREC`, `EXTNAME`];
const keywords = ds.keyword;
Expand Down Expand Up @@ -475,7 +481,7 @@ export default class Parser {
} else {
// Even if the line is useless, we need to capture the characters to be
// parsed in case it's a statement spread over multiple lines
if (currentStmtStart && currentStmtStart.content) {
if (!isFullyFree && currentStmtStart && currentStmtStart.content) {
currentStmtStart.content += ``.padEnd(baseLine.length + EOL.length);
}
}
Expand All @@ -487,17 +493,15 @@ export default class Parser {
}

let tokens: Token[] = [];
pieces = [];
parts = [];
let parts: string[];
let partsLower: string[];

if (isFullyFree || lineIsFree) {
// Free format!
if (line.trim() === ``) continue;

pieces = line.split(`;`);

const lineIsComment = line.trim().startsWith(`//`);
tokens = tokenise(pieces[0], lineNumber, lineIndex);
tokens = tokenise(getValidStatement(line), lineNumber, lineIndex);
partsLower = tokens.filter(piece => piece.value).map(piece => piece.value);
parts = partsLower.map(piece => piece.toUpperCase());

Expand Down Expand Up @@ -610,20 +614,19 @@ export default class Parser {
}
}

if (pieces.length > 1 && pieces[1].includes(`//`)) line = pieces[0] + `;`;

if (!currentStmtStart || !currentStmtStart.content) {
currentStmtStart = {line: lineNumber, index: lineIndex};
}

if (!lineIsComment) {
if (line.endsWith(`;`)) {
if (stripComment(line).endsWith(`;`)) {

if (currentStmtStart.content) {
// This means the line is just part of the end of the last statement as well.
line = currentStmtStart.content + baseLine;
line = currentStmtStart.content + getValidStatement(baseLine);

pieces = line.split(`;`);
tokens = tokenise(pieces[0], currentStmtStart.line, currentStmtStart.index);
tokens = tokenise(line, currentStmtStart.line, currentStmtStart.index);
partsLower = tokens.filter(piece => piece.value).map(piece => piece.value);
parts = partsLower.map(piece => piece.toUpperCase());

Expand All @@ -632,10 +635,11 @@ export default class Parser {

} else if (!line.endsWith(`;`)) {
currentStmtStart.content = (currentStmtStart.content || ``) + baseLine;

if (currentStmtStart.content.endsWith(`-`))
currentStmtStart.content = currentStmtStart.content.substring(0, currentStmtStart.content.length - 1) + LINEEND;
else
currentStmtStart.content += LINEEND;
currentStmtStart.content = currentStmtStart.content.substring(0, currentStmtStart.content.length - 1);

currentStmtStart.content += LINEEND;

continue;
}
Expand Down
14 changes: 10 additions & 4 deletions language/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,23 @@ export function tokenise(statement: string, lineNumber = 0, baseIndex?: number)
switch (statement[i]) {
// When it's the string character..
case stringChar:
const possibleEscape = statement[i+1] === stringChar;
if (state === ReadState.IN_STRING) {
currentText += statement[i];
result.push({ value: currentText, type: `string`, range: { start: startsAt, end: startsAt + currentText.length, line: lineNumber } });
currentText = ``;
if (possibleEscape) {
currentText += `''`;
i += 2;
} else {
currentText += statement[i];
result.push({ value: currentText, type: `string`, range: { start: startsAt, end: startsAt + currentText.length, line: lineNumber } });
currentText = ``;
}
} else {
startsAt = i;
currentText += statement[i];
}

// @ts-ignore
state = state === ReadState.IN_STRING ? ReadState.NORMAL : ReadState.IN_STRING;
state = state === ReadState.IN_STRING && !possibleEscape ? ReadState.NORMAL : ReadState.IN_STRING;
break;

// When it's any other character...
Expand Down

0 comments on commit e4ff871

Please sign in to comment.