Skip to content

Commit

Permalink
type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dhowe committed Dec 6, 2023
1 parent 822580e commit f3a611e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 40 deletions.
75 changes: 50 additions & 25 deletions src/riscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class RiScript {
console.error('Input: ' + opts.input + '\n', lexResult.errors[0].message);
throw Error('[LEXING] ' + lexResult.errors[0].message);
}
if (opts.trace) this.printTokens(lexResult.tokens);
if (opts.trace) this._printTokens(lexResult.tokens);
opts.tokens = lexResult.tokens;
// return lexResult;
}
Expand All @@ -98,12 +98,13 @@ class RiScript {
return this.visitor.start(opts);
}

lexParseVisit(opts = {}) {
this.lex(opts);
this.parse(opts);
return this.visit(opts);
}

/**
* Evaluates the input script via the RiScript parser
* @param {string} script - the script to evaluate
* @param {object} context - the context to evaluate in
* @param {object} opts - options for the evaluation
* @returns {string}
*/
evaluate(script, context, opts = {}) {
if (typeof script !== 'string') {
throw Error('RiScript.evaluate() expects a string, got ' + typeof script);
Expand All @@ -113,14 +114,50 @@ class RiScript {
return this._evaluate(opts);
}


lexParseVisit(opts = {}) {
this.lex(opts);
this.parse(opts);
return this.visit(opts);
}

/**
* Add a transform function to this instance
* @param {string} name - the name of the transform
* @param {function} def - the transform function
* @returns {RiScript} this instance
*/
addTransform(name, def) {
this.transforms[name] = def;
return this;
}

/**
* Returns the names of all existing transforms
* @returns {string[]} the names of the transforms
*/
getTransforms() {
return Object.keys(this.transforms);
}

/**
* Removes a transform function from this instance
* @param {string} name of transform to remove
*/
removeTransform(name) {
delete this.transforms[name];
}

// //////////////////////// End API ////////////////////////

_evaluate(opts) {
const { input } = opts;

// opts.onepass = true; // TMP

let last, endingBreak = this.regex.EndingBreak.test(input); // keep

let expr = this.preParse(input, opts);
let expr = this._preParse(input, opts);
if (!expr) return '';

if (opts.trace) console.log(`\nInput: '${RiScript._escapeText(input)}'`);
Expand Down Expand Up @@ -158,14 +195,14 @@ class RiScript {
}
}

return this.postParse(expr, opts) + (endingBreak ? '\n' : '');
return this._postParse(expr, opts) + (endingBreak ? '\n' : '');
}

_query(rawQuery, opts) {
return new RiQuery(this, rawQuery, opts);
}

printTokens(tokens) {
_printTokens(tokens) {
let s = tokens.reduce((str, t) => {
let { name } = t.tokenType;
let tag = name;
Expand All @@ -179,7 +216,7 @@ class RiScript {
this.visitor.lookupsToString());
}

preParse(script, opts) {
_preParse(script, opts) {
if (typeof script !== 'string') return '';

const $ = this.Symbols;
Expand Down Expand Up @@ -230,7 +267,7 @@ class RiScript {
return result;
}

postParse(input, opts) {
_postParse(input, opts) {
if (typeof input !== 'string') return '';

// replace html entities
Expand Down Expand Up @@ -303,18 +340,6 @@ class RiScript {
return result;
}

addTransform(name, def) {
return this.transforms[name] = def;
}

getTransforms() {
return Object.keys(this.transforms);
}

removeTransform(name) {
delete this.transforms[name];
}

// ========================= helpers ===============================

_addRegexes(tokens) {
Expand All @@ -323,7 +348,7 @@ class RiScript {
const open = Esc.OPEN_CHOICE;
const close = Esc.CLOSE_CHOICE;
const anysym = Esc.STATIC + Esc.DYNAMIC;

this.regex = {
LineBreaks: /\r?\n/,
EndingBreak: /\r?\n$/,
Expand Down
30 changes: 15 additions & 15 deletions test/riscript.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2427,26 +2427,26 @@ bb', {})).eq('aa bb');

it('#preParseLines', function () {
// handle new weights
expect(riscript.preParse('a (1) ')).eq('a ^1^ ');
expect(riscript.preParse('a (foo) ')).eq('a (foo) ');
expect(riscript._preParse('a (1) ')).eq('a ^1^ ');
expect(riscript._preParse('a (foo) ')).eq('a (foo) ');

expect(riscript.preParse('foo=a')).eq('foo=a');
expect(riscript.preParse('$foo=a')).eq('{$foo=a}');
expect(riscript.preParse('$foo=a\nb')).eq('{$foo=a}b');
expect(riscript.preParse('hello\n$foo=a')).eq('hello\n{$foo=a}');
expect(riscript._preParse('foo=a')).eq('foo=a');
expect(riscript._preParse('$foo=a')).eq('{$foo=a}');
expect(riscript._preParse('$foo=a\nb')).eq('{$foo=a}b');
expect(riscript._preParse('hello\n$foo=a')).eq('hello\n{$foo=a}');

expect(riscript.preParse('$foo=a[b\nc]d\ne')).eq('{$foo=a[b\nc]d}e');
expect(riscript.preParse('$foo=[cat\ndog]\n$foo')).eq('{$foo=[cat\ndog]}$foo');
expect(riscript.preParse('$foo=a\nb\n$foo')).eq('{$foo=a}b\n$foo');
expect(riscript.preParse('$foo=[\n]\n$foo')).eq('{$foo=[\n]}$foo');
expect(riscript.preParse('$foo=a[\n]b\n$foo')).eq('{$foo=a[\n]b}$foo');
expect(riscript.preParse('$foo=[cat\ndog].uc()\n$foo')).eq('{$foo=[cat\ndog].uc()}$foo');
expect(riscript._preParse('$foo=a[b\nc]d\ne')).eq('{$foo=a[b\nc]d}e');
expect(riscript._preParse('$foo=[cat\ndog]\n$foo')).eq('{$foo=[cat\ndog]}$foo');
expect(riscript._preParse('$foo=a\nb\n$foo')).eq('{$foo=a}b\n$foo');
expect(riscript._preParse('$foo=[\n]\n$foo')).eq('{$foo=[\n]}$foo');
expect(riscript._preParse('$foo=a[\n]b\n$foo')).eq('{$foo=a[\n]b}$foo');
expect(riscript._preParse('$foo=[cat\ndog].uc()\n$foo')).eq('{$foo=[cat\ndog].uc()}$foo');

expect(riscript.preParse('[ @{a: {}} hello]\n$a=2')).eq('[ @{a: {}} hello]\n{$a=2}');
expect(riscript._preParse('[ @{a: {}} hello]\n$a=2')).eq('[ @{a: {}} hello]\n{$a=2}');

expect(riscript.preParse('[ @{a: {}} hello]\n$a=2')).eq('[ @{a: {}} hello]\n{$a=2}');
expect(riscript._preParse('[ @{a: {}} hello]\n$a=2')).eq('[ @{a: {}} hello]\n{$a=2}');

let res = riscript.preParse('Some [RiTa](https://rednoise.org/rita?a=b&c=k) code');
let res = riscript._preParse('Some [RiTa](https://rednoise.org/rita?a=b&c=k) code');
let expected = 'Some [RiTa](https://rednoise.org/rita?a=b&c=k) code';
expect(res).eq(expected);
});
Expand Down

0 comments on commit f3a611e

Please sign in to comment.