[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/libraries/antlr/ -> RecognitionException.php (source)

   1  <?php
   2  /*
   3   [The "BSD licence"]
   4   Copyright (c) 2005-2008 Terence Parr
   5   All rights reserved.
   6  
   7   Redistribution and use in source and binary forms, with or without
   8   modification, are permitted provided that the following conditions
   9   are met:
  10   1. Redistributions of source code must retain the above copyright
  11      notice, this list of conditions and the following disclaimer.
  12   2. Redistributions in binary form must reproduce the above copyright
  13      notice, this list of conditions and the following disclaimer in the
  14      documentation and/or other materials provided with the distribution.
  15   3. The name of the author may not be used to endorse or promote products
  16      derived from this software without specific prior written permission.
  17  
  18   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28  */
  29  
  30  /** The root of the ANTLR exception hierarchy.
  31   *
  32   *  To avoid English-only error messages and to generally make things
  33   *  as flexible as possible, these exceptions are not created with strings,
  34   *  but rather the information necessary to generate an error.  Then
  35   *  the various reporting methods in Parser and Lexer can be overridden
  36   *  to generate a localized error message.  For example, MismatchedToken
  37   *  exceptions are built with the expected token type.
  38   *  So, don't expect getMessage() to return anything.
  39   *
  40   *  Note that as of Java 1.4, you can access the stack trace, which means
  41   *  that you can compute the complete trace of rules from the start symbol.
  42   *  This gives you considerable context information with which to generate
  43   *  useful error messages.
  44   *
  45   *  ANTLR generates code that throws exceptions upon recognition error and
  46   *  also generates code to catch these exceptions in each rule.  If you
  47   *  want to quit upon first error, you can turn off the automatic error
  48   *  handling mechanism using rulecatch action, but you still need to
  49   *  override methods mismatch and recoverFromMismatchSet.
  50   *
  51   *  In general, the recognition exceptions can track where in a grammar a
  52   *  problem occurred and/or what was the expected input.  While the parser
  53   *  knows its state (such as current input symbol and line info) that
  54   *  state can change before the exception is reported so current token index
  55   *  is computed and stored at exception time.  From this info, you can
  56   *  perhaps print an entire line of input not just a single token, for example.
  57   *  Better to just say the recognizer had a problem and then let the parser
  58   *  figure out a fancy report.
  59   */
  60  class RecognitionException extends Exception {
  61  
  62      public $line=0;
  63  
  64  
  65  	public function __construct($input) {
  66          /** What input stream did the error occur in? */
  67          $this->input = $input;
  68          /** What is index of token/char were we looking at when the error occurred? */
  69          $this->index = $input->index();
  70  
  71          /** The current Token when an error occurred.  Since not all streams
  72           *  can retrieve the ith Token, we have to track the Token object.
  73           *  For parsers.  Even when it's a tree parser, token might be set.
  74           */
  75          $this->token=null;
  76  
  77          /** If this is a tree parser exception, node is set to the node with
  78           *  the problem.
  79           */
  80          $this->node=null;
  81  
  82          /** The current char when an error occurred. For lexers. */
  83          $this->c=0;
  84  
  85          /** Track the line at which the error occurred in case this is
  86           *  generated from a lexer.  We need to track this since the
  87           *  unexpected char doesn't carry the line info.
  88           */
  89          $this->line=0;
  90  
  91          $this->charPositionInLine=0;
  92  
  93          /** If you are parsing a tree node stream, you will encounter som
  94           *  imaginary nodes w/o line/col info.  We now search backwards looking
  95           *  for most recent token with line/col info, but notify getErrorHeader()
  96           *  that info is approximate.
  97           */
  98          $this->approximateLineInfo=false;
  99          
 100  
 101          if ( $this->input instanceof TokenStream ) {
 102              $this->token = $input->LT(1);
 103              $this->line = $this->token->getLine();
 104              $this->charPositionInLine = $this->token->getCharPositionInLine();
 105          }
 106          if ( $this->input instanceof TreeNodeStream ) {
 107              $this->extractInformationFromTreeNodeStream($input);
 108          }
 109          else if ( $input instanceof CharStream ) {
 110              $this->c = $input->LA(1);
 111              $this->line = $input->getLine();
 112              $this->charPositionInLine = $input->getCharPositionInLine();
 113          }
 114          else {
 115              $this->c = $input->LA(1);
 116          }
 117      }
 118  
 119  	protected function extractInformationFromTreeNodeStream($input) {
 120          $nodes = $input;
 121          $this->node = $nodes->LT(1);
 122          $adaptor = $nodes->getTreeAdaptor();
 123          $payload = $adaptor->getToken($this->node);
 124          if ( $payload!=null ) {
 125              $this->token = $payload;
 126              if ( $payload->getLine()<= 0 ) {
 127                  // imaginary node; no line/pos info; scan backwards
 128                  $i = -1;
 129                  $priorNode = $nodes->LT($i);
 130                  while ( $priorNode!=null ) {
 131                      $priorPayload = $adaptor->getToken($priorNode);
 132                      if ( $priorPayload!=null && $priorPayload->getLine()>0 ) {
 133                          // we found the most recent real line / pos info
 134                          $this->line = $priorPayload->getLine();
 135                          $this->charPositionInLine = $priorPayload->getCharPositionInLine();
 136                          $this->approximateLineInfo = true;
 137                          break;
 138                      }
 139                      --$i;
 140                      $priorNode = $nodes->LT($i);
 141                  }
 142              }
 143              else { // node created from real token
 144                  $this->line = $payload->getLine();
 145                  $this->charPositionInLine = $payload->getCharPositionInLine();
 146              }
 147          }
 148          else if ( $this->node instanceof Tree) {
 149              $this->line = $this->node->getLine();
 150              $this->charPositionInLine = $this->node->getCharPositionInLine();
 151              if ( $this->node instanceof CommonTree) {
 152                  $this->token = $this->node->token;
 153              }
 154          }
 155          else {
 156              $type = $adaptor->getType($this->node);
 157              $text = $adaptor->getText($this->node);
 158              $this->token = CommonToken::forTypeAndText($type, $text);
 159          }
 160      }
 161  
 162      /** Return the token type or char of the unexpected input element */
 163  	public function getUnexpectedType() {
 164          if ( $this->input instanceof TokenStream ) {
 165              return $this->token->getType();
 166          }
 167          else if ( $this->input instanceof TreeNodeStream ) {
 168              $nodes = $this->input;
 169              $adaptor = $nodes->getTreeAdaptor();
 170              return $adaptor->getType($this->node);
 171          }
 172          else {
 173              return $this->c;
 174          }
 175      }
 176  }
 177  
 178  
 179  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1