KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > js > ast > JsProgram


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.js.ast;
17
18 import java.math.BigInteger JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * A JavaScript program.
24  */

25 public final class JsProgram extends JsNode {
26
27   private final JsStatement debuggerStmt = new JsDebugger();
28
29   private final Map JavaDoc decimalLiteralMap = new HashMap JavaDoc();
30
31   private final JsEmpty emptyStmt = new JsEmpty();
32
33   private final JsBooleanLiteral falseLiteral = new JsBooleanLiteral(false);
34
35   private final JsGlobalBlock globalBlock;
36
37   private final Map JavaDoc integralLiteralMap = new HashMap JavaDoc();
38
39   private final JsNullLiteral nullLiteral = new JsNullLiteral();
40
41   private final JsScope objectScope;
42
43   private final JsRootScope rootScope;
44
45   private final Map JavaDoc stringLiteralMap = new HashMap JavaDoc();
46
47   private final JsScope topScope;
48
49   private final JsBooleanLiteral trueLiteral = new JsBooleanLiteral(true);
50
51   /**
52    * Constructs a JavaScript program object.
53    */

54   public JsProgram() {
55     rootScope = new JsRootScope(this);
56     globalBlock = new JsGlobalBlock();
57     topScope = new JsScope(rootScope, "Global");
58     objectScope = new JsScope(rootScope, "Object");
59   }
60
61   /**
62    * Gets the {@link JsStatement} to use whenever parsed source include a
63    * <code>debugger</code> statement.
64    *
65    * @see #setDebuggerStmt(JsStatement)
66    */

67   public JsStatement getDebuggerStmt() {
68     return debuggerStmt;
69   }
70
71   public JsDecimalLiteral getDecimalLiteral(String JavaDoc value) {
72     JsDecimalLiteral lit = (JsDecimalLiteral) decimalLiteralMap.get(value);
73     if (lit == null) {
74       lit = new JsDecimalLiteral(value);
75       decimalLiteralMap.put(value, lit);
76     }
77     return lit;
78   }
79
80   public JsEmpty getEmptyStmt() {
81     return emptyStmt;
82   }
83
84   public JsBooleanLiteral getFalseLiteral() {
85     return falseLiteral;
86   }
87
88   /**
89    * Gets the one and only global block.
90    */

91   public JsBlock getGlobalBlock() {
92     return globalBlock;
93   }
94
95   public JsIntegralLiteral getIntegralLiteral(BigInteger JavaDoc value) {
96     JsIntegralLiteral lit = (JsIntegralLiteral) integralLiteralMap.get(value);
97     if (lit == null) {
98       lit = new JsIntegralLiteral(value);
99       integralLiteralMap.put(value, lit);
100     }
101     return lit;
102   }
103
104   public JsNullLiteral getNullLiteral() {
105     return nullLiteral;
106   }
107
108   public JsScope getObjectScope() {
109     return objectScope;
110   }
111
112   /**
113    * Gets the quasi-mythical root scope. This is not the same as the top scope;
114    * all unresolvable identifiers wind up here, because they are considered
115    * external to the program.
116    */

117   public JsRootScope getRootScope() {
118     return rootScope;
119   }
120
121   /**
122    * Gets the top level scope. This is the scope of all the statements in the
123    * main program.
124    */

125   public JsScope getScope() {
126     return topScope;
127   }
128
129   public JsStringLiteral getStringLiteral(String JavaDoc value) {
130     JsStringLiteral lit = (JsStringLiteral) stringLiteralMap.get(value);
131     if (lit == null) {
132       lit = new JsStringLiteral(value);
133       stringLiteralMap.put(value, lit);
134     }
135     return lit;
136   }
137
138   public JsBooleanLiteral getTrueLiteral() {
139     return trueLiteral;
140   }
141
142   public void traverse(JsVisitor v, JsContext ctx) {
143     if (v.visit(this, ctx)) {
144       v.accept(globalBlock);
145     }
146     v.endVisit(this, ctx);
147   }
148 }
149
Popular Tags