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 | public EbnfFormatterVisitor(EObject rootNode, FormatterConfig config) {
|
---|
35 | super(rootNode);
|
---|
36 | this.config = config;
|
---|
37 | buf = new StringBuffer();
|
---|
38 | }
|
---|
39 |
|
---|
40 | public EbnfFormatterVisitor(FormatterConfig config) {
|
---|
41 | this.config = config;
|
---|
42 | buf = new StringBuffer();
|
---|
43 | }
|
---|
44 |
|
---|
45 | public StringBuffer getBuf() {
|
---|
46 | return buf;
|
---|
47 | }
|
---|
48 |
|
---|
49 | // -----------------------------------------------------------------------------
|
---|
50 |
|
---|
51 | protected void visitBefore(EtsiBnf node) {
|
---|
52 | buf.append("grammar " + node.getName());
|
---|
53 | if (node.getType() != null)
|
---|
54 | buf.append(node.getType());
|
---|
55 | buf.append(";");
|
---|
56 | buf.append("\n");
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected void visitAfter(EtsiBnf node) {
|
---|
60 | }
|
---|
61 |
|
---|
62 | protected void visitBefore(ImportSection node) {
|
---|
63 | buf.append("\n");
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected void visitAfter(ImportSection node) {
|
---|
67 | buf.append("\n");
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected void visitBefore(BnfEntry node) {
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected void visitAfter(BnfEntry node) {
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected void visitBefore(DeltaEntry node) {
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected void visitAfter(DeltaEntry node) {
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected void visitBefore(MergeEntry node) {
|
---|
83 | }
|
---|
84 |
|
---|
85 | protected void visitAfter(MergeEntry node) {
|
---|
86 | }
|
---|
87 |
|
---|
88 | protected void visitBefore(Atom node) {
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected void visitAfter(Atom node) {
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected void visitBefore(Term node) {
|
---|
95 | }
|
---|
96 |
|
---|
97 | protected void visitAfter(Term node) {
|
---|
98 | if (!isLastElement())
|
---|
99 | buf.append(" ");
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected void visitBefore(DefinitionList node) {
|
---|
103 | }
|
---|
104 |
|
---|
105 | protected void visitAfter(DefinitionList node) {
|
---|
106 | }
|
---|
107 |
|
---|
108 | protected void visitBefore(ExtRule node) {
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected void visitAfter(ExtRule node) {
|
---|
112 | }
|
---|
113 |
|
---|
114 | protected void visitBefore(GlobalCombinator node) {
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected void visitAfter(GlobalCombinator node) {
|
---|
118 | }
|
---|
119 |
|
---|
120 | protected void visitBefore(GroupedSequence node) {
|
---|
121 | buf.append("(");
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected void visitAfter(GroupedSequence node) {
|
---|
125 | buf.append(")");
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected void visitBefore(HookCombinator node) {
|
---|
129 | }
|
---|
130 |
|
---|
131 | protected void visitAfter(HookCombinator node) {
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected void visitBefore(Import node) {
|
---|
135 | buf.append("import \"" + node.getImportURI() + "\";\n");
|
---|
136 | }
|
---|
137 |
|
---|
138 | protected void visitAfter(Import node) {
|
---|
139 | }
|
---|
140 |
|
---|
141 | protected void visitBefore(MergeRule node) {
|
---|
142 | }
|
---|
143 |
|
---|
144 | protected void visitAfter(MergeRule node) {
|
---|
145 | }
|
---|
146 |
|
---|
147 | protected void visitBefore(OptionalSequence node) {
|
---|
148 | buf.append("[");
|
---|
149 | }
|
---|
150 |
|
---|
151 | protected void visitAfter(OptionalSequence node) {
|
---|
152 | buf.append("]");
|
---|
153 | }
|
---|
154 |
|
---|
155 | protected void visitBefore(RepeatedSequence node) {
|
---|
156 | buf.append("{");
|
---|
157 | }
|
---|
158 |
|
---|
159 | protected void visitAfter(RepeatedSequence node) {
|
---|
160 | buf.append("}");
|
---|
161 | if (node.isMorethanonce())
|
---|
162 | buf.append("+");
|
---|
163 | }
|
---|
164 |
|
---|
165 | protected void visitBefore(Rule node) {
|
---|
166 | if (node.getRulenumber() > 0)
|
---|
167 | buf.append(node.getRulenumber() + ". ");
|
---|
168 |
|
---|
169 | buf.append(node.getName() + " ::= ");
|
---|
170 | }
|
---|
171 |
|
---|
172 | protected void visitAfter(Rule node) {
|
---|
173 | buf.append(";\n");
|
---|
174 | }
|
---|
175 |
|
---|
176 | protected void visitBefore(RuleCombinator node) {
|
---|
177 | }
|
---|
178 |
|
---|
179 | protected void visitAfter(RuleCombinator node) {
|
---|
180 | }
|
---|
181 |
|
---|
182 | protected void visitBefore(RuleReference node) {
|
---|
183 | buf.append(node.getRuleref().getName());
|
---|
184 | }
|
---|
185 |
|
---|
186 | protected void visitAfter(RuleReference node) {
|
---|
187 | }
|
---|
188 |
|
---|
189 | protected void visitBefore(SectionHeading node) {
|
---|
190 | if (!buf.substring(buf.length()-2).equals("\n\n"))
|
---|
191 | buf.append("\n");
|
---|
192 |
|
---|
193 | buf.append(node.getSectionHeader());
|
---|
194 | }
|
---|
195 |
|
---|
196 | protected void visitAfter(SectionHeading node) {
|
---|
197 | buf.append("\n");
|
---|
198 | }
|
---|
199 |
|
---|
200 | protected void visitBefore(SingleDefinition node) {
|
---|
201 | }
|
---|
202 |
|
---|
203 | protected void visitAfter(SingleDefinition node) {
|
---|
204 | if (!isLastElement())
|
---|
205 | buf.append(" | ");
|
---|
206 |
|
---|
207 | }
|
---|
208 |
|
---|
209 | protected void visitBefore(StringRule node) {
|
---|
210 | if (node.getLiteral() != null)
|
---|
211 | buf.append("\"" + node.getLiteral() + "\"");
|
---|
212 | else if (node.getColon() != null)
|
---|
213 | buf.append("\"\"\"");
|
---|
214 | }
|
---|
215 |
|
---|
216 | protected void visitAfter(StringRule node) {
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 |
|
---|
221 | }
|
---|