BussproofsItems.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2018-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Items for TeX parsing of bussproofs.
  19. *
  20. * @author v.sorge@mathjax.org (Volker Sorge)
  21. */
  22. import TexError from '../TexError.js';
  23. import {BaseItem, CheckType, StackItem} from '../StackItem.js';
  24. import {MmlNode} from '../../../core/MmlTree/MmlNode.js';
  25. import Stack from '../Stack.js';
  26. import * as BussproofsUtil from './BussproofsUtil.js';
  27. export class ProofTreeItem extends BaseItem {
  28. /**
  29. * The current left label.
  30. * @type {MmlNode[]}
  31. */
  32. public leftLabel: MmlNode[] = null;
  33. /**
  34. * The current right label.
  35. * @type {MmlNode[]}
  36. */
  37. public rigthLabel: MmlNode[] = null;
  38. private innerStack: Stack = new Stack(this.factory, {}, true);
  39. /**
  40. * @override
  41. */
  42. public get kind() {
  43. return 'proofTree';
  44. }
  45. /**
  46. * @override
  47. */
  48. public checkItem(item: StackItem): CheckType {
  49. if (item.isKind('end') && item.getName() === 'prooftree') {
  50. let node = this.toMml();
  51. BussproofsUtil.setProperty(node, 'proof', true);
  52. return [[this.factory.create('mml', node), item], true];
  53. }
  54. if (item.isKind('stop')) {
  55. throw new TexError('EnvMissingEnd', 'Missing \\end{%1}', this.getName());
  56. }
  57. this.innerStack.Push(item);
  58. return BaseItem.fail;
  59. }
  60. /**
  61. * @override
  62. */
  63. public toMml() {
  64. const tree = super.toMml();
  65. const start = this.innerStack.Top();
  66. if (start.isKind('start') && !start.Size()) {
  67. return tree;
  68. }
  69. this.innerStack.Push(this.factory.create('stop'));
  70. let prefix = this.innerStack.Top().toMml();
  71. return this.create('node', 'mrow', [prefix, tree], {});
  72. }
  73. }