From 7af5ba2973c3e29933f326ae5da1b919bbcc216a Mon Sep 17 00:00:00 2001 From: Remy Sharp Date: Mon, 18 Mar 2024 11:58:06 +0000 Subject: [PATCH] feat: add support to define the parser version It's pretty loose, and works mostly around IF/ELSE logic. Tokens in 208 parse in 207 (probably a bug on my side) --- cli/index.mjs | 18 +++++++++++++++--- index.mjs | 5 +++++ package.json | 2 +- parser-version.mjs | 8 +++++++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cli/index.mjs b/cli/index.mjs index 1e2ad1e..aa00f1a 100755 --- a/cli/index.mjs +++ b/cli/index.mjs @@ -2,7 +2,7 @@ import { readFileSync, writeFileSync, statSync } from 'fs'; import { dirname, resolve, basename, extname } from 'path'; import * as cli from '../index.mjs'; import pkg from '../package.json' with { type: 'json' }; -import { LATEST } from '../parser-version.mjs'; +import { LATEST, setParser, valid } from '../parser-version.mjs'; const { version } = pkg; @@ -27,7 +27,7 @@ async function main(type) { L: 'inline-load', A: 'autostart', C: 'comments-off', - P: 'parser', // currently not exposed + P: 'parser', }; const bools = [ 'bank', @@ -60,6 +60,18 @@ async function main(type) { return help(type); } + if (options.parser) { + options.parser = options.parser.toUpperCase(); + if (!valid.includes(options.parser)) { + console.error(`Unknown parser: ${options.parser} - valid options: ${valid.join(', ')}`); + process.exit(1); + } + } else { + options.parser = LATEST; + } + + setParser(options.parser); + if (options.input) { // check the file actually exists try { @@ -173,7 +185,7 @@ async function main(type) { : undefined, inlineLoad: options['inline-load'], stripComments: options['comments-off'], - parser: LATEST, + parser: options.parser, bankOutputDir: cwd, }); } diff --git a/index.mjs b/index.mjs index 599000c..3ba02b1 100644 --- a/index.mjs +++ b/index.mjs @@ -149,6 +149,7 @@ export const tokens = ( * @param {boolean} [options.validate=false] * @param {boolean} [options.defines=false] * @param {boolean} [options.bankOutputDir=process.cwd()] directory to save banks to, if this is empty or false, it doesn't write banks when split + * @param {string} [options.parser] parser version to use * @returns {Uint8Array} */ export const file2bas = (src, options = {}) => { @@ -164,6 +165,10 @@ export const file2bas = (src, options = {}) => { ...parseOptions } = options; + if (parseOptions.parser) { + parser.setParser(parseOptions.parser); + } + const bank = parseOptions.bank; let { filename = 'untitled', autostart = 0x8000 } = options; diff --git a/package.json b/package.json index e6c1e7b..474c3cf 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "build": "rm -rf dist; rollup --config && chmod 744 dist/cli/*.js", "dev": "npm run build; rollup --config --watch", "prepare": "npm run build", - "test:dev": "ava __tests__/*.test.js --no-coverage-report --watch", + "test:dev": "ava __tests__/*.test.mjs --no-coverage-report --watch", "test": "nyc --reporter=lcov ava", "semantic-release": "semantic-release" }, diff --git a/parser-version.mjs b/parser-version.mjs index 2324f04..35559d5 100644 --- a/parser-version.mjs +++ b/parser-version.mjs @@ -2,8 +2,9 @@ export const v208 = '208'; export const v207 = '207'; export const FIRST = v207; export const LATEST = v208; +export const LATEST_TEXT = 'LATEST'; -const valid = [v208, v207]; +export const valid = [LATEST_TEXT, v208, v207]; global.parser = LATEST; @@ -13,6 +14,11 @@ global.parser = LATEST; * @param {string} value */ export function setParser(value) { + value = value.toUpperCase(); + if (value === LATEST_TEXT) { + value = LATEST; + } + if (!valid.includes(value)) { throw new Error(`Unknown parser: ${value} - try "${LATEST}"`); }