KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > syntax > element > ElementBlock


1 package org.antlr.works.syntax.element;
2
3 import org.antlr.works.ate.syntax.misc.ATEToken;
4 import org.antlr.works.syntax.GrammarSyntaxParser;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8
9 /*
10
11 [The "BSD licence"]
12 Copyright (c) 2005 Jean Bovet
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
18
19 1. Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
21 2. Redistributions in binary form must reproduce the above copyright
22 notice, this list of conditions and the following disclaimer in the
23 documentation and/or other materials provided with the distribution.
24 3. The name of the author may not be used to endorse or promote products
25 derived from this software without specific prior written permission.
26
27 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 */

39
40 /** A block is, for example:
41  *
42  * tokens { FOO="a"; OTHER; }
43  *
44  * or
45  *
46  * options
47  * {
48  * tokenVocab=DataViewExpressions;
49  * output=AST;
50  * ASTLabelType=CommonTree;
51  * }
52  *
53  *
54  */

55 public class ElementBlock extends ElementScopable {
56
57     public String JavaDoc name;
58     public ATEToken start;
59     public ATEToken end;
60
61     public boolean isTokenBlock = false;
62     public boolean isOptionsBlock = false;
63
64     public List JavaDoc<ATEToken> internalTokens;
65     private List JavaDoc<ATEToken> declaredTokens;
66
67     private String JavaDoc tokenVocab;
68
69     public ElementBlock(String JavaDoc name, ATEToken start) {
70         this.name = name;
71         this.start = start;
72     }
73
74     /**
75      * Parse the content of the block to extract information that will be reused later
76      */

77     public void parse() {
78         if(name.equals(GrammarSyntaxParser.OPTIONS_BLOCK_NAME)) {
79             // Look only for the tokenVocab key/value pair.
80
parseForTokenVocab();
81             isOptionsBlock = true;
82         } else if(name.equals(GrammarSyntaxParser.TOKENS_BLOCK_NAME)) {
83             // Look for tokens
84
parseForTokens();
85             isTokenBlock = true;
86         }
87     }
88
89     /**
90      * Parses for the tokenVocab key/value pair of a block of type 'options'
91      */

92     private void parseForTokenVocab() {
93         for(int index=0; index<internalTokens.size(); index++) {
94             ATEToken t = internalTokens.get(index);
95             if(t.getAttribute().equals("tokenVocab") && index+2<internalTokens.size()) {
96                 t = internalTokens.get(index+2);
97                 tokenVocab = t.getAttribute();
98                 //System.out.println(">> tokenVocab = "+tokenVocab);
99
}
100         }
101     }
102
103     /**
104      * Parses the internal tokens of a block of type 'tokens'.
105      *
106      * The tokens are considered as the following:
107      * TOKEN_A="value";
108      * TOKEN_B;
109      * ...
110      */

111     private void parseForTokens() {
112         declaredTokens = new ArrayList JavaDoc<ATEToken>();
113         for(int index=0; index<internalTokens.size(); index++) {
114             ATEToken t = internalTokens.get(index);
115             if(t.getAttribute().equals("=")) {
116                 declaredTokens.add(internalTokens.get(index-1));
117                 // skip the value and the semi
118
index += 2;
119             } else if(t.getAttribute().equals(";")) {
120                 declaredTokens.add(internalTokens.get(index-1));
121                 // skip the semi
122
index++;
123
124             }
125         }
126         //System.out.println("Declared tokens="+declaredTokens);
127
}
128
129     public String JavaDoc getTokenVocab() {
130         return tokenVocab;
131     }
132
133     public List JavaDoc<ATEToken> getDeclaredTokens() {
134         return declaredTokens;
135     }
136
137     public List JavaDoc<String JavaDoc> getDeclaredTokensAsString() {
138         List JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>();
139         for(int t=0; t<declaredTokens.size(); t++) {
140             names.add(declaredTokens.get(t).getAttribute());
141         }
142         return names;
143     }
144
145 }
146
Popular Tags