|
| 1 | +/************************************************************* |
| 2 | + * |
| 3 | + * Copyright (c) 2017 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 | + |
| 19 | +/** |
| 20 | + * @fileoverview The parsing stack. |
| 21 | + * |
| 22 | + * @author [email protected] (Volker Sorge) |
| 23 | + */ |
| 24 | + |
| 25 | +import {Environment} from './types.js'; |
| 26 | +import * as StackItem from './stack_item.js'; |
| 27 | +import {MathItem} from '../../core/MathItem.js'; |
| 28 | + |
| 29 | + |
| 30 | +export default class Stack { |
| 31 | + |
| 32 | + public env: Environment = {}; |
| 33 | + // private tree = new Tree(); |
| 34 | + private global: Environment = {}; |
| 35 | + private data: StackItem.StackItem[] = []; |
| 36 | + |
| 37 | + // TODO: Special functions that can be removed eventually. |
| 38 | + private transformMML: (item: any) => any; |
| 39 | + private testStackItem: (item: any) => boolean; |
| 40 | + |
| 41 | + constructor(env: Environment, inner: boolean, |
| 42 | + transform: (item: any) => boolean, |
| 43 | + test: (item: any) => boolean) { |
| 44 | + this.global = {isInner: inner}; |
| 45 | + this.data = [new StackItem.Start(this.global)]; |
| 46 | + if (env) { |
| 47 | + this.data[0].env = env; |
| 48 | + } |
| 49 | + this.env = this.data[0].env; |
| 50 | + this.transformMML = transform; |
| 51 | + this.testStackItem = test; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + private pushItem(item: StackItem.StackItem) { |
| 56 | + this.data.push(item); |
| 57 | + } |
| 58 | + |
| 59 | + // This should be rewritten to rest arguments! |
| 60 | + public push(itemJson: {kind: StackItem.ItemType, content: any}) { |
| 61 | + if (!itemJson) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if (itemJson.kind === 'mml') { |
| 66 | + // Tree.parseNode(itemJson.content); |
| 67 | + } |
| 68 | + let item = StackItem.ItemFactory(itemJson.kind, itemJson.content); |
| 69 | + item.env = this.global; |
| 70 | + let top = (this.data.length ? this.top().checkItem(item) : true); |
| 71 | + |
| 72 | + if (item.hasType('mml')) { |
| 73 | + // this.tree.setRoot((item as StackItem.Mml).node); |
| 74 | + } |
| 75 | + |
| 76 | + // TODO: Can we model this with a multi-stackitem? |
| 77 | + if (top instanceof Array) { |
| 78 | + this.pop(); |
| 79 | + for (let it of top) { |
| 80 | + this.pushItem(it); |
| 81 | + } |
| 82 | + } |
| 83 | + // Do we really need this? |
| 84 | + else if (top instanceof StackItem.Base) { |
| 85 | + this.pop(); |
| 86 | + this.pushItem(top); |
| 87 | + } |
| 88 | + else if (top) { |
| 89 | + this.data.push(item); |
| 90 | + // TODO: This needs to be reconciled with the env in new stack item. |
| 91 | + if (item.env) { |
| 92 | + for (let id in this.env) { |
| 93 | + item.env[id] = this.env[id]; |
| 94 | + } |
| 95 | + this.env = item.env; |
| 96 | + } else { |
| 97 | + item.env = this.env; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public pop(): any { |
| 103 | + let item = this.data.pop(); |
| 104 | + // TODO: This needs to be reconciled with the environment in stack item. |
| 105 | + if (!item.isOpen()) { |
| 106 | + delete item.env; |
| 107 | + } |
| 108 | + this.env = (this.data.length ? this.top().env : {}); |
| 109 | + return item; |
| 110 | + } |
| 111 | + |
| 112 | + public top(n?: number): any { |
| 113 | + if (n == null) { |
| 114 | + n = 1; |
| 115 | + } |
| 116 | + if (this.data.length < n) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + return this.data[this.data.length - n]; |
| 120 | + } |
| 121 | + |
| 122 | + public prev(noPop: boolean): any { |
| 123 | + let top = this.top(); |
| 124 | + if (noPop) { |
| 125 | + return top.data[top.data.length - 1]; |
| 126 | + } |
| 127 | + // TODO: This Pop has to be small once all the step item functions are |
| 128 | + // refactored. |
| 129 | + return top.Pop(); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @override |
| 134 | + */ |
| 135 | + public toString() { |
| 136 | + return 'stack[\n ' + this.data.join(', ') + '\n]'; |
| 137 | + } |
| 138 | + |
| 139 | + // public getResult(): Tree { |
| 140 | + // if (this.top().kind === 'stop') { |
| 141 | + // console.log('Warning: incomplete parsing!'); |
| 142 | + // } |
| 143 | + // return this.tree; |
| 144 | + // } |
| 145 | + |
| 146 | +} |
0 commit comments