2
// mini js lexical analyzer. idea is to be dumb and fast.
5
* Copyright (C) 2008 10gen Inc.
7
* This program is free software: you can redistribute it and/or modify
8
* it under the terms of the GNU Affero General Public License, version 3,
9
* as published by the Free Software Foundation.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Affero General Public License for more details.
16
* You should have received a copy of the GNU Affero General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
27
using namespace stdext;
31
typedef const char * MyStr;
33
bool operator()(const MyStr & x, const MyStr & y) const {
34
if ( strcmp(x, y) > 0)
41
typedef hash_map<const char*, int, hash_compare<const char *, less_str> > strhashmap;
47
#include <ext/hash_map>
51
using namespace __gnu_cxx;
53
typedef const char * MyStr;
55
bool operator()(const MyStr & x, const MyStr & y) const {
56
if ( strcmp(x, y) == 0)
63
typedef hash_map<const char*, int, hash<const char *>, eq_str > strhashmap;
69
bool ic[256]; // ic=Identifier Character
72
// dm: very dumb about comments and escaped quotes -- but we are faster then at least,
73
// albeit returning too much (which is ok for jsbobj current usage).
74
void grabVariables(char *code /*modified and must stay in scope*/, strhashmap& vars) {
80
while ( *q && ic[*q] ) q++;
81
const char *identifier = p;
84
if ( !reserved.count(identifier) ) {
85
// we try to be smart about 'obj' but have to be careful as obj.obj
86
// can happen; this is so that nFields is right for simplistic where cases
87
// so we can stop scanning in jsobj when we find the field of interest.
88
if ( strcmp(identifier,"obj")==0 && p>code && p[-1] != '.' )
101
while ( *p && *p != '\'' ) p++;
103
else if ( *p == '"' ) {
105
while ( *p && *p != '"' ) p++;
114
assert( atest.count("bar") == 0 );
115
assert( atest.count("foo") == 1 );
116
assert( atest["foo"] == 3 );
118
for ( int i = 0; i < 256; i++ ) {
119
ic[i] = starter[i] = false;
121
for ( int i = 'a'; i <= 'z'; i++ )
122
ic[i] = starter[i] = true;
123
for ( int i = 'A'; i <= 'Z'; i++ )
124
ic[i] = starter[i] = true;
125
for ( int i = '0'; i <= '9'; i++ )
127
for ( int i = 128; i < 256; i++ )
128
ic[i] = starter[i] = true;
129
ic['$'] = starter['$'] = true;
130
ic['_'] = starter['_'] = true;
132
reserved["break"] = true;
133
reserved["case"] = true;
134
reserved["catch"] = true;
135
reserved["continue"] = true;
136
reserved["default"] = true;
137
reserved["delete"] = true;
138
reserved["do"] = true;
139
reserved["else"] = true;
140
reserved["finally"] = true;
141
reserved["for"] = true;
142
reserved["function"] = true;
143
reserved["if"] = true;
144
reserved["in"] = true;
145
reserved["instanceof"] = true;
146
reserved["new"] = true;
147
reserved["return"] = true;
148
reserved["switch"] = true;
149
reserved["this"] = true;
150
reserved["throw"] = true;
151
reserved["try"] = true;
152
reserved["typeof"] = true;
153
reserved["var"] = true;
154
reserved["void"] = true;
155
reserved["while"] = true;
156
reserved["with "] = true;