Chunk.php 5.5 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. continue;
  54. // )
  55. case 41:
  56. $parenLevel--;
  57. if( $parenLevel < 0 ){
  58. return $this->fail("missing opening `(`");
  59. }
  60. continue;
  61. // ;
  62. case 59:
  63. //if (!$parenLevel) { $this->emitChunk(); }
  64. continue;
  65. // {
  66. case 123:
  67. $level++;
  68. $lastOpening = $this->parserCurrentIndex;
  69. continue;
  70. // }
  71. case 125:
  72. $level--;
  73. if( $level < 0 ){
  74. return $this->fail("missing opening `{`");
  75. }
  76. //if (!$level && !$parenLevel) { $this->emitChunk(); }
  77. continue;
  78. // \
  79. case 92:
  80. if ($this->parserCurrentIndex < $this->input_len - 1) { $this->parserCurrentIndex++; continue; }
  81. return $this->fail("unescaped `\\`");
  82. // ", ' and `
  83. case 34:
  84. case 39:
  85. case 96:
  86. $matched = 0;
  87. $currentChunkStartIndex = $this->parserCurrentIndex;
  88. for ($this->parserCurrentIndex = $this->parserCurrentIndex + 1; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++) {
  89. $cc2 = $this->CharCode($this->parserCurrentIndex);
  90. if ($cc2 > 96) { continue; }
  91. if ($cc2 == $cc) { $matched = 1; break; }
  92. if ($cc2 == 92) { // \
  93. if ($this->parserCurrentIndex == $this->input_len - 1) {
  94. return $this->fail("unescaped `\\`");
  95. }
  96. $this->parserCurrentIndex++;
  97. }
  98. }
  99. if ($matched) { continue; }
  100. return $this->fail("unmatched `" . chr($cc) . "`", $currentChunkStartIndex);
  101. // /, check for comment
  102. case 47:
  103. if ($parenLevel || ($this->parserCurrentIndex == $this->input_len - 1)) { continue; }
  104. $cc2 = $this->CharCode($this->parserCurrentIndex+1);
  105. if ($cc2 == 47) {
  106. // //, find lnfeed
  107. for ($this->parserCurrentIndex = $this->parserCurrentIndex + 2; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++) {
  108. $cc2 = $this->CharCode($this->parserCurrentIndex);
  109. if (($cc2 <= 13) && (($cc2 == 10) || ($cc2 == 13))) { break; }
  110. }
  111. } else if ($cc2 == 42) {
  112. // /*, find */
  113. $lastMultiComment = $currentChunkStartIndex = $this->parserCurrentIndex;
  114. for ($this->parserCurrentIndex = $this->parserCurrentIndex + 2; $this->parserCurrentIndex < $this->input_len - 1; $this->parserCurrentIndex++) {
  115. $cc2 = $this->CharCode($this->parserCurrentIndex);
  116. if ($cc2 == 125) { $lastMultiCommentEndBrace = $this->parserCurrentIndex; }
  117. if ($cc2 != 42) { continue; }
  118. if ($this->CharCode($this->parserCurrentIndex+1) == 47) { break; }
  119. }
  120. if ($this->parserCurrentIndex == $this->input_len - 1) {
  121. return $this->fail("missing closing `*/`", $currentChunkStartIndex);
  122. }
  123. }
  124. continue;
  125. // *, check for unmatched */
  126. case 42:
  127. if (($this->parserCurrentIndex < $this->input_len - 1) && ($this->CharCode($this->parserCurrentIndex+1) == 47)) {
  128. return $this->fail("unmatched `/*`");
  129. }
  130. continue;
  131. }
  132. }
  133. if( $level !== 0 ){
  134. if( ($lastMultiComment > $lastOpening) && ($lastMultiCommentEndBrace > $lastMultiComment) ){
  135. return $this->fail("missing closing `}` or `*/`", $lastOpening);
  136. } else {
  137. return $this->fail("missing closing `}`", $lastOpening);
  138. }
  139. } else if ( $parenLevel !== 0 ){
  140. return $this->fail("missing closing `)`", $lastParen);
  141. }
  142. //chunk didn't fail
  143. //$this->emitChunk(true);
  144. }
  145. public function CharCode($pos){
  146. return ord($this->input[$pos]);
  147. }
  148. public function fail( $msg, $index = null ){
  149. if( !$index ){
  150. $this->index = $this->parserCurrentIndex;
  151. }else{
  152. $this->index = $index;
  153. }
  154. $this->message = 'ParseError: '.$msg;
  155. }
  156. /*
  157. function emitChunk( $force = false ){
  158. $len = $this->parserCurrentIndex - $this->emitFrom;
  159. if ((($len < 512) && !$force) || !$len) {
  160. return;
  161. }
  162. $chunks[] = substr($this->input, $this->emitFrom, $this->parserCurrentIndex + 1 - $this->emitFrom );
  163. $this->emitFrom = $this->parserCurrentIndex + 1;
  164. }
  165. */
  166. }