1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 | 1 10 10 10 10 10 1 10 2 10 10 10 5 5 3 3 2 2 2 13 13 26 3 3 23 4 4 19 5 5 14 14 3 3 3 3 3 11 1 1 11 4 4 4 4 7 6 1 1 13 13 13 4 5 5 5 5 5 3 2 2 4 4 4 4 4 4 4 4 4 4 20 20 14 14 18 5 5 2 2 3 1 18 2 2 1 1 18 18 17 20 20 20 20 3 3 3 3 3 3 3 1 3 3 3 3 3 4 4 1 1 3 3 1 1 1 1 2 2 2 2 2 1 3 2 3 2 2 2 2 2 2 1 2 1 2 2 2 5 5 5 5 1 5 3 3 11 8 8 3 3 2 1 5 8 8 8 8 5 5 5 5 3 8 2 6 4 4 4 3 4 4 4 4 2 2 | /*! * Copyright (C) 2017 Glayzzle (BSD3 License) * @authors https://github.com/glayzzle/php-parser/graphs/contributors * @url http://glayzzle.com */ module.exports = { /** * reading a class * ```ebnf * class ::= class_scope? T_CLASS T_STRING (T_EXTENDS NAMESPACE_NAME)? (T_IMPLEMENTS (NAMESPACE_NAME ',')* NAMESPACE_NAME)? '{' CLASS_BODY '}' * ``` */ read_class: function(flag) { var result = this.node('class'); this.expect(this.tok.T_CLASS); this.next().expect(this.tok.T_STRING); var propName = this.text() , propExtends = null , propImplements = null , body ; if (this.next().token == this.tok.T_EXTENDS) { propExtends = this.next().read_namespace_name(); } if (this.token == this.tok.T_IMPLEMENTS) { propImplements = this.next().read_name_list(); } this.expect('{'); body = this.nextWithComments().read_class_body(); return result( propName ,propExtends ,propImplements ,body ,flag ); } /** * Read the class visibility * ```ebnf * class_scope ::= (T_FINAL | T_ABSTRACT)? * ``` */ ,read_class_scope: function() { var result = this.token; if (result == this.tok.T_FINAL) { this.next(); return [0, 0, 2]; } else Eif (result == this.tok.T_ABSTRACT) { this.next(); return [0, 0, 1]; } return [0, 0, 0]; } /** * Reads a class body * ```ebnf * class_body ::= (member_flags? (T_VAR | T_STRING | T_FUNCTION))* * ``` */ ,read_class_body: function() { var result = []; while(this.token !== this.EOF && this.token !== '}') { if (this.token === this.tok.T_COMMENT) { result.push(this.read_comment()); continue; } if (this.token === this.tok.T_DOC_COMMENT) { result.push(this.read_doc_comment()); continue; } // check T_USE trait if (this.token === this.tok.T_USE) { result = result.concat( this.next().read_trait_use_statement() ); continue; } // read member flags var flags = this.read_member_flags(false); // check constant if (this.token === this.tok.T_CONST) { var constants = this.read_constant_list(flags); this.expect(';'); this.nextWithComments(); result = result.concat(constants); continue; } // jump over T_VAR then land on T_VARIABLE if (this.token === this.tok.T_VAR) { this.next().expect(this.tok.T_VARIABLE); flags[0] = flags[1] = 0; // public & non static var } if (this.token === this.tok.T_VARIABLE) { // reads a variable var variables = this.read_variable_list(flags); this.expect(';'); this.nextWithComments(); result = result.concat(variables); } else if (this.token === this.tok.T_FUNCTION) { // reads a function result.push(this.read_function(false, flags)); } else { // raise an error this.error([ this.tok.T_CONST, this.tok.T_VARIABLE, this.tok.T_FUNCTION ]); // ignore token this.next(); } } this.expect('}'); this.nextWithComments(); return result; } /** * Reads variable list * ```ebnf * variable_list ::= (variable_declaration ',')* variable_declaration * ``` */ ,read_variable_list: function(flags) { return this.read_list( /** * Reads a variable declaration * * ```ebnf * variable_declaration ::= T_VARIABLE '=' scalar * ``` */ function read_variable_declaration() { var result = this.node('property'); this.expect(this.tok.T_VARIABLE); var name = this.text().substring(1); // ignore $ this.next(); if (this.token === ';' || this.token === ',') { return result(name, null, flags); } else Eif(this.token === '=') { // https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L815 return result(name, this.next().read_expr(), flags); } else { this.expect([',', ';', '=']); return result(name, null, flags); } }, ',' ); } /** * Reads constant list * ```ebnf * constant_list ::= T_CONST (constant_declaration ',')* constant_declaration * ``` */ ,read_constant_list: function(flags) { Eif (this.expect(this.tok.T_CONST)) { this.next(); } return this.read_list( /** * Reads a constant declaration * * ```ebnf * constant_declaration ::= T_STRING '=' expr * ``` * @return {Constant} [:link:](AST.md#constant) */ function read_constant_declaration() { var result = this.node('classconstant'), name = null, value = null; Eif (this.expect(this.tok.T_STRING)) { name = this.text(); this.next(); } Eif (this.expect('=')) { value = this.next().read_expr(); } return result(name, value, flags); }, ',' ) ; } /** * Read member flags * @return array * 1st index : 0 => public, 1 => protected, 2 => private * 2nd index : 0 => instance member, 1 => static member * 3rd index : 0 => normal, 1 => abstract member, 2 => final member */ ,read_member_flags: function(asInterface) { var result = [-1, -1, -1]; if (this.is('T_MEMBER_FLAGS')) { var idx = 0, val = 0; do { switch(this.token) { case this.tok.T_PUBLIC: idx = 0; val = 0; break; case this.tok.T_PROTECTED: idx = 0; val = 1; break; case this.tok.T_PRIVATE: idx = 0; val = 2; break; case this.tok.T_STATIC: idx = 1; val = 1; break; case this.tok.T_ABSTRACT: idx = 2; val = 1; break; case this.tok.T_FINAL: idx = 2; val = 2; break; } if (asInterface) { Iif (idx == 0 && val == 2) { // an interface can't be private this.expect([this.tok.T_PUBLIC, this.tok.T_PROTECTED]); val = -1; } else if (idx == 2 && val == 1) { // an interface cant be abstract this.error(); val = -1; } } Iif (result[idx] !== -1) { // already defined flag this.error(); } else if (val !== -1) { result[idx] = val; } } while(this.next().is('T_MEMBER_FLAGS')); } if (result[0] == -1) result[0] = 0; if (result[1] == -1) result[1] = 0; if (result[2] == -1) result[2] = 0; return result; } /** * reading an interface * ```ebnf * interface ::= T_INTERFACE T_STRING (T_EXTENDS (NAMESPACE_NAME ',')* NAMESPACE_NAME)? '{' INTERFACE_BODY '}' * ``` */ ,read_interface: function() { var result = this.node('interface'), name = null, body = null, propExtends = null; Eif (this.expect(this.tok.T_INTERFACE)) { this.next(); } Eif (this.expect(this.tok.T_STRING)) { name = this.text(); this.next(); } if (this.token === this.tok.T_EXTENDS) { propExtends = this.next().read_name_list(); } Eif (this.expect('{')) { body = this.next().read_interface_body(); } return result(name, propExtends, body); } /** * Reads an interface body * ```ebnf * interface_body ::= (member_flags? (T_CONST | T_FUNCTION))* * ``` */ ,read_interface_body: function() { var result = []; while(this.token !== this.EOF && this.token !== '}') { Iif (this.token === this.tok.T_COMMENT) { result.push(this.read_comment()); continue; } if (this.token === this.tok.T_DOC_COMMENT) { result.push(this.read_doc_comment()); continue; } // read member flags var flags = this.read_member_flags(true); // check constant if (this.token == this.tok.T_CONST) { var constants = this.read_constant_list(flags); Eif (this.expect(';')) { this.nextWithComments(); } result = result.concat(constants); } // reads a function else Eif (this.token === this.tok.T_FUNCTION) { var method = this.read_function_declaration(2, flags); method.parseFlags(flags); result.push(method); if (this.expect(';')) { this.nextWithComments(); } } else { // raise an error this.error([ this.tok.T_CONST, this.tok.T_FUNCTION ]); this.next(); } } if (this.expect('}')) { this.next(); } return result; } /** * reading a trait * ```ebnf * trait ::= T_TRAIT T_STRING (T_EXTENDS (NAMESPACE_NAME ',')* NAMESPACE_NAME)? '{' FUNCTION* '}' * ``` */ ,read_trait: function(flag) { var result = this.node('trait'), propName = null, propExtends = null, propImplements = null, body = null; Eif (this.expect(this.tok.T_TRAIT)) { this.next(); } Eif (this.expect(this.tok.T_STRING)) { propName = this.text(); } if (this.next().token == this.tok.T_EXTENDS) { propExtends = this.next().read_namespace_name(); } if (this.token == this.tok.T_IMPLEMENTS) { propImplements = this.next().read_name_list(); } Eif (this.expect('{')) { body = this.next().read_class_body(); } return result( propName, propExtends, propImplements, body ); } /** * reading a use statement * ```ebnf * trait_use_statement ::= namespace_name (',' namespace_name)* ('{' trait_use_alias '}')? * ``` */ ,read_trait_use_statement: function() { // defines use statements var node = this.node('traituse'); var traits = [this.read_namespace_name()]; var adaptations = null; while(this.token === ',') { traits.push( this.next().read_namespace_name() ); } if (this.token === '{') { adaptations = []; // defines alias statements while(this.next().token !== this.EOF) { if (this.token === '}') break; adaptations.push(this.read_trait_use_alias()); this.expect(';'); } Eif (this.expect('}')) { this.nextWithComments(); } } else { if (this.expect(';')) { this.nextWithComments(); } } return node(traits, adaptations); } /** * Reading trait alias * ```ebnf * trait_use_alias ::= namespace_name ( T_DOUBLE_COLON T_STRING )? (T_INSTEADOF namespace_name) | (T_AS member_flags? T_STRING) * ``` */ ,read_trait_use_alias: function() { var node = this.node(); var trait = null; var method = this.read_namespace_name(); if (this.token === this.tok.T_DOUBLE_COLON) { Eif (this.next().expect(this.tok.T_STRING)) { trait = method; method = this.text(); this.next(); } } else { // convert identifier as string method = method.name; } // handle trait precedence if (this.token === this.tok.T_INSTEADOF) { return node( 'traitprecedence', trait, method, this.next().read_name_list() ); } // handle trait alias else if (this.token === this.tok.T_AS) { var flags = false; var alias = null; if (this.next().is('T_MEMBER_FLAGS')) { flags = this.read_member_flags(); } Eif (this.token === this.tok.T_STRING) { alias = this.text(); this.next(); } else if (flags === false) { // no visibility flags and no name => too bad this.expect(this.tok.T_STRING); } return node('traitalias', trait, method, alias, flags) } // handle errors this.expect([this.tok.T_AS, this.tok.T_INSTEADOF]); return node('traitalias', trait, method, null, null); } }; |