This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
/
php_tok.h
132 lines (99 loc) · 3.01 KB
/
php_tok.h
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
// PHP_TOKENIZER
// Copyright (c) 2007 Andrew Bosworth, Facebook, inc
// All rights reserved
#ifndef PHPTOK_H
#define PHPTOK_H
#include "php_stl.h"
#include "php_tok_consts.h"
#include <ext/hash_map>
using __gnu_cxx::hash_map;
// the basic token structure
typedef struct{
int type;
string content;
} token;
/*
* TOKEN_CONT - a simple token container class, handles data and access
*/
class token_cont
{
public:
token_cont() {};
~token_cont() {};
// add a token to this object
inline void push_back(token &t){ tokens.push_back(t); }
// reset the token to the start
inline void reset(){ iter = tokens.begin(); }
// count the number of tokens we contain
inline size_t count(){ return tokens.size(); }
// return the next token pointed at by the iterator and advance it
token get_next_token(){
// we are out of tokens!
if(iter == tokens.end()){
token t;
t.type = T_NOMORE;
return t;
}
token t = *iter;
iter++;
return t;
}
// for debug
void dump_tokens(FILE *f){
for(vector<token>::iterator it = tokens.begin(); it != tokens.end(); it++){
fprintf(f, "%4d: %s\n", it->type, (it->content).c_str());
}
}
private:
vector<token> tokens;
vector<token>::iterator iter;
// we could add an iterator stack here to allow users to save state for
// a subroutine, but until we have an application for it lets keep it simple
};
// for convenience
typedef hash_map<const char *, token_cont > token_map;
/*
* PHP_TOK - a multifile PHP tokenizer class
*/
class php_tok : public php_stl
{
public:
php_tok(bool bOut = true, bool bWarn = true);
~php_tok(){};
//
// TOKENIZERS
//
// parse the given file for tokens, initialize relevant iterator
// this is named after its analog in php
php_ret token_get_all(const char *file);
// tokenize the given file and all child includes
php_ret token_get_all_recursive(const char *file);
//
// ACCESSORS
// users can treat this class like a multimap iterator
//
// return a 2-D array of all the files currently tokenized
// we use a vector of char* because char ** is too hard to use right
vector<const char *> get_tokenized_files();
// reset the iterator associated with a given file
// returns false if that file isn't tokenized
bool reset_iterator(const char *file);
// get the number of tokens in the given file
size_t count(const char *file);
// gets the next token from the iterator for the specified file
// returns T_NOMORE when done or if the specified file isn't tokenized
token get_next_token(const char *file);
// get all files included in a given file and populate the includes argument
// return false if the file hasn't been tokenized yet
//
// NOTE: this will reset your iterator! Do this first if you need it!
bool get_includes(const char *file, vector<string> &includes);
//
// FOR DEBUGGING
//
void dump_tokens(FILE *f = stdout);
private:
// the map from files to token iterators (lists of tokens w/ iterators)
token_map alltokens;
};
#endif