UNPKG

ace-code-editor

Version:

Ajax.org Code Editor is a full featured source code highlighting editor that powers the Cloud9 IDE

1,504 lines (1,314 loc) 175 kB
/* php.js 0.1.0 <http://phpjs.hertzen.com/> Copyright (c) 2013 Niklas von Hertzen Released under MIT License This file contains: - [var PHP = {Constants:{}};] - src/modules/tokenizer/constants.js - src/parser/lexer.js - src/parser/parser.js - src/parser/yyn.js - src/parser/yyn_stmt.js - src/parser/yyn_expr.js - src/parser/yyn_scalar.js */ define(function(require, exports, module) { var PHP = {Constants:{}}; PHP.Constants.T_INCLUDE = 262; PHP.Constants.T_INCLUDE_ONCE = 261; PHP.Constants.T_EVAL = 260; PHP.Constants.T_REQUIRE = 259; PHP.Constants.T_REQUIRE_ONCE = 258; PHP.Constants.T_LOGICAL_OR = 263; PHP.Constants.T_LOGICAL_XOR = 264; PHP.Constants.T_LOGICAL_AND = 265; PHP.Constants.T_PRINT = 266; PHP.Constants.T_PLUS_EQUAL = 277; PHP.Constants.T_MINUS_EQUAL = 276; PHP.Constants.T_MUL_EQUAL = 275; PHP.Constants.T_DIV_EQUAL = 274; PHP.Constants.T_CONCAT_EQUAL = 273; PHP.Constants.T_MOD_EQUAL = 272; PHP.Constants.T_AND_EQUAL = 271; PHP.Constants.T_OR_EQUAL = 270; PHP.Constants.T_XOR_EQUAL = 269; PHP.Constants.T_SL_EQUAL = 268; PHP.Constants.T_SR_EQUAL = 267; PHP.Constants.T_BOOLEAN_OR = 278; PHP.Constants.T_BOOLEAN_AND = 279; PHP.Constants.T_IS_EQUAL = 283; PHP.Constants.T_IS_NOT_EQUAL = 282; PHP.Constants.T_IS_IDENTICAL = 281; PHP.Constants.T_IS_NOT_IDENTICAL = 280; PHP.Constants.T_IS_SMALLER_OR_EQUAL = 285; PHP.Constants.T_IS_GREATER_OR_EQUAL = 284; PHP.Constants.T_SL = 287; PHP.Constants.T_SR = 286; PHP.Constants.T_INSTANCEOF = 288; PHP.Constants.T_INC = 297; PHP.Constants.T_DEC = 296; PHP.Constants.T_INT_CAST = 295; PHP.Constants.T_DOUBLE_CAST = 294; PHP.Constants.T_STRING_CAST = 293; PHP.Constants.T_ARRAY_CAST = 292; PHP.Constants.T_OBJECT_CAST = 291; PHP.Constants.T_BOOL_CAST = 290; PHP.Constants.T_UNSET_CAST = 289; PHP.Constants.T_NEW = 299; PHP.Constants.T_CLONE = 298; PHP.Constants.T_EXIT = 300; PHP.Constants.T_IF = 301; PHP.Constants.T_ELSEIF = 302; PHP.Constants.T_ELSE = 303; PHP.Constants.T_ENDIF = 304; PHP.Constants.T_LNUMBER = 305; PHP.Constants.T_DNUMBER = 306; PHP.Constants.T_STRING = 307; PHP.Constants.T_STRING_VARNAME = 308; PHP.Constants.T_VARIABLE = 309; PHP.Constants.T_NUM_STRING = 310; PHP.Constants.T_INLINE_HTML = 311; PHP.Constants.T_CHARACTER = 312; PHP.Constants.T_BAD_CHARACTER = 313; PHP.Constants.T_ENCAPSED_AND_WHITESPACE = 314; PHP.Constants.T_CONSTANT_ENCAPSED_STRING = 315; PHP.Constants.T_ECHO = 316; PHP.Constants.T_DO = 317; PHP.Constants.T_WHILE = 318; PHP.Constants.T_ENDWHILE = 319; PHP.Constants.T_FOR = 320; PHP.Constants.T_ENDFOR = 321; PHP.Constants.T_FOREACH = 322; PHP.Constants.T_ENDFOREACH = 323; PHP.Constants.T_DECLARE = 324; PHP.Constants.T_ENDDECLARE = 325; PHP.Constants.T_AS = 326; PHP.Constants.T_SWITCH = 327; PHP.Constants.T_ENDSWITCH = 328; PHP.Constants.T_CASE = 329; PHP.Constants.T_DEFAULT = 330; PHP.Constants.T_BREAK = 331; PHP.Constants.T_CONTINUE = 332; PHP.Constants.T_GOTO = 333; PHP.Constants.T_FUNCTION = 334; PHP.Constants.T_CONST = 335; PHP.Constants.T_RETURN = 336; PHP.Constants.T_TRY = 337; PHP.Constants.T_CATCH = 338; PHP.Constants.T_THROW = 339; PHP.Constants.T_USE = 340; //PHP.Constants.T_INSTEADOF = ; PHP.Constants.T_GLOBAL = 341; PHP.Constants.T_STATIC = 347; PHP.Constants.T_ABSTRACT = 346; PHP.Constants.T_FINAL = 345; PHP.Constants.T_PRIVATE = 344; PHP.Constants.T_PROTECTED = 343; PHP.Constants.T_PUBLIC = 342; PHP.Constants.T_VAR = 348; PHP.Constants.T_UNSET = 349; PHP.Constants.T_ISSET = 350; PHP.Constants.T_EMPTY = 351; PHP.Constants.T_HALT_COMPILER = 352; PHP.Constants.T_CLASS = 353; PHP.Constants.T_TRAIT = 382; PHP.Constants.T_INTERFACE = 354; PHP.Constants.T_EXTENDS = 355; PHP.Constants.T_IMPLEMENTS = 356; PHP.Constants.T_OBJECT_OPERATOR = 357; PHP.Constants.T_DOUBLE_ARROW = 358; PHP.Constants.T_LIST = 359; PHP.Constants.T_ARRAY = 360; //PHP.Constants.T_CALLABLE = ; PHP.Constants.T_CLASS_C = 361; PHP.Constants.T_TRAIT_C = 381; PHP.Constants.T_METHOD_C = 362; PHP.Constants.T_FUNC_C = 363; PHP.Constants.T_LINE = 364; PHP.Constants.T_FILE = 365; PHP.Constants.T_COMMENT = 366; PHP.Constants.T_DOC_COMMENT = 367; PHP.Constants.T_OPEN_TAG = 368; PHP.Constants.T_OPEN_TAG_WITH_ECHO = 369; PHP.Constants.T_CLOSE_TAG = 370; PHP.Constants.T_WHITESPACE = 371; PHP.Constants.T_START_HEREDOC = 372; PHP.Constants.T_END_HEREDOC = 373; PHP.Constants.T_DOLLAR_OPEN_CURLY_BRACES = 374; PHP.Constants.T_CURLY_OPEN = 375; PHP.Constants.T_PAAMAYIM_NEKUDOTAYIM = 376; PHP.Constants.T_DOUBLE_COLON = 376; PHP.Constants.T_NAMESPACE = 377; PHP.Constants.T_NS_C = 378; PHP.Constants.T_DIR = 379; PHP.Constants.T_NS_SEPARATOR = 380; PHP.Lexer = function( src, ini ) { var heredoc, lineBreaker = function( result ) { if (result.match(/\n/) !== null) { var quote = result.substring(0, 1); result = '[' + result.split(/\n/).join( quote + "," + quote ) + '].join("\\n")'; } return result; }, prev, openTag = (ini === undefined || (/^(on|true|1)$/i.test(ini.short_open_tag) ) ? /(\<\?php\s|\<\?|\<\%|\<script language\=('|")?php('|")?\>)/i : /(\<\?php\s|<\?=|\<script language\=('|")?php('|")?\>)/i), openTagStart = (ini === undefined || (/^(on|true|1)$/i.test(ini.short_open_tag)) ? /^(\<\?php\s|\<\?|\<\%|\<script language\=('|")?php('|")?\>)/i : /^(\<\?php\s|<\?=|\<script language\=('|")?php('|")?\>)/i), tokens = [ { value: PHP.Constants.T_NAMESPACE, re: /^namespace(?=\s)/i }, { value: PHP.Constants.T_USE, re: /^use(?=\s)/i }, { value: PHP.Constants.T_ABSTRACT, re: /^abstract(?=\s)/i }, { value: PHP.Constants.T_IMPLEMENTS, re: /^implements(?=\s)/i }, { value: PHP.Constants.T_INTERFACE, re: /^interface(?=\s)/i }, { value: PHP.Constants.T_CONST, re: /^const(?=\s)/i }, { value: PHP.Constants.T_STATIC, re: /^static(?=\s)/i }, { value: PHP.Constants.T_FINAL, re: /^final(?=\s)/i }, { value: PHP.Constants.T_VAR, re: /^var(?=\s)/i }, { value: PHP.Constants.T_GLOBAL, re: /^global(?=\s)/i }, { value: PHP.Constants.T_CLONE, re: /^clone(?=\s)/i }, { value: PHP.Constants.T_THROW, re: /^throw(?=\s)/i }, { value: PHP.Constants.T_EXTENDS, re: /^extends(?=\s)/i }, { value: PHP.Constants.T_AND_EQUAL, re: /^&=/ }, { value: PHP.Constants.T_AS, re: /^as(?=\s)/i }, { value: PHP.Constants.T_ARRAY_CAST, re: /^\(array\)/i }, { value: PHP.Constants.T_BOOL_CAST, re: /^\((bool|boolean)\)/i }, { value: PHP.Constants.T_DOUBLE_CAST, re: /^\((real|float|double)\)/i }, { value: PHP.Constants.T_INT_CAST, re: /^\((int|integer)\)/i }, { value: PHP.Constants.T_OBJECT_CAST, re: /^\(object\)/i }, { value: PHP.Constants.T_STRING_CAST, re: /^\(string\)/i }, { value: PHP.Constants.T_UNSET_CAST, re: /^\(unset\)/i }, { value: PHP.Constants.T_TRY, re: /^try(?=\s*{)/i }, { value: PHP.Constants.T_CATCH, re: /^catch(?=\s*\()/i }, { value: PHP.Constants.T_INSTANCEOF, re: /^instanceof(?=\s)/i }, { value: PHP.Constants.T_LOGICAL_OR, re: /^or(?=\s)/i }, { value: PHP.Constants.T_LOGICAL_AND, re: /^and(?=\s)/i }, { value: PHP.Constants.T_LOGICAL_XOR, re: /^xor(?=\s)/i }, { value: PHP.Constants.T_BOOLEAN_AND, re: /^&&/ }, { value: PHP.Constants.T_BOOLEAN_OR, re: /^\|\|/ }, { value: PHP.Constants.T_CONTINUE, re: /^continue(?=\s|;)/i }, { value: PHP.Constants.T_BREAK, re: /^break(?=\s|;)/i }, { value: PHP.Constants.T_ENDDECLARE, re: /^enddeclare(?=\s|;)/i }, { value: PHP.Constants.T_ENDFOR, re: /^endfor(?=\s|;)/i }, { value: PHP.Constants.T_ENDFOREACH, re: /^endforeach(?=\s|;)/i }, { value: PHP.Constants.T_ENDIF, re: /^endif(?=\s|;)/i }, { value: PHP.Constants.T_ENDSWITCH, re: /^endswitch(?=\s|;)/i }, { value: PHP.Constants.T_ENDWHILE, re: /^endwhile(?=\s|;)/i }, { value: PHP.Constants.T_CASE, re: /^case(?=\s)/i }, { value: PHP.Constants.T_DEFAULT, re: /^default(?=\s|:)/i }, { value: PHP.Constants.T_SWITCH, re: /^switch(?=[ (])/i }, { value: PHP.Constants.T_EXIT, re: /^(exit|die)(?=[ \(;])/i }, { value: PHP.Constants.T_CLOSE_TAG, re: /^(\?\>|\%\>|\<\/script\>)\s?\s?/i, func: function( result ) { insidePHP = false; return result; } }, { value: PHP.Constants.T_DOUBLE_ARROW, re: /^\=\>/ }, { value: PHP.Constants.T_DOUBLE_COLON, re: /^\:\:/ }, { value: PHP.Constants.T_METHOD_C, re: /^__METHOD__/ }, { value: PHP.Constants.T_LINE, re: /^__LINE__/ }, { value: PHP.Constants.T_FILE, re: /^__FILE__/ }, { value: PHP.Constants.T_FUNC_C, re: /^__FUNCTION__/ }, { value: PHP.Constants.T_NS_C, re: /^__NAMESPACE__/ }, { value: PHP.Constants.T_TRAIT_C, re: /^__TRAIT__/ }, { value: PHP.Constants.T_DIR, re: /^__DIR__/ }, { value: PHP.Constants.T_CLASS_C, re: /^__CLASS__/ }, { value: PHP.Constants.T_INC, re: /^\+\+/ }, { value: PHP.Constants.T_DEC, re: /^\-\-/ }, { value: PHP.Constants.T_CONCAT_EQUAL, re: /^\.\=/ }, { value: PHP.Constants.T_DIV_EQUAL, re: /^\/\=/ }, { value: PHP.Constants.T_XOR_EQUAL, re: /^\^\=/ }, { value: PHP.Constants.T_MUL_EQUAL, re: /^\*\=/ }, { value: PHP.Constants.T_MOD_EQUAL, re: /^\%\=/ }, { value: PHP.Constants.T_SL_EQUAL, re: /^<<=/ }, { value: PHP.Constants.T_START_HEREDOC, re: /^<<<[A-Z_0-9]+\s/i, func: function( result ){ heredoc = result.substring(3, result.length - 1); return result; } }, { value: PHP.Constants.T_SL, re: /^<</ }, { value: PHP.Constants.T_IS_SMALLER_OR_EQUAL, re: /^<=/ }, { value: PHP.Constants.T_SR_EQUAL, re: /^>>=/ }, { value: PHP.Constants.T_SR, re: /^>>/ }, { value: PHP.Constants.T_IS_GREATER_OR_EQUAL, re: /^>=/ }, { value: PHP.Constants.T_OR_EQUAL, re: /^\|\=/ }, { value: PHP.Constants.T_PLUS_EQUAL, re: /^\+\=/ }, { value: PHP.Constants.T_MINUS_EQUAL, re: /^-\=/ }, { value: PHP.Constants.T_OBJECT_OPERATOR, re: /^\-\>/i }, { value: PHP.Constants.T_CLASS, re: /^class(?=[\s\{])/i, afterWhitespace: true }, { value: PHP.Constants.T_TRAIT, re: /^trait(?=[\s]+[A-Za-z])/i, }, { value: PHP.Constants.T_PUBLIC, re: /^public(?=[\s])/i }, { value: PHP.Constants.T_PRIVATE, re: /^private(?=[\s])/i }, { value: PHP.Constants.T_PROTECTED, re: /^protected(?=[\s])/i }, { value: PHP.Constants.T_ARRAY, re: /^array(?=\s*?\()/i }, { value: PHP.Constants.T_EMPTY, re: /^empty(?=[ \(])/i }, { value: PHP.Constants.T_ISSET, re: /^isset(?=[ \(])/i }, { value: PHP.Constants.T_UNSET, re: /^unset(?=[ \(])/i }, { value: PHP.Constants.T_RETURN, re: /^return(?=[ "'(;])/i }, { value: PHP.Constants.T_FUNCTION, re: /^function(?=[ "'(;])/i }, { value: PHP.Constants.T_ECHO, re: /^echo(?=[ "'(;])/i }, { value: PHP.Constants.T_LIST, re: /^list(?=\s*?\()/i }, { value: PHP.Constants.T_PRINT, re: /^print(?=[ "'(;])/i }, { value: PHP.Constants.T_INCLUDE, re: /^include(?=[ "'(;])/i }, { value: PHP.Constants.T_INCLUDE_ONCE, re: /^include_once(?=[ "'(;])/i }, { value: PHP.Constants.T_REQUIRE, re: /^require(?=[ "'(;])/i }, { value: PHP.Constants.T_REQUIRE_ONCE, re: /^require_once(?=[ "'(;])/i }, { value: PHP.Constants.T_NEW, re: /^new(?=[ ])/i }, { value: PHP.Constants.T_COMMENT, re: /^\/\*([\S\s]*?)(?:\*\/|$)/ }, { value: PHP.Constants.T_COMMENT, re: /^\/\/.*(\s)?/ }, { value: PHP.Constants.T_COMMENT, re: /^\#.*(\s)?/ }, { value: PHP.Constants.T_ELSEIF, re: /^elseif(?=[\s(])/i }, { value: PHP.Constants.T_GOTO, re: /^goto(?=[\s(])/i }, { value: PHP.Constants.T_ELSE, re: /^else(?=[\s{:])/i }, { value: PHP.Constants.T_IF, re: /^if(?=[\s(])/i }, { value: PHP.Constants.T_DO, re: /^do(?=[ {])/i }, { value: PHP.Constants.T_WHILE, re: /^while(?=[ (])/i }, { value: PHP.Constants.T_FOREACH, re: /^foreach(?=[ (])/i }, { value: PHP.Constants.T_ISSET, re: /^isset(?=[ (])/i }, { value: PHP.Constants.T_IS_IDENTICAL, re: /^===/ }, { value: PHP.Constants.T_IS_EQUAL, re: /^==/ }, { value: PHP.Constants.T_IS_NOT_IDENTICAL, re: /^\!==/ }, { value: PHP.Constants.T_IS_NOT_EQUAL, re: /^(\!=|\<\>)/ }, { value: PHP.Constants.T_FOR, re: /^for(?=[ (])/i }, { value: PHP.Constants.T_DNUMBER, re: /^[0-9]*\.[0-9]+([eE][-]?[0-9]*)?/ /*, func: function( result ) { // transform e to E - token_get_all_variation1.phpt return (result - 0).toString().toUpperCase(); }*/ }, { value: PHP.Constants.T_LNUMBER, re: /^(0x[0-9A-F]+|[0-9]+)/i }, { value: PHP.Constants.T_OPEN_TAG_WITH_ECHO, re: /^(\<\?=|\<\%=)/i }, { value: PHP.Constants.T_OPEN_TAG, re: openTagStart }, { value: PHP.Constants.T_VARIABLE, re: /^\$[a-zA-Z_\x7f-\uffff][a-zA-Z0-9_\x7f-\uffff]*/ }, { value: PHP.Constants.T_WHITESPACE, re: /^\s+/ }, { value: PHP.Constants.T_CONSTANT_ENCAPSED_STRING, re: /^("(?:[^"\\]|\\[\s\S])*"|'(?:[^'\\]|\\[\s\S])*')/, func: function( result, token ) { var curlyOpen = 0, len, bracketOpen = 0; if (result.substring( 0,1 ) === "'") { return result; } var match = result.match( /(?:[^\\]|\\.)*[^\\]\$[a-zA-Z_\x7f-\uffff][a-zA-Z0-9_\x7f-\uffff]*/g ); if ( match !== null ) { // string has a variable while( result.length > 0 ) { len = result.length; match = result.match( /^[\[\]\;\:\?\(\)\!\.\,\>\<\=\+\-\/\*\|\&\@\^\%\"\'\{\}]/ ); if ( match !== null ) { results.push( match[ 0 ] ); result = result.substring( 1 ); if ( curlyOpen > 0 && match[ 0 ] === "}") { curlyOpen--; } if ( match[ 0 ] === "[" ) { bracketOpen++; } if ( match[ 0 ] === "]" ) { bracketOpen--; } } match = result.match(/^\$[a-zA-Z_\x7f-\uffff][a-zA-Z0-9_\x7f-\uffff]*/); if ( match !== null ) { results.push([ parseInt(PHP.Constants.T_VARIABLE, 10), match[ 0 ], line ]); result = result.substring( match[ 0 ].length ); match = result.match(/^(\-\>)\s*([a-zA-Z_\x7f-\uffff][a-zA-Z0-9_\x7f-\uffff]*)\s*(\()/); if ( match !== null ) { results.push([ parseInt(PHP.Constants.T_OBJECT_OPERATOR, 10), match[ 1 ], line ]); results.push([ parseInt(PHP.Constants.T_STRING, 10), match[ 2 ], line ]); if (match[3]) { results.push(match[3]); } result = result.substring( match[ 0 ].length ); } if ( result.match( /^\[/g ) !== null ) { continue; } } var re; if ( curlyOpen > 0) { re = /^([^\\\$"{}\]\(\)\->]|\\.)+/g; } else { re = /^([^\\\$"{]|\\.|{[^\$]|\$(?=[^a-zA-Z_\x7f-\uffff]))+/g;; } var type, match2; while(( match = result.match( re )) !== null ) { if (result.length === 1) { throw new Error(match); } type = 0; if( curlyOpen > 0 ){ if( match2 = match[0].match(/^[\[\]\;\:\?\(\)\!\.\,\>\<\=\+\-\/\*\|\&\{\}\@\^\%\$\~]/) ){ results.push(match2[0]); }else{ type = PHP.Constants.T_STRING; } }else{ type = PHP.Constants.T_ENCAPSED_AND_WHITESPACE; } if( type ){ results.push([ parseInt(type, 10), match[ 0 ].replace(/\n/g,"\\n").replace(/\r/g,""), line ]); } line += match[ 0 ].split('\n').length - 1; result = result.substring( match[ 0 ].length ); } if( curlyOpen > 0 && result.match(/^\->/) !== null ) { results.push([ parseInt(PHP.Constants.T_OBJECT_OPERATOR, 10), '->', line ]); result = result.substring( 2 ); } if( result.match(/^{\$/) !== null ) { results.push([ parseInt(PHP.Constants.T_CURLY_OPEN, 10), "{", line ]); result = result.substring( 1 ); curlyOpen++; } if (len === result.length) { // nothing has been found yet if ((match = result.match( /^(([^\\]|\\.)*?[^\\]\$[a-zA-Z_\x7f-\uffff][a-zA-Z0-9_\x7f-\uffff]*)/g )) !== null) { return; } } } return undefined; } else { result = result.replace(/\r/g,""); } /* if (result.match(/\r\n/) !== null) { var quote = result.substring(0, 1); result = '[' + result.split(/\r\n/).join( quote + "," + quote ) + '].join("\\n")'; } */ return result; } }, { value: PHP.Constants.T_NS_SEPARATOR, re: /^\\(?=[a-zA-Z_])/ }, { value: PHP.Constants.T_STRING, re: /^[a-zA-Z_\x7f-\uffff][a-zA-Z0-9_\x7f-\uffff]*/ }, { value: -1, re: /^[\[\]\;\:\?\(\)\!\.\,\>\<\=\+\-\/\*\|\&\{\}\@\^\%\"\'\$\~]/ }]; var results = [], line = 1, insidePHP = false, cancel = true; if ( src === null ) { return results; } if ( typeof src !== "string" ) { src = src.toString(); } while (src.length > 0 && cancel === true) { if ( insidePHP === true ) { if ( heredoc !== undefined ) { // we are in a heredoc var regexp = new RegExp('([\\S\\s]*?)(\\r\\n|\\n|\\r)(' + heredoc + ')(;|\\r\\n|\\n)',"i"); var result = src.match( regexp ); if ( result !== null ) { // contents results.push([ parseInt(PHP.Constants.T_ENCAPSED_AND_WHITESPACE, 10), result[ 1 ].replace(/^\n/g,"").replace(/\\\$/g,"$") + "\n", line ]); // note the no - 1 for length as regexp include one line as well line += result[ 1 ].split('\n').length; // heredoc end tag results.push([ parseInt(PHP.Constants.T_END_HEREDOC, 10), result[ 3 ], line ]); src = src.substring( result[1].length + result[2].length + result[3].length ); heredoc = undefined; } if (result === null) { throw Error("sup"); } } else { cancel = tokens.some(function( token ){ if ( token.afterWhitespace === true ) { // check last var last = results[ results.length - 1 ]; if ( !Array.isArray( last ) || (last[ 0 ] !== PHP.Constants.T_WHITESPACE && last[ 0 ] !== PHP.Constants.T_OPEN_TAG && last[ 0 ] !== PHP.Constants.T_COMMENT)) { return false; } } var result = src.match( token.re ); if ( result !== null ) { if ( token.value !== -1) { var resultString = result[ 0 ]; if (token.func !== undefined ) { resultString = token.func( resultString, token ); } if (resultString !== undefined ) { results.push([ parseInt(token.value, 10), resultString, line ]); line += resultString.split('\n').length - 1; } } else { // character token results.push( result[ 0 ] ); } src = src.substring(result[ 0 ].length); return true; } return false; }); } } else { var result = openTag.exec( src ); if ( result !== null ) { if ( result.index > 0 ) { var resultString = src.substring(0, result.index); results.push ([ parseInt(PHP.Constants.T_INLINE_HTML, 10), resultString, line ]); line += resultString.split('\n').length - 1; src = src.substring( result.index ); } insidePHP = true; } else { results.push ([ parseInt(PHP.Constants.T_INLINE_HTML, 10), src.replace(/^\n/, ""), line ]); return results; } // src = src.substring(result[ 0 ].length); } } return results; }; /* * @author Niklas von Hertzen <niklas at hertzen.com> * @created 15.6.2012 * @website http://hertzen.com */ /* * The skeleton for this parser was written by Moriyoshi Koizumi and is based on * the work by Masato Bito and is in the PUBLIC DOMAIN. * Ported to JavaScript by Niklas von Hertzen */ PHP.Parser = function ( preprocessedTokens, eval ) { var yybase = this.yybase, yydefault = this.yydefault, yycheck = this.yycheck, yyaction = this.yyaction, yylen = this.yylen, yygbase = this.yygbase, yygcheck = this.yygcheck, yyp = this.yyp, yygoto = this.yygoto, yylhs = this.yylhs, terminals = this.terminals, translate = this.translate, yygdefault = this.yygdefault; this.pos = -1; this.line = 1; this.tokenMap = this.createTokenMap( ); this.dropTokens = {}; this.dropTokens[ PHP.Constants.T_WHITESPACE ] = 1; this.dropTokens[ PHP.Constants.T_OPEN_TAG ] = 1; var tokens = []; // pre-process preprocessedTokens.forEach( function( token, index ) { if ( typeof token === "object" && token[ 0 ] === PHP.Constants.T_OPEN_TAG_WITH_ECHO) { tokens.push([ PHP.Constants.T_OPEN_TAG, token[ 1 ], token[ 2 ] ]); tokens.push([ PHP.Constants.T_ECHO, token[ 1 ], token[ 2 ] ]); } else { tokens.push( token ); } }); this.tokens = tokens; // We start off with no lookahead-token var tokenId = this.TOKEN_NONE; // The attributes for a node are taken from the first and last token of the node. // From the first token only the startAttributes are taken and from the last only // the endAttributes. Both are merged using the array union operator (+). this.startAttributes = { 'startLine': 1 }; this.endAttributes = {}; // In order to figure out the attributes for the starting token, we have to keep // them in a stack var attributeStack = [ this.startAttributes ]; // Start off in the initial state and keep a stack of previous states var state = 0; var stateStack = [ state ]; // AST stack this.yyastk = []; // Current position in the stack(s) this.stackPos = 0; var yyn; var origTokenId; for (;;) { if ( yybase[ state ] === 0 ) { yyn = yydefault[ state ]; } else { if (tokenId === this.TOKEN_NONE ) { // fetch the next token id from the lexer and fetch additional info by-ref origTokenId = this.getNextToken( ); // map the lexer token id to the internally used token id's tokenId = (origTokenId >= 0 && origTokenId < this.TOKEN_MAP_SIZE) ? translate[ origTokenId ] : this.TOKEN_INVALID; attributeStack[ this.stackPos ] = this.startAttributes; } if (((yyn = yybase[ state ] + tokenId) >= 0 && yyn < this.YYLAST && yycheck[ yyn ] === tokenId || (state < this.YY2TBLSTATE && (yyn = yybase[state + this.YYNLSTATES] + tokenId) >= 0 && yyn < this.YYLAST && yycheck[ yyn ] === tokenId)) && (yyn = yyaction[ yyn ]) !== this.YYDEFAULT ) { /* * >= YYNLSTATE: shift and reduce * > 0: shift * = 0: accept * < 0: reduce * = -YYUNEXPECTED: error */ if (yyn > 0) { /* shift */ ++this.stackPos; stateStack[ this.stackPos ] = state = yyn; this.yyastk[ this.stackPos ] = this.tokenValue; attributeStack[ this.stackPos ] = this.startAttributes; tokenId = this.TOKEN_NONE; if (yyn < this.YYNLSTATES) continue; /* $yyn >= YYNLSTATES means shift-and-reduce */ yyn -= this.YYNLSTATES; } else { yyn = -yyn; } } else { yyn = yydefault[ state ]; } } for (;;) { /* reduce/error */ if ( yyn === 0 ) { /* accept */ return this.yyval; } else if (yyn !== this.YYUNEXPECTED ) { /* reduce */ for (var attr in this.endAttributes) { attributeStack[ this.stackPos - yylen[ yyn ] ][ attr ] = this.endAttributes[ attr ]; } try { this['yyn' + yyn](attributeStack[ this.stackPos - yylen[ yyn ] ]); } catch (e) { /* if (-1 === $e->getRawLine()) { $e->setRawLine($startAttributes['startLine']); } */ throw e; } /* Goto - shift nonterminal */ this.stackPos -= yylen[ yyn ]; yyn = yylhs[ yyn ]; if ((yyp = yygbase[ yyn ] + stateStack[ this.stackPos ]) >= 0 && yyp < this.YYGLAST && yygcheck[ yyp ] === yyn) { state = yygoto[ yyp ]; } else { state = yygdefault[ yyn ]; } ++this.stackPos; stateStack[ this.stackPos ] = state; this.yyastk[ this.stackPos ] = this.yyval; attributeStack[ this.stackPos ] = this.startAttributes; } else { /* error */ if (eval !== true) { var expected = []; for (var i = 0; i < this.TOKEN_MAP_SIZE; ++i) { if ((yyn = yybase[ state ] + i) >= 0 && yyn < this.YYLAST && yycheck[ yyn ] == i || state < this.YY2TBLSTATE && (yyn = yybase[ state + this.YYNLSTATES] + i) && yyn < this.YYLAST && yycheck[ yyn ] == i ) { if (yyaction[ yyn ] != this.YYUNEXPECTED) { if (expected.length == 4) { /* Too many expected tokens */ expected = []; break; } expected.push( this.terminals[ i ] ); } } } var expectedString = ''; if (expected.length) { expectedString = ', expecting ' + expected.join(' or '); } throw new PHP.ParseError('syntax error, unexpected ' + terminals[ tokenId ] + expectedString, this.startAttributes['startLine']); } else { return this.startAttributes['startLine']; } } if (state < this.YYNLSTATES) break; /* >= YYNLSTATES means shift-and-reduce */ yyn = state - this.YYNLSTATES; } } }; PHP.ParseError = function( msg, line ) { this.message = msg; this.line = line; }; PHP.Parser.prototype.MODIFIER_PUBLIC = 1; PHP.Parser.prototype.MODIFIER_PROTECTED = 2; PHP.Parser.prototype.MODIFIER_PRIVATE = 4; PHP.Parser.prototype.MODIFIER_STATIC = 8; PHP.Parser.prototype.MODIFIER_ABSTRACT = 16; PHP.Parser.prototype.MODIFIER_FINAL = 32; PHP.Parser.prototype.getNextToken = function( ) { this.startAttributes = {}; this.endAttributes = {}; var token, tmp; while (this.tokens[++this.pos] !== undefined) { token = this.tokens[this.pos]; if (typeof token === "string") { this.startAttributes['startLine'] = this.line; this.endAttributes['endLine'] = this.line; // bug in token_get_all if ('b"' === token) { this.tokenValue = 'b"'; return '"'.charCodeAt(0); } else { this.tokenValue = token; return token.charCodeAt(0); } } else { this.line += ((tmp = token[ 1 ].match(/\n/g)) === null) ? 0 : tmp.length; if (PHP.Constants.T_COMMENT === token[0]) { if (!Array.isArray(this.startAttributes['comments'])) { this.startAttributes['comments'] = []; } this.startAttributes['comments'].push( { type: "comment", comment: token[1], line: token[2] }); } else if (PHP.Constants.T_DOC_COMMENT === token[0]) { this.startAttributes['comments'].push( new PHPParser_Comment_Doc(token[1], token[2]) ); } else if (this.dropTokens[token[0]] === undefined) { this.tokenValue = token[1]; this.startAttributes['startLine'] = token[2]; this.endAttributes['endLine'] = this.line; return this.tokenMap[token[0]]; } } } this.startAttributes['startLine'] = this.line; // 0 is the EOF token return 0; }; PHP.Parser.prototype.tokenName = function( token ) { var constants = ["T_INCLUDE","T_INCLUDE_ONCE","T_EVAL","T_REQUIRE","T_REQUIRE_ONCE","T_LOGICAL_OR","T_LOGICAL_XOR","T_LOGICAL_AND","T_PRINT","T_PLUS_EQUAL","T_MINUS_EQUAL","T_MUL_EQUAL","T_DIV_EQUAL","T_CONCAT_EQUAL","T_MOD_EQUAL","T_AND_EQUAL","T_OR_EQUAL","T_XOR_EQUAL","T_SL_EQUAL","T_SR_EQUAL","T_BOOLEAN_OR","T_BOOLEAN_AND","T_IS_EQUAL","T_IS_NOT_EQUAL","T_IS_IDENTICAL","T_IS_NOT_IDENTICAL","T_IS_SMALLER_OR_EQUAL","T_IS_GREATER_OR_EQUAL","T_SL","T_SR","T_INSTANCEOF","T_INC","T_DEC","T_INT_CAST","T_DOUBLE_CAST","T_STRING_CAST","T_ARRAY_CAST","T_OBJECT_CAST","T_BOOL_CAST","T_UNSET_CAST","T_NEW","T_CLONE","T_EXIT","T_IF","T_ELSEIF","T_ELSE","T_ENDIF","T_LNUMBER","T_DNUMBER","T_STRING","T_STRING_VARNAME","T_VARIABLE","T_NUM_STRING","T_INLINE_HTML","T_CHARACTER","T_BAD_CHARACTER","T_ENCAPSED_AND_WHITESPACE","T_CONSTANT_ENCAPSED_STRING","T_ECHO","T_DO","T_WHILE","T_ENDWHILE","T_FOR","T_ENDFOR","T_FOREACH","T_ENDFOREACH","T_DECLARE","T_ENDDECLARE","T_AS","T_SWITCH","T_ENDSWITCH","T_CASE","T_DEFAULT","T_BREAK","T_CONTINUE","T_GOTO","T_FUNCTION","T_CONST","T_RETURN","T_TRY","T_CATCH","T_THROW","T_USE","T_INSTEADOF","T_GLOBAL","T_STATIC","T_ABSTRACT","T_FINAL","T_PRIVATE","T_PROTECTED","T_PUBLIC","T_VAR","T_UNSET","T_ISSET","T_EMPTY","T_HALT_COMPILER","T_CLASS","T_TRAIT","T_INTERFACE","T_EXTENDS","T_IMPLEMENTS","T_OBJECT_OPERATOR","T_DOUBLE_ARROW","T_LIST","T_ARRAY","T_CALLABLE","T_CLASS_C","T_TRAIT_C","T_METHOD_C","T_FUNC_C","T_LINE","T_FILE","T_COMMENT","T_DOC_COMMENT","T_OPEN_TAG","T_OPEN_TAG_WITH_ECHO","T_CLOSE_TAG","T_WHITESPACE","T_START_HEREDOC","T_END_HEREDOC","T_DOLLAR_OPEN_CURLY_BRACES","T_CURLY_OPEN","T_PAAMAYIM_NEKUDOTAYIM","T_DOUBLE_COLON","T_NAMESPACE","T_NS_C","T_DIR","T_NS_SEPARATOR"]; var current = "UNKNOWN"; constants.some(function( constant ) { if (PHP.Constants[ constant ] === token) { current = constant; return true; } else { return false; } }); return current; }; /** * Creates the token map. * * The token map maps the PHP internal token identifiers * to the identifiers used by the PHP.Parser. Additionally it * maps T_OPEN_TAG_WITH_ECHO to T_ECHO and T_CLOSE_TAG to ';'. * * @return array The token map */ PHP.Parser.prototype.createTokenMap = function() { var tokenMap = {}, name, i; var T_DOUBLE_COLON = PHP.Constants.T_PAAMAYIM_NEKUDOTAYIM; // 256 is the minimum possible token number, as everything below // it is an ASCII value for ( i = 256; i < 1000; ++i ) { // T_DOUBLE_COLON is equivalent to T_PAAMAYIM_NEKUDOTAYIM if ( T_DOUBLE_COLON === i ) { tokenMap[ i ] = this.T_PAAMAYIM_NEKUDOTAYIM; // T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO } else if( PHP.Constants.T_OPEN_TAG_WITH_ECHO === i ) { tokenMap[ i ] = PHP.Constants.T_ECHO; // T_CLOSE_TAG is equivalent to ';' } else if( PHP.Constants.T_CLOSE_TAG === i ) { tokenMap[ i ] = 59; // and the others can be mapped directly } else if ( 'UNKNOWN' !== (name = this.tokenName( i ) ) ) { tokenMap[ i ] = this[name]; } } return tokenMap; }; var yynStandard = function () { this.yyval = this.yyastk[ this.stackPos-(1-1) ]; }; // todo fix PHP.Parser.prototype.MakeArray = function( arr ) { return Array.isArray( arr ) ? arr : [ arr ]; } PHP.Parser.prototype.parseString = function( str ) { var bLength = 0; if ('b' === str[0]) { bLength = 1; } if ('\'' === str[ bLength ]) { str = str.replace( ['\\\\', '\\\''], [ '\\', '\'']); } else { str = this.parseEscapeSequences( str, '"'); } return str; }; PHP.Parser.prototype.parseEscapeSequences = function( str, quote ) { if (undefined !== quote) { str = str.replace(new RegExp('\\' + quote, "g"), quote); } var replacements = { '\\': '\\', '$': '$', 'n': "\n", 'r': "\r", 't': "\t", 'f': "\f", 'v': "\v", 'e': "\x1B" }; return str.replace( /~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3})~/g, function ( matches ){ var str = matches[1]; if ( replacements[ str ] !== undefined ) { return replacements[ str ]; } else if ('x' === str[ 0 ] || 'X' === str[ 0 ]) { return chr(hexdec(str)); } else { return chr(octdec(str)); } } ); }; /* This is an automatically GENERATED file, which should not be manually edited. * Instead edit one of the following: * * the grammar file grammar/zend_language_parser.jsy * * the parser skeleton grammar/kymacc.js.parser * * the preprocessing script grammar/rebuildParser.php * * The skeleton for this parser was written by Moriyoshi Koizumi and is based on * the work by Masato Bito and is in the PUBLIC DOMAIN. * Ported to JavaScript by Niklas von Hertzen */ PHP.Parser.prototype.TOKEN_NONE = -1; PHP.Parser.prototype.TOKEN_INVALID = 149; PHP.Parser.prototype.TOKEN_MAP_SIZE = 384; PHP.Parser.prototype.YYLAST = 913; PHP.Parser.prototype.YY2TBLSTATE = 328; PHP.Parser.prototype.YYGLAST = 415; PHP.Parser.prototype.YYNLSTATES = 544; PHP.Parser.prototype.YYUNEXPECTED = 32767; PHP.Parser.prototype.YYDEFAULT = -32766; // {{{ Tokens PHP.Parser.prototype.YYERRTOK = 256; PHP.Parser.prototype.T_INCLUDE = 257; PHP.Parser.prototype.T_INCLUDE_ONCE = 258; PHP.Parser.prototype.T_EVAL = 259; PHP.Parser.prototype.T_REQUIRE = 260; PHP.Parser.prototype.T_REQUIRE_ONCE = 261; PHP.Parser.prototype.T_LOGICAL_OR = 262; PHP.Parser.prototype.T_LOGICAL_XOR = 263; PHP.Parser.prototype.T_LOGICAL_AND = 264; PHP.Parser.prototype.T_PRINT = 265; PHP.Parser.prototype.T_PLUS_EQUAL = 266; PHP.Parser.prototype.T_MINUS_EQUAL = 267; PHP.Parser.prototype.T_MUL_EQUAL = 268; PHP.Parser.prototype.T_DIV_EQUAL = 269; PHP.Parser.prototype.T_CONCAT_EQUAL = 270; PHP.Parser.prototype.T_MOD_EQUAL = 271; PHP.Parser.prototype.T_AND_EQUAL = 272; PHP.Parser.prototype.T_OR_EQUAL = 273; PHP.Parser.prototype.T_XOR_EQUAL = 274; PHP.Parser.prototype.T_SL_EQUAL = 275; PHP.Parser.prototype.T_SR_EQUAL = 276; PHP.Parser.prototype.T_BOOLEAN_OR = 277; PHP.Parser.prototype.T_BOOLEAN_AND = 278; PHP.Parser.prototype.T_IS_EQUAL = 279; PHP.Parser.prototype.T_IS_NOT_EQUAL = 280; PHP.Parser.prototype.T_IS_IDENTICAL = 281; PHP.Parser.prototype.T_IS_NOT_IDENTICAL = 282; PHP.Parser.prototype.T_IS_SMALLER_OR_EQUAL = 283; PHP.Parser.prototype.T_IS_GREATER_OR_EQUAL = 284; PHP.Parser.prototype.T_SL = 285; PHP.Parser.prototype.T_SR = 286; PHP.Parser.prototype.T_INSTANCEOF = 287; PHP.Parser.prototype.T_INC = 288; PHP.Parser.prototype.T_DEC = 289; PHP.Parser.prototype.T_INT_CAST = 290; PHP.Parser.prototype.T_DOUBLE_CAST = 291; PHP.Parser.prototype.T_STRING_CAST = 292; PHP.Parser.prototype.T_ARRAY_CAST = 293; PHP.Parser.prototype.T_OBJECT_CAST = 294; PHP.Parser.prototype.T_BOOL_CAST = 295; PHP.Parser.prototype.T_UNSET_CAST = 296; PHP.Parser.prototype.T_NEW = 297; PHP.Parser.prototype.T_CLONE = 298; PHP.Parser.prototype.T_EXIT = 299; PHP.Parser.prototype.T_IF = 300; PHP.Parser.prototype.T_ELSEIF = 301; PHP.Parser.prototype.T_ELSE = 302; PHP.Parser.prototype.T_ENDIF = 303; PHP.Parser.prototype.T_LNUMBER = 304; PHP.Parser.prototype.T_DNUMBER = 305; PHP.Parser.prototype.T_STRING = 306; PHP.Parser.prototype.T_STRING_VARNAME = 307; PHP.Parser.prototype.T_VARIABLE = 308; PHP.Parser.prototype.T_NUM_STRING = 309; PHP.Parser.prototype.T_INLINE_HTML = 310; PHP.Parser.prototype.T_CHARACTER = 311; PHP.Parser.prototype.T_BAD_CHARACTER = 312; PHP.Parser.prototype.T_ENCAPSED_AND_WHITESPACE = 313; PHP.Parser.prototype.T_CONSTANT_ENCAPSED_STRING = 314; PHP.Parser.prototype.T_ECHO = 315; PHP.Parser.prototype.T_DO = 316; PHP.Parser.prototype.T_WHILE = 317; PHP.Parser.prototype.T_ENDWHILE = 318; PHP.Parser.prototype.T_FOR = 319; PHP.Parser.prototype.T_ENDFOR = 320; PHP.Parser.prototype.T_FOREACH = 321; PHP.Parser.prototype.T_ENDFOREACH = 322; PHP.Parser.prototype.T_DECLARE = 323; PHP.Parser.prototype.T_ENDDECLARE = 324; PHP.Parser.prototype.T_AS = 325; PHP.Parser.prototype.T_SWITCH = 326; PHP.Parser.prototype.T_ENDSWITCH = 327; PHP.Parser.prototype.T_CASE = 328; PHP.Parser.prototype.T_DEFAULT = 329; PHP.Parser.prototype.T_BREAK = 330; PHP.Parser.prototype.T_CONTINUE = 331; PHP.Parser.prototype.T_GOTO = 332; PHP.Parser.prototype.T_FUNCTION = 333; PHP.Parser.prototype.T_CONST = 334; PHP.Parser.prototype.T_RETURN = 335; PHP.Parser.prototype.T_TRY = 336; PHP.Parser.prototype.T_CATCH = 337; PHP.Parser.prototype.T_THROW = 338; PHP.Parser.prototype.T_USE = 339; PHP.Parser.prototype.T_INSTEADOF = 340; PHP.Parser.prototype.T_GLOBAL = 341; PHP.Parser.prototype.T_STATIC = 342; PHP.Parser.prototype.T_ABSTRACT = 343; PHP.Parser.prototype.T_FINAL = 344; PHP.Parser.prototype.T_PRIVATE = 345; PHP.Parser.prototype.T_PROTECTED = 346; PHP.Parser.prototype.T_PUBLIC = 347; PHP.Parser.prototype.T_VAR = 348; PHP.Parser.prototype.T_UNSET = 349; PHP.Parser.prototype.T_ISSET = 350; PHP.Parser.prototype.T_EMPTY = 351; PHP.Parser.prototype.T_HALT_COMPILER = 352; PHP.Parser.prototype.T_CLASS = 353; PHP.Parser.prototype.T_TRAIT = 354; PHP.Parser.prototype.T_INTERFACE = 355; PHP.Parser.prototype.T_EXTENDS = 356; PHP.Parser.prototype.T_IMPLEMENTS = 357; PHP.Parser.prototype.T_OBJECT_OPERATOR = 358; PHP.Parser.prototype.T_DOUBLE_ARROW = 359; PHP.Parser.prototype.T_LIST = 360; PHP.Parser.prototype.T_ARRAY = 361; PHP.Parser.prototype.T_CALLABLE = 362; PHP.Parser.prototype