Guest User

Untitled

a guest
Sep 9th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * Copyright (c) 2009 Tim Whitlock, http://timwhitlock.info
  4.  * Permission is hereby granted, free of charge, to any person obtaining
  5.  * a copy of this software and associated documentation files (the
  6.  * "Software"), to deal in the Software without restriction, including
  7.  * without limitation the rights to use, copy, modify, merge, publish,
  8.  * distribute, sublicense, and/or sell copies of the Software, and to
  9.  * permit persons to whom the Software is furnished to do so, subject to
  10.  * the following conditions:
  11.  * The above copyright notice and this permission notice shall be
  12.  * included in all copies or substantial portions of the Software.
  13.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16.  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17.  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18.  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19.  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20.  * --
  21.  * This library was built by the PLUG framework.
  22.  * For original source code download the devel package from http://web.2point1.com/tag/jparser
  23.  * Sat, 14 Nov 2009 17:19:32 +0000
  24.  */
  25. define('P_EPSILON', -1);
  26. define('P_EOF', -2);
  27. define('P_GOAL', -3);
  28.  
  29. abstract class Lex
  30. {
  31.     private static $singletons = array();
  32.     protected $i;
  33.     protected $names = array(P_EPSILON => 'P_EPSILON', P_EOF => 'P_EOF', P_GOAL => 'P_GOAL',);
  34.     protected $literals = array();
  35.  
  36.     function __construct($i = null)
  37.     {
  38.         if (!is_null($i)) {
  39.             $this->i = (int)$i;
  40.         }
  41.     }
  42.  
  43.     function destroy()
  44.     {
  45.         $class = get_class($this);
  46.         unset(self::$singletons[$class]);
  47.         unset($this->names);
  48.         unset($this->literals);
  49.     }
  50.  
  51.     static function get($class)
  52.     {
  53.         if (!isset(self::$singletons[$class])) {
  54.             self::$singletons[$class] = new $class;
  55.         }
  56.  
  57.         return self::$singletons[$class];
  58.     }
  59.  
  60.     function defined($c)
  61.     {
  62.         if (isset($this->literals[$c])) {
  63.             return true;
  64.         }
  65.         if (!defined($c)) {
  66.             return false;
  67.         }
  68.         $i = constant($c);
  69.  
  70.         return isset($this->names[$i]) && $this->names[$i] === $c;
  71.     }
  72.  
  73.     function name($i)
  74.     {
  75.         if (is_int($i)) {
  76.             if (!isset($this->names[$i])) {
  77.                 trigger_error("symbol " . var_export($i, 1) . " is unknown in " . get_class($this), E_USER_NOTICE);
  78.  
  79.                 return 'UNKNOWN';
  80.             } else {
  81.                 return $this->names[$i];
  82.             }
  83.         } else {
  84.             if (!isset($this->literals[$i])) {
  85.                 trigger_error("literal symbol " . var_export($i, 1) . " is unknown in " . get_class($this),
  86.                     E_USER_NOTICE);
  87.             }
  88.         }
  89.  
  90.         return $i;
  91.     }
  92.  
  93.     function implode($s, array $a)
  94.     {
  95.         $b = array();
  96.         foreach ($a as $t) {
  97.             $b[] = $this->name($t);
  98.         }
  99.  
  100.         return implode($s, $b);
  101.     }
  102.  
  103.     function dump()
  104.     {
  105.         asort($this->names, SORT_STRING);
  106.         $t = max(2, strlen((string)$this->i));
  107.         foreach ($this->names as $i => $n) {
  108.             $i = str_pad($i, $t, ' ', STR_PAD_LEFT);
  109.             echo "$i => $n \n";
  110.         }
  111.     }
  112. }
  113.  
  114. define('J_FUNCTION', 1);
  115. define('J_IDENTIFIER', 2);
  116. define('J_VAR', 3);
  117. define('J_IF', 4);
  118. define('J_ELSE', 5);
  119. define('J_DO', 6);
  120. define('J_WHILE', 7);
  121. define('J_FOR', 8);
  122. define('J_IN', 9);
  123. define('J_CONTINUE', 10);
  124. define('J_BREAK', 11);
  125. define('J_RETURN', 12);
  126. define('J_WITH', 13);
  127. define('J_SWITCH', 14);
  128. define('J_CASE', 15);
  129. define('J_DEFAULT', 16);
  130. define('J_THROW', 17);
  131. define('J_TRY', 18);
  132. define('J_CATCH', 19);
  133. define('J_FINALLY', 20);
  134. define('J_THIS', 21);
  135. define('J_STRING_LITERAL', 22);
  136. define('J_NUMERIC_LITERAL', 23);
  137. define('J_TRUE', 24);
  138. define('J_FALSE', 25);
  139. define('J_NULL', 26);
  140. define('J_REGEX', 27);
  141. define('J_NEW', 28);
  142. define('J_DELETE', 29);
  143. define('J_VOID', 30);
  144. define('J_TYPEOF', 31);
  145. define('J_INSTANCEOF', 32);
  146. define('J_COMMENT', 33);
  147. define('J_WHITESPACE', 34);
  148. define('J_LINE_TERMINATOR', 35);
  149. define('J_ABSTRACT', 36);
  150. define('J_ENUM', 37);
  151. define('J_INT', 38);
  152. define('J_SHORT', 39);
  153. define('J_BOOLEAN', 40);
  154. define('J_EXPORT', 41);
  155. define('J_INTERFACE', 42);
  156. define('J_STATIC', 43);
  157. define('J_BYTE', 44);
  158. define('J_EXTENDS', 45);
  159. define('J_LONG', 46);
  160. define('J_SUPER', 47);
  161. define('J_CHAR', 48);
  162. define('J_FINAL', 49);
  163. define('J_NATIVE', 50);
  164. define('J_SYNCHRONIZED', 51);
  165. define('J_CLASS', 52);
  166. define('J_FLOAT', 53);
  167. define('J_PACKAGE', 54);
  168. define('J_THROWS', 55);
  169. define('J_CONST', 56);
  170. define('J_GOTO', 57);
  171. define('J_PRIVATE', 58);
  172. define('J_TRANSIENT', 59);
  173. define('J_DEBUGGER', 60);
  174. define('J_IMPLEMENTS', 61);
  175. define('J_PROTECTED', 62);
  176. define('J_VOLATILE', 63);
  177. define('J_DOUBLE', 64);
  178. define('J_IMPORT', 65);
  179. define('J_PUBLIC', 66);
  180. define('J_PROGRAM', 67);
  181. define('J_ELEMENTS', 68);
  182. define('J_ELEMENT', 69);
  183. define('J_STATEMENT', 70);
  184. define('J_FUNC_DECL', 71);
  185. define('J_PARAM_LIST', 72);
  186. define('J_FUNC_BODY', 73);
  187. define('J_FUNC_EXPR', 74);
  188. define('J_BLOCK', 75);
  189. define('J_VAR_STATEMENT', 76);
  190. define('J_EMPTY_STATEMENT', 77);
  191. define('J_EXPR_STATEMENT', 78);
  192. define('J_IF_STATEMENT', 79);
  193. define('J_ITER_STATEMENT', 80);
  194. define('J_CONT_STATEMENT', 81);
  195. define('J_BREAK_STATEMENT', 82);
  196. define('J_RETURN_STATEMENT', 83);
  197. define('J_WITH_STATEMENT', 84);
  198. define('J_LABELLED_STATEMENT', 85);
  199. define('J_SWITCH_STATEMENT', 86);
  200. define('J_THROW_STATEMENT', 87);
  201. define('J_TRY_STATEMENT', 88);
  202. define('J_STATEMENT_LIST', 89);
  203. define('J_VAR_DECL_LIST', 90);
  204. define('J_VAR_DECL', 91);
  205. define('J_VAR_DECL_LIST_NO_IN', 92);
  206. define('J_VAR_DECL_NO_IN', 93);
  207. define('J_INITIALIZER', 94);
  208. define('J_INITIALIZER_NO_IN', 95);
  209. define('J_ASSIGN_EXPR', 96);
  210. define('J_ASSIGN_EXPR_NO_IN', 97);
  211. define('J_EXPR', 98);
  212. define('J_EXPR_NO_IN', 99);
  213. define('J_LHS_EXPR', 100);
  214. define('J_CASE_BLOCK', 101);
  215. define('J_CASE_CLAUSES', 102);
  216. define('J_CASE_DEFAULT', 103);
  217. define('J_CASE_CLAUSE', 104);
  218. define('J_CATCH_CLAUSE', 105);
  219. define('J_FINALLY_CLAUSE', 106);
  220. define('J_PRIMARY_EXPR', 107);
  221. define('J_ARRAY_LITERAL', 108);
  222. define('J_OBJECT_LITERAL', 109);
  223. define('J_ELISION', 110);
  224. define('J_ELEMENT_LIST', 111);
  225. define('J_PROP_LIST', 112);
  226. define('J_PROP_NAME', 113);
  227. define('J_MEMBER_EXPR', 114);
  228. define('J_ARGS', 115);
  229. define('J_NEW_EXPR', 116);
  230. define('J_CALL_EXPR', 117);
  231. define('J_ARG_LIST', 118);
  232. define('J_POSTFIX_EXPR', 119);
  233. define('J_UNARY_EXPR', 120);
  234. define('J_MULT_EXPR', 121);
  235. define('J_ADD_EXPR', 122);
  236. define('J_SHIFT_EXPR', 123);
  237. define('J_REL_EXPR', 124);
  238. define('J_REL_EXPR_NO_IN', 125);
  239. define('J_EQ_EXPR', 126);
  240. define('J_EQ_EXPR_NO_IN', 127);
  241. define('J_BIT_AND_EXPR', 128);
  242. define('J_BIT_AND_EXPR_NO_IN', 129);
  243. define('J_BIT_XOR_EXPR', 130);
  244. define('J_BIT_XOR_EXPR_NO_IN', 131);
  245. define('J_BIT_OR_EXPR', 132);
  246. define('J_BIT_OR_EXPR_NO_IN', 133);
  247. define('J_LOG_AND_EXPR', 134);
  248. define('J_LOG_AND_EXPR_NO_IN', 135);
  249. define('J_LOG_OR_EXPR', 136);
  250. define('J_LOG_OR_EXPR_NO_IN', 137);
  251. define('J_COND_EXPR', 138);
  252. define('J_COND_EXPR_NO_IN', 139);
  253. define('J_ASSIGN_OP', 140);
  254. define('J_IGNORE', 141);
  255. define('J_RESERVED', 142);
  256.  
  257. class JLexBase extends Lex
  258. {
  259.     protected $i = 142;
  260.     protected $names = array(
  261.         -3 => 'P_GOAL',
  262.         -2 => 'P_EOF',
  263.         -1 => 'P_EPSILON',
  264.         1 => 'J_FUNCTION',
  265.         2 => 'J_IDENTIFIER',
  266.         3 => 'J_VAR',
  267.         4 => 'J_IF',
  268.         5 => 'J_ELSE',
  269.         6 => 'J_DO',
  270.         7 => 'J_WHILE',
  271.         8 => 'J_FOR',
  272.         9 => 'J_IN',
  273.         10 => 'J_CONTINUE',
  274.         11 => 'J_BREAK',
  275.         12 => 'J_RETURN',
  276.         13 => 'J_WITH',
  277.         14 => 'J_SWITCH',
  278.         15 => 'J_CASE',
  279.         16 => 'J_DEFAULT',
  280.         17 => 'J_THROW',
  281.         18 => 'J_TRY',
  282.         19 => 'J_CATCH',
  283.         20 => 'J_FINALLY',
  284.         21 => 'J_THIS',
  285.         22 => 'J_STRING_LITERAL',
  286.         23 => 'J_NUMERIC_LITERAL',
  287.         24 => 'J_TRUE',
  288.         25 => 'J_FALSE',
  289.         26 => 'J_NULL',
  290.         27 => 'J_REGEX',
  291.         28 => 'J_NEW',
  292.         29 => 'J_DELETE',
  293.         30 => 'J_VOID',
  294.         31 => 'J_TYPEOF',
  295.         32 => 'J_INSTANCEOF',
  296.         33 => 'J_COMMENT',
  297.         34 => 'J_WHITESPACE',
  298.         35 => 'J_LINE_TERMINATOR',
  299.         36 => 'J_ABSTRACT',
  300.         37 => 'J_ENUM',
  301.         38 => 'J_INT',
  302.         39 => 'J_SHORT',
  303.         40 => 'J_BOOLEAN',
  304.         41 => 'J_EXPORT',
  305.         42 => 'J_INTERFACE',
  306.         43 => 'J_STATIC',
  307.         44 => 'J_BYTE',
  308.         45 => 'J_EXTENDS',
  309.         46 => 'J_LONG',
  310.         47 => 'J_SUPER',
  311.         48 => 'J_CHAR',
  312.         49 => 'J_FINAL',
  313.         50 => 'J_NATIVE',
  314.         51 => 'J_SYNCHRONIZED',
  315.         52 => 'J_CLASS',
  316.         53 => 'J_FLOAT',
  317.         54 => 'J_PACKAGE',
  318.         55 => 'J_THROWS',
  319.         56 => 'J_CONST',
  320.         57 => 'J_GOTO',
  321.         58 => 'J_PRIVATE',
  322.         59 => 'J_TRANSIENT',
  323.         60 => 'J_DEBUGGER',
  324.         61 => 'J_IMPLEMENTS',
  325.         62 => 'J_PROTECTED',
  326.         63 => 'J_VOLATILE',
  327.         64 => 'J_DOUBLE',
  328.         65 => 'J_IMPORT',
  329.         66 => 'J_PUBLIC',
  330.         67 => 'J_PROGRAM',
  331.         68 => 'J_ELEMENTS',
  332.         69 => 'J_ELEMENT',
  333.         70 => 'J_STATEMENT',
  334.         71 => 'J_FUNC_DECL',
  335.         72 => 'J_PARAM_LIST',
  336.         73 => 'J_FUNC_BODY',
  337.         74 => 'J_FUNC_EXPR',
  338.         75 => 'J_BLOCK',
  339.         76 => 'J_VAR_STATEMENT',
  340.         77 => 'J_EMPTY_STATEMENT',
  341.         78 => 'J_EXPR_STATEMENT',
  342.         79 => 'J_IF_STATEMENT',
  343.         80 => 'J_ITER_STATEMENT',
  344.         81 => 'J_CONT_STATEMENT',
  345.         82 => 'J_BREAK_STATEMENT',
  346.         83 => 'J_RETURN_STATEMENT',
  347.         84 => 'J_WITH_STATEMENT',
  348.         85 => 'J_LABELLED_STATEMENT',
  349.         86 => 'J_SWITCH_STATEMENT',
  350.         87 => 'J_THROW_STATEMENT',
  351.         88 => 'J_TRY_STATEMENT',
  352.         89 => 'J_STATEMENT_LIST',
  353.         90 => 'J_VAR_DECL_LIST',
  354.         91 => 'J_VAR_DECL',
  355.         92 => 'J_VAR_DECL_LIST_NO_IN',
  356.         93 => 'J_VAR_DECL_NO_IN',
  357.         94 => 'J_INITIALIZER',
  358.         95 => 'J_INITIALIZER_NO_IN',
  359.         96 => 'J_ASSIGN_EXPR',
  360.         97 => 'J_ASSIGN_EXPR_NO_IN',
  361.         98 => 'J_EXPR',
  362.         99 => 'J_EXPR_NO_IN',
  363.         100 => 'J_LHS_EXPR',
  364.         101 => 'J_CASE_BLOCK',
  365.         102 => 'J_CASE_CLAUSES',
  366.         103 => 'J_CASE_DEFAULT',
  367.         104 => 'J_CASE_CLAUSE',
  368.         105 => 'J_CATCH_CLAUSE',
  369.         106 => 'J_FINALLY_CLAUSE',
  370.         107 => 'J_PRIMARY_EXPR',
  371.         108 => 'J_ARRAY_LITERAL',
  372.         109 => 'J_OBJECT_LITERAL',
  373.         110 => 'J_ELISION',
  374.         111 => 'J_ELEMENT_LIST',
  375.         112 => 'J_PROP_LIST',
  376.         113 => 'J_PROP_NAME',
  377.         114 => 'J_MEMBER_EXPR',
  378.         115 => 'J_ARGS',
  379.         116 => 'J_NEW_EXPR',
  380.         117 => 'J_CALL_EXPR',
  381.         118 => 'J_ARG_LIST',
  382.         119 => 'J_POSTFIX_EXPR',
  383.         120 => 'J_UNARY_EXPR',
  384.         121 => 'J_MULT_EXPR',
  385.         122 => 'J_ADD_EXPR',
  386.         123 => 'J_SHIFT_EXPR',
  387.         124 => 'J_REL_EXPR',
  388.         125 => 'J_REL_EXPR_NO_IN',
  389.         126 => 'J_EQ_EXPR',
  390.         127 => 'J_EQ_EXPR_NO_IN',
  391.         128 => 'J_BIT_AND_EXPR',
  392.         129 => 'J_BIT_AND_EXPR_NO_IN',
  393.         130 => 'J_BIT_XOR_EXPR',
  394.         131 => 'J_BIT_XOR_EXPR_NO_IN',
  395.         132 => 'J_BIT_OR_EXPR',
  396.         133 => 'J_BIT_OR_EXPR_NO_IN',
  397.         134 => 'J_LOG_AND_EXPR',
  398.         135 => 'J_LOG_AND_EXPR_NO_IN',
  399.         136 => 'J_LOG_OR_EXPR',
  400.         137 => 'J_LOG_OR_EXPR_NO_IN',
  401.         138 => 'J_COND_EXPR',
  402.         139 => 'J_COND_EXPR_NO_IN',
  403.         140 => 'J_ASSIGN_OP',
  404.         141 => 'J_IGNORE',
  405.         142 => 'J_RESERVED',
  406.     );
  407.     protected $literals = array(
  408.         '(' => 1,
  409.         ')' => 1,
  410.         '{' => 1,
  411.         '}' => 1,
  412.         ',' => 1,
  413.         ';' => 1,
  414.         '=' => 1,
  415.         ':' => 1,
  416.         '[' => 1,
  417.         ']' => 1,
  418.         '.' => 1,
  419.         '++' => 2,
  420.         '--' => 2,
  421.         '+' => 1,
  422.         '-' => 1,
  423.         '~' => 1,
  424.         '!' => 1,
  425.         '*' => 1,
  426.         '/' => 1,
  427.         '%' => 1,
  428.         '<<' => 2,
  429.         '>>' => 2,
  430.         '>>>' => 3,
  431.         '<' => 1,
  432.         '>' => 1,
  433.         '<=' => 2,
  434.         '>=' => 2,
  435.         '==' => 2,
  436.         '!=' => 2,
  437.         '===' => 3,
  438.         '!==' => 3,
  439.         '&' => 1,
  440.         '^' => 1,
  441.         '|' => 1,
  442.         '&&' => 2,
  443.         '||' => 2,
  444.         '?' => 1,
  445.         '*=' => 2,
  446.         '/=' => 2,
  447.         '%=' => 2,
  448.         '+=' => 2,
  449.         '-=' => 2,
  450.         '<<=' => 3,
  451.         '>>=' => 3,
  452.         '>>>=' => 4,
  453.         '&=' => 2,
  454.         '^=' => 2,
  455.         '|=' => 2,
  456.     );
  457. }
  458.  
  459. class JLex extends JLexBase
  460. {
  461.     protected $words = array(
  462.         'true' => J_TRUE,
  463.         'false' => J_FALSE,
  464.         'null' => J_NULL,
  465.         'break' => J_BREAK,
  466.         'else' => J_ELSE,
  467.         'new' => J_NEW,
  468.         'var' => J_VAR,
  469.         'case' => J_CASE,
  470.         'finally' => J_FINALLY,
  471.         'return' => J_RETURN,
  472.         'void' => J_VOID,
  473.         'catch' => J_CATCH,
  474.         'for' => J_FOR,
  475.         'switch' => J_SWITCH,
  476.         'while' => J_WHILE,
  477.         'continue' => J_CONTINUE,
  478.         'function' => J_FUNCTION,
  479.         'this' => J_THIS,
  480.         'with' => J_WITH,
  481.         'default' => J_DEFAULT,
  482.         'if' => J_IF,
  483.         'throw' => J_THROW,
  484.         'delete' => J_DELETE,
  485.         'in' => J_IN,
  486.         'try' => J_TRY,
  487.         'do' => J_DO,
  488.         'instanceof' => J_INSTANCEOF,
  489.         'typeof' => J_TYPEOF,
  490.         'abstract' => J_ABSTRACT,
  491.         'enum' => J_ENUM,
  492.         'int' => J_INT,
  493.         'short' => J_SHORT,
  494.         'boolean' => J_BOOLEAN,
  495.         'export' => J_EXPORT,
  496.         'interface' => J_INTERFACE,
  497.         'static' => J_STATIC,
  498.         'byte' => J_BYTE,
  499.         'extends' => J_EXTENDS,
  500.         'long' => J_LONG,
  501.         'super' => J_SUPER,
  502.         'char' => J_CHAR,
  503.         'final' => J_FINAL,
  504.         'native' => J_NATIVE,
  505.         'synchronized' => J_SYNCHRONIZED,
  506.         'class' => J_CLASS,
  507.         'float' => J_FLOAT,
  508.         'package' => J_PACKAGE,
  509.         'throws' => J_THROWS,
  510.         'const' => J_CONST,
  511.         'goto' => J_GOTO,
  512.         'private' => J_PRIVATE,
  513.         'transient' => J_TRANSIENT,
  514.         'debugger' => J_DEBUGGER,
  515.         'implements' => J_IMPLEMENTS,
  516.         'protected' => J_PROTECTED,
  517.         'volatile' => J_VOLATILE,
  518.         'double' => J_DOUBLE,
  519.         'import' => J_IMPORT,
  520.         'public' => J_PUBLIC,
  521.     );
  522.  
  523.     function is_word($s)
  524.     {
  525.         return isset($this->words[$s]) ? $this->words[$s] : false;
  526.     }
  527.  
  528.     static function singleton()
  529.     {
  530.         return Lex::get(__CLASS__);
  531.     }
  532. }
  533.  
  534. abstract class JTokenizerBase
  535. {
  536.     private $line;
  537.     private $col;
  538.     private $divmode;
  539.     private $src;
  540.     private $whitespace;
  541.     private $unicode;
  542.     private $regRegex;
  543.     private $regDQuote;
  544.     private $regSQuote;
  545.     private $regWord;
  546.     private $regWhite;
  547.     private $regBreak;
  548.     private $regJunk;
  549.     private $regLines;
  550.     private $regNumber;
  551.     private $regComment;
  552.     private $regCommentMulti;
  553.     protected $regPunc;
  554.     protected $Lex;
  555.  
  556.     function __construct($whitespace, $unicode)
  557.     {
  558.         $this->whitespace = $whitespace;
  559.         $this->unicode = $unicode;
  560.         if ($this->unicode) {
  561.             $this->regRegex = '!^/(?:\\\\.|[^\r\n\p{Zl}\p{Zp}/\\\\])+/[gi]*!u';
  562.             $this->regDQuote = '/^"(?:\\\\(?:.|\r\n)|[^\r\n\p{Zl}\p{Zp}"\\\\])*"/su';
  563.             $this->regSQuote = "/^'(?:\\\\(?:.|\r\n)|[^\r\n\p{Zl}\p{Zp}'\\\\])*'/su";
  564.             $this->regWord = '/^(?:\\\\u[0-9A-F]{4,4}|[\$_\pL\p{Nl}])(?:\\\\u[0-9A-F]{4,4}|[\$_\pL\pN\p{Mn}\p{Mc}\p{Pc}])*/ui';
  565.             $this->regWhite = '/^[\x20\x09\x0B\x0C\xA0\p{Zs}]+/u';
  566.             $this->regBreak = '/^[\r\n\p{Zl}\p{Zp}]+/u';
  567.             $this->regJunk = '/^./u';
  568.             $this->regLines = '/(\r\n|[\r\n\p{Zl}\p{Zp}])/u';
  569.         } else {
  570.             $this->regRegex = '!^/(?:\\\\.|[^\r\n/\\\\])+/[gi]*!';
  571.             $this->regDQuote = '/^"(?:\\\\(?:.|\r\n)|[^\r\n"\\\\])*"/s';
  572.             $this->regSQuote = "/^'(?:\\\\(?:.|\r\n)|[^\r\n'\\\\])*'/s";
  573.             $this->regWord = '/^[\$_A-Z][\$_A-Z0-9]*/i';
  574.             $this->regWhite = '/^[\x20\x09\x0B\x0C\xA0]+/';
  575.             $this->regBreak = '/^[\r\n]+/';
  576.             $this->regJunk = '/^./';
  577.             $this->regLines = '/(\r\n|\r|\n)/';
  578.         }
  579.         $this->regNumber = '/^(?:0x[A-F0-9]+|\d*\.\d+(?:E(?:\+|\-)?\d+)?|\d+)/i';
  580.         $this->regComment = '/^\/\/.*/';
  581.         $this->regCommentMulti = '/^\/\*.*\*\//Us';
  582.     }
  583.  
  584.     function init($src)
  585.     {
  586.         $this->src = $src;
  587.         $this->line = 1;
  588.         $this->col = 1;
  589.         $this->divmode = false;
  590.     }
  591.  
  592.     function get_all_tokens($src)
  593.     {
  594.         $this->init($src);
  595.         $tokens = array();
  596.         while ($this->src) {
  597.             $token = $this->get_next_token() and $tokens[] = $token;
  598.         }
  599.  
  600.         return $tokens;
  601.     }
  602.  
  603.     function get_next_token()
  604.     {
  605.         $c = $this->src{0};
  606.         if ($c === '"') {
  607.             if (!preg_match($this->regDQuote, $this->src, $r)) {
  608.                 trigger_error("Unterminated string constant on line $this->line", E_USER_NOTICE);
  609.                 $s = $t = '"';
  610.             } else {
  611.                 $s = $r[0];
  612.                 $t = J_STRING_LITERAL;
  613.             }
  614.             $this->divmode = true;
  615.         } else {
  616.             if ($c === "'") {
  617.                 if (!preg_match($this->regSQuote, $this->src, $r)) {
  618.                     trigger_error("Unterminated string constant on line $this->line", E_USER_NOTICE);
  619.                     $s = $t = "'";
  620.                 } else {
  621.                     $s = $r[0];
  622.                     $t = J_STRING_LITERAL;
  623.                 }
  624.                 $this->divmode = true;
  625.             } else {
  626.                 if ($c === '/') {
  627.                     if ($this->src{1} === '/' && preg_match($this->regComment, $this->src, $r)) {
  628.                         $t = $this->whitespace ? J_COMMENT : false;
  629.                         $s = $r[0];
  630.                     } else {
  631.                         if ($this->src{1} === '*' && preg_match($this->regCommentMulti, $this->src, $r)) {
  632.                             $s = $r[0];
  633.                             if ($this->whitespace) {
  634.                                 $t = J_COMMENT;
  635.                             } else {
  636.                                 $breaks = preg_match($this->regLines, $s, $r);
  637.                                 $t = $breaks ? J_LINE_TERMINATOR : false;
  638.                             }
  639.                         } else {
  640.                             if (!$this->divmode) {
  641.                                 if (!preg_match($this->regRegex, $this->src, $r)) {
  642.                                     trigger_error("Bad regular expression literal on line $this->line", E_USER_NOTICE);
  643.                                     $s = $t = '/';
  644.                                     $this->divmode = false;
  645.                                 } else {
  646.                                     $s = $r[0];
  647.                                     $t = J_REGEX;
  648.                                     $this->divmode = true;
  649.                                 }
  650.                             } else {
  651.                                 if ($this->src{1} === '=') {
  652.                                     $s = $t = '/=';
  653.                                     $this->divmode = false;
  654.                                 } else {
  655.                                     $s = $t = '/';
  656.                                     $this->divmode = false;
  657.                                 }
  658.                             }
  659.                         }
  660.                     }
  661.                 } else {
  662.                     if (preg_match($this->regBreak, $this->src, $r)) {
  663.                         $t = J_LINE_TERMINATOR;
  664.                         $s = $r[0];
  665.                         $this->divmode = false;
  666.                     } else {
  667.                         if (preg_match($this->regWhite, $this->src, $r)) {
  668.                             $t = $this->whitespace ? J_WHITESPACE : false;
  669.                             $s = $r[0];
  670.                         } else {
  671.                             if (preg_match($this->regNumber, $this->src, $r)) {
  672.                                 $t = J_NUMERIC_LITERAL;
  673.                                 $s = $r[0];
  674.                                 $this->divmode = true;
  675.                             } else {
  676.                                 if (preg_match($this->regWord, $this->src, $r)) {
  677.                                     $s = $r[0];
  678.                                     $t = $this->Lex->is_word($s) or $t = J_IDENTIFIER;
  679.                                     switch ($t) {
  680.                                         case J_IDENTIFIER;
  681.                                             $this->divmode = true;
  682.                                             break;
  683.                                         default:
  684.                                             $this->divmode = null;
  685.                                     }
  686.                                 } else {
  687.                                     if (preg_match($this->regPunc, $this->src, $r)) {
  688.                                         $s = $t = $r[0];
  689.                                         switch ($t) {
  690.                                             case ']':
  691.                                             case ')':
  692.                                                 $this->divmode = true;
  693.                                                 break;
  694.                                             default:
  695.                                                 $this->divmode = false;
  696.                                         }
  697.                                     } else {
  698.                                         preg_match($this->regJunk, $this->src, $r);
  699.                                         $s = $t = $r[0];
  700.                                         trigger_error("Junk on line $this->line, $s", E_USER_NOTICE);
  701.                                     }
  702.                                 }
  703.                             }
  704.                         }
  705.                     }
  706.                 }
  707.             }
  708.         }
  709.         $len = strlen($s);
  710.         if ($len === 0) {
  711.             throw new Exception('Failed to extract anything');
  712.         }
  713.         if ($t !== false) {
  714.             $token = array($t, $s, $this->line, $this->col);
  715.         }
  716.         $this->src = substr($this->src, $len);
  717.         if ($t === J_LINE_TERMINATOR || $t === J_COMMENT) {
  718.             $this->line += preg_match_all($this->regLines, $s, $r);
  719.             $cbreak = end($r[0]);
  720.             $this->col = $len - strrpos($s, $cbreak);
  721.         } else {
  722.             $this->col += $len;
  723.         }
  724.  
  725.         return isset($token) ? $token : null;
  726.     }
  727. }
  728.  
  729. class JTokenizer extends JTokenizerBase
  730. {
  731.     protected $regPunc = '/(?:\>\>\>\=|\>\>\>|\<\<\=|\>\>\=|\!\=\=|\=\=\=|&&|\<\<|\>\>|\|\||\*\=|\|\=|\^\=|&\=|%\=|-\=|\+\+|\+\=|--|\=\=|\>\=|\!\=|\<\=|;|,|\<|\>|\.|\]|\}|\(|\)|\[|\=|\:|\||&|-|\{|\^|\!|\?|\*|%|~|\+)/';
  732.  
  733.     function __construct($whitespace, $unicode)
  734.     {
  735.         parent::__construct($whitespace, $unicode);
  736.         $this->Lex = Lex::get('JLex');
  737.     }
  738. }
  739.  
  740. function j_token_get_all($src, $whitespace = true, $unicode = true)
  741. {
  742.     $Tokenizer = new JTokenizer($whitespace, $unicode);
  743.  
  744.     return $Tokenizer->get_all_tokens($src);
  745. }
  746.  
  747. function j_token_name($t)
  748. {
  749.     $Lex = Lex::get('JLex');
  750.  
  751.     return $Lex->name($t);
  752. }
Advertisement
Add Comment
Please, Sign In to add comment