1
// Copyright 2011 Google Inc. All Rights Reserved.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
15
// Author: jmarantz@google.com
17
// This is based on third_party/libpagespeed/src/pagespeed/js/js_minify.cc by
18
// mdsteele@google.com
20
#ifndef PAGESPEED_KERNEL_JS_JS_KEYWORDS_H_
21
#define PAGESPEED_KERNEL_JS_JS_KEYWORDS_H_
23
#include "pagespeed/kernel/base/string_util.h"
25
namespace net_instaweb {
68
// reserved for future use
76
// reserved for future use in strict code
87
// Sentinel value for gperf.
90
// Other types of lexical tokens; returned by lexer, but not gperf.
91
kComment, // A block or line comment (not including the linebreak).
92
kWhitespace, // Whitespace not containing any linebreaks.
93
kLineSeparator, // Whitespace with linebreaks, but no semicolon insertion.
94
kSemiInsert, // Whitespace that triggers semicolon insertion.
95
kRegex, // A regex literal, such as /foo/i or /a+b*/
96
kStringLiteral, // A string literal, such as 'foo' or "bar"
97
kNumber, // A numeric literal, such as 3.5 or 017 or .2e+10
98
kOperator, // An operator or symbol, such as && or <<= or (
99
kIdentifier, // An identifier (variable name, label, etc).
100
kEndOfInput, // End of input was reached without errors.
101
kError // A syntax error occurred.
104
static bool IsAKeyword(Type type) { return type < kNotAKeyword; }
106
// Returns true if name is a javascript keyword that can precede a regular
107
// expression. Keywords such as 'return' and 'throw' can precede a regex '/'
108
// but keywords such as 'while' cannot.
109
static bool CanKeywordPrecedeRegEx(const StringPiece& name);
114
kIsReservedNonStrict,
116
kCanPrecedeRegEx // keywords that can be placed directly before a regex
119
// Finds a Keyword based on a keyword string. If not found, returns
120
// kNotAKeyword. Otherwise, this always returns a Type for which
121
// IsAKeyword is true.
122
static Type Lookup(const StringPiece& name, Flag* flag);
124
// Limited iterator (not an STL iterator). Example usage:
125
// for (JsKeywords::Iterator iter; !iter.AtEnd(); iter.Next()) {
126
// use(iter.keyword(), iter.name());
130
Iterator() : index_(-1) { Next(); }
133
Type keyword() const;
134
const char* name() const;
139
// Implicit copy and assign ok. The members can be safely copied by bits.
143
// TODO(jkarlin): Get rid of the net_instaweb namespace once JsLexer is
144
// moved into kernel/js.
145
friend class net_instaweb::JsLexer;
147
// Returns the number of keywords recognized by the Lookup function. This is
148
// used by the Lexer to size the keyword-sring array prior to iterating over
149
// the keywords to populate it.
150
static int num_keywords();
153
} // namespace pagespeed
155
#endif // PAGESPEED_KERNEL_JS_JS_KEYWORDS_H_