KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > ParserImpl


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.ParserImpl
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.impl.sql.compile.QueryTreeNode;
25 import org.apache.derby.iapi.sql.compile.Parser;
26 import org.apache.derby.iapi.sql.Statement;
27 import org.apache.derby.iapi.sql.compile.CompilerContext;
28
29 import org.apache.derby.iapi.reference.SQLState;
30 import org.apache.derby.iapi.error.StandardException;
31 import org.apache.derby.iapi.services.sanity.SanityManager;
32
33 public class ParserImpl implements Parser
34 {
35     /*
36     ** We will use the following constant to pass in to
37     ** our CharStream. It is the size of the internal
38     ** buffers that are used to buffer tokens. It
39     ** should be set to what is typically around the
40     ** largest token that is likely to be hit. Note
41     ** that if the size is exceeded, the buffer will
42     ** automatically be expanded by 2048, so it is ok
43     ** to choose something that is smaller than the
44     ** max token supported.
45     **
46     ** Since, JavaCC generates parser and tokenmanagers classes
47     ** tightly connected, to use another parser or tokenmanager
48     ** inherit this class, override the following methods
49     ** to use specific instances:<ul>
50     ** <li>getTokenManager()</li>
51     ** <li>getParser()</li>
52     ** <li>parseGoalProduction(...)</li>
53     ** </ul>
54     **
55     */

56     static final int LARGE_TOKEN_SIZE = 128;
57
58         /* Don't ever access these objects directly, call getParser(), and getTokenManager() */
59         protected Object JavaDoc cachedParser;
60     protected Object JavaDoc cachedTokenManager;
61
62     protected CharStream charStream;
63         protected String JavaDoc SQLtext;
64
65         protected final CompilerContext cc;
66
67     /**
68      * Constructor for Parser
69      */

70
71     public ParserImpl(CompilerContext cc)
72     {
73         this.cc = cc;
74     }
75
76     public QueryTreeNode parseStatement(String JavaDoc statementSQLText)
77         throws StandardException
78     {
79         return parseStatement(statementSQLText, (Object JavaDoc[])null);
80     }
81
82         /**
83      * Returns a initialized (clean) TokenManager, paired w. the Parser in getParser,
84      * Appropriate for this ParserImpl object.
85      */

86         protected Object JavaDoc getTokenManager()
87         {
88         /* returned a cached tokenmanager if already exists, otherwise create */
89         SQLParserTokenManager tm = (SQLParserTokenManager) cachedTokenManager;
90         if (tm == null) {
91         tm = new SQLParserTokenManager(charStream);
92         cachedTokenManager = tm;
93         } else {
94         tm.ReInit(charStream);
95         }
96         return tm;
97     }
98
99         /**
100      * new parser, appropriate for the ParserImpl object.
101      */

102         protected Object JavaDoc getParser()
103         {
104         SQLParserTokenManager tm = (SQLParserTokenManager) getTokenManager();
105         /* returned a cached Parser if already exists, otherwise create */
106         SQLParser p = (SQLParser) cachedParser;
107         if (p == null) {
108         p = new SQLParser(tm);
109         p.setCompilerContext(cc);
110         cachedParser = p;
111         } else {
112         p.ReInit(tm);
113         }
114         return p;
115     }
116
117     /**
118      * Parse a statement and return a query tree. Implements the Parser
119      * interface
120      *
121      * @param statementSQLText Statement to parse
122      * @param paramDefaults parameter defaults. Passed around as an array
123      * of objects, but is really an array of StorableDataValues
124      * @return A QueryTree representing the parsed statement
125      *
126      * @exception StandardException Thrown on error
127      */

128
129     public QueryTreeNode parseStatement(String JavaDoc statementSQLText, Object JavaDoc[] paramDefaults)
130         throws StandardException
131     {
132
133         java.io.Reader JavaDoc sqlText = new java.io.StringReader JavaDoc(statementSQLText);
134
135         /* Get a char stream if we don't have one already */
136         if (charStream == null)
137         {
138             charStream = new UCode_CharStream(sqlText, 1, 1, LARGE_TOKEN_SIZE);
139         }
140         else
141         {
142             charStream.ReInit(sqlText, 1, 1, LARGE_TOKEN_SIZE);
143         }
144
145         /* remember the string that we're parsing */
146         SQLtext = statementSQLText;
147
148         /* Parse the statement, and return the QueryTree */
149         try
150         {
151             return parseGoalProduction( statementSQLText, paramDefaults);
152         }
153         catch (ParseException e)
154         {
155             throw StandardException.newException(SQLState.LANG_SYNTAX_ERROR, e.getMessage());
156         }
157         catch (TokenMgrError e)
158         {
159             throw StandardException.newException(SQLState.LANG_LEXICAL_ERROR, e.getMessage());
160         }
161     }
162
163         /**
164      * Parse the goal production, e.g. "statement" for the normal SQL parser.
165      *
166      * @param statementSQLText The Statement to parse
167      * @param paramDefaults parameter defaults. Passed around as an array
168      * of objects, but is really an array of StorableDataValues
169          *
170      * @return A QueryTree representing the parsed statement
171      *
172      * @exception ParseException
173          * @exception TokenMgrError
174          */

175         protected QueryTreeNode parseGoalProduction( String JavaDoc statementSQLText,
176                                                    Object JavaDoc[] paramDefaults)
177             throws ParseException, TokenMgrError, StandardException
178         {
179         SQLParser p = (SQLParser) getParser();
180         return p.Statement( statementSQLText, paramDefaults);
181     } // End of parseGoalProduction
182

183     /**
184      * Returns the current SQL text string that is being parsed.
185      *
186      * @return Current SQL text string.
187      *
188      */

189     public String JavaDoc getSQLtext()
190     { return SQLtext; }
191 }
192
Popular Tags