Stack.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * PHPExcel_Calculation_Token_Stack
  4. *
  5. * Copyright (c) 2006 - 2015 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Calculation
  23. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_Calculation_Token_Stack
  28. {
  29. /**
  30. * The parser stack for formulae
  31. *
  32. * @var mixed[]
  33. */
  34. private $stack = array();
  35. /**
  36. * Count of entries in the parser stack
  37. *
  38. * @var integer
  39. */
  40. private $count = 0;
  41. /**
  42. * Return the number of entries on the stack
  43. *
  44. * @return integer
  45. */
  46. public function count()
  47. {
  48. return $this->count;
  49. }
  50. /**
  51. * Push a new entry onto the stack
  52. *
  53. * @param mixed $type
  54. * @param mixed $value
  55. * @param mixed $reference
  56. */
  57. public function push($type, $value, $reference = null)
  58. {
  59. $this->stack[$this->count++] = array(
  60. 'type' => $type,
  61. 'value' => $value,
  62. 'reference' => $reference
  63. );
  64. if ($type == 'Function') {
  65. $localeFunction = PHPExcel_Calculation::localeFunc($value);
  66. if ($localeFunction != $value) {
  67. $this->stack[($this->count - 1)]['localeValue'] = $localeFunction;
  68. }
  69. }
  70. }
  71. /**
  72. * Pop the last entry from the stack
  73. *
  74. * @return mixed
  75. */
  76. public function pop()
  77. {
  78. if ($this->count > 0) {
  79. return $this->stack[--$this->count];
  80. }
  81. return null;
  82. }
  83. /**
  84. * Return an entry from the stack without removing it
  85. *
  86. * @param integer $n number indicating how far back in the stack we want to look
  87. * @return mixed
  88. */
  89. public function last($n = 1)
  90. {
  91. if ($this->count - $n < 0) {
  92. return null;
  93. }
  94. return $this->stack[$this->count - $n];
  95. }
  96. /**
  97. * Clear the stack
  98. */
  99. public function clear()
  100. {
  101. $this->stack = array();
  102. $this->count = 0;
  103. }
  104. }