oqlinterpreter.class.inc.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Wrapper to execute the parser, lexical analyzer and normalization of an OQL query
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  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. public function GetUserFriendlyDescription()
  38. {
  39. $sWrongClass = $this->GetWrongWord();
  40. $sSuggest = self::FindClosestString($sWrongClass, $this->GetSuggestions());
  41. if ($sSuggest != '')
  42. {
  43. return Dict::Format('UI:OQL:UnknownClassAndFix', $sWrongClass, $sSuggest);
  44. }
  45. else
  46. {
  47. return Dict::Format('UI:OQL:UnknownClassNoFix', $sWrongClass);
  48. }
  49. }
  50. }
  51. class OqlInterpreterException extends OQLException
  52. {
  53. }
  54. class OqlInterpreter
  55. {
  56. public $m_sQuery;
  57. public function __construct($sQuery)
  58. {
  59. $this->m_sQuery = $sQuery;
  60. }
  61. // Note: this function is left public for unit test purposes
  62. public function Parse()
  63. {
  64. $oLexer = new OQLLexer($this->m_sQuery);
  65. $oParser = new OQLParser($this->m_sQuery);
  66. while($oLexer->yylex())
  67. {
  68. $oParser->doParse($oLexer->token, $oLexer->value, $oLexer->getTokenPos());
  69. }
  70. $res = $oParser->doFinish();
  71. return $res;
  72. }
  73. public function ParseObjectQuery()
  74. {
  75. $oRes = $this->Parse();
  76. if (!$oRes instanceof OqlObjectQuery)
  77. {
  78. throw new OQLException('Expecting an OQL query', $this->m_sQuery, 0, 0, get_class($oRes));
  79. }
  80. return $oRes;
  81. }
  82. public function ParseExpression()
  83. {
  84. $oRes = $this->Parse();
  85. if (!$oRes instanceof Expression)
  86. {
  87. throw new OQLException('Expecting an OQL expression', $this->m_sQuery, 0, 0, get_class($oRes), array('Expression'));
  88. }
  89. return $oRes;
  90. }
  91. }
  92. ?>