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 | 1 1 1 137 137 1 58 1 3170 3170 58 3170 3170 1974 1974 1974 31 31 8 31 31 31 1974 442 1974 1974 1974 1974 1974 31 31 20 1943 10 10 3 1974 1 86 86 86 1 | /*! * Copyright (C) 2017 Glayzzle (BSD3 License) * @authors https://github.com/glayzzle/php-parser/graphs/contributors * @url http://gla*yzzle.com */ var Location = require('./ast/location'); var Position = require('./ast/position'); /** * ## Class hierarchy * * - [Location](#location) * - [Position](#position) * - [Node](#node) * - [Identifier](#identifier) * - [TraitUse](#traituse) * - [TraitAlias](#traitalias) * - [TraitPrecedence](#traitprecedence) * - [Entry](#entry) * - [Case](#case) * - [Label](#label) * - [Doc](#doc) * - [Error](#error) * - [Expression](#expression) * - [Array](#array) * - [Variable](#variable) * - [Variadic](#variadic) * - [ConstRef](#constref) * - [Yield](#yield) * - [YieldFrom](#yieldfrom) * - [Lookup](#lookup) * - [PropertyLookup](#propertylookup) * - [StaticLookup](#staticlookup) * - [OffsetLookup](#offsetlookup) * - [Operation](#operation) * - [Pre](#pre) * - [Post](#post) * - [Bin](#bin) * - [Parenthesis](#parenthesis) * - [Unary](#unary) * - [Cast](#cast) * - [Literal](#literal) * - [Boolean](#boolean) * - [String](#string) * - [Number](#number) * - [Inline](#inline) * - [Magic](#magic) * - [Nowdoc](#nowdoc) * - [Encapsed](#encapsed) * - [Statement](#statement) * - [Eval](#eval) * - [Exit](#exit) * - [Halt](#halt) * - [Clone](#clone) * - [Declare](#declare) * - [Global](#global) * - [Static](#static) * - [Include](#include) * - [Assign](#assign) * - [RetIf](#retif) * - [If](#if) * - [Do](#do) * - [While](#while) * - [For](#for) * - [Foreach](#foreach) * - [Switch](#switch) * - [Goto](#goto) * - [Silent](#silent) * - [Try](#try) * - [Catch](#catch) * - [Throw](#throw) * - [Call](#call) * - [Closure](#closure) * - [New](#new) * - [UseGroup](#usegroup) * - [UseItem](#useitem) * - [Block](#block) * - [Program](#program) * - [Namespace](#namespace) * - [Sys](#sys) * - [Echo](#echo) * - [List](#list) * - [Print](#print) * - [Isset](#isset) * - [Unset](#unset) * - [Empty](#empty) * - [Declaration](#declaration) * - [Class](#class) * - [Interface](#interface) * - [Trait](#trait) * - [Constant](#constant) * - [ClassConstant](#classconstant) * - [Function](#function) * - [Method](#method) * - [Parameter](#parameter) * - [Property](#property) * --- */ /** * The AST builder class * @constructor AST * @property {Boolean} withPositions - Should locate any node (by default false) * @property {Boolean} withSource - Should extract the node original code (by default false) */ var AST = function(withPositions, withSource) { this.withPositions = withPositions; this.withSource = withSource; }; /** * Create a position node from specified parser * including it's lexer current state * @param {Parser} * @return {Position} * @private */ AST.prototype.position = function(parser) { return new Position( parser.lexer.yylloc.first_line, parser.lexer.yylloc.first_column, parser.lexer.yylloc.first_offset ); }; /** * Prepares an AST node * @param {String|null} kind - Defines the node type * (if null, the kind must be passed at the function call) * @param {Parser} parser - The parser instance (use for extracting locations) * @return {Function} */ AST.prototype.prepare = function(kind, parser) { var start = null; if (this.withPositions || this.withSource) { start = this.position(parser); } var self = this; // returns the node return function() { var location = null; var args = Array.prototype.slice.call(arguments); if (self.withPositions || self.withSource) { var src = null; if (self.withSource) { src = parser.lexer._input.substring( start.offset, parser.lexer.yylloc.prev_offset ); } Eif (self.withPositions) { location = new Location(src, start, new Position( parser.lexer.yylloc.prev_line, parser.lexer.yylloc.prev_column, parser.lexer.yylloc.prev_offset )); } else { location = new Location(src, null, null); } // last argument is allways the location args.push(location); } // handle lazy kind definitions if (!kind) { kind = args.shift(); } // build the object var node = self[kind]; Iif (typeof node !== 'function') { throw new Error('Undefined node "'+kind+'"'); } var result = Object.create(node.prototype); node.apply(result, args); if ( result.kind === 'bin' && result.right && typeof result.right.precedence === 'function' ) { var out = result.right.precedence(result); if (out) { // shift with precedence result = out; } } else if (result.kind === 'unary' && result.what) { var out = result.precedence(result.what); if (out) { // shift with precedence result = out; } } return result; }; }; // Define all AST nodes [ require('./ast/array'), require('./ast/assign'), require('./ast/bin'), require('./ast/block'), require('./ast/boolean'), require('./ast/break'), require('./ast/call'), require('./ast/case'), require('./ast/cast'), require('./ast/catch'), require('./ast/class'), require('./ast/classconstant'), require('./ast/clone'), require('./ast/closure'), require('./ast/constant'), require('./ast/constref'), require('./ast/continue'), require('./ast/declaration'), require('./ast/declare'), require('./ast/do'), require('./ast/doc'), require('./ast/echo'), require('./ast/empty'), require('./ast/encapsed'), require('./ast/entry'), require('./ast/error'), require('./ast/eval'), require('./ast/exit'), require('./ast/expression'), require('./ast/for'), require('./ast/foreach'), require('./ast/function'), require('./ast/global'), require('./ast/goto'), require('./ast/halt'), require('./ast/identifier'), require('./ast/if'), require('./ast/include'), require('./ast/inline'), require('./ast/interface'), require('./ast/isset'), require('./ast/label'), require('./ast/list'), require('./ast/literal'), require('./ast/lookup'), require('./ast/magic'), require('./ast/method'), require('./ast/namespace'), require('./ast/new'), require('./ast/node'), require('./ast/nowdoc'), require('./ast/number'), require('./ast/offsetlookup'), require('./ast/operation'), require('./ast/parameter'), require('./ast/parenthesis'), require('./ast/post'), require('./ast/pre'), require('./ast/print'), require('./ast/program'), require('./ast/property'), require('./ast/propertylookup'), require('./ast/retif'), require('./ast/return'), require('./ast/silent'), require('./ast/statement'), require('./ast/static'), require('./ast/staticlookup'), require('./ast/string'), require('./ast/switch'), require('./ast/sys'), require('./ast/throw'), require('./ast/trait'), require('./ast/traitalias'), require('./ast/traitprecedence'), require('./ast/traituse'), require('./ast/try'), require('./ast/unary'), require('./ast/unset'), require('./ast/usegroup'), require('./ast/useitem'), require('./ast/variable'), require('./ast/variadic'), require('./ast/while'), require('./ast/yield'), require('./ast/yieldfrom') ].forEach(function (ctor) { var kind = ctor.prototype.constructor.name.toLowerCase(); if (kind[0] === '_') kind = kind.substring(1); AST.prototype[kind] = ctor; }); module.exports = AST; |