KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > SemanticVerifier


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;
19
20 import org.sablecc.sablecc.exception.InternalException;
21 import org.sablecc.sablecc.exception.SemanticException;
22 import org.sablecc.sablecc.structure.Language;
23 import org.sablecc.sablecc.syntax3.node.TIdentifier;
24 import org.sablecc.sablecc.syntax3.node.TNumber;
25 import org.sablecc.sablecc.walkers.DeclarationExtractor;
26 import org.sablecc.sablecc.walkers.LanguageNameExtractor;
27 import org.sablecc.sablecc.walkers.SyntaxVersionExtractor;
28
29 public class SemanticVerifier {
30
31     public SemanticVerifier(
32             GlobalInformation globalInformation)
33             throws SemanticException {
34
35         if (globalInformation == null) {
36             throw new InternalException("globalInformation may not be null");
37         }
38
39         Language language = new Language(extractLanguageName(globalInformation)
40                 .getText());
41
42         globalInformation.setLanguage(language);
43
44         switch (globalInformation.getVerbosity()) {
45         case NORMAL:
46         case VERBOSE:
47             System.out.println(" Analyzing language '" + language.getName()
48                     + "'");
49         }
50
51         switch (globalInformation.getVerbosity()) {
52         case NORMAL:
53         case VERBOSE:
54             System.out.println(" Verifying semantics");
55         }
56
57         switch (globalInformation.getVerbosity()) {
58         case VERBOSE:
59             System.out.println(" Checking syntax version");
60         }
61
62         checkSyntaxVersion(globalInformation);
63
64         switch (globalInformation.getVerbosity()) {
65         case VERBOSE:
66             System.out.println(" Extracting declarations");
67         }
68
69         extractDeclarations(globalInformation);
70     }
71
72     private TIdentifier extractLanguageName(
73             GlobalInformation globalInformation) {
74
75         return LanguageNameExtractor.getLanguageName(globalInformation);
76     }
77
78     private void checkSyntaxVersion(
79             GlobalInformation globalInformation)
80             throws SemanticException {
81
82         TNumber versionToken = SyntaxVersionExtractor
83                 .getSyntaxVersion(globalInformation);
84
85         try {
86             int syntaxVersion = Integer.parseInt(versionToken.getText());
87
88             // we only support version 4
89
if (syntaxVersion != 4) {
90                 throw new SemanticException("unsupported syntax version",
91                         versionToken);
92             }
93         }
94         catch (NumberFormatException JavaDoc e) {
95             throw new SemanticException("unsupported syntax version",
96                     versionToken, e);
97         }
98     }
99
100     private void extractDeclarations(
101             GlobalInformation globalInformation)
102             throws SemanticException {
103
104         DeclarationExtractor.extractDeclarations(globalInformation);
105     }
106 }
107
Popular Tags