[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

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

   1  <?php
   2      CharStreamConst::$EOF = -1;
   3  
   4      class ANTLRStringStream implements CharStream {
   5  
   6          /** Copy data in string to a local char array */
   7  		public function __construct($input) {
   8              $this->p=0;
   9              $this->line = 1;
  10              $this->charPositionInLine = 0;
  11              $this->markDepth = 0;
  12              $this->markers = null;
  13              $this->lastMarker=0;
  14              $this->name=null;
  15              
  16              $this->data = strToIntArray($input);
  17              $this->n = strlen($input);
  18          }
  19  
  20          /** Reset the stream so that it's in the same state it was
  21           *  when the object was created *except* the data array is not
  22           *  touched.
  23           */
  24  		public function reset() {
  25              $this->p = 0;
  26              $this->line = 1;
  27              $this->charPositionInLine = 0;
  28              $this->markDepth = 0;
  29          }
  30  
  31  	    public function consume() {
  32              if ( $this->p < $this->n ) {
  33                  $this->charPositionInLine++;
  34                  if ( $this->data[$this->p]==ord("\n") ) {
  35                      $this->line++;
  36                      $this->charPositionInLine=0;
  37                  }
  38                  $this->p++;
  39              }
  40          }
  41  
  42          public function LA($i) {
  43              if ( $i==0 ) {
  44                  return 0; // undefined
  45              }
  46              if ( $i<0 ) {
  47                  $i++; // e.g., translate LA(-1) to use offset i=0; then data[p+0-1]
  48                  if ( ($this->p+$i-1) < 0 ) {
  49                      return CharStreamConst::$EOF; // invalid; no char before first char
  50                  }
  51              }
  52  
  53              if ( ($this->p+$i-1) >= $this->n ) {
  54                  //System.out.println("char LA("+i+")=EOF; p="+p);
  55                  return CharStreamConst::$EOF;
  56              }
  57              //System.out.println("char LA("+i+")="+(char)data[p+i-1]+"; p="+p);
  58              //System.out.println("LA("+i+"); p="+p+" n="+n+" data.length="+data.length);
  59              return $this->data[$this->p+$i-1];
  60          }
  61  
  62          public function LT($i) {
  63              return $this->LA($i);
  64          }
  65  
  66          /** Return the current input symbol index 0..n where n indicates the
  67           *  last symbol has been read.  The index is the index of char to
  68           *  be returned from LA(1).
  69           */
  70  	    public function index() {
  71              return $this->p;
  72          }
  73  
  74  		public function size() {
  75              return $this->n;
  76          }
  77  
  78  		public function mark() {
  79              if ( $this->markers == null) {
  80                  $this->markers = array();
  81                  $this->markers[] = null; // depth 0 means no backtracking, leave blank
  82              }
  83              $this->markDepth++;
  84              $state = null;
  85              if ($this->markDepth>=sizeof($this->markers)) {
  86                  $state = new CharStreamState();
  87                  $this->markers[] = $state;
  88              }
  89              else {
  90                  $state = $this->markers[$this->markDepth];
  91              }
  92              $state->p = $this->p;
  93              $state->line = $this->line;
  94              $state->charPositionInLine = $this->charPositionInLine;
  95              $this->lastMarker = $this->markDepth;
  96              return $this->markDepth;
  97          }
  98  
  99  	    public function rewind($m=null) {
 100              if($m===null){
 101                  $this->rewind((int)$this->lastMarker);
 102              }else{
 103                  $state = $this->markers[$m];
 104                  // restore stream state
 105                  $this->seek($state->p);
 106                  $this->line = $state->line;
 107                  $this->charPositionInLine = $state->charPositionInLine;
 108                  $this->release($m);
 109              }
 110          }
 111  
 112  		public function release($marker) {
 113              // unwind any other markers made after m and release m
 114              $this->markDepth = $marker;
 115              // release this marker
 116              $this->markDepth--;
 117          }
 118  
 119          /** consume() ahead until p==index; can't just set p=index as we must
 120           *  update line and charPositionInLine.
 121           */
 122  		public function seek($index) {
 123              if ( $index<=$this->p ) {
 124                  $this->p = $index; // just jump; don't update stream state (line, ...)
 125                  return;
 126              }
 127              // seek forward, consume until p hits index
 128              while ( $this->p<$index ) {
 129                  $this->consume();
 130              }
 131          }
 132  
 133  		public function substring($start, $stop) {
 134              return implode(array_map('chr', array_slice($this->data, $start, $stop-$start+1)));
 135          }
 136  
 137  		public function getLine() {
 138              return $this->line;
 139          }
 140  
 141  		public function getCharPositionInLine() {
 142              return $this->charPositionInLine;
 143          }
 144  
 145  		public function setLine($line) {
 146              $this->line = $line;
 147          }
 148  
 149  		public function setCharPositionInLine($pos) {
 150              $this->charPositionInLine = $pos;
 151          }
 152  
 153  		public function getSourceName() {
 154              return $this->name;
 155          }
 156      }
 157  
 158  ?>


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