Debian Codegraph @

util/org/j_paine/formatter/FormatParser.jj

raw
1 /* FormatParser.java */
2
3
4 /*
5 This parser parses Fortran format strings.
6 */
7
8
9 options {
10 STATIC = true;
11 DEBUG_PARSER = false;
12 DEBUG_TOKEN_MANAGER = false;
13 DEBUG_LOOKAHEAD = false;
14 }
15
16 PARSER_BEGIN(FormatParser)
17 package org.j_paine.formatter;
18
19 class FormatParser
20 {
21 }
22
23 PARSER_END(FormatParser)
24
25
26 SKIP :
27 {
28 <(" ")+>
29 }
30
31
32 TOKEN :
33 {
34 <INTEGER: (["0"-"9"])+ >
35 }
36 // An unsigned integer, for repetition factors, field widths, etc.
37 // previously: <INTEGER: ["1"-"9"] (["0"-"9"])* >
38
39
40 TOKEN :
41 {
42 <STRING: "'" ( ~["'"] )* "'" >
43 }
44 // A string literal inside a format. We haven't implemented
45 // embedded quotes yet.
46
47 TOKEN : { < A_DESC : "A" | "a" > }
48 TOKEN : { < P_DESC : "P" | "p" > }
49 TOKEN : { < X_DESC : "X" | "x" > }
50 TOKEN : { < I_DESC : "I" | "i" > }
51 TOKEN : { < F_DESC : "F" | "f" > }
52 TOKEN : { < D_DESC : "D" | "d" > }
53 TOKEN : { < E_DESC : "E" | "e" > }
54 TOKEN : { < G_DESC : "G" | "g" > }
55 TOKEN : { < L_DESC : "L" | "l" > }
56
57 int Integer():
58 { Token t;
59 }
60 {
61 t=<INTEGER>
62 { return (Integer.valueOf(t.image)).intValue(); }
63 }
64
65 /* I split FormatIOElement into FormatIOElementFloat and
66 * FormatIOElementNonFloat because a floating point edit
67 * descriptor (F, E, D, or G) may follow a P edit descriptor
68 * without a comma. --kgs
69 */
70
71 FormatElement FormatIOElementFloat():
72 { FormatElement fe;
73 int w, d, m;
74 w = d = m = -1;
75 }
76 {
77 // for Iw.m, ignore the .m value
78 /* added D and G edit descriptors, but just use the E implementation. --kgs */
79 (
80 <F_DESC> w=Integer() "." d=Integer() { fe=new FormatF(w,d); }
81 | <D_DESC> w=Integer() "." d=Integer() { fe=new FormatE(w,d); }
82 | <E_DESC> w=Integer() "." d=Integer() { fe=new FormatE(w,d); }
83 | <G_DESC> w=Integer() "." d=Integer() { fe=new FormatE(w,d); }
84 )
85 { return fe; }
86 }
87
88 FormatElement FormatIOElementNonFloat():
89 { FormatElement fe;
90 int w, d, m;
91 w = d = m = -1;
92 }
93 {
94 // for Iw.m, ignore the .m value
95 /* added L edit descriptor. --kgs */
96 (
97 <A_DESC> [w=Integer()] { fe=new FormatA(w); }
98 | <I_DESC> w=Integer() ["." m=Integer()] { fe=new FormatI(w); }
99 | <L_DESC> w=Integer() { fe=new FormatL(w); }
100 )
101 { return fe; }
102 }
103 // This represents a format element that transfers one
104 // data item.
105
106
107 FormatElement FormatNonIOElement(): {}
108 {
109 <X_DESC> { return new FormatX(); }
110 }
111 // This represents a format element that doesn't transfer
112 // any data items.
113
114
115 FormatElement FormatElement():
116 { FormatElement fe;
117 }
118 {
119 ( fe=FormatIOElementFloat()
120 | fe=FormatIOElementNonFloat()
121 | fe=FormatNonIOElement()
122 | fe=FormatScale()
123 )
124 { return fe; }
125 }
126
127 FormatElement FormatScale():
128 { FormatElement fe = null;
129 int r=1;
130 }
131 {
132 /* Commas may be omitted between a P edit descriptor and an
133 * immediately following F, E, D, or G edit descriptor (13.5.9).
134 * --kgs
135 */
136
137 <P_DESC> [ [r=Integer()] (fe=FormatIOElementFloat()) ]
138 {
139 return new FormatP(r, fe);
140 }
141 }
142
143 FormatSlash FormatSlash(): {}
144 {
145 "/" { return new FormatSlash(); }
146 }
147 // These are a special case. Unlike other format elements,
148 // Fortran permits several slashes to be concatenated without
149 // commas to separate them, and you can't use a repetition
150 // factor on them.
151
152
153 FormatString FormatString():
154 { Token t;
155 String s;
156 }
157 {
158 ( t=<STRING> )
159 { s = t.image;
160 s = s.substring(1,s.length()-1); // Remove the quotes.
161 return new FormatString(s);
162 }
163 }
164 // Another special case that can't be repeated, and can be
165 // concatenated to other elements without commas.
166
167
168 void OptionalFormatSlashesOrStrings( Format f ):
169 { FormatUniv fs;
170 }
171 {
172 ( (fs=FormatSlash() | fs=FormatString()) { f.addElement(fs); } )*
173 }
174
175
176 FormatRepeatedItem FormatRepeatedItem():
177 { int r=1;
178 FormatUniv fu;
179 }
180 {
181 [ r=Integer() ]
182 ( "(" fu=Format() ")"
183 | fu=FormatElement()
184 )
185 {
186 /* here we check whether the parsed format element is a P edit
187 * descriptor. in that case, it may have parsed a floating point
188 * edit descriptor along with it (if it followed without a comma)
189 * so return that element here. --kgs
190 */
191
192 if(fu instanceof FormatP) {
193 FormatRepeatedItem ritem;
194
195 ritem = ((FormatP)fu).getRepeatedItem();
196
197 if(ritem != null)
198 return ritem;
199 else
200 return new FormatRepeatedItem( r, fu );
201 }
202 else
203 return new FormatRepeatedItem( r, fu );
204 }
205 }
206
207
208 void FormatGroup( Format f ):
209 { FormatRepeatedItem fri;
210 }
211 {
212 ( OptionalFormatSlashesOrStrings( f )
213 [ fri = FormatRepeatedItem() { if(fri != null) f.addElement(fri); }
214 OptionalFormatSlashesOrStrings( f )
215 ]
216 )
217 }
218 // This rather messy syntax allows us to have slashes and/or
219 // strings either side of a format element or repeated group
220 // without needing to separate them from each other or the element
221 // with commas.
222 // It also means that we can have empty format groups and format
223 // groups that don't transfer any data elements. So for example,
224 // the format ,/, is valid under this grammar.
225
226
227 Format Format():
228 { FormatRepeatedItem fri;
229 Format f = new Format();
230 }
231 {
232 ( FormatGroup(f) )
233 ( "," ( FormatGroup(f) ) )*
234 { return f; }
235 }