--- /dev/null
+<?php\r
+\r
+/**\r
+ * Chunk Exception\r
+ *\r
+ * @package Less\r
+ * @subpackage exception\r
+ */\r
+class Less_Exception_Chunk extends Less_Exception_Parser{\r
+\r
+\r
+ protected $parserCurrentIndex = 0;\r
+\r
+ protected $emitFrom = 0;\r
+\r
+ protected $input_len;\r
+\r
+\r
+ /**\r
+ * Constructor\r
+ *\r
+ * @param string $input\r
+ * @param Exception $previous Previous exception\r
+ * @param integer $index The current parser index\r
+ * @param Less_FileInfo|string $currentFile The file\r
+ * @param integer $code The exception code\r
+ */\r
+ public function __construct($input, Exception $previous = null, $index = null, $currentFile = null, $code = 0){\r
+\r
+ $this->message = 'ParseError: Unexpected input'; //default message\r
+\r
+ $this->index = $index;\r
+\r
+ $this->currentFile = $currentFile;\r
+\r
+ $this->input = $input;\r
+ $this->input_len = strlen($input);\r
+\r
+ $this->Chunks();\r
+ $this->genMessage();\r
+ }\r
+\r
+\r
+ /**\r
+ * See less.js chunks()\r
+ * We don't actually need the chunks\r
+ *\r
+ */\r
+ protected function Chunks(){\r
+ $level = 0;\r
+ $parenLevel = 0;\r
+ $lastMultiCommentEndBrace = null;\r
+ $lastOpening = null;\r
+ $lastMultiComment = null;\r
+ $lastParen = null;\r
+\r
+ for( $this->parserCurrentIndex = 0; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ){\r
+ $cc = $this->CharCode($this->parserCurrentIndex);\r
+ if ((($cc >= 97) && ($cc <= 122)) || ($cc < 34)) {\r
+ // a-z or whitespace\r
+ continue;\r
+ }\r
+\r
+ switch ($cc) {\r
+\r
+ // (\r
+ case 40:\r
+ $parenLevel++;\r
+ $lastParen = $this->parserCurrentIndex;\r
+ continue;\r
+\r
+ // )\r
+ case 41:\r
+ $parenLevel--;\r
+ if( $parenLevel < 0 ){\r
+ return $this->fail("missing opening `(`");\r
+ }\r
+ continue;\r
+\r
+ // ;\r
+ case 59:\r
+ //if (!$parenLevel) { $this->emitChunk(); }\r
+ continue;\r
+\r
+ // {\r
+ case 123:\r
+ $level++;\r
+ $lastOpening = $this->parserCurrentIndex;\r
+ continue;\r
+\r
+ // }\r
+ case 125:\r
+ $level--;\r
+ if( $level < 0 ){\r
+ return $this->fail("missing opening `{`");\r
+\r
+ }\r
+ //if (!$level && !$parenLevel) { $this->emitChunk(); }\r
+ continue;\r
+ // \\r
+ case 92:\r
+ if ($this->parserCurrentIndex < $this->input_len - 1) { $this->parserCurrentIndex++; continue; }\r
+ return $this->fail("unescaped `\\`");\r
+\r
+ // ", ' and `\r
+ case 34:\r
+ case 39:\r
+ case 96:\r
+ $matched = 0;\r
+ $currentChunkStartIndex = $this->parserCurrentIndex;\r
+ for ($this->parserCurrentIndex = $this->parserCurrentIndex + 1; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++) {\r
+ $cc2 = $this->CharCode($this->parserCurrentIndex);\r
+ if ($cc2 > 96) { continue; }\r
+ if ($cc2 == $cc) { $matched = 1; break; }\r
+ if ($cc2 == 92) { // \\r
+ if ($this->parserCurrentIndex == $this->input_len - 1) {\r
+ return $this->fail("unescaped `\\`");\r
+ }\r
+ $this->parserCurrentIndex++;\r
+ }\r
+ }\r
+ if ($matched) { continue; }\r
+ return $this->fail("unmatched `" . chr($cc) . "`", $currentChunkStartIndex);\r
+\r
+ // /, check for comment\r
+ case 47:\r
+ if ($parenLevel || ($this->parserCurrentIndex == $this->input_len - 1)) { continue; }\r
+ $cc2 = $this->CharCode($this->parserCurrentIndex+1);\r
+ if ($cc2 == 47) {\r
+ // //, find lnfeed\r
+ for ($this->parserCurrentIndex = $this->parserCurrentIndex + 2; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++) {\r
+ $cc2 = $this->CharCode($this->parserCurrentIndex);\r
+ if (($cc2 <= 13) && (($cc2 == 10) || ($cc2 == 13))) { break; }\r
+ }\r
+ } else if ($cc2 == 42) {\r
+ // /*, find */\r
+ $lastMultiComment = $currentChunkStartIndex = $this->parserCurrentIndex;\r
+ for ($this->parserCurrentIndex = $this->parserCurrentIndex + 2; $this->parserCurrentIndex < $this->input_len - 1; $this->parserCurrentIndex++) {\r
+ $cc2 = $this->CharCode($this->parserCurrentIndex);\r
+ if ($cc2 == 125) { $lastMultiCommentEndBrace = $this->parserCurrentIndex; }\r
+ if ($cc2 != 42) { continue; }\r
+ if ($this->CharCode($this->parserCurrentIndex+1) == 47) { break; }\r
+ }\r
+ if ($this->parserCurrentIndex == $this->input_len - 1) {\r
+ return $this->fail("missing closing `*/`", $currentChunkStartIndex);\r
+ }\r
+ }\r
+ continue;\r
+\r
+ // *, check for unmatched */\r
+ case 42:\r
+ if (($this->parserCurrentIndex < $this->input_len - 1) && ($this->CharCode($this->parserCurrentIndex+1) == 47)) {\r
+ return $this->fail("unmatched `/*`");\r
+ }\r
+ continue;\r
+ }\r
+ }\r
+\r
+ if( $level !== 0 ){\r
+ if( ($lastMultiComment > $lastOpening) && ($lastMultiCommentEndBrace > $lastMultiComment) ){\r
+ return $this->fail("missing closing `}` or `*/`", $lastOpening);\r
+ } else {\r
+ return $this->fail("missing closing `}`", $lastOpening);\r
+ }\r
+ } else if ( $parenLevel !== 0 ){\r
+ return $this->fail("missing closing `)`", $lastParen);\r
+ }\r
+\r
+\r
+ //chunk didn't fail\r
+\r
+\r
+ //$this->emitChunk(true);\r
+ }\r
+\r
+ public function CharCode($pos){\r
+ return ord($this->input[$pos]);\r
+ }\r
+\r
+\r
+ public function fail( $msg, $index = null ){\r
+\r
+ if( !$index ){\r
+ $this->index = $this->parserCurrentIndex;\r
+ }else{\r
+ $this->index = $index;\r
+ }\r
+ $this->message = 'ParseError: '.$msg;\r
+ }\r
+\r
+\r
+ /*\r
+ function emitChunk( $force = false ){\r
+ $len = $this->parserCurrentIndex - $this->emitFrom;\r
+ if ((($len < 512) && !$force) || !$len) {\r
+ return;\r
+ }\r
+ $chunks[] = substr($this->input, $this->emitFrom, $this->parserCurrentIndex + 1 - $this->emitFrom );\r
+ $this->emitFrom = $this->parserCurrentIndex + 1;\r
+ }\r
+ */\r
+\r
+}\r