~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/JavaScriptCore/parser/SyntaxChecker.h

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 Apple Inc. All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
7
 * 1. Redistributions of source code must retain the above copyright
 
8
 *    notice, this list of conditions and the following disclaimer.
 
9
 * 2. Redistributions in binary form must reproduce the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer in the
 
11
 *    documentation and/or other materials provided with the distribution.
 
12
 *
 
13
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 
14
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
15
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
16
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 
17
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
18
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
19
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
20
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
21
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
22
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 
23
 * THE POSSIBILITY OF SUCH DAMAGE.
 
24
 */
 
25
 
 
26
#ifndef SyntaxChecker_h
 
27
#define SyntaxChecker_h
 
28
 
 
29
#include "Lexer.h"
 
30
#include <yarr/YarrSyntaxChecker.h>
 
31
 
 
32
namespace JSC {
 
33
    
 
34
class SyntaxChecker {
 
35
public:
 
36
    struct BinaryExprContext {
 
37
        BinaryExprContext(SyntaxChecker& context)
 
38
            : m_context(&context)
 
39
        {
 
40
            m_context->m_topBinaryExprs.append(m_context->m_topBinaryExpr);
 
41
            m_context->m_topBinaryExpr = 0;
 
42
        }
 
43
        ~BinaryExprContext()
 
44
        {
 
45
            m_context->m_topBinaryExpr = m_context->m_topBinaryExprs.last();
 
46
            m_context->m_topBinaryExprs.removeLast();
 
47
        }
 
48
    private:
 
49
        SyntaxChecker* m_context;
 
50
    };
 
51
    struct UnaryExprContext {
 
52
        UnaryExprContext(SyntaxChecker& context)
 
53
            : m_context(&context)
 
54
        {
 
55
            m_context->m_topUnaryTokens.append(m_context->m_topUnaryToken);
 
56
            m_context->m_topUnaryToken = 0;
 
57
        }
 
58
        ~UnaryExprContext()
 
59
        {
 
60
            m_context->m_topUnaryToken = m_context->m_topUnaryTokens.last();
 
61
            m_context->m_topUnaryTokens.removeLast();
 
62
        }
 
63
    private:
 
64
        SyntaxChecker* m_context;
 
65
    };
 
66
    
 
67
    SyntaxChecker(JSGlobalData* , void*)
 
68
    {
 
69
    }
 
70
 
 
71
    typedef SyntaxChecker FunctionBodyBuilder;
 
72
    enum { NoneExpr = 0,
 
73
        ResolveEvalExpr, ResolveExpr, NumberExpr, StringExpr,
 
74
        ThisExpr, NullExpr, BoolExpr, RegExpExpr, ObjectLiteralExpr,
 
75
        FunctionExpr, BracketExpr, DotExpr, CallExpr,
 
76
        NewExpr, PreExpr, PostExpr, UnaryExpr, BinaryExpr,
 
77
        ConditionalExpr, AssignmentExpr, TypeofExpr,
 
78
        DeleteExpr, ArrayLiteralExpr };
 
79
    typedef int ExpressionType;
 
80
 
 
81
    typedef ExpressionType Expression;
 
82
    typedef int SourceElements;
 
83
    typedef int Arguments;
 
84
    typedef ExpressionType Comma;
 
85
    struct Property {
 
86
        ALWAYS_INLINE Property(void* = 0)
 
87
            : type((PropertyNode::Type)0)
 
88
        {
 
89
        }
 
90
        ALWAYS_INLINE Property(const Identifier* ident, PropertyNode::Type ty)
 
91
        : name(ident)
 
92
        , type(ty)
 
93
        {
 
94
        }
 
95
        ALWAYS_INLINE Property(PropertyNode::Type ty)
 
96
            : name(0)
 
97
            , type(ty)
 
98
        {
 
99
        }
 
100
        ALWAYS_INLINE bool operator!() { return !type; }
 
101
        const Identifier* name;
 
102
        PropertyNode::Type type;
 
103
    };
 
104
    typedef int PropertyList;
 
105
    typedef int ElementList;
 
106
    typedef int ArgumentsList;
 
107
    typedef int FormalParameterList;
 
108
    typedef int FunctionBody;
 
109
    typedef int Statement;
 
110
    typedef int ClauseList;
 
111
    typedef int Clause;
 
112
    typedef int ConstDeclList;
 
113
    typedef int BinaryOperand;
 
114
    
 
115
    static const bool CreatesAST = false;
 
116
    static const bool NeedsFreeVariableInfo = false;
 
117
    static const bool CanUseFunctionCache = true;
 
118
    static const unsigned DontBuildKeywords = LexexFlagsDontBuildKeywords;
 
119
    static const unsigned DontBuildStrings = LexerFlagsDontBuildStrings;
 
120
 
 
121
    int createSourceElements() { return 1; }
 
122
    ExpressionType makeFunctionCallNode(const JSTokenLocation&, int, int, int, int, int) { return CallExpr; }
 
123
    void appendToComma(ExpressionType& base, ExpressionType right) { base = right; }
 
124
    ExpressionType createCommaExpr(const JSTokenLocation&, ExpressionType, ExpressionType right) { return right; }
 
125
    ExpressionType makeAssignNode(const JSTokenLocation&, ExpressionType, Operator, ExpressionType, bool, bool, int, int, int) { return AssignmentExpr; }
 
126
    ExpressionType makePrefixNode(const JSTokenLocation&, ExpressionType, Operator, int, int, int) { return PreExpr; }
 
127
    ExpressionType makePostfixNode(const JSTokenLocation&, ExpressionType, Operator, int, int, int) { return PostExpr; }
 
128
    ExpressionType makeTypeOfNode(const JSTokenLocation&, ExpressionType) { return TypeofExpr; }
 
129
    ExpressionType makeDeleteNode(const JSTokenLocation&, ExpressionType, int, int, int) { return DeleteExpr; }
 
130
    ExpressionType makeNegateNode(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
 
131
    ExpressionType makeBitwiseNotNode(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
 
132
    ExpressionType createLogicalNot(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
 
133
    ExpressionType createUnaryPlus(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
 
134
    ExpressionType createVoid(const JSTokenLocation&, ExpressionType) { return UnaryExpr; }
 
135
    ExpressionType thisExpr(const JSTokenLocation&) { return ThisExpr; }
 
136
    ExpressionType createResolve(const JSTokenLocation&, const Identifier*, int) { return ResolveExpr; }
 
137
    ExpressionType createObjectLiteral(const JSTokenLocation&) { return ObjectLiteralExpr; }
 
138
    ExpressionType createObjectLiteral(const JSTokenLocation&, int) { return ObjectLiteralExpr; }
 
139
    ExpressionType createArray(const JSTokenLocation&, int) { return ArrayLiteralExpr; }
 
140
    ExpressionType createArray(const JSTokenLocation&, int, int) { return ArrayLiteralExpr; }
 
141
    ExpressionType createNumberExpr(const JSTokenLocation&, double) { return NumberExpr; }
 
142
    ExpressionType createString(const JSTokenLocation&, const Identifier*) { return StringExpr; }
 
143
    ExpressionType createBoolean(const JSTokenLocation&, bool) { return BoolExpr; }
 
144
    ExpressionType createNull(const JSTokenLocation&) { return NullExpr; }
 
145
    ExpressionType createBracketAccess(const JSTokenLocation&, ExpressionType, ExpressionType, bool, int, int, int) { return BracketExpr; }
 
146
    ExpressionType createDotAccess(const JSTokenLocation&, ExpressionType, const Identifier*, int, int, int) { return DotExpr; }
 
147
    ExpressionType createRegExp(const JSTokenLocation&, const Identifier& pattern, const Identifier&, int) { return Yarr::checkSyntax(pattern.string()) ? 0 : RegExpExpr; }
 
148
    ExpressionType createNewExpr(const JSTokenLocation&, ExpressionType, int, int, int, int) { return NewExpr; }
 
149
    ExpressionType createNewExpr(const JSTokenLocation&, ExpressionType, int, int) { return NewExpr; }
 
150
    ExpressionType createConditionalExpr(const JSTokenLocation&, ExpressionType, ExpressionType, ExpressionType) { return ConditionalExpr; }
 
151
    ExpressionType createAssignResolve(const JSTokenLocation&, const Identifier&, ExpressionType, int, int, int) { return AssignmentExpr; }
 
152
    ExpressionType createFunctionExpr(const JSTokenLocation&, const Identifier*, int, int, int, int, int, int) { return FunctionExpr; }
 
153
    int createFunctionBody(const JSTokenLocation&, bool) { return 1; }
 
154
    int createArguments() { return 1; }
 
155
    int createArguments(int) { return 1; }
 
156
    int createArgumentsList(const JSTokenLocation&, int) { return 1; }
 
157
    int createArgumentsList(const JSTokenLocation&, int, int) { return 1; }
 
158
    template <bool complete> Property createProperty(const Identifier* name, int, PropertyNode::Type type)
 
159
    {
 
160
        if (!complete)
 
161
            return Property(type);
 
162
        ASSERT(name);
 
163
        return Property(name, type);
 
164
    }
 
165
    template <bool complete> Property createProperty(JSGlobalData* globalData, double name, int, PropertyNode::Type type)
 
166
    {
 
167
        if (!complete)
 
168
            return Property(type);
 
169
        return Property(&globalData->parserArena->identifierArena().makeNumericIdentifier(globalData, name), type);
 
170
    }
 
171
    int createPropertyList(const JSTokenLocation&, Property) { return 1; }
 
172
    int createPropertyList(const JSTokenLocation&, Property, int) { return 1; }
 
173
    int createElementList(int, int) { return 1; }
 
174
    int createElementList(int, int, int) { return 1; }
 
175
    int createFormalParameterList(const Identifier&) { return 1; }
 
176
    int createFormalParameterList(int, const Identifier&) { return 1; }
 
177
    int createClause(int, int) { return 1; }
 
178
    int createClauseList(int) { return 1; }
 
179
    int createClauseList(int, int) { return 1; }
 
180
    void setUsesArguments(int) { }
 
181
    int createFuncDeclStatement(const JSTokenLocation&, const Identifier*, int, int, int, int, int, int) { return 1; }
 
182
    int createBlockStatement(const JSTokenLocation&, int, int, int) { return 1; }
 
183
    int createExprStatement(const JSTokenLocation&, int, int, int) { return 1; }
 
184
    int createIfStatement(const JSTokenLocation&, int, int, int, int) { return 1; }
 
185
    int createIfStatement(const JSTokenLocation&, int, int, int, int, int) { return 1; }
 
186
    int createForLoop(const JSTokenLocation&, int, int, int, int, int, int) { return 1; }
 
187
    int createForInLoop(const JSTokenLocation&, const Identifier*, int, int, int, int, int, int, int, int, int, int) { return 1; }
 
188
    int createForInLoop(const JSTokenLocation&, int, int, int, int, int, int, int, int) { return 1; }
 
189
    int createEmptyStatement(const JSTokenLocation&) { return 1; }
 
190
    int createVarStatement(const JSTokenLocation&, int, int, int) { return 1; }
 
191
    int createReturnStatement(const JSTokenLocation&, int, int, int, int, int) { return 1; }
 
192
    int createBreakStatement(const JSTokenLocation&, int, int, int, int) { return 1; }
 
193
    int createBreakStatement(const JSTokenLocation&, const Identifier*, int, int, int, int) { return 1; }
 
194
    int createContinueStatement(const JSTokenLocation&, int, int, int, int) { return 1; }
 
195
    int createContinueStatement(const JSTokenLocation&, const Identifier*, int, int, int, int) { return 1; }
 
196
    int createTryStatement(const JSTokenLocation&, int, const Identifier*, int, int, int, int) { return 1; }
 
197
    int createSwitchStatement(const JSTokenLocation&, int, int, int, int, int, int) { return 1; }
 
198
    int createWhileStatement(const JSTokenLocation&, int, int, int, int) { return 1; }
 
199
    int createWithStatement(const JSTokenLocation&, int, int, int, int, int, int) { return 1; }
 
200
    int createDoWhileStatement(const JSTokenLocation&, int, int, int, int) { return 1; }
 
201
    int createLabelStatement(const JSTokenLocation&, const Identifier*, int, int, int) { return 1; }
 
202
    int createThrowStatement(const JSTokenLocation&, int, int, int, int, int) { return 1; }
 
203
    int createDebugger(const JSTokenLocation&, int, int) { return 1; }
 
204
    int createConstStatement(const JSTokenLocation&, int, int, int) { return 1; }
 
205
    int appendConstDecl(const JSTokenLocation&, int, const Identifier*, int) { return 1; }
 
206
    template <bool strict> Property createGetterOrSetterProperty(const JSTokenLocation&, PropertyNode::Type type, const Identifier* name, int, int, int, int, int, int)
 
207
    {
 
208
        ASSERT(name);
 
209
        if (!strict)
 
210
            return Property(type);
 
211
        return Property(name, type);
 
212
    }
 
213
    template <bool strict> Property createGetterOrSetterProperty(JSGlobalData* globalData, const JSTokenLocation&, PropertyNode::Type type, double name, int, int, int, int, int, int)
 
214
    {
 
215
        if (!strict)
 
216
            return Property(type);
 
217
        return Property(&globalData->parserArena->identifierArena().makeNumericIdentifier(globalData, name), type);
 
218
    }
 
219
 
 
220
    void appendStatement(int, int) { }
 
221
    void addVar(const Identifier*, bool) { }
 
222
    int combineCommaNodes(const JSTokenLocation&, int, int) { return 1; }
 
223
    int evalCount() const { return 0; }
 
224
    void appendBinaryExpressionInfo(int& operandStackDepth, int expr, int, int, int, bool)
 
225
    {
 
226
        if (!m_topBinaryExpr)
 
227
            m_topBinaryExpr = expr;
 
228
        else
 
229
            m_topBinaryExpr = BinaryExpr;
 
230
        operandStackDepth++;
 
231
    }
 
232
    
 
233
    // Logic to handle datastructures used during parsing of binary expressions
 
234
    void operatorStackPop(int& operatorStackDepth) { operatorStackDepth--; }
 
235
    bool operatorStackHasHigherPrecedence(int&, int) { return true; }
 
236
    BinaryOperand getFromOperandStack(int) { return m_topBinaryExpr; }
 
237
    void shrinkOperandStackBy(int& operandStackDepth, int amount) { operandStackDepth -= amount; }
 
238
    void appendBinaryOperation(const JSTokenLocation&, int& operandStackDepth, int&, BinaryOperand, BinaryOperand) { operandStackDepth++; }
 
239
    void operatorStackAppend(int& operatorStackDepth, int, int) { operatorStackDepth++; }
 
240
    int popOperandStack(int&) { int res = m_topBinaryExpr; m_topBinaryExpr = 0; return res; }
 
241
    
 
242
    void appendUnaryToken(int& stackDepth, int tok, int) { stackDepth = 1; m_topUnaryToken = tok; }
 
243
    int unaryTokenStackLastType(int&) { return m_topUnaryToken; }
 
244
    int unaryTokenStackLastStart(int&) { return 0; }
 
245
    void unaryTokenStackRemoveLast(int& stackDepth) { stackDepth = 0; }
 
246
    
 
247
    void assignmentStackAppend(int, int, int, int, int, Operator) { }
 
248
    int createAssignment(const JSTokenLocation&, int, int, int, int, int) { ASSERT_NOT_REACHED(); return 1; }
 
249
    const Identifier& getName(const Property& property) const { ASSERT(property.name); return *property.name; }
 
250
    PropertyNode::Type getType(const Property& property) const { return property.type; }
 
251
    bool isResolve(ExpressionType expr) const { return expr == ResolveExpr || expr == ResolveEvalExpr; }
 
252
    
 
253
private:
 
254
    int m_topBinaryExpr;
 
255
    int m_topUnaryToken;
 
256
    Vector<int, 8> m_topBinaryExprs;
 
257
    Vector<int, 8> m_topUnaryTokens;
 
258
};
 
259
 
 
260
}
 
261
 
 
262
#endif