~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to db/minilex.h

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// minilex.h
 
2
// mini js lexical analyzer.  idea is to be dumb and fast.
 
3
 
 
4
/**
 
5
*    Copyright (C) 2008 10gen Inc.
 
6
*
 
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.
 
10
*
 
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.
 
15
*
 
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/>.
 
18
*/
 
19
 
 
20
namespace mongo {
 
21
 
 
22
#if defined(_WIN32)
 
23
    
 
24
} // namespace mongo
 
25
 
 
26
#include <hash_map>
 
27
using namespace stdext;
 
28
 
 
29
namespace mongo {
 
30
    
 
31
    typedef const char * MyStr;
 
32
    struct less_str {
 
33
        bool operator()(const MyStr & x, const MyStr & y) const {
 
34
            if ( strcmp(x, y) > 0)
 
35
                return true;
 
36
            
 
37
            return false;
 
38
        }
 
39
    };
 
40
    
 
41
    typedef hash_map<const char*, int, hash_compare<const char *, less_str> > strhashmap;
 
42
    
 
43
#else
 
44
    
 
45
} // namespace mongo
 
46
 
 
47
#include <ext/hash_map>
 
48
 
 
49
namespace mongo {
 
50
    
 
51
    using namespace __gnu_cxx;
 
52
 
 
53
    typedef const char * MyStr;
 
54
    struct eq_str {
 
55
        bool operator()(const MyStr & x, const MyStr & y) const {
 
56
            if ( strcmp(x, y) == 0)
 
57
                return true;
 
58
            
 
59
            return false;
 
60
        }
 
61
    };
 
62
    
 
63
    typedef hash_map<const char*, int, hash<const char *>, eq_str > strhashmap;
 
64
    
 
65
#endif
 
66
    
 
67
    struct MiniLex {
 
68
        strhashmap reserved;
 
69
        bool ic[256]; // ic=Identifier Character
 
70
        bool starter[256];
 
71
 
 
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) {
 
75
            char *p = code;
 
76
            char last = 0;
 
77
            while ( *p ) {
 
78
                if ( starter[*p] ) {
 
79
                    char *q = p+1;
 
80
                    while ( *q && ic[*q] ) q++;
 
81
                    const char *identifier = p;
 
82
                    bool done = *q == 0;
 
83
                    *q = 0;
 
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] != '.' )
 
89
                            ;
 
90
                        else
 
91
                            vars[identifier] = 1;
 
92
                    }
 
93
                    if ( done )
 
94
                        break;
 
95
                    p = q + 1;
 
96
                    continue;
 
97
                }
 
98
 
 
99
                if ( *p == '\'' ) {
 
100
                    p++;
 
101
                    while ( *p && *p != '\'' ) p++;
 
102
                }
 
103
                else if ( *p == '"' ) {
 
104
                    p++;
 
105
                    while ( *p && *p != '"' ) p++;
 
106
                }
 
107
                p++;
 
108
            }
 
109
        }
 
110
 
 
111
        MiniLex() {
 
112
            strhashmap atest;
 
113
            atest["foo"] = 3;
 
114
            assert( atest.count("bar") == 0 );
 
115
            assert( atest.count("foo") == 1 );
 
116
            assert( atest["foo"] == 3 );
 
117
 
 
118
            for ( int i = 0; i < 256; i++ ) {
 
119
                ic[i] = starter[i] = false;
 
120
            }
 
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++ )
 
126
                ic[i] = true;
 
127
            for ( int i = 128; i < 256; i++ )
 
128
                ic[i] = starter[i] = true;
 
129
            ic['$'] = starter['$'] = true;
 
130
            ic['_'] = starter['_'] = true;
 
131
 
 
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;
 
157
        }
 
158
    };
 
159
 
 
160
} // namespace mongo