@atomist/automation-client
Version:
Atomist API for software low-level client
69 lines • 2.65 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const tree_path_1 = require("@atomist/tree-path");
/**
* Allow path expressions against results from a regex with capture groups
*/
class RegexFileParser {
constructor(opts) {
this.opts = opts;
}
get rootName() {
return this.opts.rootName;
}
toAst(f) {
return __awaiter(this, void 0, void 0, function* () {
const content = yield f.getContent();
const matches = [];
let remaining = content;
do {
const m = this.opts.regex.exec(remaining);
if (!!m) {
matches.push(m);
// Rewrite index in outer string
m.index += content.length - remaining.length;
remaining = content.substr(m.index + m[0].length);
}
else {
break;
}
} while (true);
const root = {
$name: this.opts.rootName,
};
root.$children = matches.map(match => new RegexTreeNode(this.opts, match, root));
tree_path_1.defineDynamicProperties(root);
tree_path_1.fillInEmptyNonTerminalValues(root, content);
return root;
});
}
}
exports.RegexFileParser = RegexFileParser;
class RegexTreeNode {
constructor(reo, m, $parent) {
this.$parent = $parent;
this.$name = reo.matchName;
this.$offset = m.index;
this.$value = m[0];
this.$children = !!reo.captureGroupNames ?
reo.captureGroupNames.map(($name, i) => {
const tn = {
$name,
$value: m[i + 1],
};
tn.$offset = m.index + m[0].indexOf(tn.$value);
return tn;
}) :
undefined;
}
}
//# sourceMappingURL=RegexFileParser.js.map