1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
%{
/*
* Copyright (c) 2003 Tama Communications Corporation
*
* This file is part of GNU GLOBAL.
*
* GNU GLOBAL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* GNU GLOBAL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* scanner for PHP source code.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#include "defined.h"
#include "die.h"
#include "gparam.h"
#include "gctags.h"
#include "linetable.h"
#include "strbuf.h"
#include "php_res.h"
#define lex_symbol_generation_rule(x) php_ ## x
#define LEXLEX lex_symbol_generation_rule(lex)
#define LEXTEXT lex_symbol_generation_rule(text)
#define LEXLENG lex_symbol_generation_rule(leng)
#define LEXRESTART lex_symbol_generation_rule(restart)
#define LEXLINENO lex_symbol_generation_rule(lineno)
#define PHP_TOKEN 1
#define PHP_VARIABLE 2
#define PHP_STRING 3
#define PHP_POINTER 4
#define PHP_DOLLAR 5
#define PHP_LPAREN '('
#define PHP_RPAREN ')'
#define PHP_LBRACE '{'
#define PHP_RBRACE '}'
#define PHP_LBRACK '['
#define PHP_RBRACK ']'
#ifdef HAVE_STDARG_H
static void debug_print(const char *, ...);
#else
static void debug_print();
#endif
static int level; /* block nest level */
static STRBUF *string; /* string */
/*
* For debug.
*/
static void
#ifdef HAVE_STDARG_H
debug_print(const char *s, ...)
#else
debug_print(s, va_alist)
const char *s;
va_dcl
#endif
{
va_list ap;
if (!debug)
return;
#ifdef HAVE_STDARG_H
va_start(ap, s);
#else
va_start(ap);
#endif
(void)vfprintf(stderr, s, ap);
va_end(ap);
}
#ifdef YYLMAX
#undef YYLMAX
#endif
#define YYLMAX 1024
#ifdef ECHO
#undef ECHO
#endif
#define ECHO debug_print("%s", LEXTEXT)
#ifdef PUT
#undef PUT
#endif
#define PUT(tag, lno, file) do { \
if (!nflag) { \
fprintf(stdout, "%-16s %4d %-16s ",tag, lno, file); \
linetable_print(stdout, lno); \
} \
} while (0)
/*
* IO routine.
*/
#define YY_INPUT(buf,result,max_size) \
do { \
if ((result = linetable_read(buf, max_size)) == -1) \
result = YY_NULL; \
} while (0)
%}
/* Definitions */
H 0[Xx][0-9A-Fa-f]+
N [0-9]+
L {N}L?
D1 {N}\.{N}([Ee][+-]?{N})?
D2 \.{N}([Ee][+-]?{N})?
NUMBER -?({L}|{D1}|{D2})
/* We accept multi-bytes character */
ALPHA [a-zA-Z_\x80-\xff]
ALPHANUM [a-zA-Z_\x80-\xff0-9]
WORD {ALPHA}{ALPHANUM}*
%start PHP STRING LITERAL
%option 8bit caseless noyywrap nounput yylineno prefix="php_"
%%
\n ECHO;
/* Start PHP */
<INITIAL>"<?=" ECHO; BEGIN PHP;
<INITIAL>"<?" ECHO; BEGIN PHP;
<INITIAL>"<?php" ECHO; BEGIN PHP;
<INITIAL>"<%" ECHO; BEGIN PHP;
<INITIAL>"<script[ \t]+language=(\")?php(\")?>" ECHO; BEGIN PHP;
/* Ignore HTML */
<INITIAL>. ECHO;
/* End of PHP */
<PHP>"?>" ECHO; BEGIN INITIAL;
<PHP>"%>" ECHO; BEGIN INITIAL;
<PHP>"</script>" ECHO; BEGIN INITIAL;
/* Comment */
<PHP>"/*" {
int c;
debug_print("</*");
while ((c = input()) != EOF) {
debug_print("%c", c);
if (c == '*') {
while ((c = input()) != EOF && c == '*')
debug_print("%c", c);
debug_print("%c", c);
if (c == EOF || c == '/')
break;
}
}
if (c == EOF)
die("unexpected end of comment.");
debug_print(">");
}
<PHP>"//".* debug_print("<%s>", LEXTEXT);
<PHP>"#".* debug_print("<%s>", LEXTEXT);
/* String */
<PHP>\" { strbuf_reset(string); BEGIN STRING; }
<STRING>\" {
debug_print("<S:%s>", strbuf_value(string));
BEGIN PHP;
return PHP_STRING;
}
<STRING>\\. strbuf_puts(string, LEXTEXT);
<STRING>. strbuf_putc(string, LEXTEXT[0]);
/* Literal */
<PHP>\' { strbuf_reset(string); BEGIN LITERAL; }
<LITERAL>\' {
debug_print("<L:%s>", strbuf_value(string));
BEGIN PHP;
return PHP_STRING;
}
<LITERAL>\\. strbuf_puts(string, LEXTEXT);
<LITERAL>. strbuf_putc(string, LEXTEXT[0]);
/* Cast */
<PHP>\([ \t]*(bool|boolean|int|integer|real|double|float|string|array|object)[ \t]*\)
;
<PHP,STRING>$\{{WORD}\} {
/*
* 0123456 yyleng = 6
* ${abc}\0
*/
if (YY_START == STRING)
strbuf_puts(string, LEXTEXT);
memcpy(LEXTEXT, &LEXTEXT[2], LEXLENG - 3);
LEXTEXT[LEXLENG - 3] = '\0';
LEXLENG = LEXLENG - 3;
debug_print("<V:%s>", LEXTEXT);
return PHP_VARIABLE;
}
<PHP,STRING>${WORD} {
/*
* 01234 yyleng = 4
* $abc\0
*/
if (YY_START == STRING)
strbuf_puts(string, LEXTEXT);
memcpy(LEXTEXT, &LEXTEXT[1], LEXLENG - 1);
LEXTEXT[LEXLENG - 1] = '\0';
LEXLENG = LEXLENG - 1;
debug_print("<V:%s>", LEXTEXT);
return PHP_VARIABLE;
}
<PHP>{NUMBER} debug_print("<N:%s>", LEXTEXT);
<PHP>{WORD} {
int id = php_reserved_word(LEXTEXT, LEXLENG);
if (id) {
debug_print("<Reserved:%s>", LEXTEXT);
return id;
} else {
debug_print("<T:%s>", LEXTEXT);
return PHP_TOKEN;
}
}
/* Operator */
<PHP>[{}] {
int c = LEXTEXT[0];
if (c == PHP_LBRACE)
level++;
else
level--;
debug_print("%c[%d]", c, level);
return c;
}
<PHP>[][()] {
return LEXTEXT[0];
}
<PHP>[-+*/%&~^]=? ECHO;
<PHP>[=><!]= ECHO;
<PHP>[-+&|<>]{2}=? ECHO;
<PHP>"<>"|"<<<" ECHO;
<PHP>"$" { ECHO; return PHP_DOLLAR; }
<PHP>. ECHO;
%%
/*
* php: read PHP file and pickup tag entries.
*/
void
php(file)
const char *file;
{
int token;
int target = (sflag) ? SYM : (rflag) ? REF : DEF;
level = 0;
string = strbuf_open(0);
if (linetable_open(file) == -1)
die("'%s' cannot open.", file);
LEXRESTART(NULL);
while ((token = LEXLEX()) != 0) {
switch (token) {
case PHP_DEFINE:
if (LEXLEX() != PHP_LPAREN)
break;
if (LEXLEX() != PHP_STRING)
break;
if (target == DEF)
PUT(strbuf_value(string), LEXLINENO, file);
break;
case PHP_CLASS:
if (LEXLEX() != PHP_TOKEN)
break;
if (target == DEF)
PUT(LEXTEXT, LEXLINENO, file);
break;
case PHP_FUNCTION:
case PHP_CFUNCTION:
case PHP_OLD_FUNCTION:
if (LEXLEX() != PHP_TOKEN)
break;
if (target == DEF)
PUT(LEXTEXT, LEXLINENO, file);
break;
case PHP_VARIABLE:
if (php_reserved_variable(LEXTEXT, LEXLENG)) {
if (target == SYM)
PUT(LEXTEXT, LEXLINENO, file);
if (LEXLEX() == PHP_LBRACK && LEXLEX() == PHP_STRING && LEXLEX() == PHP_RBRACK) {
char *str = strbuf_value(string);
if (strchr(str, '$') == 0)
if (target == SYM)
PUT(str, LEXLINENO, file);
}
} else if (!strcmp(LEXTEXT, "this")) {
;
} else {
if (target == SYM)
PUT(LEXTEXT, LEXLINENO, file);
}
break;
case PHP_POINTER:
if (LEXLEX() != PHP_TOKEN)
break;
/* FALLTHROUGH */
case PHP_TOKEN:
if (target == REF) {
if (defined(LEXTEXT))
PUT(LEXTEXT, LEXLINENO, file);
} else if (target == SYM) {
if (!defined(LEXTEXT))
PUT(LEXTEXT, LEXLINENO, file);
}
break;
case PHP_NEW:
if (LEXLEX() != PHP_TOKEN)
break;
if (target == REF)
PUT(LEXTEXT, LEXLINENO, file);
break;
/*
* ${x->y}
*/
case PHP_DOLLAR:
if (LEXLEX() != PHP_LBRACE)
break;
while ((token = LEXLEX()) != PHP_RBRACE) {
if (token == PHP_TOKEN) {
if (target == REF) {
if (defined(LEXTEXT))
PUT(LEXTEXT, LEXLINENO, file);
} else if (target == SYM) {
if (!defined(LEXTEXT))
PUT(LEXTEXT, LEXLINENO, file);
}
}
}
break;
default:
break;
}
}
linetable_close();
strbuf_close(string);
}
|