UNPKG

@atomist/automation-client

Version:

Atomist API for software low-level client

73 lines 3.1 kB
"use strict"; 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 PatternMatch_1 = require("@atomist/microgrammar/lib/PatternMatch"); const tree_path_1 = require("@atomist/tree-path"); /** * Allow path expressions against results from a single microgrammar */ class MicrogrammarBasedFileParser { /** * Create a new MicrogrammarBasedFileParser, around a single microgrammar * @param {string} rootName name of the root element * @param {string} matchName name of each match * @param {Grammar<any>} grammar */ constructor(rootName, matchName, grammar) { this.rootName = rootName; this.matchName = matchName; this.grammar = grammar; } toAst(f) { return __awaiter(this, void 0, void 0, function* () { const content = yield f.getContent(); const matches = this.grammar.findMatches(content); const root = { $name: this.rootName, $children: matches.map(m => new MicrogrammarBackedTreeNode(this.matchName, m, undefined)), }; tree_path_1.defineDynamicProperties(root); tree_path_1.fillInEmptyNonTerminalValues(root, content); return root; }); } } exports.MicrogrammarBasedFileParser = MicrogrammarBasedFileParser; /** * TreeNode implementation backed by a microgrammar match */ class MicrogrammarBackedTreeNode { constructor($name, m, $parent) { this.$name = $name; this.$parent = $parent; this.$offset = m.$offset; // Copy properties from the match Object.getOwnPropertyNames(m) .filter(prop => !prop.startsWith("$")) .forEach(prop => { this[prop] = m[prop]; }); if (PatternMatch_1.isTreePatternMatch(m)) { const subs = m.submatches(); this.$children = Object.getOwnPropertyNames(subs) .map(prop => { const sub = subs[prop]; // console.log("Exposing child %s.%s as [%s]", $name, prop, stringify(sub)); return new MicrogrammarBackedTreeNode(prop, sub, this); }); } else { // console.log("Exposing terminal %s as [%s]: value=[%s]", $name, stringify(m), m.$matched); this.$value = String(m.$value); } } } //# sourceMappingURL=MicrogrammarBasedFileParser.js.map