zend_language_scanner.l 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2016 Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Marcus Boerger <helly@php.net> |
  16. | Nuno Lopes <nlopess@php.net> |
  17. | Scott MacVicar <scottmac@php.net> |
  18. | Flex version authors: |
  19. | Andi Gutmans <andi@zend.com> |
  20. | Zeev Suraski <zeev@zend.com> |
  21. +----------------------------------------------------------------------+
  22. */
  23. /* $Id$ */
  24. #if 0
  25. # define YYDEBUG(s, c) printf("state: %d char: %c\n", s, c)
  26. #else
  27. # define YYDEBUG(s, c)
  28. #endif
  29. #include "zend_language_scanner_defs.h"
  30. #include <errno.h>
  31. #include "zend.h"
  32. #ifdef PHP_WIN32
  33. # include <Winuser.h>
  34. #endif
  35. #include "zend_alloc.h"
  36. #include <zend_language_parser.h>
  37. #include "zend_compile.h"
  38. #include "zend_language_scanner.h"
  39. #include "zend_highlight.h"
  40. #include "zend_constants.h"
  41. #include "zend_variables.h"
  42. #include "zend_operators.h"
  43. #include "zend_API.h"
  44. #include "zend_strtod.h"
  45. #include "zend_exceptions.h"
  46. #include "zend_virtual_cwd.h"
  47. #include "tsrm_config_common.h"
  48. #define YYCTYPE unsigned char
  49. #define YYFILL(n) { if ((YYCURSOR + n) >= (YYLIMIT + ZEND_MMAP_AHEAD)) { return 0; } }
  50. #define YYCURSOR SCNG(yy_cursor)
  51. #define YYLIMIT SCNG(yy_limit)
  52. #define YYMARKER SCNG(yy_marker)
  53. #define YYGETCONDITION() SCNG(yy_state)
  54. #define YYSETCONDITION(s) SCNG(yy_state) = s
  55. #define STATE(name) yyc##name
  56. /* emulate flex constructs */
  57. #define BEGIN(state) YYSETCONDITION(STATE(state))
  58. #define YYSTATE YYGETCONDITION()
  59. #define yytext ((char*)SCNG(yy_text))
  60. #define yyleng SCNG(yy_leng)
  61. #define yyless(x) do { YYCURSOR = (unsigned char*)yytext + x; \
  62. yyleng = (unsigned int)x; } while(0)
  63. #define yymore() goto yymore_restart
  64. /* perform sanity check. If this message is triggered you should
  65. increase the ZEND_MMAP_AHEAD value in the zend_streams.h file */
  66. /*!max:re2c */
  67. #if ZEND_MMAP_AHEAD < YYMAXFILL
  68. # error ZEND_MMAP_AHEAD should be greater than or equal to YYMAXFILL
  69. #endif
  70. #ifdef HAVE_STDARG_H
  71. # include <stdarg.h>
  72. #endif
  73. #ifdef HAVE_UNISTD_H
  74. # include <unistd.h>
  75. #endif
  76. /* Globals Macros */
  77. #define SCNG LANG_SCNG
  78. #ifdef ZTS
  79. ZEND_API ts_rsrc_id language_scanner_globals_id;
  80. #else
  81. ZEND_API zend_php_scanner_globals language_scanner_globals;
  82. #endif
  83. #define HANDLE_NEWLINES(s, l) \
  84. do { \
  85. char *p = (s), *boundary = p+(l); \
  86. \
  87. while (p<boundary) { \
  88. if (*p == '\n' || (*p == '\r' && (*(p+1) != '\n'))) { \
  89. CG(zend_lineno)++; \
  90. } \
  91. p++; \
  92. } \
  93. } while (0)
  94. #define HANDLE_NEWLINE(c) \
  95. { \
  96. if (c == '\n' || c == '\r') { \
  97. CG(zend_lineno)++; \
  98. } \
  99. }
  100. /* To save initial string length after scanning to first variable, CG(doc_comment_len) can be reused */
  101. #define SET_DOUBLE_QUOTES_SCANNED_LENGTH(len) CG(doc_comment_len) = (len)
  102. #define GET_DOUBLE_QUOTES_SCANNED_LENGTH() CG(doc_comment_len)
  103. #define IS_LABEL_START(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z') || (c) == '_' || (c) >= 0x7F)
  104. #define ZEND_IS_OCT(c) ((c)>='0' && (c)<='7')
  105. #define ZEND_IS_HEX(c) (((c)>='0' && (c)<='9') || ((c)>='a' && (c)<='f') || ((c)>='A' && (c)<='F'))
  106. BEGIN_EXTERN_C()
  107. static size_t encoding_filter_script_to_internal(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length TSRMLS_DC)
  108. {
  109. const zend_encoding *internal_encoding = zend_multibyte_get_internal_encoding(TSRMLS_C);
  110. assert(internal_encoding && zend_multibyte_check_lexer_compatibility(internal_encoding));
  111. return zend_multibyte_encoding_converter(to, to_length, from, from_length, internal_encoding, LANG_SCNG(script_encoding) TSRMLS_CC);
  112. }
  113. static size_t encoding_filter_script_to_intermediate(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length TSRMLS_DC)
  114. {
  115. return zend_multibyte_encoding_converter(to, to_length, from, from_length, zend_multibyte_encoding_utf8, LANG_SCNG(script_encoding) TSRMLS_CC);
  116. }
  117. static size_t encoding_filter_intermediate_to_script(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length TSRMLS_DC)
  118. {
  119. return zend_multibyte_encoding_converter(to, to_length, from, from_length,
  120. LANG_SCNG(script_encoding), zend_multibyte_encoding_utf8 TSRMLS_CC);
  121. }
  122. static size_t encoding_filter_intermediate_to_internal(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length TSRMLS_DC)
  123. {
  124. const zend_encoding *internal_encoding = zend_multibyte_get_internal_encoding(TSRMLS_C);
  125. assert(internal_encoding && zend_multibyte_check_lexer_compatibility(internal_encoding));
  126. return zend_multibyte_encoding_converter(to, to_length, from, from_length,
  127. internal_encoding, zend_multibyte_encoding_utf8 TSRMLS_CC);
  128. }
  129. static void _yy_push_state(int new_state TSRMLS_DC)
  130. {
  131. zend_stack_push(&SCNG(state_stack), (void *) &YYGETCONDITION(), sizeof(int));
  132. YYSETCONDITION(new_state);
  133. }
  134. #define yy_push_state(state_and_tsrm) _yy_push_state(yyc##state_and_tsrm)
  135. static void yy_pop_state(TSRMLS_D)
  136. {
  137. int *stack_state;
  138. zend_stack_top(&SCNG(state_stack), (void **) &stack_state);
  139. YYSETCONDITION(*stack_state);
  140. zend_stack_del_top(&SCNG(state_stack));
  141. }
  142. static void yy_scan_buffer(char *str, unsigned int len TSRMLS_DC)
  143. {
  144. YYCURSOR = (YYCTYPE*)str;
  145. YYLIMIT = YYCURSOR + len;
  146. if (!SCNG(yy_start)) {
  147. SCNG(yy_start) = YYCURSOR;
  148. }
  149. }
  150. void startup_scanner(TSRMLS_D)
  151. {
  152. CG(parse_error) = 0;
  153. CG(doc_comment) = NULL;
  154. CG(doc_comment_len) = 0;
  155. zend_stack_init(&SCNG(state_stack));
  156. zend_ptr_stack_init(&SCNG(heredoc_label_stack));
  157. }
  158. static void heredoc_label_dtor(zend_heredoc_label *heredoc_label) {
  159. efree(heredoc_label->label);
  160. }
  161. void shutdown_scanner(TSRMLS_D)
  162. {
  163. CG(parse_error) = 0;
  164. RESET_DOC_COMMENT();
  165. zend_stack_destroy(&SCNG(state_stack));
  166. zend_ptr_stack_clean(&SCNG(heredoc_label_stack), (void (*)(void *)) &heredoc_label_dtor, 1);
  167. zend_ptr_stack_destroy(&SCNG(heredoc_label_stack));
  168. }
  169. ZEND_API void zend_save_lexical_state(zend_lex_state *lex_state TSRMLS_DC)
  170. {
  171. lex_state->yy_leng = SCNG(yy_leng);
  172. lex_state->yy_start = SCNG(yy_start);
  173. lex_state->yy_text = SCNG(yy_text);
  174. lex_state->yy_cursor = SCNG(yy_cursor);
  175. lex_state->yy_marker = SCNG(yy_marker);
  176. lex_state->yy_limit = SCNG(yy_limit);
  177. lex_state->state_stack = SCNG(state_stack);
  178. zend_stack_init(&SCNG(state_stack));
  179. lex_state->heredoc_label_stack = SCNG(heredoc_label_stack);
  180. zend_ptr_stack_init(&SCNG(heredoc_label_stack));
  181. lex_state->in = SCNG(yy_in);
  182. lex_state->yy_state = YYSTATE;
  183. lex_state->filename = zend_get_compiled_filename(TSRMLS_C);
  184. lex_state->lineno = CG(zend_lineno);
  185. lex_state->script_org = SCNG(script_org);
  186. lex_state->script_org_size = SCNG(script_org_size);
  187. lex_state->script_filtered = SCNG(script_filtered);
  188. lex_state->script_filtered_size = SCNG(script_filtered_size);
  189. lex_state->input_filter = SCNG(input_filter);
  190. lex_state->output_filter = SCNG(output_filter);
  191. lex_state->script_encoding = SCNG(script_encoding);
  192. }
  193. ZEND_API void zend_restore_lexical_state(zend_lex_state *lex_state TSRMLS_DC)
  194. {
  195. SCNG(yy_leng) = lex_state->yy_leng;
  196. SCNG(yy_start) = lex_state->yy_start;
  197. SCNG(yy_text) = lex_state->yy_text;
  198. SCNG(yy_cursor) = lex_state->yy_cursor;
  199. SCNG(yy_marker) = lex_state->yy_marker;
  200. SCNG(yy_limit) = lex_state->yy_limit;
  201. zend_stack_destroy(&SCNG(state_stack));
  202. SCNG(state_stack) = lex_state->state_stack;
  203. zend_ptr_stack_clean(&SCNG(heredoc_label_stack), (void (*)(void *)) &heredoc_label_dtor, 1);
  204. zend_ptr_stack_destroy(&SCNG(heredoc_label_stack));
  205. SCNG(heredoc_label_stack) = lex_state->heredoc_label_stack;
  206. SCNG(yy_in) = lex_state->in;
  207. YYSETCONDITION(lex_state->yy_state);
  208. CG(zend_lineno) = lex_state->lineno;
  209. zend_restore_compiled_filename(lex_state->filename TSRMLS_CC);
  210. if (SCNG(script_filtered)) {
  211. efree(SCNG(script_filtered));
  212. SCNG(script_filtered) = NULL;
  213. }
  214. SCNG(script_org) = lex_state->script_org;
  215. SCNG(script_org_size) = lex_state->script_org_size;
  216. SCNG(script_filtered) = lex_state->script_filtered;
  217. SCNG(script_filtered_size) = lex_state->script_filtered_size;
  218. SCNG(input_filter) = lex_state->input_filter;
  219. SCNG(output_filter) = lex_state->output_filter;
  220. SCNG(script_encoding) = lex_state->script_encoding;
  221. RESET_DOC_COMMENT();
  222. }
  223. ZEND_API void zend_destroy_file_handle(zend_file_handle *file_handle TSRMLS_DC)
  224. {
  225. zend_llist_del_element(&CG(open_files), file_handle, (int (*)(void *, void *)) zend_compare_file_handles);
  226. /* zend_file_handle_dtor() operates on the copy, so we have to NULLify the original here */
  227. file_handle->opened_path = NULL;
  228. if (file_handle->free_filename) {
  229. file_handle->filename = NULL;
  230. }
  231. }
  232. #define BOM_UTF32_BE "\x00\x00\xfe\xff"
  233. #define BOM_UTF32_LE "\xff\xfe\x00\x00"
  234. #define BOM_UTF16_BE "\xfe\xff"
  235. #define BOM_UTF16_LE "\xff\xfe"
  236. #define BOM_UTF8 "\xef\xbb\xbf"
  237. static const zend_encoding *zend_multibyte_detect_utf_encoding(const unsigned char *script, size_t script_size TSRMLS_DC)
  238. {
  239. const unsigned char *p;
  240. int wchar_size = 2;
  241. int le = 0;
  242. /* utf-16 or utf-32? */
  243. p = script;
  244. while ((p-script) < script_size) {
  245. p = memchr(p, 0, script_size-(p-script)-2);
  246. if (!p) {
  247. break;
  248. }
  249. if (*(p+1) == '\0' && *(p+2) == '\0') {
  250. wchar_size = 4;
  251. break;
  252. }
  253. /* searching for UTF-32 specific byte orders, so this will do */
  254. p += 4;
  255. }
  256. /* BE or LE? */
  257. p = script;
  258. while ((p-script) < script_size) {
  259. if (*p == '\0' && *(p+wchar_size-1) != '\0') {
  260. /* BE */
  261. le = 0;
  262. break;
  263. } else if (*p != '\0' && *(p+wchar_size-1) == '\0') {
  264. /* LE* */
  265. le = 1;
  266. break;
  267. }
  268. p += wchar_size;
  269. }
  270. if (wchar_size == 2) {
  271. return le ? zend_multibyte_encoding_utf16le : zend_multibyte_encoding_utf16be;
  272. } else {
  273. return le ? zend_multibyte_encoding_utf32le : zend_multibyte_encoding_utf32be;
  274. }
  275. return NULL;
  276. }
  277. static const zend_encoding* zend_multibyte_detect_unicode(TSRMLS_D)
  278. {
  279. const zend_encoding *script_encoding = NULL;
  280. int bom_size;
  281. unsigned char *pos1, *pos2;
  282. if (LANG_SCNG(script_org_size) < sizeof(BOM_UTF32_LE)-1) {
  283. return NULL;
  284. }
  285. /* check out BOM */
  286. if (!memcmp(LANG_SCNG(script_org), BOM_UTF32_BE, sizeof(BOM_UTF32_BE)-1)) {
  287. script_encoding = zend_multibyte_encoding_utf32be;
  288. bom_size = sizeof(BOM_UTF32_BE)-1;
  289. } else if (!memcmp(LANG_SCNG(script_org), BOM_UTF32_LE, sizeof(BOM_UTF32_LE)-1)) {
  290. script_encoding = zend_multibyte_encoding_utf32le;
  291. bom_size = sizeof(BOM_UTF32_LE)-1;
  292. } else if (!memcmp(LANG_SCNG(script_org), BOM_UTF16_BE, sizeof(BOM_UTF16_BE)-1)) {
  293. script_encoding = zend_multibyte_encoding_utf16be;
  294. bom_size = sizeof(BOM_UTF16_BE)-1;
  295. } else if (!memcmp(LANG_SCNG(script_org), BOM_UTF16_LE, sizeof(BOM_UTF16_LE)-1)) {
  296. script_encoding = zend_multibyte_encoding_utf16le;
  297. bom_size = sizeof(BOM_UTF16_LE)-1;
  298. } else if (!memcmp(LANG_SCNG(script_org), BOM_UTF8, sizeof(BOM_UTF8)-1)) {
  299. script_encoding = zend_multibyte_encoding_utf8;
  300. bom_size = sizeof(BOM_UTF8)-1;
  301. }
  302. if (script_encoding) {
  303. /* remove BOM */
  304. LANG_SCNG(script_org) += bom_size;
  305. LANG_SCNG(script_org_size) -= bom_size;
  306. return script_encoding;
  307. }
  308. /* script contains NULL bytes -> auto-detection */
  309. if ((pos1 = memchr(LANG_SCNG(script_org), 0, LANG_SCNG(script_org_size)))) {
  310. /* check if the NULL byte is after the __HALT_COMPILER(); */
  311. pos2 = LANG_SCNG(script_org);
  312. while (pos1 - pos2 >= sizeof("__HALT_COMPILER();")-1) {
  313. pos2 = memchr(pos2, '_', pos1 - pos2);
  314. if (!pos2) break;
  315. pos2++;
  316. if (strncasecmp((char*)pos2, "_HALT_COMPILER", sizeof("_HALT_COMPILER")-1) == 0) {
  317. pos2 += sizeof("_HALT_COMPILER")-1;
  318. while (*pos2 == ' ' ||
  319. *pos2 == '\t' ||
  320. *pos2 == '\r' ||
  321. *pos2 == '\n') {
  322. pos2++;
  323. }
  324. if (*pos2 == '(') {
  325. pos2++;
  326. while (*pos2 == ' ' ||
  327. *pos2 == '\t' ||
  328. *pos2 == '\r' ||
  329. *pos2 == '\n') {
  330. pos2++;
  331. }
  332. if (*pos2 == ')') {
  333. pos2++;
  334. while (*pos2 == ' ' ||
  335. *pos2 == '\t' ||
  336. *pos2 == '\r' ||
  337. *pos2 == '\n') {
  338. pos2++;
  339. }
  340. if (*pos2 == ';') {
  341. return NULL;
  342. }
  343. }
  344. }
  345. }
  346. }
  347. /* make best effort if BOM is missing */
  348. return zend_multibyte_detect_utf_encoding(LANG_SCNG(script_org), LANG_SCNG(script_org_size) TSRMLS_CC);
  349. }
  350. return NULL;
  351. }
  352. static const zend_encoding* zend_multibyte_find_script_encoding(TSRMLS_D)
  353. {
  354. const zend_encoding *script_encoding;
  355. if (CG(detect_unicode)) {
  356. /* check out bom(byte order mark) and see if containing wchars */
  357. script_encoding = zend_multibyte_detect_unicode(TSRMLS_C);
  358. if (script_encoding != NULL) {
  359. /* bom or wchar detection is prior to 'script_encoding' option */
  360. return script_encoding;
  361. }
  362. }
  363. /* if no script_encoding specified, just leave alone */
  364. if (!CG(script_encoding_list) || !CG(script_encoding_list_size)) {
  365. return NULL;
  366. }
  367. /* if multiple encodings specified, detect automagically */
  368. if (CG(script_encoding_list_size) > 1) {
  369. return zend_multibyte_encoding_detector(LANG_SCNG(script_org), LANG_SCNG(script_org_size), CG(script_encoding_list), CG(script_encoding_list_size) TSRMLS_CC);
  370. }
  371. return CG(script_encoding_list)[0];
  372. }
  373. ZEND_API int zend_multibyte_set_filter(const zend_encoding *onetime_encoding TSRMLS_DC)
  374. {
  375. const zend_encoding *internal_encoding = zend_multibyte_get_internal_encoding(TSRMLS_C);
  376. const zend_encoding *script_encoding = onetime_encoding ? onetime_encoding: zend_multibyte_find_script_encoding(TSRMLS_C);
  377. if (!script_encoding) {
  378. return FAILURE;
  379. }
  380. /* judge input/output filter */
  381. LANG_SCNG(script_encoding) = script_encoding;
  382. LANG_SCNG(input_filter) = NULL;
  383. LANG_SCNG(output_filter) = NULL;
  384. if (!internal_encoding || LANG_SCNG(script_encoding) == internal_encoding) {
  385. if (!zend_multibyte_check_lexer_compatibility(LANG_SCNG(script_encoding))) {
  386. /* and if not, work around w/ script_encoding -> utf-8 -> script_encoding conversion */
  387. LANG_SCNG(input_filter) = encoding_filter_script_to_intermediate;
  388. LANG_SCNG(output_filter) = encoding_filter_intermediate_to_script;
  389. } else {
  390. LANG_SCNG(input_filter) = NULL;
  391. LANG_SCNG(output_filter) = NULL;
  392. }
  393. return SUCCESS;
  394. }
  395. if (zend_multibyte_check_lexer_compatibility(internal_encoding)) {
  396. LANG_SCNG(input_filter) = encoding_filter_script_to_internal;
  397. LANG_SCNG(output_filter) = NULL;
  398. } else if (zend_multibyte_check_lexer_compatibility(LANG_SCNG(script_encoding))) {
  399. LANG_SCNG(input_filter) = NULL;
  400. LANG_SCNG(output_filter) = encoding_filter_script_to_internal;
  401. } else {
  402. /* both script and internal encodings are incompatible w/ flex */
  403. LANG_SCNG(input_filter) = encoding_filter_script_to_intermediate;
  404. LANG_SCNG(output_filter) = encoding_filter_intermediate_to_internal;
  405. }
  406. return 0;
  407. }
  408. ZEND_API int open_file_for_scanning(zend_file_handle *file_handle TSRMLS_DC)
  409. {
  410. const char *file_path = NULL;
  411. char *buf;
  412. size_t size, offset = 0;
  413. /* The shebang line was read, get the current position to obtain the buffer start */
  414. if (CG(start_lineno) == 2 && file_handle->type == ZEND_HANDLE_FP && file_handle->handle.fp) {
  415. if ((offset = ftell(file_handle->handle.fp)) == -1) {
  416. offset = 0;
  417. }
  418. }
  419. if (zend_stream_fixup(file_handle, &buf, &size TSRMLS_CC) == FAILURE) {
  420. return FAILURE;
  421. }
  422. zend_llist_add_element(&CG(open_files), file_handle);
  423. if (file_handle->handle.stream.handle >= (void*)file_handle && file_handle->handle.stream.handle <= (void*)(file_handle+1)) {
  424. zend_file_handle *fh = (zend_file_handle*)zend_llist_get_last(&CG(open_files));
  425. size_t diff = (char*)file_handle->handle.stream.handle - (char*)file_handle;
  426. fh->handle.stream.handle = (void*)(((char*)fh) + diff);
  427. file_handle->handle.stream.handle = fh->handle.stream.handle;
  428. }
  429. /* Reset the scanner for scanning the new file */
  430. SCNG(yy_in) = file_handle;
  431. SCNG(yy_start) = NULL;
  432. if (size != -1) {
  433. if (CG(multibyte)) {
  434. SCNG(script_org) = (unsigned char*)buf;
  435. SCNG(script_org_size) = size;
  436. SCNG(script_filtered) = NULL;
  437. zend_multibyte_set_filter(NULL TSRMLS_CC);
  438. if (SCNG(input_filter)) {
  439. if ((size_t)-1 == SCNG(input_filter)(&SCNG(script_filtered), &SCNG(script_filtered_size), SCNG(script_org), SCNG(script_org_size) TSRMLS_CC)) {
  440. zend_error_noreturn(E_COMPILE_ERROR, "Could not convert the script from the detected "
  441. "encoding \"%s\" to a compatible encoding", zend_multibyte_get_encoding_name(LANG_SCNG(script_encoding)));
  442. }
  443. buf = (char*)SCNG(script_filtered);
  444. size = SCNG(script_filtered_size);
  445. }
  446. }
  447. SCNG(yy_start) = (unsigned char *)buf - offset;
  448. yy_scan_buffer(buf, size TSRMLS_CC);
  449. } else {
  450. zend_error_noreturn(E_COMPILE_ERROR, "zend_stream_mmap() failed");
  451. }
  452. BEGIN(INITIAL);
  453. if (file_handle->opened_path) {
  454. file_path = file_handle->opened_path;
  455. } else {
  456. file_path = file_handle->filename;
  457. }
  458. zend_set_compiled_filename(file_path TSRMLS_CC);
  459. if (CG(start_lineno)) {
  460. CG(zend_lineno) = CG(start_lineno);
  461. CG(start_lineno) = 0;
  462. } else {
  463. CG(zend_lineno) = 1;
  464. }
  465. RESET_DOC_COMMENT();
  466. CG(increment_lineno) = 0;
  467. return SUCCESS;
  468. }
  469. END_EXTERN_C()
  470. ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type TSRMLS_DC)
  471. {
  472. zend_lex_state original_lex_state;
  473. zend_op_array *op_array = (zend_op_array *) emalloc(sizeof(zend_op_array));
  474. zend_op_array *original_active_op_array = CG(active_op_array);
  475. zend_op_array *retval=NULL;
  476. int compiler_result;
  477. zend_bool compilation_successful=0;
  478. znode retval_znode;
  479. zend_bool original_in_compilation = CG(in_compilation);
  480. retval_znode.op_type = IS_CONST;
  481. INIT_PZVAL(&retval_znode.u.constant);
  482. ZVAL_LONG(&retval_znode.u.constant, 1);
  483. zend_save_lexical_state(&original_lex_state TSRMLS_CC);
  484. retval = op_array; /* success oriented */
  485. if (open_file_for_scanning(file_handle TSRMLS_CC)==FAILURE) {
  486. if (type==ZEND_REQUIRE) {
  487. zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, file_handle->filename TSRMLS_CC);
  488. zend_bailout();
  489. } else {
  490. zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, file_handle->filename TSRMLS_CC);
  491. }
  492. compilation_successful=0;
  493. } else {
  494. init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE TSRMLS_CC);
  495. CG(in_compilation) = 1;
  496. CG(active_op_array) = op_array;
  497. zend_stack_push(&CG(context_stack), (void *) &CG(context), sizeof(CG(context)));
  498. zend_init_compiler_context(TSRMLS_C);
  499. compiler_result = zendparse(TSRMLS_C);
  500. zend_do_return(&retval_znode, 0 TSRMLS_CC);
  501. CG(in_compilation) = original_in_compilation;
  502. if (compiler_result != 0) { /* parser error */
  503. zend_bailout();
  504. }
  505. compilation_successful=1;
  506. }
  507. if (retval) {
  508. CG(active_op_array) = original_active_op_array;
  509. if (compilation_successful) {
  510. pass_two(op_array TSRMLS_CC);
  511. zend_release_labels(0 TSRMLS_CC);
  512. } else {
  513. efree(op_array);
  514. retval = NULL;
  515. }
  516. }
  517. zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
  518. return retval;
  519. }
  520. zend_op_array *compile_filename(int type, zval *filename TSRMLS_DC)
  521. {
  522. zend_file_handle file_handle;
  523. zval tmp;
  524. zend_op_array *retval;
  525. char *opened_path = NULL;
  526. if (filename->type != IS_STRING) {
  527. tmp = *filename;
  528. zval_copy_ctor(&tmp);
  529. convert_to_string(&tmp);
  530. filename = &tmp;
  531. }
  532. file_handle.filename = Z_STRVAL_P(filename);
  533. file_handle.free_filename = 0;
  534. file_handle.type = ZEND_HANDLE_FILENAME;
  535. file_handle.opened_path = NULL;
  536. file_handle.handle.fp = NULL;
  537. retval = zend_compile_file(&file_handle, type TSRMLS_CC);
  538. if (retval && file_handle.handle.stream.handle) {
  539. int dummy = 1;
  540. if (!file_handle.opened_path) {
  541. file_handle.opened_path = opened_path = estrndup(Z_STRVAL_P(filename), Z_STRLEN_P(filename));
  542. }
  543. zend_hash_add(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1, (void *)&dummy, sizeof(int), NULL);
  544. if (opened_path) {
  545. efree(opened_path);
  546. }
  547. }
  548. zend_destroy_file_handle(&file_handle TSRMLS_CC);
  549. if (filename==&tmp) {
  550. zval_dtor(&tmp);
  551. }
  552. return retval;
  553. }
  554. ZEND_API int zend_prepare_string_for_scanning(zval *str, char *filename TSRMLS_DC)
  555. {
  556. char *buf;
  557. size_t size;
  558. /* enforce ZEND_MMAP_AHEAD trailing NULLs for flex... */
  559. Z_STRVAL_P(str) = str_erealloc(Z_STRVAL_P(str), Z_STRLEN_P(str) + ZEND_MMAP_AHEAD);
  560. memset(Z_STRVAL_P(str) + Z_STRLEN_P(str), 0, ZEND_MMAP_AHEAD);
  561. SCNG(yy_in) = NULL;
  562. SCNG(yy_start) = NULL;
  563. buf = Z_STRVAL_P(str);
  564. size = Z_STRLEN_P(str);
  565. if (CG(multibyte)) {
  566. SCNG(script_org) = (unsigned char*)buf;
  567. SCNG(script_org_size) = size;
  568. SCNG(script_filtered) = NULL;
  569. zend_multibyte_set_filter(zend_multibyte_get_internal_encoding(TSRMLS_C) TSRMLS_CC);
  570. if (SCNG(input_filter)) {
  571. if ((size_t)-1 == SCNG(input_filter)(&SCNG(script_filtered), &SCNG(script_filtered_size), SCNG(script_org), SCNG(script_org_size) TSRMLS_CC)) {
  572. zend_error_noreturn(E_COMPILE_ERROR, "Could not convert the script from the detected "
  573. "encoding \"%s\" to a compatible encoding", zend_multibyte_get_encoding_name(LANG_SCNG(script_encoding)));
  574. }
  575. buf = (char*)SCNG(script_filtered);
  576. size = SCNG(script_filtered_size);
  577. }
  578. }
  579. yy_scan_buffer(buf, size TSRMLS_CC);
  580. zend_set_compiled_filename(filename TSRMLS_CC);
  581. CG(zend_lineno) = 1;
  582. CG(increment_lineno) = 0;
  583. RESET_DOC_COMMENT();
  584. return SUCCESS;
  585. }
  586. ZEND_API size_t zend_get_scanned_file_offset(TSRMLS_D)
  587. {
  588. size_t offset = SCNG(yy_cursor) - SCNG(yy_start);
  589. if (SCNG(input_filter)) {
  590. size_t original_offset = offset, length = 0;
  591. do {
  592. unsigned char *p = NULL;
  593. if ((size_t)-1 == SCNG(input_filter)(&p, &length, SCNG(script_org), offset TSRMLS_CC)) {
  594. return (size_t)-1;
  595. }
  596. efree(p);
  597. if (length > original_offset) {
  598. offset--;
  599. } else if (length < original_offset) {
  600. offset++;
  601. }
  602. } while (original_offset != length);
  603. }
  604. return offset;
  605. }
  606. zend_op_array *compile_string(zval *source_string, char *filename TSRMLS_DC)
  607. {
  608. zend_lex_state original_lex_state;
  609. zend_op_array *op_array = (zend_op_array *) emalloc(sizeof(zend_op_array));
  610. zend_op_array *original_active_op_array = CG(active_op_array);
  611. zend_op_array *retval;
  612. zval tmp;
  613. int compiler_result;
  614. zend_bool original_in_compilation = CG(in_compilation);
  615. if (Z_STRLEN_P(source_string)==0) {
  616. efree(op_array);
  617. return NULL;
  618. }
  619. CG(in_compilation) = 1;
  620. tmp = *source_string;
  621. zval_copy_ctor(&tmp);
  622. convert_to_string(&tmp);
  623. source_string = &tmp;
  624. zend_save_lexical_state(&original_lex_state TSRMLS_CC);
  625. if (zend_prepare_string_for_scanning(source_string, filename TSRMLS_CC)==FAILURE) {
  626. efree(op_array);
  627. retval = NULL;
  628. } else {
  629. zend_bool orig_interactive = CG(interactive);
  630. CG(interactive) = 0;
  631. init_op_array(op_array, ZEND_EVAL_CODE, INITIAL_OP_ARRAY_SIZE TSRMLS_CC);
  632. CG(interactive) = orig_interactive;
  633. CG(active_op_array) = op_array;
  634. zend_stack_push(&CG(context_stack), (void *) &CG(context), sizeof(CG(context)));
  635. zend_init_compiler_context(TSRMLS_C);
  636. BEGIN(ST_IN_SCRIPTING);
  637. compiler_result = zendparse(TSRMLS_C);
  638. if (SCNG(script_filtered)) {
  639. efree(SCNG(script_filtered));
  640. SCNG(script_filtered) = NULL;
  641. }
  642. if (compiler_result != 0) {
  643. CG(active_op_array) = original_active_op_array;
  644. CG(unclean_shutdown)=1;
  645. destroy_op_array(op_array TSRMLS_CC);
  646. efree(op_array);
  647. retval = NULL;
  648. } else {
  649. zend_do_return(NULL, 0 TSRMLS_CC);
  650. CG(active_op_array) = original_active_op_array;
  651. pass_two(op_array TSRMLS_CC);
  652. zend_release_labels(0 TSRMLS_CC);
  653. retval = op_array;
  654. }
  655. }
  656. zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
  657. zval_dtor(&tmp);
  658. CG(in_compilation) = original_in_compilation;
  659. return retval;
  660. }
  661. BEGIN_EXTERN_C()
  662. int highlight_file(char *filename, zend_syntax_highlighter_ini *syntax_highlighter_ini TSRMLS_DC)
  663. {
  664. zend_lex_state original_lex_state;
  665. zend_file_handle file_handle;
  666. file_handle.type = ZEND_HANDLE_FILENAME;
  667. file_handle.filename = filename;
  668. file_handle.free_filename = 0;
  669. file_handle.opened_path = NULL;
  670. zend_save_lexical_state(&original_lex_state TSRMLS_CC);
  671. if (open_file_for_scanning(&file_handle TSRMLS_CC)==FAILURE) {
  672. zend_message_dispatcher(ZMSG_FAILED_HIGHLIGHT_FOPEN, filename TSRMLS_CC);
  673. zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
  674. return FAILURE;
  675. }
  676. zend_highlight(syntax_highlighter_ini TSRMLS_CC);
  677. if (SCNG(script_filtered)) {
  678. efree(SCNG(script_filtered));
  679. SCNG(script_filtered) = NULL;
  680. }
  681. zend_destroy_file_handle(&file_handle TSRMLS_CC);
  682. zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
  683. return SUCCESS;
  684. }
  685. int highlight_string(zval *str, zend_syntax_highlighter_ini *syntax_highlighter_ini, char *str_name TSRMLS_DC)
  686. {
  687. zend_lex_state original_lex_state;
  688. zval tmp = *str;
  689. str = &tmp;
  690. zval_copy_ctor(str);
  691. zend_save_lexical_state(&original_lex_state TSRMLS_CC);
  692. if (zend_prepare_string_for_scanning(str, str_name TSRMLS_CC)==FAILURE) {
  693. zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
  694. return FAILURE;
  695. }
  696. BEGIN(INITIAL);
  697. zend_highlight(syntax_highlighter_ini TSRMLS_CC);
  698. if (SCNG(script_filtered)) {
  699. efree(SCNG(script_filtered));
  700. SCNG(script_filtered) = NULL;
  701. }
  702. zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
  703. zval_dtor(str);
  704. return SUCCESS;
  705. }
  706. ZEND_API void zend_multibyte_yyinput_again(zend_encoding_filter old_input_filter, const zend_encoding *old_encoding TSRMLS_DC)
  707. {
  708. size_t length;
  709. unsigned char *new_yy_start;
  710. /* convert and set */
  711. if (!SCNG(input_filter)) {
  712. if (SCNG(script_filtered)) {
  713. efree(SCNG(script_filtered));
  714. SCNG(script_filtered) = NULL;
  715. }
  716. SCNG(script_filtered_size) = 0;
  717. length = SCNG(script_org_size);
  718. new_yy_start = SCNG(script_org);
  719. } else {
  720. if ((size_t)-1 == SCNG(input_filter)(&new_yy_start, &length, SCNG(script_org), SCNG(script_org_size) TSRMLS_CC)) {
  721. zend_error_noreturn(E_COMPILE_ERROR, "Could not convert the script from the detected "
  722. "encoding \"%s\" to a compatible encoding", zend_multibyte_get_encoding_name(LANG_SCNG(script_encoding)));
  723. }
  724. SCNG(script_filtered) = new_yy_start;
  725. SCNG(script_filtered_size) = length;
  726. }
  727. SCNG(yy_cursor) = new_yy_start + (SCNG(yy_cursor) - SCNG(yy_start));
  728. SCNG(yy_marker) = new_yy_start + (SCNG(yy_marker) - SCNG(yy_start));
  729. SCNG(yy_text) = new_yy_start + (SCNG(yy_text) - SCNG(yy_start));
  730. SCNG(yy_limit) = new_yy_start + (SCNG(yy_limit) - SCNG(yy_start));
  731. SCNG(yy_start) = new_yy_start;
  732. }
  733. # define zend_copy_value(zendlval, yytext, yyleng) \
  734. if (SCNG(output_filter)) { \
  735. size_t sz = 0; \
  736. SCNG(output_filter)((unsigned char **)&Z_STRVAL_P(zendlval), &sz, (unsigned char *)yytext, (size_t)yyleng TSRMLS_CC); \
  737. Z_STRLEN_P(zendlval) = sz; \
  738. } else { \
  739. Z_STRVAL_P(zendlval) = (char *) estrndup(yytext, yyleng); \
  740. Z_STRLEN_P(zendlval) = yyleng; \
  741. }
  742. static void zend_scan_escape_string(zval *zendlval, char *str, int len, char quote_type TSRMLS_DC)
  743. {
  744. register char *s, *t;
  745. char *end;
  746. ZVAL_STRINGL(zendlval, str, len, 1);
  747. /* convert escape sequences */
  748. s = t = Z_STRVAL_P(zendlval);
  749. end = s+Z_STRLEN_P(zendlval);
  750. while (s<end) {
  751. if (*s=='\\') {
  752. s++;
  753. if (s >= end) {
  754. *t++ = '\\';
  755. break;
  756. }
  757. switch(*s) {
  758. case 'n':
  759. *t++ = '\n';
  760. Z_STRLEN_P(zendlval)--;
  761. break;
  762. case 'r':
  763. *t++ = '\r';
  764. Z_STRLEN_P(zendlval)--;
  765. break;
  766. case 't':
  767. *t++ = '\t';
  768. Z_STRLEN_P(zendlval)--;
  769. break;
  770. case 'f':
  771. *t++ = '\f';
  772. Z_STRLEN_P(zendlval)--;
  773. break;
  774. case 'v':
  775. *t++ = '\v';
  776. Z_STRLEN_P(zendlval)--;
  777. break;
  778. case 'e':
  779. #ifdef PHP_WIN32
  780. *t++ = VK_ESCAPE;
  781. #else
  782. *t++ = '\e';
  783. #endif
  784. Z_STRLEN_P(zendlval)--;
  785. break;
  786. case '"':
  787. case '`':
  788. if (*s != quote_type) {
  789. *t++ = '\\';
  790. *t++ = *s;
  791. break;
  792. }
  793. case '\\':
  794. case '$':
  795. *t++ = *s;
  796. Z_STRLEN_P(zendlval)--;
  797. break;
  798. case 'x':
  799. case 'X':
  800. if (ZEND_IS_HEX(*(s+1))) {
  801. char hex_buf[3] = { 0, 0, 0 };
  802. Z_STRLEN_P(zendlval)--; /* for the 'x' */
  803. hex_buf[0] = *(++s);
  804. Z_STRLEN_P(zendlval)--;
  805. if (ZEND_IS_HEX(*(s+1))) {
  806. hex_buf[1] = *(++s);
  807. Z_STRLEN_P(zendlval)--;
  808. }
  809. *t++ = (char) strtol(hex_buf, NULL, 16);
  810. } else {
  811. *t++ = '\\';
  812. *t++ = *s;
  813. }
  814. break;
  815. default:
  816. /* check for an octal */
  817. if (ZEND_IS_OCT(*s)) {
  818. char octal_buf[4] = { 0, 0, 0, 0 };
  819. octal_buf[0] = *s;
  820. Z_STRLEN_P(zendlval)--;
  821. if (ZEND_IS_OCT(*(s+1))) {
  822. octal_buf[1] = *(++s);
  823. Z_STRLEN_P(zendlval)--;
  824. if (ZEND_IS_OCT(*(s+1))) {
  825. octal_buf[2] = *(++s);
  826. Z_STRLEN_P(zendlval)--;
  827. }
  828. }
  829. *t++ = (char) strtol(octal_buf, NULL, 8);
  830. } else {
  831. *t++ = '\\';
  832. *t++ = *s;
  833. }
  834. break;
  835. }
  836. } else {
  837. *t++ = *s;
  838. }
  839. if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
  840. CG(zend_lineno)++;
  841. }
  842. s++;
  843. }
  844. *t = 0;
  845. if (SCNG(output_filter)) {
  846. size_t sz = 0;
  847. s = Z_STRVAL_P(zendlval);
  848. SCNG(output_filter)((unsigned char **)&Z_STRVAL_P(zendlval), &sz, (unsigned char *)s, (size_t)Z_STRLEN_P(zendlval) TSRMLS_CC);
  849. Z_STRLEN_P(zendlval) = sz;
  850. efree(s);
  851. }
  852. }
  853. int lex_scan(zval *zendlval TSRMLS_DC)
  854. {
  855. restart:
  856. SCNG(yy_text) = YYCURSOR;
  857. yymore_restart:
  858. /*!re2c
  859. re2c:yyfill:check = 0;
  860. LNUM [0-9]+
  861. DNUM ([0-9]*"."[0-9]+)|([0-9]+"."[0-9]*)
  862. EXPONENT_DNUM (({LNUM}|{DNUM})[eE][+-]?{LNUM})
  863. HNUM "0x"[0-9a-fA-F]+
  864. BNUM "0b"[01]+
  865. LABEL [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
  866. WHITESPACE [ \n\r\t]+
  867. TABS_AND_SPACES [ \t]*
  868. TOKENS [;:,.\[\]()|^&+-/*=%!~$<>?@]
  869. ANY_CHAR [^]
  870. NEWLINE ("\r"|"\n"|"\r\n")
  871. /* compute yyleng before each rule */
  872. <!*> := yyleng = YYCURSOR - SCNG(yy_text);
  873. <ST_IN_SCRIPTING>"exit" {
  874. return T_EXIT;
  875. }
  876. <ST_IN_SCRIPTING>"die" {
  877. return T_EXIT;
  878. }
  879. <ST_IN_SCRIPTING>"function" {
  880. return T_FUNCTION;
  881. }
  882. <ST_IN_SCRIPTING>"const" {
  883. return T_CONST;
  884. }
  885. <ST_IN_SCRIPTING>"return" {
  886. return T_RETURN;
  887. }
  888. <ST_IN_SCRIPTING>"yield" {
  889. return T_YIELD;
  890. }
  891. <ST_IN_SCRIPTING>"try" {
  892. return T_TRY;
  893. }
  894. <ST_IN_SCRIPTING>"catch" {
  895. return T_CATCH;
  896. }
  897. <ST_IN_SCRIPTING>"finally" {
  898. return T_FINALLY;
  899. }
  900. <ST_IN_SCRIPTING>"throw" {
  901. return T_THROW;
  902. }
  903. <ST_IN_SCRIPTING>"if" {
  904. return T_IF;
  905. }
  906. <ST_IN_SCRIPTING>"elseif" {
  907. return T_ELSEIF;
  908. }
  909. <ST_IN_SCRIPTING>"endif" {
  910. return T_ENDIF;
  911. }
  912. <ST_IN_SCRIPTING>"else" {
  913. return T_ELSE;
  914. }
  915. <ST_IN_SCRIPTING>"while" {
  916. return T_WHILE;
  917. }
  918. <ST_IN_SCRIPTING>"endwhile" {
  919. return T_ENDWHILE;
  920. }
  921. <ST_IN_SCRIPTING>"do" {
  922. return T_DO;
  923. }
  924. <ST_IN_SCRIPTING>"for" {
  925. return T_FOR;
  926. }
  927. <ST_IN_SCRIPTING>"endfor" {
  928. return T_ENDFOR;
  929. }
  930. <ST_IN_SCRIPTING>"foreach" {
  931. return T_FOREACH;
  932. }
  933. <ST_IN_SCRIPTING>"endforeach" {
  934. return T_ENDFOREACH;
  935. }
  936. <ST_IN_SCRIPTING>"declare" {
  937. return T_DECLARE;
  938. }
  939. <ST_IN_SCRIPTING>"enddeclare" {
  940. return T_ENDDECLARE;
  941. }
  942. <ST_IN_SCRIPTING>"instanceof" {
  943. return T_INSTANCEOF;
  944. }
  945. <ST_IN_SCRIPTING>"as" {
  946. return T_AS;
  947. }
  948. <ST_IN_SCRIPTING>"switch" {
  949. return T_SWITCH;
  950. }
  951. <ST_IN_SCRIPTING>"endswitch" {
  952. return T_ENDSWITCH;
  953. }
  954. <ST_IN_SCRIPTING>"case" {
  955. return T_CASE;
  956. }
  957. <ST_IN_SCRIPTING>"default" {
  958. return T_DEFAULT;
  959. }
  960. <ST_IN_SCRIPTING>"break" {
  961. return T_BREAK;
  962. }
  963. <ST_IN_SCRIPTING>"continue" {
  964. return T_CONTINUE;
  965. }
  966. <ST_IN_SCRIPTING>"goto" {
  967. return T_GOTO;
  968. }
  969. <ST_IN_SCRIPTING>"echo" {
  970. return T_ECHO;
  971. }
  972. <ST_IN_SCRIPTING>"print" {
  973. return T_PRINT;
  974. }
  975. <ST_IN_SCRIPTING>"class" {
  976. return T_CLASS;
  977. }
  978. <ST_IN_SCRIPTING>"interface" {
  979. return T_INTERFACE;
  980. }
  981. <ST_IN_SCRIPTING>"trait" {
  982. return T_TRAIT;
  983. }
  984. <ST_IN_SCRIPTING>"extends" {
  985. return T_EXTENDS;
  986. }
  987. <ST_IN_SCRIPTING>"implements" {
  988. return T_IMPLEMENTS;
  989. }
  990. <ST_IN_SCRIPTING>"->" {
  991. yy_push_state(ST_LOOKING_FOR_PROPERTY TSRMLS_CC);
  992. return T_OBJECT_OPERATOR;
  993. }
  994. <ST_IN_SCRIPTING,ST_LOOKING_FOR_PROPERTY>{WHITESPACE}+ {
  995. ZVAL_STRINGL(zendlval, yytext, yyleng, 0); /* no copying - intentional */
  996. HANDLE_NEWLINES(yytext, yyleng);
  997. return T_WHITESPACE;
  998. }
  999. <ST_LOOKING_FOR_PROPERTY>"->" {
  1000. return T_OBJECT_OPERATOR;
  1001. }
  1002. <ST_LOOKING_FOR_PROPERTY>{LABEL} {
  1003. yy_pop_state(TSRMLS_C);
  1004. zend_copy_value(zendlval, yytext, yyleng);
  1005. zendlval->type = IS_STRING;
  1006. return T_STRING;
  1007. }
  1008. <ST_LOOKING_FOR_PROPERTY>{ANY_CHAR} {
  1009. yyless(0);
  1010. yy_pop_state(TSRMLS_C);
  1011. goto restart;
  1012. }
  1013. <ST_IN_SCRIPTING>"::" {
  1014. return T_PAAMAYIM_NEKUDOTAYIM;
  1015. }
  1016. <ST_IN_SCRIPTING>"\\" {
  1017. return T_NS_SEPARATOR;
  1018. }
  1019. <ST_IN_SCRIPTING>"..." {
  1020. return T_ELLIPSIS;
  1021. }
  1022. <ST_IN_SCRIPTING>"new" {
  1023. return T_NEW;
  1024. }
  1025. <ST_IN_SCRIPTING>"clone" {
  1026. return T_CLONE;
  1027. }
  1028. <ST_IN_SCRIPTING>"var" {
  1029. return T_VAR;
  1030. }
  1031. <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("int"|"integer"){TABS_AND_SPACES}")" {
  1032. return T_INT_CAST;
  1033. }
  1034. <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("real"|"double"|"float"){TABS_AND_SPACES}")" {
  1035. return T_DOUBLE_CAST;
  1036. }
  1037. <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("string"|"binary"){TABS_AND_SPACES}")" {
  1038. return T_STRING_CAST;
  1039. }
  1040. <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"array"{TABS_AND_SPACES}")" {
  1041. return T_ARRAY_CAST;
  1042. }
  1043. <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"object"{TABS_AND_SPACES}")" {
  1044. return T_OBJECT_CAST;
  1045. }
  1046. <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("bool"|"boolean"){TABS_AND_SPACES}")" {
  1047. return T_BOOL_CAST;
  1048. }
  1049. <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("unset"){TABS_AND_SPACES}")" {
  1050. return T_UNSET_CAST;
  1051. }
  1052. <ST_IN_SCRIPTING>"eval" {
  1053. return T_EVAL;
  1054. }
  1055. <ST_IN_SCRIPTING>"include" {
  1056. return T_INCLUDE;
  1057. }
  1058. <ST_IN_SCRIPTING>"include_once" {
  1059. return T_INCLUDE_ONCE;
  1060. }
  1061. <ST_IN_SCRIPTING>"require" {
  1062. return T_REQUIRE;
  1063. }
  1064. <ST_IN_SCRIPTING>"require_once" {
  1065. return T_REQUIRE_ONCE;
  1066. }
  1067. <ST_IN_SCRIPTING>"namespace" {
  1068. return T_NAMESPACE;
  1069. }
  1070. <ST_IN_SCRIPTING>"use" {
  1071. return T_USE;
  1072. }
  1073. <ST_IN_SCRIPTING>"insteadof" {
  1074. return T_INSTEADOF;
  1075. }
  1076. <ST_IN_SCRIPTING>"global" {
  1077. return T_GLOBAL;
  1078. }
  1079. <ST_IN_SCRIPTING>"isset" {
  1080. return T_ISSET;
  1081. }
  1082. <ST_IN_SCRIPTING>"empty" {
  1083. return T_EMPTY;
  1084. }
  1085. <ST_IN_SCRIPTING>"__halt_compiler" {
  1086. return T_HALT_COMPILER;
  1087. }
  1088. <ST_IN_SCRIPTING>"static" {
  1089. return T_STATIC;
  1090. }
  1091. <ST_IN_SCRIPTING>"abstract" {
  1092. return T_ABSTRACT;
  1093. }
  1094. <ST_IN_SCRIPTING>"final" {
  1095. return T_FINAL;
  1096. }
  1097. <ST_IN_SCRIPTING>"private" {
  1098. return T_PRIVATE;
  1099. }
  1100. <ST_IN_SCRIPTING>"protected" {
  1101. return T_PROTECTED;
  1102. }
  1103. <ST_IN_SCRIPTING>"public" {
  1104. return T_PUBLIC;
  1105. }
  1106. <ST_IN_SCRIPTING>"unset" {
  1107. return T_UNSET;
  1108. }
  1109. <ST_IN_SCRIPTING>"=>" {
  1110. return T_DOUBLE_ARROW;
  1111. }
  1112. <ST_IN_SCRIPTING>"list" {
  1113. return T_LIST;
  1114. }
  1115. <ST_IN_SCRIPTING>"array" {
  1116. return T_ARRAY;
  1117. }
  1118. <ST_IN_SCRIPTING>"callable" {
  1119. return T_CALLABLE;
  1120. }
  1121. <ST_IN_SCRIPTING>"++" {
  1122. return T_INC;
  1123. }
  1124. <ST_IN_SCRIPTING>"--" {
  1125. return T_DEC;
  1126. }
  1127. <ST_IN_SCRIPTING>"===" {
  1128. return T_IS_IDENTICAL;
  1129. }
  1130. <ST_IN_SCRIPTING>"!==" {
  1131. return T_IS_NOT_IDENTICAL;
  1132. }
  1133. <ST_IN_SCRIPTING>"==" {
  1134. return T_IS_EQUAL;
  1135. }
  1136. <ST_IN_SCRIPTING>"!="|"<>" {
  1137. return T_IS_NOT_EQUAL;
  1138. }
  1139. <ST_IN_SCRIPTING>"<=" {
  1140. return T_IS_SMALLER_OR_EQUAL;
  1141. }
  1142. <ST_IN_SCRIPTING>">=" {
  1143. return T_IS_GREATER_OR_EQUAL;
  1144. }
  1145. <ST_IN_SCRIPTING>"+=" {
  1146. return T_PLUS_EQUAL;
  1147. }
  1148. <ST_IN_SCRIPTING>"-=" {
  1149. return T_MINUS_EQUAL;
  1150. }
  1151. <ST_IN_SCRIPTING>"*=" {
  1152. return T_MUL_EQUAL;
  1153. }
  1154. <ST_IN_SCRIPTING>"*\*" {
  1155. return T_POW;
  1156. }
  1157. <ST_IN_SCRIPTING>"*\*=" {
  1158. return T_POW_EQUAL;
  1159. }
  1160. <ST_IN_SCRIPTING>"/=" {
  1161. return T_DIV_EQUAL;
  1162. }
  1163. <ST_IN_SCRIPTING>".=" {
  1164. return T_CONCAT_EQUAL;
  1165. }
  1166. <ST_IN_SCRIPTING>"%=" {
  1167. return T_MOD_EQUAL;
  1168. }
  1169. <ST_IN_SCRIPTING>"<<=" {
  1170. return T_SL_EQUAL;
  1171. }
  1172. <ST_IN_SCRIPTING>">>=" {
  1173. return T_SR_EQUAL;
  1174. }
  1175. <ST_IN_SCRIPTING>"&=" {
  1176. return T_AND_EQUAL;
  1177. }
  1178. <ST_IN_SCRIPTING>"|=" {
  1179. return T_OR_EQUAL;
  1180. }
  1181. <ST_IN_SCRIPTING>"^=" {
  1182. return T_XOR_EQUAL;
  1183. }
  1184. <ST_IN_SCRIPTING>"||" {
  1185. return T_BOOLEAN_OR;
  1186. }
  1187. <ST_IN_SCRIPTING>"&&" {
  1188. return T_BOOLEAN_AND;
  1189. }
  1190. <ST_IN_SCRIPTING>"OR" {
  1191. return T_LOGICAL_OR;
  1192. }
  1193. <ST_IN_SCRIPTING>"AND" {
  1194. return T_LOGICAL_AND;
  1195. }
  1196. <ST_IN_SCRIPTING>"XOR" {
  1197. return T_LOGICAL_XOR;
  1198. }
  1199. <ST_IN_SCRIPTING>"<<" {
  1200. return T_SL;
  1201. }
  1202. <ST_IN_SCRIPTING>">>" {
  1203. return T_SR;
  1204. }
  1205. <ST_IN_SCRIPTING>{TOKENS} {
  1206. return yytext[0];
  1207. }
  1208. <ST_IN_SCRIPTING>"{" {
  1209. yy_push_state(ST_IN_SCRIPTING TSRMLS_CC);
  1210. return '{';
  1211. }
  1212. <ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"${" {
  1213. yy_push_state(ST_LOOKING_FOR_VARNAME TSRMLS_CC);
  1214. return T_DOLLAR_OPEN_CURLY_BRACES;
  1215. }
  1216. <ST_IN_SCRIPTING>"}" {
  1217. RESET_DOC_COMMENT();
  1218. if (!zend_stack_is_empty(&SCNG(state_stack))) {
  1219. yy_pop_state(TSRMLS_C);
  1220. }
  1221. return '}';
  1222. }
  1223. <ST_LOOKING_FOR_VARNAME>{LABEL}[[}] {
  1224. yyless(yyleng - 1);
  1225. zend_copy_value(zendlval, yytext, yyleng);
  1226. zendlval->type = IS_STRING;
  1227. yy_pop_state(TSRMLS_C);
  1228. yy_push_state(ST_IN_SCRIPTING TSRMLS_CC);
  1229. return T_STRING_VARNAME;
  1230. }
  1231. <ST_LOOKING_FOR_VARNAME>{ANY_CHAR} {
  1232. yyless(0);
  1233. yy_pop_state(TSRMLS_C);
  1234. yy_push_state(ST_IN_SCRIPTING TSRMLS_CC);
  1235. goto restart;
  1236. }
  1237. <ST_IN_SCRIPTING>{BNUM} {
  1238. char *bin = yytext + 2; /* Skip "0b" */
  1239. int len = yyleng - 2;
  1240. /* Skip any leading 0s */
  1241. while (*bin == '0') {
  1242. ++bin;
  1243. --len;
  1244. }
  1245. if (len < SIZEOF_LONG * 8) {
  1246. if (len == 0) {
  1247. Z_LVAL_P(zendlval) = 0;
  1248. } else {
  1249. Z_LVAL_P(zendlval) = strtol(bin, NULL, 2);
  1250. }
  1251. zendlval->type = IS_LONG;
  1252. return T_LNUMBER;
  1253. } else {
  1254. ZVAL_DOUBLE(zendlval, zend_bin_strtod(bin, NULL));
  1255. return T_DNUMBER;
  1256. }
  1257. }
  1258. <ST_IN_SCRIPTING>{LNUM} {
  1259. if (yyleng < MAX_LENGTH_OF_LONG - 1) { /* Won't overflow */
  1260. Z_LVAL_P(zendlval) = strtol(yytext, NULL, 0);
  1261. } else {
  1262. errno = 0;
  1263. Z_LVAL_P(zendlval) = strtol(yytext, NULL, 0);
  1264. if (errno == ERANGE) { /* Overflow */
  1265. if (yytext[0] == '0') { /* octal overflow */
  1266. Z_DVAL_P(zendlval) = zend_oct_strtod(yytext, NULL);
  1267. } else {
  1268. Z_DVAL_P(zendlval) = zend_strtod(yytext, NULL);
  1269. }
  1270. zendlval->type = IS_DOUBLE;
  1271. return T_DNUMBER;
  1272. }
  1273. }
  1274. zendlval->type = IS_LONG;
  1275. return T_LNUMBER;
  1276. }
  1277. <ST_IN_SCRIPTING>{HNUM} {
  1278. char *hex = yytext + 2; /* Skip "0x" */
  1279. int len = yyleng - 2;
  1280. /* Skip any leading 0s */
  1281. while (*hex == '0') {
  1282. hex++;
  1283. len--;
  1284. }
  1285. if (len < SIZEOF_LONG * 2 || (len == SIZEOF_LONG * 2 && *hex <= '7')) {
  1286. if (len == 0) {
  1287. Z_LVAL_P(zendlval) = 0;
  1288. } else {
  1289. Z_LVAL_P(zendlval) = strtol(hex, NULL, 16);
  1290. }
  1291. zendlval->type = IS_LONG;
  1292. return T_LNUMBER;
  1293. } else {
  1294. ZVAL_DOUBLE(zendlval, zend_hex_strtod(hex, NULL));
  1295. return T_DNUMBER;
  1296. }
  1297. }
  1298. <ST_VAR_OFFSET>[0]|([1-9][0-9]*) { /* Offset could be treated as a long */
  1299. if (yyleng < MAX_LENGTH_OF_LONG - 1 || (yyleng == MAX_LENGTH_OF_LONG - 1 && strcmp(yytext, long_min_digits) < 0)) {
  1300. ZVAL_LONG(zendlval, strtol(yytext, NULL, 10));
  1301. } else {
  1302. ZVAL_STRINGL(zendlval, yytext, yyleng, 1);
  1303. }
  1304. return T_NUM_STRING;
  1305. }
  1306. <ST_VAR_OFFSET>{LNUM}|{HNUM}|{BNUM} { /* Offset must be treated as a string */
  1307. ZVAL_STRINGL(zendlval, yytext, yyleng, 1);
  1308. return T_NUM_STRING;
  1309. }
  1310. <ST_IN_SCRIPTING>{DNUM}|{EXPONENT_DNUM} {
  1311. ZVAL_DOUBLE(zendlval, zend_strtod(yytext, NULL));
  1312. return T_DNUMBER;
  1313. }
  1314. <ST_IN_SCRIPTING>"__CLASS__" {
  1315. zend_class_entry *ce = CG(active_class_entry);
  1316. if (ce && ZEND_ACC_TRAIT == (ce->ce_flags & ZEND_ACC_TRAIT)) {
  1317. /* We create a special __CLASS__ constant that is going to be resolved
  1318. at run-time */
  1319. Z_STRLEN_P(zendlval) = sizeof("__CLASS__")-1;
  1320. Z_STRVAL_P(zendlval) = estrndup("__CLASS__", Z_STRLEN_P(zendlval));
  1321. zendlval->type = IS_CONSTANT;
  1322. } else {
  1323. if (ce && ce->name) {
  1324. ZVAL_STRINGL(zendlval, ce->name, ce->name_length, 1);
  1325. } else {
  1326. ZVAL_EMPTY_STRING(zendlval);
  1327. }
  1328. }
  1329. return T_CLASS_C;
  1330. }
  1331. <ST_IN_SCRIPTING>"__TRAIT__" {
  1332. zend_class_entry *ce = CG(active_class_entry);
  1333. if (ce && ce->name && ZEND_ACC_TRAIT == (ce->ce_flags & ZEND_ACC_TRAIT)) {
  1334. ZVAL_STRINGL(zendlval, ce->name, ce->name_length, 1);
  1335. } else {
  1336. ZVAL_EMPTY_STRING(zendlval);
  1337. }
  1338. return T_TRAIT_C;
  1339. }
  1340. <ST_IN_SCRIPTING>"__FUNCTION__" {
  1341. zend_op_array *op_array = CG(active_op_array);
  1342. if (op_array && op_array->function_name) {
  1343. ZVAL_STRING(zendlval, op_array->function_name, 1);
  1344. } else {
  1345. ZVAL_EMPTY_STRING(zendlval);
  1346. }
  1347. return T_FUNC_C;
  1348. }
  1349. <ST_IN_SCRIPTING>"__METHOD__" {
  1350. const char *class_name = CG(active_class_entry) ? CG(active_class_entry)->name : NULL;
  1351. const char *func_name = CG(active_op_array)? CG(active_op_array)->function_name : NULL;
  1352. Z_STRLEN_P(zendlval) = zend_spprintf(&Z_STRVAL_P(zendlval), 0, "%s%s%s",
  1353. class_name ? class_name : "",
  1354. class_name && func_name ? "::" : "",
  1355. func_name ? func_name : ""
  1356. );
  1357. zendlval->type = IS_STRING;
  1358. return T_METHOD_C;
  1359. }
  1360. <ST_IN_SCRIPTING>"__LINE__" {
  1361. ZVAL_LONG(zendlval, CG(zend_lineno));
  1362. return T_LINE;
  1363. }
  1364. <ST_IN_SCRIPTING>"__FILE__" {
  1365. char *filename = zend_get_compiled_filename(TSRMLS_C);
  1366. if (!filename) {
  1367. filename = "";
  1368. }
  1369. ZVAL_STRING(zendlval, filename, 1);
  1370. return T_FILE;
  1371. }
  1372. <ST_IN_SCRIPTING>"__DIR__" {
  1373. char *filename = zend_get_compiled_filename(TSRMLS_C);
  1374. const size_t filename_len = strlen(filename);
  1375. char *dirname;
  1376. if (!filename) {
  1377. filename = "";
  1378. }
  1379. dirname = estrndup(filename, filename_len);
  1380. zend_dirname(dirname, filename_len);
  1381. if (strcmp(dirname, ".") == 0) {
  1382. dirname = erealloc(dirname, MAXPATHLEN);
  1383. #if HAVE_GETCWD
  1384. VCWD_GETCWD(dirname, MAXPATHLEN);
  1385. #elif HAVE_GETWD
  1386. VCWD_GETWD(dirname);
  1387. #endif
  1388. }
  1389. ZVAL_STRING(zendlval, dirname, 0);
  1390. return T_DIR;
  1391. }
  1392. <ST_IN_SCRIPTING>"__NAMESPACE__" {
  1393. if (CG(current_namespace)) {
  1394. *zendlval = *CG(current_namespace);
  1395. zval_copy_ctor(zendlval);
  1396. } else {
  1397. ZVAL_EMPTY_STRING(zendlval);
  1398. }
  1399. return T_NS_C;
  1400. }
  1401. <INITIAL>"<script"{WHITESPACE}+"language"{WHITESPACE}*"="{WHITESPACE}*("php"|"\"php\""|"'php'"){WHITESPACE}*">" {
  1402. YYCTYPE *bracket = (YYCTYPE*)zend_memrchr(yytext, '<', yyleng - (sizeof("script language=php>") - 1));
  1403. if (bracket != SCNG(yy_text)) {
  1404. /* Handle previously scanned HTML, as possible <script> tags found are assumed to not be PHP's */
  1405. YYCURSOR = bracket;
  1406. goto inline_html;
  1407. }
  1408. HANDLE_NEWLINES(yytext, yyleng);
  1409. ZVAL_STRINGL(zendlval, yytext, yyleng, 0); /* no copying - intentional */
  1410. BEGIN(ST_IN_SCRIPTING);
  1411. return T_OPEN_TAG;
  1412. }
  1413. <INITIAL>"<%=" {
  1414. if (CG(asp_tags)) {
  1415. ZVAL_STRINGL(zendlval, yytext, yyleng, 0); /* no copying - intentional */
  1416. BEGIN(ST_IN_SCRIPTING);
  1417. return T_OPEN_TAG_WITH_ECHO;
  1418. } else {
  1419. goto inline_char_handler;
  1420. }
  1421. }
  1422. <INITIAL>"<?=" {
  1423. ZVAL_STRINGL(zendlval, yytext, yyleng, 0); /* no copying - intentional */
  1424. BEGIN(ST_IN_SCRIPTING);
  1425. return T_OPEN_TAG_WITH_ECHO;
  1426. }
  1427. <INITIAL>"<%" {
  1428. if (CG(asp_tags)) {
  1429. ZVAL_STRINGL(zendlval, yytext, yyleng, 0); /* no copying - intentional */
  1430. BEGIN(ST_IN_SCRIPTING);
  1431. return T_OPEN_TAG;
  1432. } else {
  1433. goto inline_char_handler;
  1434. }
  1435. }
  1436. <INITIAL>"<?php"([ \t]|{NEWLINE}) {
  1437. ZVAL_STRINGL(zendlval, yytext, yyleng, 0); /* no copying - intentional */
  1438. HANDLE_NEWLINE(yytext[yyleng-1]);
  1439. BEGIN(ST_IN_SCRIPTING);
  1440. return T_OPEN_TAG;
  1441. }
  1442. <INITIAL>"<?" {
  1443. if (CG(short_tags)) {
  1444. ZVAL_STRINGL(zendlval, yytext, yyleng, 0); /* no copying - intentional */
  1445. BEGIN(ST_IN_SCRIPTING);
  1446. return T_OPEN_TAG;
  1447. } else {
  1448. goto inline_char_handler;
  1449. }
  1450. }
  1451. <INITIAL>{ANY_CHAR} {
  1452. if (YYCURSOR > YYLIMIT) {
  1453. return 0;
  1454. }
  1455. inline_char_handler:
  1456. while (1) {
  1457. YYCTYPE *ptr = memchr(YYCURSOR, '<', YYLIMIT - YYCURSOR);
  1458. YYCURSOR = ptr ? ptr + 1 : YYLIMIT;
  1459. if (YYCURSOR < YYLIMIT) {
  1460. switch (*YYCURSOR) {
  1461. case '?':
  1462. if (CG(short_tags) || !strncasecmp((char*)YYCURSOR + 1, "php", 3) || (*(YYCURSOR + 1) == '=')) { /* Assume [ \t\n\r] follows "php" */
  1463. break;
  1464. }
  1465. continue;
  1466. case '%':
  1467. if (CG(asp_tags)) {
  1468. break;
  1469. }
  1470. continue;
  1471. case 's':
  1472. case 'S':
  1473. /* Probably NOT an opening PHP <script> tag, so don't end the HTML chunk yet
  1474. * If it is, the PHP <script> tag rule checks for any HTML scanned before it */
  1475. YYCURSOR--;
  1476. yymore();
  1477. default:
  1478. continue;
  1479. }
  1480. YYCURSOR--;
  1481. }
  1482. break;
  1483. }
  1484. inline_html:
  1485. yyleng = YYCURSOR - SCNG(yy_text);
  1486. if (SCNG(output_filter)) {
  1487. int readsize;
  1488. size_t sz = 0;
  1489. readsize = SCNG(output_filter)((unsigned char **)&Z_STRVAL_P(zendlval), &sz, (unsigned char *)yytext, (size_t)yyleng TSRMLS_CC);
  1490. Z_STRLEN_P(zendlval) = sz;
  1491. if (readsize < yyleng) {
  1492. yyless(readsize);
  1493. }
  1494. } else {
  1495. Z_STRVAL_P(zendlval) = (char *) estrndup(yytext, yyleng);
  1496. Z_STRLEN_P(zendlval) = yyleng;
  1497. }
  1498. zendlval->type = IS_STRING;
  1499. HANDLE_NEWLINES(yytext, yyleng);
  1500. return T_INLINE_HTML;
  1501. }
  1502. /* Make sure a label character follows "->", otherwise there is no property
  1503. * and "->" will be taken literally
  1504. */
  1505. <ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"->"[a-zA-Z_\x7f-\xff] {
  1506. yyless(yyleng - 3);
  1507. yy_push_state(ST_LOOKING_FOR_PROPERTY TSRMLS_CC);
  1508. zend_copy_value(zendlval, (yytext+1), (yyleng-1));
  1509. zendlval->type = IS_STRING;
  1510. return T_VARIABLE;
  1511. }
  1512. /* A [ always designates a variable offset, regardless of what follows
  1513. */
  1514. <ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"[" {
  1515. yyless(yyleng - 1);
  1516. yy_push_state(ST_VAR_OFFSET TSRMLS_CC);
  1517. zend_copy_value(zendlval, (yytext+1), (yyleng-1));
  1518. zendlval->type = IS_STRING;
  1519. return T_VARIABLE;
  1520. }
  1521. <ST_IN_SCRIPTING,ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE,ST_VAR_OFFSET>"$"{LABEL} {
  1522. zend_copy_value(zendlval, (yytext+1), (yyleng-1));
  1523. zendlval->type = IS_STRING;
  1524. return T_VARIABLE;
  1525. }
  1526. <ST_VAR_OFFSET>"]" {
  1527. yy_pop_state(TSRMLS_C);
  1528. return ']';
  1529. }
  1530. <ST_VAR_OFFSET>{TOKENS}|[{}"`] {
  1531. /* Only '[' can be valid, but returning other tokens will allow a more explicit parse error */
  1532. return yytext[0];
  1533. }
  1534. <ST_VAR_OFFSET>[ \n\r\t\\'#] {
  1535. /* Invalid rule to return a more explicit parse error with proper line number */
  1536. yyless(0);
  1537. yy_pop_state(TSRMLS_C);
  1538. return T_ENCAPSED_AND_WHITESPACE;
  1539. }
  1540. <ST_IN_SCRIPTING,ST_VAR_OFFSET>{LABEL} {
  1541. zend_copy_value(zendlval, yytext, yyleng);
  1542. zendlval->type = IS_STRING;
  1543. return T_STRING;
  1544. }
  1545. <ST_IN_SCRIPTING>"#"|"//" {
  1546. while (YYCURSOR < YYLIMIT) {
  1547. switch (*YYCURSOR++) {
  1548. case '\r':
  1549. if (*YYCURSOR == '\n') {
  1550. YYCURSOR++;
  1551. }
  1552. /* fall through */
  1553. case '\n':
  1554. CG(zend_lineno)++;
  1555. break;
  1556. case '%':
  1557. if (!CG(asp_tags)) {
  1558. continue;
  1559. }
  1560. /* fall through */
  1561. case '?':
  1562. if (*YYCURSOR == '>') {
  1563. YYCURSOR--;
  1564. break;
  1565. }
  1566. /* fall through */
  1567. default:
  1568. continue;
  1569. }
  1570. break;
  1571. }
  1572. yyleng = YYCURSOR - SCNG(yy_text);
  1573. return T_COMMENT;
  1574. }
  1575. <ST_IN_SCRIPTING>"/*"|"/**"{WHITESPACE} {
  1576. int doc_com;
  1577. if (yyleng > 2) {
  1578. doc_com = 1;
  1579. RESET_DOC_COMMENT();
  1580. } else {
  1581. doc_com = 0;
  1582. }
  1583. while (YYCURSOR < YYLIMIT) {
  1584. if (*YYCURSOR++ == '*' && *YYCURSOR == '/') {
  1585. break;
  1586. }
  1587. }
  1588. if (YYCURSOR < YYLIMIT) {
  1589. YYCURSOR++;
  1590. } else {
  1591. zend_error(E_COMPILE_WARNING, "Unterminated comment starting line %d", CG(zend_lineno));
  1592. }
  1593. yyleng = YYCURSOR - SCNG(yy_text);
  1594. HANDLE_NEWLINES(yytext, yyleng);
  1595. if (doc_com) {
  1596. CG(doc_comment) = estrndup(yytext, yyleng);
  1597. CG(doc_comment_len) = yyleng;
  1598. return T_DOC_COMMENT;
  1599. }
  1600. return T_COMMENT;
  1601. }
  1602. <ST_IN_SCRIPTING>("?>"|"</script"{WHITESPACE}*">"){NEWLINE}? {
  1603. ZVAL_STRINGL(zendlval, yytext, yyleng, 0); /* no copying - intentional */
  1604. BEGIN(INITIAL);
  1605. return T_CLOSE_TAG; /* implicit ';' at php-end tag */
  1606. }
  1607. <ST_IN_SCRIPTING>"%>"{NEWLINE}? {
  1608. if (CG(asp_tags)) {
  1609. BEGIN(INITIAL);
  1610. ZVAL_STRINGL(zendlval, yytext, yyleng, 0); /* no copying - intentional */
  1611. return T_CLOSE_TAG; /* implicit ';' at php-end tag */
  1612. } else {
  1613. yyless(1);
  1614. return yytext[0];
  1615. }
  1616. }
  1617. <ST_IN_SCRIPTING>b?['] {
  1618. register char *s, *t;
  1619. char *end;
  1620. int bprefix = (yytext[0] != '\'') ? 1 : 0;
  1621. while (1) {
  1622. if (YYCURSOR < YYLIMIT) {
  1623. if (*YYCURSOR == '\'') {
  1624. YYCURSOR++;
  1625. yyleng = YYCURSOR - SCNG(yy_text);
  1626. break;
  1627. } else if (*YYCURSOR++ == '\\' && YYCURSOR < YYLIMIT) {
  1628. YYCURSOR++;
  1629. }
  1630. } else {
  1631. yyleng = YYLIMIT - SCNG(yy_text);
  1632. /* Unclosed single quotes; treat similar to double quotes, but without a separate token
  1633. * for ' (unrecognized by parser), instead of old flex fallback to "Unexpected character..."
  1634. * rule, which continued in ST_IN_SCRIPTING state after the quote */
  1635. return T_ENCAPSED_AND_WHITESPACE;
  1636. }
  1637. }
  1638. ZVAL_STRINGL(zendlval, yytext+bprefix+1, yyleng-bprefix-2, 1);
  1639. /* convert escape sequences */
  1640. s = t = Z_STRVAL_P(zendlval);
  1641. end = s+Z_STRLEN_P(zendlval);
  1642. while (s<end) {
  1643. if (*s=='\\') {
  1644. s++;
  1645. switch(*s) {
  1646. case '\\':
  1647. case '\'':
  1648. *t++ = *s;
  1649. Z_STRLEN_P(zendlval)--;
  1650. break;
  1651. default:
  1652. *t++ = '\\';
  1653. *t++ = *s;
  1654. break;
  1655. }
  1656. } else {
  1657. *t++ = *s;
  1658. }
  1659. if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
  1660. CG(zend_lineno)++;
  1661. }
  1662. s++;
  1663. }
  1664. *t = 0;
  1665. if (SCNG(output_filter)) {
  1666. size_t sz = 0;
  1667. s = Z_STRVAL_P(zendlval);
  1668. SCNG(output_filter)((unsigned char **)&Z_STRVAL_P(zendlval), &sz, (unsigned char *)s, (size_t)Z_STRLEN_P(zendlval) TSRMLS_CC);
  1669. Z_STRLEN_P(zendlval) = sz;
  1670. efree(s);
  1671. }
  1672. return T_CONSTANT_ENCAPSED_STRING;
  1673. }
  1674. <ST_IN_SCRIPTING>b?["] {
  1675. int bprefix = (yytext[0] != '"') ? 1 : 0;
  1676. while (YYCURSOR < YYLIMIT) {
  1677. switch (*YYCURSOR++) {
  1678. case '"':
  1679. yyleng = YYCURSOR - SCNG(yy_text);
  1680. zend_scan_escape_string(zendlval, yytext+bprefix+1, yyleng-bprefix-2, '"' TSRMLS_CC);
  1681. return T_CONSTANT_ENCAPSED_STRING;
  1682. case '$':
  1683. if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
  1684. break;
  1685. }
  1686. continue;
  1687. case '{':
  1688. if (*YYCURSOR == '$') {
  1689. break;
  1690. }
  1691. continue;
  1692. case '\\':
  1693. if (YYCURSOR < YYLIMIT) {
  1694. YYCURSOR++;
  1695. }
  1696. /* fall through */
  1697. default:
  1698. continue;
  1699. }
  1700. YYCURSOR--;
  1701. break;
  1702. }
  1703. /* Remember how much was scanned to save rescanning */
  1704. SET_DOUBLE_QUOTES_SCANNED_LENGTH(YYCURSOR - SCNG(yy_text) - yyleng);
  1705. YYCURSOR = SCNG(yy_text) + yyleng;
  1706. BEGIN(ST_DOUBLE_QUOTES);
  1707. return '"';
  1708. }
  1709. <ST_IN_SCRIPTING>b?"<<<"{TABS_AND_SPACES}({LABEL}|([']{LABEL}['])|(["]{LABEL}["])){NEWLINE} {
  1710. char *s;
  1711. int bprefix = (yytext[0] != '<') ? 1 : 0;
  1712. zend_heredoc_label *heredoc_label = emalloc(sizeof(zend_heredoc_label));
  1713. CG(zend_lineno)++;
  1714. heredoc_label->length = yyleng-bprefix-3-1-(yytext[yyleng-2]=='\r'?1:0);
  1715. s = yytext+bprefix+3;
  1716. while ((*s == ' ') || (*s == '\t')) {
  1717. s++;
  1718. heredoc_label->length--;
  1719. }
  1720. if (*s == '\'') {
  1721. s++;
  1722. heredoc_label->length -= 2;
  1723. BEGIN(ST_NOWDOC);
  1724. } else {
  1725. if (*s == '"') {
  1726. s++;
  1727. heredoc_label->length -= 2;
  1728. }
  1729. BEGIN(ST_HEREDOC);
  1730. }
  1731. heredoc_label->label = estrndup(s, heredoc_label->length);
  1732. /* Check for ending label on the next line */
  1733. if (heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, s, heredoc_label->length)) {
  1734. YYCTYPE *end = YYCURSOR + heredoc_label->length;
  1735. if (*end == ';') {
  1736. end++;
  1737. }
  1738. if (*end == '\n' || *end == '\r') {
  1739. BEGIN(ST_END_HEREDOC);
  1740. }
  1741. }
  1742. zend_ptr_stack_push(&SCNG(heredoc_label_stack), (void *) heredoc_label);
  1743. return T_START_HEREDOC;
  1744. }
  1745. <ST_IN_SCRIPTING>[`] {
  1746. BEGIN(ST_BACKQUOTE);
  1747. return '`';
  1748. }
  1749. <ST_END_HEREDOC>{ANY_CHAR} {
  1750. zend_heredoc_label *heredoc_label = zend_ptr_stack_pop(&SCNG(heredoc_label_stack));
  1751. YYCURSOR += heredoc_label->length - 1;
  1752. yyleng = heredoc_label->length;
  1753. heredoc_label_dtor(heredoc_label);
  1754. efree(heredoc_label);
  1755. BEGIN(ST_IN_SCRIPTING);
  1756. return T_END_HEREDOC;
  1757. }
  1758. <ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"{$" {
  1759. Z_LVAL_P(zendlval) = (long) '{';
  1760. yy_push_state(ST_IN_SCRIPTING TSRMLS_CC);
  1761. yyless(1);
  1762. return T_CURLY_OPEN;
  1763. }
  1764. <ST_DOUBLE_QUOTES>["] {
  1765. BEGIN(ST_IN_SCRIPTING);
  1766. return '"';
  1767. }
  1768. <ST_BACKQUOTE>[`] {
  1769. BEGIN(ST_IN_SCRIPTING);
  1770. return '`';
  1771. }
  1772. <ST_DOUBLE_QUOTES>{ANY_CHAR} {
  1773. if (GET_DOUBLE_QUOTES_SCANNED_LENGTH()) {
  1774. YYCURSOR += GET_DOUBLE_QUOTES_SCANNED_LENGTH() - 1;
  1775. SET_DOUBLE_QUOTES_SCANNED_LENGTH(0);
  1776. goto double_quotes_scan_done;
  1777. }
  1778. if (YYCURSOR > YYLIMIT) {
  1779. return 0;
  1780. }
  1781. if (yytext[0] == '\\' && YYCURSOR < YYLIMIT) {
  1782. YYCURSOR++;
  1783. }
  1784. while (YYCURSOR < YYLIMIT) {
  1785. switch (*YYCURSOR++) {
  1786. case '"':
  1787. break;
  1788. case '$':
  1789. if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
  1790. break;
  1791. }
  1792. continue;
  1793. case '{':
  1794. if (*YYCURSOR == '$') {
  1795. break;
  1796. }
  1797. continue;
  1798. case '\\':
  1799. if (YYCURSOR < YYLIMIT) {
  1800. YYCURSOR++;
  1801. }
  1802. /* fall through */
  1803. default:
  1804. continue;
  1805. }
  1806. YYCURSOR--;
  1807. break;
  1808. }
  1809. double_quotes_scan_done:
  1810. yyleng = YYCURSOR - SCNG(yy_text);
  1811. zend_scan_escape_string(zendlval, yytext, yyleng, '"' TSRMLS_CC);
  1812. return T_ENCAPSED_AND_WHITESPACE;
  1813. }
  1814. <ST_BACKQUOTE>{ANY_CHAR} {
  1815. if (YYCURSOR > YYLIMIT) {
  1816. return 0;
  1817. }
  1818. if (yytext[0] == '\\' && YYCURSOR < YYLIMIT) {
  1819. YYCURSOR++;
  1820. }
  1821. while (YYCURSOR < YYLIMIT) {
  1822. switch (*YYCURSOR++) {
  1823. case '`':
  1824. break;
  1825. case '$':
  1826. if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
  1827. break;
  1828. }
  1829. continue;
  1830. case '{':
  1831. if (*YYCURSOR == '$') {
  1832. break;
  1833. }
  1834. continue;
  1835. case '\\':
  1836. if (YYCURSOR < YYLIMIT) {
  1837. YYCURSOR++;
  1838. }
  1839. /* fall through */
  1840. default:
  1841. continue;
  1842. }
  1843. YYCURSOR--;
  1844. break;
  1845. }
  1846. yyleng = YYCURSOR - SCNG(yy_text);
  1847. zend_scan_escape_string(zendlval, yytext, yyleng, '`' TSRMLS_CC);
  1848. return T_ENCAPSED_AND_WHITESPACE;
  1849. }
  1850. <ST_HEREDOC>{ANY_CHAR} {
  1851. int newline = 0;
  1852. zend_heredoc_label *heredoc_label = zend_ptr_stack_top(&SCNG(heredoc_label_stack));
  1853. if (YYCURSOR > YYLIMIT) {
  1854. return 0;
  1855. }
  1856. YYCURSOR--;
  1857. while (YYCURSOR < YYLIMIT) {
  1858. switch (*YYCURSOR++) {
  1859. case '\r':
  1860. if (*YYCURSOR == '\n') {
  1861. YYCURSOR++;
  1862. }
  1863. /* fall through */
  1864. case '\n':
  1865. /* Check for ending label on the next line */
  1866. if (IS_LABEL_START(*YYCURSOR) && heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, heredoc_label->label, heredoc_label->length)) {
  1867. YYCTYPE *end = YYCURSOR + heredoc_label->length;
  1868. if (*end == ';') {
  1869. end++;
  1870. }
  1871. if (*end == '\n' || *end == '\r') {
  1872. /* newline before label will be subtracted from returned text, but
  1873. * yyleng/yytext will include it, for zend_highlight/strip, tokenizer, etc. */
  1874. if (YYCURSOR[-2] == '\r' && YYCURSOR[-1] == '\n') {
  1875. newline = 2; /* Windows newline */
  1876. } else {
  1877. newline = 1;
  1878. }
  1879. CG(increment_lineno) = 1; /* For newline before label */
  1880. BEGIN(ST_END_HEREDOC);
  1881. goto heredoc_scan_done;
  1882. }
  1883. }
  1884. continue;
  1885. case '$':
  1886. if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
  1887. break;
  1888. }
  1889. continue;
  1890. case '{':
  1891. if (*YYCURSOR == '$') {
  1892. break;
  1893. }
  1894. continue;
  1895. case '\\':
  1896. if (YYCURSOR < YYLIMIT && *YYCURSOR != '\n' && *YYCURSOR != '\r') {
  1897. YYCURSOR++;
  1898. }
  1899. /* fall through */
  1900. default:
  1901. continue;
  1902. }
  1903. YYCURSOR--;
  1904. break;
  1905. }
  1906. heredoc_scan_done:
  1907. yyleng = YYCURSOR - SCNG(yy_text);
  1908. zend_scan_escape_string(zendlval, yytext, yyleng - newline, 0 TSRMLS_CC);
  1909. return T_ENCAPSED_AND_WHITESPACE;
  1910. }
  1911. <ST_NOWDOC>{ANY_CHAR} {
  1912. int newline = 0;
  1913. zend_heredoc_label *heredoc_label = zend_ptr_stack_top(&SCNG(heredoc_label_stack));
  1914. if (YYCURSOR > YYLIMIT) {
  1915. return 0;
  1916. }
  1917. YYCURSOR--;
  1918. while (YYCURSOR < YYLIMIT) {
  1919. switch (*YYCURSOR++) {
  1920. case '\r':
  1921. if (*YYCURSOR == '\n') {
  1922. YYCURSOR++;
  1923. }
  1924. /* fall through */
  1925. case '\n':
  1926. /* Check for ending label on the next line */
  1927. if (IS_LABEL_START(*YYCURSOR) && heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, heredoc_label->label, heredoc_label->length)) {
  1928. YYCTYPE *end = YYCURSOR + heredoc_label->length;
  1929. if (*end == ';') {
  1930. end++;
  1931. }
  1932. if (*end == '\n' || *end == '\r') {
  1933. /* newline before label will be subtracted from returned text, but
  1934. * yyleng/yytext will include it, for zend_highlight/strip, tokenizer, etc. */
  1935. if (YYCURSOR[-2] == '\r' && YYCURSOR[-1] == '\n') {
  1936. newline = 2; /* Windows newline */
  1937. } else {
  1938. newline = 1;
  1939. }
  1940. CG(increment_lineno) = 1; /* For newline before label */
  1941. BEGIN(ST_END_HEREDOC);
  1942. goto nowdoc_scan_done;
  1943. }
  1944. }
  1945. /* fall through */
  1946. default:
  1947. continue;
  1948. }
  1949. }
  1950. nowdoc_scan_done:
  1951. yyleng = YYCURSOR - SCNG(yy_text);
  1952. zend_copy_value(zendlval, yytext, yyleng - newline);
  1953. zendlval->type = IS_STRING;
  1954. HANDLE_NEWLINES(yytext, yyleng - newline);
  1955. return T_ENCAPSED_AND_WHITESPACE;
  1956. }
  1957. <ST_IN_SCRIPTING,ST_VAR_OFFSET>{ANY_CHAR} {
  1958. if (YYCURSOR > YYLIMIT) {
  1959. return 0;
  1960. }
  1961. zend_error(E_COMPILE_WARNING,"Unexpected character in input: '%c' (ASCII=%d) state=%d", yytext[0], yytext[0], YYSTATE);
  1962. goto restart;
  1963. }
  1964. */
  1965. }