oqlinterpreter.class.inc.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Wrapper to execute the parser, lexical analyzer and normalization of an OQL query
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. class OqlNormalizeException extends OQLException
  25. {
  26. public function __construct($sIssue, $sInput, OqlName $oName, $aExpecting = null)
  27. {
  28. parent::__construct($sIssue, $sInput, 0, $oName->GetPos(), $oName->GetValue(), $aExpecting);
  29. }
  30. }
  31. class UnknownClassOqlException extends OqlNormalizeException
  32. {
  33. public function __construct($sInput, OqlName $oName, $aExpecting = null)
  34. {
  35. parent::__construct('Unknown class', $sInput, $oName, $aExpecting);
  36. }
  37. }
  38. class OqlInterpreterException extends OQLException
  39. {
  40. }
  41. class OqlInterpreter
  42. {
  43. public $m_sQuery;
  44. public function __construct($sQuery)
  45. {
  46. $this->m_sQuery = $sQuery;
  47. }
  48. // Note: this function is left public for unit test purposes
  49. public function Parse()
  50. {
  51. $oLexer = new OQLLexer($this->m_sQuery);
  52. $oParser = new OQLParser($this->m_sQuery);
  53. while($oLexer->yylex())
  54. {
  55. $oParser->doParse($oLexer->token, $oLexer->value, $oLexer->getTokenPos());
  56. }
  57. $res = $oParser->doFinish();
  58. return $res;
  59. }
  60. public function ParseObjectQuery()
  61. {
  62. $oRes = $this->Parse();
  63. if (!$oRes instanceof OqlObjectQuery)
  64. {
  65. throw new OQLException('Expecting an OQL query', $this->m_sQuery, 0, 0, get_class($oRes));
  66. }
  67. return $oRes;
  68. }
  69. public function ParseExpression()
  70. {
  71. $oRes = $this->Parse();
  72. if (!$oRes instanceof Expression)
  73. {
  74. throw new OQLException('Expecting an OQL expression', $this->m_sQuery, 0, 0, get_class($oRes), array('Expression'));
  75. }
  76. return $oRes;
  77. }
  78. }
  79. ?>