'use strict;';
const funcs = require('./result');
const setProperties = function seProperties(p, readonly) {
const exp = p.thisThis;
const prop = {
writable: readonly,
enumerable: false,
configurable: true,
};
Object.defineProperty(exp, 'input', prop);
Object.defineProperty(exp, 'leftContext', prop);
Object.defineProperty(exp, 'lastMatch', prop);
Object.defineProperty(exp, 'rightContext', prop);
Object.defineProperty(exp, '$_', prop);
Object.defineProperty(exp, '$`', prop);
Object.defineProperty(exp, '$&', prop);
Object.defineProperty(exp, "$'", prop);
prop.enumerable = true;
Object.defineProperty(exp, 'rules', prop);
if (!exp.rules) {
exp.rules = [];
}
for (const name in exp.rules) {
const des = `\${${name}}`;
Object.defineProperty(exp, des, prop);
Object.defineProperty(exp.rules, name, prop);
}
};
const sResult = function sResult(p) {
const ret = {
index: p.result.index,
length: p.result.length,
input: p.charsToString(p.chars, 0),
treeDepth: p.result.treeDepth,
nodeHits: p.result.nodeHits,
rules: [],
toText() {
return funcs.s.resultToText(this);
},
toHtml() {
return funcs.s.resultToHtml(this);
},
toHtmlPage() {
return funcs.s.resultToHtmlPage(this);
},
};
ret[0] = p.charsToString(p.chars, p.result.index, p.result.length);
for (const name in p.result.rules) {
const rule = p.result.rules[name];
if (rule) {
ret.rules[name] = [];
for (let i = 0; i < rule.length; i += 1) {
ret.rules[name][i] = {
index: rule[i].index,
phrase: p.charsToString(p.chars, rule[i].index, rule[i].length),
};
}
} else {
ret.rules[name] = undefined;
}
}
return ret;
};
const uResult = function uResult(p) {
const { chars } = p;
const { result } = p;
let beg;
let end;
const ret = {
index: result.index,
length: result.length,
input: chars.slice(0),
treeDepth: result.treeDepth,
nodeHits: result.nodeHits,
rules: [],
toText(mode) {
return funcs.u.resultToText(this, mode);
},
toHtml(mode) {
return funcs.u.resultToHtml(this, mode);
},
toHtmlPage(mode) {
return funcs.u.resultToHtmlPage(this, mode);
},
};
beg = result.index;
end = beg + result.length;
ret[0] = chars.slice(beg, end);
for (const name in result.rules) {
const rule = result.rules[name];
if (rule) {
ret.rules[name] = [];
for (let i = 0; i < rule.length; i += 1) {
beg = rule[i].index;
end = beg + rule[i].length;
ret.rules[name][i] = {
index: beg,
phrase: chars.slice(beg, end),
};
}
} else {
ret.rules[name] = undefined;
}
}
return ret;
};
const sLastMatch = function sLastMatch(p, result) {
const exp = p.thisThis;
let temp;