Chunk.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Chunk Exception
  4. *
  5. * @package Less
  6. * @subpackage exception
  7. */
  8. class Less_Exception_Chunk extends Less_Exception_Parser {
  9. protected $parserCurrentIndex = 0;
  10. protected $emitFrom = 0;
  11. protected $input_len;
  12. /**
  13. * Constructor
  14. *
  15. * @param string $input
  16. * @param Exception $previous Previous exception
  17. * @param integer $index The current parser index
  18. * @param Less_FileInfo|string $currentFile The file
  19. * @param integer $code The exception code
  20. */
  21. public function __construct( $input, Exception $previous = null, $index = null, $currentFile = null, $code = 0 ) {
  22. $this->message = 'ParseError: Unexpected input'; // default message
  23. $this->index = $index;
  24. $this->currentFile = $currentFile;
  25. $this->input = $input;
  26. $this->input_len = strlen( $input );
  27. $this->Chunks();
  28. $this->genMessage();
  29. }
  30. /**
  31. * See less.js chunks()
  32. * We don't actually need the chunks
  33. *
  34. */
  35. protected function Chunks() {
  36. $level = 0;
  37. $parenLevel = 0;
  38. $lastMultiCommentEndBrace = null;
  39. $lastOpening = null;
  40. $lastMultiComment = null;
  41. $lastParen = null;
  42. for ( $this->parserCurrentIndex = 0; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ) {
  43. $cc = $this->CharCode( $this->parserCurrentIndex );
  44. if ( ( ( $cc >= 97 ) && ( $cc <= 122 ) ) || ( $cc < 34 ) ) {
  45. // a-z or whitespace
  46. continue;
  47. }
  48. switch ( $cc ) {
  49. // (
  50. case 40:
  51. $parenLevel++;
  52. $lastParen = $this->parserCurrentIndex;
  53. break;
  54. // )
  55. case 41:
  56. $parenLevel--;
  57. if ( $parenLevel < 0 ) {
  58. return $this->fail( "missing opening `(`" );
  59. }
  60. break;
  61. // ;
  62. case 59:
  63. // if (!$parenLevel) { $this->emitChunk(); }
  64. break;
  65. // {
  66. case 123:
  67. $level++;
  68. $lastOpening = $this->parserCurrentIndex;
  69. break;
  70. // }
  71. case 125:
  72. $level--;
  73. if ( $level < 0 ) {
  74. return $this->fail( "missing opening `{`" );
  75. }
  76. // if (!$level && !$parenLevel) { $this->emitChunk(); }
  77. break;
  78. // \
  79. case 92:
  80. if ( $this->parserCurrentIndex < $this->input_len - 1 ) { $this->parserCurrentIndex++; break;
  81. }
  82. return $this->fail( "unescaped `\\`" );
  83. // ", ' and `
  84. case 34:
  85. case 39:
  86. case 96:
  87. $matched = 0;
  88. $currentChunkStartIndex = $this->parserCurrentIndex;
  89. for ( $this->parserCurrentIndex = $this->parserCurrentIndex + 1; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ) {
  90. $cc2 = $this->CharCode( $this->parserCurrentIndex );
  91. if ( $cc2 > 96 ) { continue;
  92. }
  93. if ( $cc2 == $cc ) { $matched = 1; break;
  94. }
  95. if ( $cc2 == 92 ) { // \
  96. if ( $this->parserCurrentIndex == $this->input_len - 1 ) {
  97. return $this->fail( "unescaped `\\`" );
  98. }
  99. $this->parserCurrentIndex++;
  100. }
  101. }
  102. if ( $matched ) { break;
  103. }
  104. return $this->fail( "unmatched `" . chr( $cc ) . "`", $currentChunkStartIndex );
  105. // /, check for comment
  106. case 47:
  107. if ( $parenLevel || ( $this->parserCurrentIndex == $this->input_len - 1 ) ) { break;
  108. }
  109. $cc2 = $this->CharCode( $this->parserCurrentIndex + 1 );
  110. if ( $cc2 == 47 ) {
  111. // //, find lnfeed
  112. for ( $this->parserCurrentIndex = $this->parserCurrentIndex + 2; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ) {
  113. $cc2 = $this->CharCode( $this->parserCurrentIndex );
  114. if ( ( $cc2 <= 13 ) && ( ( $cc2 == 10 ) || ( $cc2 == 13 ) ) ) { break;
  115. }
  116. }
  117. } else if ( $cc2 == 42 ) {
  118. // /*, find */
  119. $lastMultiComment = $currentChunkStartIndex = $this->parserCurrentIndex;
  120. for ( $this->parserCurrentIndex = $this->parserCurrentIndex + 2; $this->parserCurrentIndex < $this->input_len - 1; $this->parserCurrentIndex++ ) {
  121. $cc2 = $this->CharCode( $this->parserCurrentIndex );
  122. if ( $cc2 == 125 ) { $lastMultiCommentEndBrace = $this->parserCurrentIndex;
  123. }
  124. if ( $cc2 != 42 ) { continue;
  125. }
  126. if ( $this->CharCode( $this->parserCurrentIndex + 1 ) == 47 ) { break;
  127. }
  128. }
  129. if ( $this->parserCurrentIndex == $this->input_len - 1 ) {
  130. return $this->fail( "missing closing `*/`", $currentChunkStartIndex );
  131. }
  132. }
  133. break;
  134. // *, check for unmatched */
  135. case 42:
  136. if ( ( $this->parserCurrentIndex < $this->input_len - 1 ) && ( $this->CharCode( $this->parserCurrentIndex + 1 ) == 47 ) ) {
  137. return $this->fail( "unmatched `/*`" );
  138. }
  139. break;
  140. }
  141. }
  142. if ( $level !== 0 ) {
  143. if ( ( $lastMultiComment > $lastOpening ) && ( $lastMultiCommentEndBrace > $lastMultiComment ) ) {
  144. return $this->fail( "missing closing `}` or `*/`", $lastOpening );
  145. } else {
  146. return $this->fail( "missing closing `}`", $lastOpening );
  147. }
  148. } else if ( $parenLevel !== 0 ) {
  149. return $this->fail( "missing closing `)`", $lastParen );
  150. }
  151. // chunk didn't fail
  152. //$this->emitChunk(true);
  153. }
  154. public function CharCode( $pos ) {
  155. return ord( $this->input[$pos] );
  156. }
  157. public function fail( $msg, $index = null ) {
  158. if ( !$index ) {
  159. $this->index = $this->parserCurrentIndex;
  160. } else {
  161. $this->index = $index;
  162. }
  163. $this->message = 'ParseError: '.$msg;
  164. }
  165. /*
  166. function emitChunk( $force = false ){
  167. $len = $this->parserCurrentIndex - $this->emitFrom;
  168. if ((($len < 512) && !$force) || !$len) {
  169. return;
  170. }
  171. $chunks[] = substr($this->input, $this->emitFrom, $this->parserCurrentIndex + 1 - $this->emitFrom );
  172. $this->emitFrom = $this->parserCurrentIndex + 1;
  173. }
  174. */
  175. }