zhao
2021-06-11 98186752629a7bd38965418af84db382d90b9c07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) Under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for Additional information regarding copyright ownership.
   The ASF licenses this file to You Under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed Under the License is distributed on an "AS Is" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations Under the License.
==================================================================== */
 
namespace HH.WMS.Utils.NPOI.HSSF.Model
{
    using System;
 
    using HH.WMS.Utils.NPOI.SS.Formula;
    using HH.WMS.Utils.NPOI.SS.Formula.Function;
    using HH.WMS.Utils.NPOI.SS.Formula.PTG;
    /**
     * Represents a syntactic element from a formula by encapsulating the corresponding <c>Ptg</c>
     * token.  Each <c>ParseNode</c> may have child <c>ParseNode</c>s in the case when the wrapped
     * <c>Ptg</c> is non-atomic.
     * 
     * @author Josh Micich
     */
    class ParseNode
    {
 
        public static ParseNode[] EMPTY_ARRAY = { };
        private Ptg _token;
        private ParseNode[] _children;
        private bool _isIf;
        private int _tokenCount;
 
        public ParseNode(Ptg token, ParseNode[] children)
        {
            _token = token;
            _children = children;
            _isIf = IsIf(token);
            int tokenCount = 1;
            for (int i = 0; i < children.Length; i++)
            {
                tokenCount += children[i].GetTokenCount();
            }
            if (_isIf)
            {
                // there will be 2 or 3 extra tAttr tokens according to whether the false param is present
                tokenCount += children.Length;
            }
            _tokenCount = tokenCount;
        }
        public ParseNode(Ptg token):this(token, EMPTY_ARRAY) 
        {
            
        }
        public ParseNode(Ptg token, ParseNode child0):this(token, new ParseNode[] { child0, })
        {
            
        }
        public ParseNode(Ptg token, ParseNode child0, ParseNode child1): this(token, new ParseNode[] { child0, child1, })
        {
           
        }
        private int GetTokenCount()
        {
            return _tokenCount;
        }
 
        /// <summary>
        /// Collects the array of Ptg
        ///  tokens for the specified tree.
        /// </summary>
        /// <param name="rootNode">The root node.</param>
        /// <returns></returns>
        public static Ptg[] ToTokenArray(ParseNode rootNode)
        {
            TokenCollector temp = new TokenCollector(rootNode.GetTokenCount());
            rootNode.CollectPtgs(temp);
            return temp.Result;
        }
        private void CollectPtgs(TokenCollector temp)
        {
            if (IsIf(this.Token))
            {
                CollectIfPtgs(temp);
                return;
            }
            for (int i = 0; i < Children.Length; i++)
            {
                Children[i].CollectPtgs(temp);
            }
            temp.Add(this.Token);
        }
        /// <summary>
        /// The IF() function Gets marked up with two or three tAttr tokens.
        /// Similar logic will be required for CHOOSE() when it is supported
        /// See excelfileformat.pdf sec 3.10.5 "tAttr (19H)
        /// </summary>
        /// <param name="temp">The temp.</param>
        private void CollectIfPtgs(TokenCollector temp)
        {
 
            // condition goes first
            Children[0].CollectPtgs(temp);
 
            // placeholder for tAttrIf
            int ifAttrIndex = temp.CreatePlaceholder();
 
            // true parameter
            Children[1].CollectPtgs(temp);
 
            // placeholder for first skip attr
            int skipAfterTrueParamIndex = temp.CreatePlaceholder();
            int trueParamSize = temp.SumTokenSizes(ifAttrIndex + 1, skipAfterTrueParamIndex);
 
            AttrPtg attrIf = AttrPtg.CreateIf(trueParamSize + 4); // distance to start of false parameter/tFuncVar. +4 for tAttrSkip after true
            
            if (Children.Length > 2)
            {
                // false param present
 
                // false parameter
                Children[2].CollectPtgs(temp);
 
                int skipAfterFalseParamIndex = temp.CreatePlaceholder();
                int falseParamSize = temp.SumTokenSizes(skipAfterTrueParamIndex + 1, skipAfterFalseParamIndex);
 
                AttrPtg attrSkipAfterTrue = AttrPtg.CreateSkip(falseParamSize + 4 + 4 - 1); // 1 less than distance to end of if FuncVar(size=4). +4 for attr skip before
                AttrPtg attrSkipAfterFalse = AttrPtg.CreateSkip(4 - 1); // 1 less than distance to end of if FuncVar(size=4).
 
                temp.SetPlaceholder(ifAttrIndex, attrIf);
                temp.SetPlaceholder(skipAfterTrueParamIndex, attrSkipAfterTrue);
                temp.SetPlaceholder(skipAfterFalseParamIndex, attrSkipAfterFalse);
            }
            else
            {
                // false parameter not present
                AttrPtg attrSkipAfterTrue = AttrPtg.CreateSkip(4 - 1); // 1 less than distance to end of if FuncVar(size=4).
 
                temp.SetPlaceholder(ifAttrIndex, attrIf);
                temp.SetPlaceholder(skipAfterTrueParamIndex, attrSkipAfterTrue);
            }
 
            temp.Add(_token);
        }
 
        private static bool IsIf(Ptg token)
        {
            if (token is FuncVarPtg)
            {
                FuncVarPtg func = (FuncVarPtg)token;
                if (FunctionMetadataRegistry.FUNCTION_NAME_IF.Equals(func.Name))
                {
                    return true;
                }
            }
            return false;
        }
 
        public Ptg Token
        {
            get
            {
                return _token;
            }
        }
 
        public ParseNode[] Children
        {
            get { return _children; }
        }
 
        private class TokenCollector
        {
 
            private Ptg[] _ptgs;
            private int _offset;
 
            public TokenCollector(int tokenCount)
            {
                _ptgs = new Ptg[tokenCount];
                _offset = 0;
            }
 
            public int SumTokenSizes(int fromIx, int toIx)
            {
                int result = 0;
                for (int i = fromIx; i < toIx; i++)
                {
                    result += _ptgs[i].Size;
                }
                return result;
            }
 
            public int CreatePlaceholder()
            {
                return _offset++;
            }
 
            public void Add(Ptg token)
            {
                if (token == null)
                {
                    throw new ArgumentException("token must not be null");
                }
                _ptgs[_offset] = token;
                _offset++;
            }
 
            public void SetPlaceholder(int index, Ptg token)
            {
                if (_ptgs[index] != null)
                {
                    throw new InvalidOperationException("Invalid placeholder index (" + index + ")");
                }
                _ptgs[index] = token;
            }
 
            public Ptg[] Result
            {
                get
                {
                    return _ptgs;
                }
            }
        }
    }
}