From 2165de7e8703a7ec7ab84380960a0cd1c335c29d Mon Sep 17 00:00:00 2001 From: Nadav Ivgi Date: Wed, 9 Feb 2022 03:16:12 +0200 Subject: [PATCH 1/3] Support multiple ops in a single line (#19) Using a new Regex-based parser. --- src/components/ScriptEditor/ScriptEditor.tsx | 64 +++++++++----------- src/helper/index.ts | 1 - 2 files changed, 27 insertions(+), 38 deletions(-) diff --git a/src/components/ScriptEditor/ScriptEditor.tsx b/src/components/ScriptEditor/ScriptEditor.tsx index 1c9b99d..497b622 100644 --- a/src/components/ScriptEditor/ScriptEditor.tsx +++ b/src/components/ScriptEditor/ScriptEditor.tsx @@ -51,45 +51,35 @@ const ScriptEditor: React.FC = ({ scriptWiz }) => { const parseInput = useCallback( (inputText: string) => { + // 1: hex, 2: bin, 3/4: quoted text, 5: number, 6: opcode, 7: label + const reWord = /^\s*(?:<(?:0x([a-fA-F0-9]+)|0b([01]+)|"([^"]+)"|'([^']+)'|(\d+))>|(OP_\w+)|(\$\w+))(?:\s+|$)/; + + let lineRemain = inputText.trim(); + while (lineRemain.length) { + const matches = lineRemain.match(reWord); + if (!matches) { + console.error('UI: Invalid input value:', lineRemain); + // TODO user-visible error message + return; + } - // Look for $label assignments, keep them for later processing and strip them from the line string. - const labelMatches = inputText.match(/\$\w+$/) - if (labelMatches) { - inputText = inputText.replace(/\s*\$\w+$/, '') - } - - if (inputText.startsWith('<') && inputText.endsWith('>')) { - const inputTextValue = inputText.substring(1, inputText.length - 1); - - if (inputTextValue.startsWith('0x')) { - scriptWiz.parseHex(inputTextValue.substring(2)); - } else if (inputTextValue.startsWith('0b')) { - scriptWiz.parseBin(inputTextValue.substring(2)); - } else if ( - (inputTextValue.startsWith('"') && inputTextValue.endsWith('"')) || - (inputTextValue.startsWith("'") && inputTextValue.endsWith("'")) - ) { - const inputTextValueString = inputTextValue.substring(1, inputTextValue.length - 1); - scriptWiz.parseText(inputTextValueString); - } else if (!isNaN(Number(inputTextValue))) { - scriptWiz.parseNumber(Number(inputTextValue)); - } else if (inputTextValue.startsWith('OP_')) { - const opwordToOphex = scriptWiz.opCodes.wordHex(inputTextValue); - scriptWiz.parseHex(opwordToOphex.substring(2)); - } else { - console.error('UI: Invalid input value!!!'); + if (matches[1]) { + scriptWiz.parseHex(matches[1]); + } else if (matches[2]) { + scriptWiz.parseBin(matches[2]); + } else if (matches[3] || matches[4]) { + scriptWiz.parseText(matches[3] || matches[4]); + } else if (matches[5]) { + scriptWiz.parseNumber(+matches[5]); + } else if (matches[6]) { + scriptWiz.parseOpcode(matches[6]); + } else if (matches[7]) { + if (!scriptWiz.stackDataList.main.length) throw new Error('nothing to label'); + const lastStack = scriptWiz.stackDataList.main[scriptWiz.stackDataList.main.length-1]; + lastStack.label = matches[7]; } - } else if (inputText.startsWith('OP_')) { - scriptWiz.parseOpcode(inputText); - } else if (inputText !== '') { - console.error('UI: Invalid input value!!!'); - } - // Assign the label to the last element on the stack - if (labelMatches) { - if (!scriptWiz.stackDataList.main.length) throw new Error('nothing to label'); - const lastStack = scriptWiz.stackDataList.main[scriptWiz.stackDataList.main.length-1]; - lastStack.label = labelMatches[0]; + lineRemain = lineRemain.slice(matches[0].length); } }, [scriptWiz], @@ -111,7 +101,7 @@ const ScriptEditor: React.FC = ({ scriptWiz }) => { if (lines) { for (let i = 0; i < lines.length; i++) { - const line = lines[i]; + const line = lines[i].trim(); if (line !== '') { parseInput(line); diff --git a/src/helper/index.ts b/src/helper/index.ts index 5c39d4d..7fba3ad 100644 --- a/src/helper/index.ts +++ b/src/helper/index.ts @@ -1,6 +1,5 @@ const convertEditorLines = (formattedLines: string): string[] => { let lines = formattedLines.split('\n'); - lines = lines.map((line) => line.replace(/ /g, '')); lines = lines.map((line) => line.replaceAll('\r', '')); From ded381486a9558f01b26db1c91924b2377c37fe3 Mon Sep 17 00:00:00 2001 From: Nadav Ivgi Date: Thu, 10 Feb 2022 00:02:17 +0200 Subject: [PATCH 2/3] Use new assignLabel() method To fix an issue where the last stack element was labeled even if the `$