semantic_pred.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isType = isType;
  4. exports.isRole = isRole;
  5. exports.isAccent = isAccent;
  6. exports.isSimpleFunctionScope = isSimpleFunctionScope;
  7. exports.isPrefixFunctionBoundary = isPrefixFunctionBoundary;
  8. exports.isBigOpBoundary = isBigOpBoundary;
  9. exports.isIntegralDxBoundary = isIntegralDxBoundary;
  10. exports.isIntegralDxBoundarySingle = isIntegralDxBoundarySingle;
  11. exports.isGeneralFunctionBoundary = isGeneralFunctionBoundary;
  12. exports.isEmbellished = isEmbellished;
  13. exports.isOperator = isOperator;
  14. exports.isRelation = isRelation;
  15. exports.isPunctuation = isPunctuation;
  16. exports.isFence = isFence;
  17. exports.isElligibleEmbellishedFence = isElligibleEmbellishedFence;
  18. exports.isTableOrMultiline = isTableOrMultiline;
  19. exports.tableIsMatrixOrVector = tableIsMatrixOrVector;
  20. exports.isFencedElement = isFencedElement;
  21. exports.tableIsCases = tableIsCases;
  22. exports.tableIsMultiline = tableIsMultiline;
  23. exports.lineIsLabelled = lineIsLabelled;
  24. exports.isBinomial = isBinomial;
  25. exports.isLimitBase = isLimitBase;
  26. exports.isSimpleFunctionHead = isSimpleFunctionHead;
  27. exports.singlePunctAtPosition = singlePunctAtPosition;
  28. exports.isSimpleFunction = isSimpleFunction;
  29. exports.isSetNode = isSetNode;
  30. exports.isSingletonSetContent = isSingletonSetContent;
  31. exports.isUnitCounter = isUnitCounter;
  32. exports.isPureUnit = isPureUnit;
  33. exports.isUnitProduct = isUnitProduct;
  34. exports.isImplicit = isImplicit;
  35. exports.isImplicitOp = isImplicitOp;
  36. exports.isNeutralFence = isNeutralFence;
  37. exports.compareNeutralFences = compareNeutralFences;
  38. exports.elligibleLeftNeutral = elligibleLeftNeutral;
  39. exports.elligibleRightNeutral = elligibleRightNeutral;
  40. exports.isMembership = isMembership;
  41. const semantic_attr_js_1 = require("./semantic_attr.js");
  42. const semantic_meaning_js_1 = require("./semantic_meaning.js");
  43. const semantic_util_js_1 = require("./semantic_util.js");
  44. function isType(node, attr) {
  45. return node.type === attr;
  46. }
  47. function embellishedType(node, attr) {
  48. return node.embellished === attr;
  49. }
  50. function isRole(node, attr) {
  51. return node.role === attr;
  52. }
  53. function isAccent(node) {
  54. return (isType(node, semantic_meaning_js_1.SemanticType.FENCE) ||
  55. isType(node, semantic_meaning_js_1.SemanticType.PUNCTUATION) ||
  56. isType(node, semantic_meaning_js_1.SemanticType.OPERATOR) ||
  57. isType(node, semantic_meaning_js_1.SemanticType.RELATION));
  58. }
  59. function isSimpleFunctionScope(node) {
  60. const children = node.childNodes;
  61. if (children.length === 0) {
  62. return true;
  63. }
  64. if (children.length > 1) {
  65. return false;
  66. }
  67. const child = children[0];
  68. if (child.type === semantic_meaning_js_1.SemanticType.INFIXOP) {
  69. if (child.role !== semantic_meaning_js_1.SemanticRole.IMPLICIT) {
  70. return false;
  71. }
  72. if (child.childNodes.some((x) => isType(x, semantic_meaning_js_1.SemanticType.INFIXOP))) {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. function isPrefixFunctionBoundary(node) {
  79. return ((isOperator(node) && !isRole(node, semantic_meaning_js_1.SemanticRole.DIVISION)) ||
  80. isType(node, semantic_meaning_js_1.SemanticType.APPL) ||
  81. isGeneralFunctionBoundary(node));
  82. }
  83. function isBigOpBoundary(node) {
  84. return isOperator(node) || isGeneralFunctionBoundary(node);
  85. }
  86. function isIntegralDxBoundary(firstNode, secondNode) {
  87. return (!!secondNode &&
  88. isType(secondNode, semantic_meaning_js_1.SemanticType.IDENTIFIER) &&
  89. semantic_attr_js_1.SemanticMap.Secondary.has(firstNode.textContent, semantic_meaning_js_1.SemanticSecondary.D));
  90. }
  91. function isIntegralDxBoundarySingle(node) {
  92. if (isType(node, semantic_meaning_js_1.SemanticType.IDENTIFIER)) {
  93. const firstChar = node.textContent[0];
  94. return (firstChar &&
  95. node.textContent[1] &&
  96. semantic_attr_js_1.SemanticMap.Secondary.has(firstChar, semantic_meaning_js_1.SemanticSecondary.D));
  97. }
  98. return false;
  99. }
  100. function isGeneralFunctionBoundary(node) {
  101. return isRelation(node) || isPunctuation(node);
  102. }
  103. function isEmbellished(node) {
  104. if (node.embellished) {
  105. return node.embellished;
  106. }
  107. if (isEmbellishedType(node.type)) {
  108. return node.type;
  109. }
  110. return null;
  111. }
  112. function isEmbellishedType(type) {
  113. return (type === semantic_meaning_js_1.SemanticType.OPERATOR ||
  114. type === semantic_meaning_js_1.SemanticType.RELATION ||
  115. type === semantic_meaning_js_1.SemanticType.FENCE ||
  116. type === semantic_meaning_js_1.SemanticType.PUNCTUATION);
  117. }
  118. function isOperator(node) {
  119. return (isType(node, semantic_meaning_js_1.SemanticType.OPERATOR) ||
  120. embellishedType(node, semantic_meaning_js_1.SemanticType.OPERATOR));
  121. }
  122. function isRelation(node) {
  123. return (isType(node, semantic_meaning_js_1.SemanticType.RELATION) ||
  124. embellishedType(node, semantic_meaning_js_1.SemanticType.RELATION));
  125. }
  126. function isPunctuation(node) {
  127. return (isType(node, semantic_meaning_js_1.SemanticType.PUNCTUATION) ||
  128. embellishedType(node, semantic_meaning_js_1.SemanticType.PUNCTUATION));
  129. }
  130. function isFence(node) {
  131. return (isType(node, semantic_meaning_js_1.SemanticType.FENCE) ||
  132. embellishedType(node, semantic_meaning_js_1.SemanticType.FENCE));
  133. }
  134. function isElligibleEmbellishedFence(node) {
  135. if (!node || !isFence(node)) {
  136. return false;
  137. }
  138. if (!node.embellished) {
  139. return true;
  140. }
  141. return recurseBaseNode(node);
  142. }
  143. function bothSide(node) {
  144. return (isType(node, semantic_meaning_js_1.SemanticType.TENSOR) &&
  145. (!isType(node.childNodes[1], semantic_meaning_js_1.SemanticType.EMPTY) ||
  146. !isType(node.childNodes[2], semantic_meaning_js_1.SemanticType.EMPTY)) &&
  147. (!isType(node.childNodes[3], semantic_meaning_js_1.SemanticType.EMPTY) ||
  148. !isType(node.childNodes[4], semantic_meaning_js_1.SemanticType.EMPTY)));
  149. }
  150. function recurseBaseNode(node) {
  151. if (!node.embellished) {
  152. return true;
  153. }
  154. if (bothSide(node)) {
  155. return false;
  156. }
  157. if (isRole(node, semantic_meaning_js_1.SemanticRole.CLOSE) && isType(node, semantic_meaning_js_1.SemanticType.TENSOR)) {
  158. return false;
  159. }
  160. if (isRole(node, semantic_meaning_js_1.SemanticRole.OPEN) &&
  161. (isType(node, semantic_meaning_js_1.SemanticType.SUBSCRIPT) ||
  162. isType(node, semantic_meaning_js_1.SemanticType.SUPERSCRIPT))) {
  163. return false;
  164. }
  165. return recurseBaseNode(node.childNodes[0]);
  166. }
  167. function isTableOrMultiline(node) {
  168. return (!!node &&
  169. (isType(node, semantic_meaning_js_1.SemanticType.TABLE) || isType(node, semantic_meaning_js_1.SemanticType.MULTILINE)));
  170. }
  171. function tableIsMatrixOrVector(node) {
  172. return (!!node && isFencedElement(node) && isTableOrMultiline(node.childNodes[0]));
  173. }
  174. function isFencedElement(node) {
  175. return (!!node &&
  176. isType(node, semantic_meaning_js_1.SemanticType.FENCED) &&
  177. (isRole(node, semantic_meaning_js_1.SemanticRole.LEFTRIGHT) || isNeutralFence(node)) &&
  178. node.childNodes.length === 1);
  179. }
  180. function tableIsCases(_table, prevNodes) {
  181. return (prevNodes.length > 0 &&
  182. isRole(prevNodes[prevNodes.length - 1], semantic_meaning_js_1.SemanticRole.OPENFENCE));
  183. }
  184. function tableIsMultiline(table) {
  185. return table.childNodes.every(function (row) {
  186. const length = row.childNodes.length;
  187. return length <= 1;
  188. });
  189. }
  190. function lineIsLabelled(line) {
  191. return (isType(line, semantic_meaning_js_1.SemanticType.LINE) &&
  192. line.contentNodes.length &&
  193. isRole(line.contentNodes[0], semantic_meaning_js_1.SemanticRole.LABEL));
  194. }
  195. function isBinomial(table) {
  196. return table.childNodes.length === 2;
  197. }
  198. function isLimitBase(node) {
  199. return (isType(node, semantic_meaning_js_1.SemanticType.LARGEOP) ||
  200. isType(node, semantic_meaning_js_1.SemanticType.LIMBOTH) ||
  201. isType(node, semantic_meaning_js_1.SemanticType.LIMLOWER) ||
  202. isType(node, semantic_meaning_js_1.SemanticType.LIMUPPER) ||
  203. (isType(node, semantic_meaning_js_1.SemanticType.FUNCTION) &&
  204. isRole(node, semantic_meaning_js_1.SemanticRole.LIMFUNC)) ||
  205. ((isType(node, semantic_meaning_js_1.SemanticType.OVERSCORE) ||
  206. isType(node, semantic_meaning_js_1.SemanticType.UNDERSCORE)) &&
  207. isLimitBase(node.childNodes[0])));
  208. }
  209. function isSimpleFunctionHead(node) {
  210. return (node.type === semantic_meaning_js_1.SemanticType.IDENTIFIER ||
  211. node.role === semantic_meaning_js_1.SemanticRole.LATINLETTER ||
  212. node.role === semantic_meaning_js_1.SemanticRole.GREEKLETTER ||
  213. node.role === semantic_meaning_js_1.SemanticRole.OTHERLETTER);
  214. }
  215. function singlePunctAtPosition(nodes, puncts, position) {
  216. return (puncts.length === 1 &&
  217. (nodes[position].type === semantic_meaning_js_1.SemanticType.PUNCTUATION ||
  218. nodes[position].embellished === semantic_meaning_js_1.SemanticType.PUNCTUATION) &&
  219. nodes[position] === puncts[0]);
  220. }
  221. function isSimpleFunction(node) {
  222. return (isType(node, semantic_meaning_js_1.SemanticType.IDENTIFIER) &&
  223. isRole(node, semantic_meaning_js_1.SemanticRole.SIMPLEFUNC));
  224. }
  225. function isLeftBrace(node) {
  226. const leftBrace = ['{', '﹛', '{'];
  227. return !!node && leftBrace.indexOf(node.textContent) !== -1;
  228. }
  229. function isRightBrace(node) {
  230. const rightBrace = ['}', '﹜', '}'];
  231. return !!node && rightBrace.indexOf(node.textContent) !== -1;
  232. }
  233. function isSetNode(node) {
  234. return (isLeftBrace(node.contentNodes[0]) && isRightBrace(node.contentNodes[1]));
  235. }
  236. const illegalSingleton = [
  237. semantic_meaning_js_1.SemanticType.PUNCTUATION,
  238. semantic_meaning_js_1.SemanticType.PUNCTUATED,
  239. semantic_meaning_js_1.SemanticType.RELSEQ,
  240. semantic_meaning_js_1.SemanticType.MULTIREL,
  241. semantic_meaning_js_1.SemanticType.TABLE,
  242. semantic_meaning_js_1.SemanticType.MULTILINE,
  243. semantic_meaning_js_1.SemanticType.CASES,
  244. semantic_meaning_js_1.SemanticType.INFERENCE
  245. ];
  246. const scriptedElement = [
  247. semantic_meaning_js_1.SemanticType.LIMUPPER,
  248. semantic_meaning_js_1.SemanticType.LIMLOWER,
  249. semantic_meaning_js_1.SemanticType.LIMBOTH,
  250. semantic_meaning_js_1.SemanticType.SUBSCRIPT,
  251. semantic_meaning_js_1.SemanticType.SUPERSCRIPT,
  252. semantic_meaning_js_1.SemanticType.UNDERSCORE,
  253. semantic_meaning_js_1.SemanticType.OVERSCORE,
  254. semantic_meaning_js_1.SemanticType.TENSOR
  255. ];
  256. function isSingletonSetContent(node) {
  257. const type = node.type;
  258. if (illegalSingleton.indexOf(type) !== -1 ||
  259. (type === semantic_meaning_js_1.SemanticType.INFIXOP && node.role !== semantic_meaning_js_1.SemanticRole.IMPLICIT)) {
  260. return false;
  261. }
  262. if (type === semantic_meaning_js_1.SemanticType.FENCED) {
  263. return node.role === semantic_meaning_js_1.SemanticRole.LEFTRIGHT
  264. ? isSingletonSetContent(node.childNodes[0])
  265. : true;
  266. }
  267. if (scriptedElement.indexOf(type) !== -1) {
  268. return isSingletonSetContent(node.childNodes[0]);
  269. }
  270. return true;
  271. }
  272. function isNumber(node) {
  273. return (node.type === semantic_meaning_js_1.SemanticType.NUMBER &&
  274. (node.role === semantic_meaning_js_1.SemanticRole.INTEGER || node.role === semantic_meaning_js_1.SemanticRole.FLOAT));
  275. }
  276. function isUnitCounter(node) {
  277. return (isNumber(node) ||
  278. node.role === semantic_meaning_js_1.SemanticRole.VULGAR ||
  279. node.role === semantic_meaning_js_1.SemanticRole.MIXED);
  280. }
  281. function isPureUnit(node) {
  282. const children = node.childNodes;
  283. return (node.role === semantic_meaning_js_1.SemanticRole.UNIT &&
  284. (!children.length || children[0].role === semantic_meaning_js_1.SemanticRole.UNIT));
  285. }
  286. function isUnitProduct(node) {
  287. const children = node.childNodes;
  288. return (node.type === semantic_meaning_js_1.SemanticType.INFIXOP &&
  289. (node.role === semantic_meaning_js_1.SemanticRole.MULTIPLICATION ||
  290. node.role === semantic_meaning_js_1.SemanticRole.IMPLICIT) &&
  291. children.length &&
  292. (isPureUnit(children[0]) || isUnitCounter(children[0])) &&
  293. node.childNodes.slice(1).every(isPureUnit));
  294. }
  295. function isImplicit(node) {
  296. return (node.type === semantic_meaning_js_1.SemanticType.INFIXOP &&
  297. (node.role === semantic_meaning_js_1.SemanticRole.IMPLICIT ||
  298. (node.role === semantic_meaning_js_1.SemanticRole.UNIT &&
  299. !!node.contentNodes.length &&
  300. node.contentNodes[0].textContent === semantic_attr_js_1.NamedSymbol.invisibleTimes)));
  301. }
  302. function isImplicitOp(node) {
  303. return (node.type === semantic_meaning_js_1.SemanticType.INFIXOP && node.role === semantic_meaning_js_1.SemanticRole.IMPLICIT);
  304. }
  305. function isNeutralFence(fence) {
  306. return (fence.role === semantic_meaning_js_1.SemanticRole.NEUTRAL || fence.role === semantic_meaning_js_1.SemanticRole.METRIC);
  307. }
  308. function compareNeutralFences(fence1, fence2) {
  309. return (isNeutralFence(fence1) &&
  310. isNeutralFence(fence2) &&
  311. (0, semantic_util_js_1.getEmbellishedInner)(fence1).textContent ===
  312. (0, semantic_util_js_1.getEmbellishedInner)(fence2).textContent);
  313. }
  314. function elligibleLeftNeutral(fence) {
  315. if (!isNeutralFence(fence)) {
  316. return false;
  317. }
  318. if (!fence.embellished) {
  319. return true;
  320. }
  321. if (fence.type === semantic_meaning_js_1.SemanticType.SUPERSCRIPT ||
  322. fence.type === semantic_meaning_js_1.SemanticType.SUBSCRIPT) {
  323. return false;
  324. }
  325. if (fence.type === semantic_meaning_js_1.SemanticType.TENSOR &&
  326. (fence.childNodes[3].type !== semantic_meaning_js_1.SemanticType.EMPTY ||
  327. fence.childNodes[4].type !== semantic_meaning_js_1.SemanticType.EMPTY)) {
  328. return false;
  329. }
  330. return true;
  331. }
  332. function elligibleRightNeutral(fence) {
  333. if (!isNeutralFence(fence)) {
  334. return false;
  335. }
  336. if (!fence.embellished) {
  337. return true;
  338. }
  339. if (fence.type === semantic_meaning_js_1.SemanticType.TENSOR &&
  340. (fence.childNodes[1].type !== semantic_meaning_js_1.SemanticType.EMPTY ||
  341. fence.childNodes[2].type !== semantic_meaning_js_1.SemanticType.EMPTY)) {
  342. return false;
  343. }
  344. return true;
  345. }
  346. function isMembership(element) {
  347. return [
  348. semantic_meaning_js_1.SemanticRole.ELEMENT,
  349. semantic_meaning_js_1.SemanticRole.NONELEMENT,
  350. semantic_meaning_js_1.SemanticRole.REELEMENT,
  351. semantic_meaning_js_1.SemanticRole.RENONELEMENT
  352. ].includes(element.role);
  353. }