KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > walkers > DeclarationExtractor


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

17
18 package org.sablecc.sablecc.walkers;
19
20 import org.sablecc.sablecc.GlobalInformation;
21 import org.sablecc.sablecc.exception.BypassSemanticException;
22 import org.sablecc.sablecc.exception.InternalException;
23 import org.sablecc.sablecc.exception.SemanticException;
24 import org.sablecc.sablecc.syntax3.analysis.DepthFirstAdapter;
25 import org.sablecc.sablecc.syntax3.node.AGroup;
26 import org.sablecc.sablecc.syntax3.node.AHelper;
27 import org.sablecc.sablecc.syntax3.node.AInternalState;
28 import org.sablecc.sablecc.syntax3.node.AInvestigator;
29 import org.sablecc.sablecc.syntax3.node.ANoStatesLexer;
30 import org.sablecc.sablecc.syntax3.node.ANormalState;
31 import org.sablecc.sablecc.syntax3.node.ASelectionToken;
32 import org.sablecc.sablecc.syntax3.node.ASimpleToken;
33 import org.sablecc.sablecc.syntax3.node.ASimpleTokens;
34
35 public class DeclarationExtractor
36         extends DepthFirstAdapter {
37
38     private final GlobalInformation globalInformation;
39
40     private AGroup enclosingGroup = null;
41
42     private DeclarationExtractor(
43             GlobalInformation globalInformation) {
44
45         this.globalInformation = globalInformation;
46     }
47
48     @Override JavaDoc
49     public void inANoStatesLexer(
50             ANoStatesLexer node) {
51
52         this.globalInformation.getLanguage().addState(null);
53     }
54
55     @Override JavaDoc
56     public void caseAHelper(
57             AHelper node) {
58
59         try {
60             this.globalInformation.addHelper(node);
61         }
62         catch (SemanticException e) {
63             throw new BypassSemanticException(e);
64         }
65     }
66
67     @Override JavaDoc
68     public void inASimpleTokens(
69             ASimpleTokens node) {
70
71         this.globalInformation.getLanguage().addGroup(null);
72     }
73
74     @Override JavaDoc
75     public void inAGroup(
76             AGroup node) {
77
78         try {
79             this.globalInformation.addGroup(node);
80         }
81         catch (SemanticException e) {
82             throw new BypassSemanticException(e);
83         }
84
85         this.enclosingGroup = node;
86     }
87
88     @Override JavaDoc
89     public void outAGroup(
90             AGroup node) {
91
92         this.enclosingGroup = null;
93     }
94
95     @Override JavaDoc
96     public void caseASimpleToken(
97             ASimpleToken node) {
98
99         try {
100             this.globalInformation.addSimpleToken(node, this.enclosingGroup);
101         }
102         catch (SemanticException e) {
103             throw new BypassSemanticException(e);
104         }
105     }
106
107     @Override JavaDoc
108     public void caseASelectionToken(
109             ASelectionToken node) {
110
111         try {
112             this.globalInformation.addSelectionToken(node, this.enclosingGroup);
113         }
114         catch (SemanticException e) {
115             throw new BypassSemanticException(e);
116         }
117     }
118
119     @Override JavaDoc
120     public void caseAInvestigator(
121             AInvestigator node) {
122
123         try {
124             this.globalInformation.addInvestigator(node);
125         }
126         catch (SemanticException e) {
127             throw new BypassSemanticException(e);
128         }
129     }
130
131     @Override JavaDoc
132     public void caseANormalState(
133             ANormalState node) {
134
135         try {
136             this.globalInformation.addState(node.getName());
137         }
138         catch (SemanticException e) {
139             throw new BypassSemanticException(e);
140         }
141     }
142
143     @Override JavaDoc
144     public void caseAInternalState(
145             AInternalState node) {
146
147         try {
148             this.globalInformation.addState(node.getName());
149         }
150         catch (SemanticException e) {
151             throw new BypassSemanticException(e);
152         }
153     }
154
155     public static void extractDeclarations(
156             GlobalInformation globalInformation)
157             throws SemanticException {
158
159         if (globalInformation == null) {
160             throw new InternalException("ast may not be null");
161         }
162
163         DeclarationExtractor declarationExtractor = new DeclarationExtractor(
164                 globalInformation);
165
166         try {
167             globalInformation.getAst().apply(declarationExtractor);
168         }
169         catch (BypassSemanticException e) {
170             throw e.getSemanticException();
171         }
172     }
173 }
174
Popular Tags