1 | package de.ugoe.cs.swe.bnftools.ui.formatter;
|
---|
2 |
|
---|
3 | import org.eclipse.emf.common.util.EList;
|
---|
4 | import org.eclipse.emf.ecore.EObject;
|
---|
5 |
|
---|
6 | import de.ugoe.cs.swe.bnftools.ebnf.Atom;
|
---|
7 | import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry;
|
---|
8 | import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList;
|
---|
9 | import de.ugoe.cs.swe.bnftools.ebnf.DeltaEntry;
|
---|
10 | import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf;
|
---|
11 | import de.ugoe.cs.swe.bnftools.ebnf.ExtRule;
|
---|
12 | import de.ugoe.cs.swe.bnftools.ebnf.GlobalCombinator;
|
---|
13 | import de.ugoe.cs.swe.bnftools.ebnf.GroupedSequence;
|
---|
14 | import de.ugoe.cs.swe.bnftools.ebnf.HookCombinator;
|
---|
15 | import de.ugoe.cs.swe.bnftools.ebnf.Import;
|
---|
16 | import de.ugoe.cs.swe.bnftools.ebnf.ImportSection;
|
---|
17 | import de.ugoe.cs.swe.bnftools.ebnf.MergeEntry;
|
---|
18 | import de.ugoe.cs.swe.bnftools.ebnf.MergeRule;
|
---|
19 | import de.ugoe.cs.swe.bnftools.ebnf.OptionalSequence;
|
---|
20 | import de.ugoe.cs.swe.bnftools.ebnf.RepeatedSequence;
|
---|
21 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
22 | import de.ugoe.cs.swe.bnftools.ebnf.RuleCombinator;
|
---|
23 | import de.ugoe.cs.swe.bnftools.ebnf.RuleReference;
|
---|
24 | import de.ugoe.cs.swe.bnftools.ebnf.SectionHeading;
|
---|
25 | import de.ugoe.cs.swe.bnftools.ebnf.SingleDefinition;
|
---|
26 | import de.ugoe.cs.swe.bnftools.ebnf.StringRule;
|
---|
27 | import de.ugoe.cs.swe.bnftools.ebnf.Term;
|
---|
28 | import de.ugoe.cs.swe.bnftools.visitor.EbnfVisitor;
|
---|
29 |
|
---|
30 | public class EbnfFormatterVisitor extends EbnfVisitor {
|
---|
31 | private StringBuffer buf;
|
---|
32 | private FormatterConfig config;
|
---|
33 |
|
---|
34 | private boolean lastWasSectionHeading=false;
|
---|
35 |
|
---|
36 | public EbnfFormatterVisitor(EObject rootNode, FormatterConfig config) {
|
---|
37 | super(rootNode);
|
---|
38 | this.config = config;
|
---|
39 | buf = new StringBuffer();
|
---|
40 | }
|
---|
41 |
|
---|
42 | public EbnfFormatterVisitor(FormatterConfig config) {
|
---|
43 | this.config = config;
|
---|
44 | buf = new StringBuffer();
|
---|
45 | }
|
---|
46 |
|
---|
47 | public StringBuffer getBuf() {
|
---|
48 | return buf;
|
---|
49 | }
|
---|
50 |
|
---|
51 | // -----------------------------------------------------------------------------
|
---|
52 |
|
---|
53 | protected void visitBefore(EtsiBnf node) {
|
---|
54 | buf.append("grammar " + node.getName());
|
---|
55 | if (node.getType() != null)
|
---|
56 | buf.append(node.getType());
|
---|
57 | buf.append(";");
|
---|
58 | buf.append("\n");
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected void visitAfter(EtsiBnf node) {
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected void visitBefore(ImportSection node) {
|
---|
65 | buf.append("\n");
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected void visitAfter(ImportSection node) {
|
---|
69 | buf.append("\n");
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected void visitBefore(BnfEntry node) {
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected void visitAfter(BnfEntry node) {
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected void visitBefore(DeltaEntry node) {
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected void visitAfter(DeltaEntry node) {
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected void visitBefore(MergeEntry node) {
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected void visitAfter(MergeEntry node) {
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected void visitBefore(Atom node) {
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected void visitAfter(Atom node) {
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected void visitBefore(Term node) {
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected void visitAfter(Term node) {
|
---|
100 | if (!isLastElement())
|
---|
101 | buf.append(" ");
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected void visitBefore(DefinitionList node) {
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected void visitAfter(DefinitionList node) {
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected void visitBefore(ExtRule node) {
|
---|
111 | }
|
---|
112 |
|
---|
113 | protected void visitAfter(ExtRule node) {
|
---|
114 | }
|
---|
115 |
|
---|
116 | protected void visitBefore(GlobalCombinator node) {
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected void visitAfter(GlobalCombinator node) {
|
---|
120 | }
|
---|
121 |
|
---|
122 | protected void visitBefore(GroupedSequence node) {
|
---|
123 | buf.append("(");
|
---|
124 | }
|
---|
125 |
|
---|
126 | protected void visitAfter(GroupedSequence node) {
|
---|
127 | buf.append(")");
|
---|
128 | }
|
---|
129 |
|
---|
130 | protected void visitBefore(HookCombinator node) {
|
---|
131 | }
|
---|
132 |
|
---|
133 | protected void visitAfter(HookCombinator node) {
|
---|
134 | }
|
---|
135 |
|
---|
136 | protected void visitBefore(Import node) {
|
---|
137 | buf.append("import \"" + node.getImportURI() + "\";\n");
|
---|
138 | }
|
---|
139 |
|
---|
140 | protected void visitAfter(Import node) {
|
---|
141 | }
|
---|
142 |
|
---|
143 | protected void visitBefore(MergeRule node) {
|
---|
144 | }
|
---|
145 |
|
---|
146 | protected void visitAfter(MergeRule node) {
|
---|
147 | }
|
---|
148 |
|
---|
149 | protected void visitBefore(OptionalSequence node) {
|
---|
150 | buf.append("[");
|
---|
151 | }
|
---|
152 |
|
---|
153 | protected void visitAfter(OptionalSequence node) {
|
---|
154 | buf.append("]");
|
---|
155 | }
|
---|
156 |
|
---|
157 | protected void visitBefore(RepeatedSequence node) {
|
---|
158 | buf.append("{");
|
---|
159 | }
|
---|
160 |
|
---|
161 | protected void visitAfter(RepeatedSequence node) {
|
---|
162 | buf.append("}");
|
---|
163 | if (node.isMorethanonce())
|
---|
164 | buf.append("+");
|
---|
165 | }
|
---|
166 |
|
---|
167 | protected void visitBefore(Rule node) {
|
---|
168 | if (lastWasSectionHeading)
|
---|
169 | buf.append("\n");
|
---|
170 |
|
---|
171 | lastWasSectionHeading=false;
|
---|
172 |
|
---|
173 | if (node.getRulenumber() > 0)
|
---|
174 | buf.append(node.getRulenumber() + ". ");
|
---|
175 |
|
---|
176 | buf.append(node.getName() + " ::= ");
|
---|
177 | }
|
---|
178 |
|
---|
179 | protected void visitAfter(Rule node) {
|
---|
180 | buf.append(";\n");
|
---|
181 | }
|
---|
182 |
|
---|
183 | protected void visitBefore(RuleCombinator node) {
|
---|
184 | }
|
---|
185 |
|
---|
186 | protected void visitAfter(RuleCombinator node) {
|
---|
187 | }
|
---|
188 |
|
---|
189 | protected void visitBefore(RuleReference node) {
|
---|
190 | buf.append(node.getRuleref().getName());
|
---|
191 | }
|
---|
192 |
|
---|
193 | protected void visitAfter(RuleReference node) {
|
---|
194 | }
|
---|
195 |
|
---|
196 | protected void visitBefore(SectionHeading node) {
|
---|
197 | if (!lastWasSectionHeading && !buf.substring(buf.length()-2).equals("\n\n"))
|
---|
198 | buf.append("\n");
|
---|
199 |
|
---|
200 | lastWasSectionHeading=true;
|
---|
201 | // if (!lastWasSectionHeading || !buf.substring(buf.length()-2).equals("\n\n"))
|
---|
202 | // if (!buf.substring(buf.length()-2).equals("\n\n"))
|
---|
203 | // buf.append("\n");
|
---|
204 |
|
---|
205 | buf.append(node.getSectionHeader());
|
---|
206 | }
|
---|
207 |
|
---|
208 | protected void visitAfter(SectionHeading node) {
|
---|
209 | // buf.append("\n");
|
---|
210 | }
|
---|
211 |
|
---|
212 | protected void visitBefore(SingleDefinition node) {
|
---|
213 | }
|
---|
214 |
|
---|
215 | protected void visitAfter(SingleDefinition node) {
|
---|
216 | if (!isLastElement())
|
---|
217 | buf.append(" | ");
|
---|
218 |
|
---|
219 | }
|
---|
220 |
|
---|
221 | protected void visitBefore(StringRule node) {
|
---|
222 | if (node.getLiteral() != null)
|
---|
223 | buf.append("\"" + node.getLiteral() + "\"");
|
---|
224 | else if (node.getColon() != null)
|
---|
225 | buf.append("\"\"\"");
|
---|
226 | }
|
---|
227 |
|
---|
228 | protected void visitAfter(StringRule node) {
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 |
|
---|
233 | }
|
---|