UNPKG

parseit.js

Version:

Customizable parsing library for converting a list of tokens into an AST tree

43 lines 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.tokenShouldBeIgnored = exports.adaptNodeData = exports.tokenMatches = exports.isToken = exports.isRule = void 0; const rule_1 = require("../grammar/rule"); function isRule(item) { return item instanceof rule_1.GrammarAtom && item.type == rule_1.GrammarAtomType.RULE; } exports.isRule = isRule; function isToken(item) { return item instanceof rule_1.GrammarAtom && item.type == rule_1.GrammarAtomType.TOKEN; } exports.isToken = isToken; function tokenMatches(token, item) { if (!token) return false; const matchValue = item.value !== undefined ? token.value == item.value : true; return token && item.type == rule_1.GrammarAtomType.TOKEN && token.type == item.name && matchValue; } exports.tokenMatches = tokenMatches; function adaptNodeData(data, rule) { if (rule.match == rule_1.GrammarRuleSpecialMatch.PASS) { return data[0]; } else if (rule.match == rule_1.GrammarRuleSpecialMatch.FUNC) { return rule.func(data); } else { const targetNode = rule.match; return new targetNode(...data); } } exports.adaptNodeData = adaptNodeData; function tokenShouldBeIgnored(token, grammar, rule) { if (!token) return; const atom = grammar.getIgnored(token, rule); return atom && tokenMatches(token, atom); } exports.tokenShouldBeIgnored = tokenShouldBeIgnored; //# sourceMappingURL=util.js.map