oqlinterpreter.class.inc.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 OqlInterpreterException extends OQLException
  32. {
  33. }
  34. class OqlInterpreter
  35. {
  36. public $m_sQuery;
  37. public function __construct($sQuery)
  38. {
  39. $this->m_sQuery = $sQuery;
  40. }
  41. // Note: this function is left public for unit test purposes
  42. public function Parse()
  43. {
  44. $oLexer = new OQLLexer($this->m_sQuery);
  45. $oParser = new OQLParser($this->m_sQuery);
  46. while($oLexer->yylex())
  47. {
  48. $oParser->doParse($oLexer->token, $oLexer->value, $oLexer->getTokenPos());
  49. }
  50. $res = $oParser->doFinish();
  51. return $res;
  52. }
  53. public function ParseObjectQuery()
  54. {
  55. $oRes = $this->Parse();
  56. if (!$oRes instanceof OqlObjectQuery)
  57. {
  58. throw new OQLException('Expecting an OQL query', $this->m_sQuery, 0, 0, get_class($oRes));
  59. }
  60. return $oRes;
  61. }
  62. public function ParseExpression()
  63. {
  64. $oRes = $this->Parse();
  65. if (!$oRes instanceof Expression)
  66. {
  67. throw new OQLException('Expecting an OQL expression', $this->m_sQuery, 0, 0, get_class($oRes), array('Expression'));
  68. }
  69. return $oRes;
  70. }
  71. }
  72. ?>