source: default/trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfFormatterVisitor.java @ 17

Last change on this file since 17 was 17, checked in by zeiss, 14 years ago

sdfsd

  • Property svn:mime-type set to text/plain
File size: 5.5 KB
Line 
1package de.ugoe.cs.swe.bnftools.ui.formatter;
2
3import org.eclipse.emf.common.util.EList;
4import org.eclipse.emf.ecore.EObject;
5
6import de.ugoe.cs.swe.bnftools.ebnf.Atom;
7import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry;
8import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList;
9import de.ugoe.cs.swe.bnftools.ebnf.DeltaEntry;
10import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf;
11import de.ugoe.cs.swe.bnftools.ebnf.ExtRule;
12import de.ugoe.cs.swe.bnftools.ebnf.GlobalCombinator;
13import de.ugoe.cs.swe.bnftools.ebnf.GroupedSequence;
14import de.ugoe.cs.swe.bnftools.ebnf.HookCombinator;
15import de.ugoe.cs.swe.bnftools.ebnf.Import;
16import de.ugoe.cs.swe.bnftools.ebnf.ImportSection;
17import de.ugoe.cs.swe.bnftools.ebnf.MergeEntry;
18import de.ugoe.cs.swe.bnftools.ebnf.MergeRule;
19import de.ugoe.cs.swe.bnftools.ebnf.OptionalSequence;
20import de.ugoe.cs.swe.bnftools.ebnf.RepeatedSequence;
21import de.ugoe.cs.swe.bnftools.ebnf.Rule;
22import de.ugoe.cs.swe.bnftools.ebnf.RuleCombinator;
23import de.ugoe.cs.swe.bnftools.ebnf.RuleReference;
24import de.ugoe.cs.swe.bnftools.ebnf.SectionHeading;
25import de.ugoe.cs.swe.bnftools.ebnf.SingleDefinition;
26import de.ugoe.cs.swe.bnftools.ebnf.StringRule;
27import de.ugoe.cs.swe.bnftools.ebnf.Term;
28import de.ugoe.cs.swe.bnftools.visitor.EbnfVisitor;
29
30public 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}
Note: See TracBrowser for help on using the repository browser.