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

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

sdfsd

  • Property svn:mime-type set to text/plain
File size: 5.5 KB
RevLine 
[9]1package de.ugoe.cs.swe.bnftools.ui.formatter;
2
[11]3import org.eclipse.emf.common.util.EList;
[9]4import org.eclipse.emf.ecore.EObject;
5
6import de.ugoe.cs.swe.bnftools.ebnf.Atom;
[13]7import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry;
[9]8import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList;
[13]9import de.ugoe.cs.swe.bnftools.ebnf.DeltaEntry;
[9]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;
[12]15import de.ugoe.cs.swe.bnftools.ebnf.Import;
[13]16import de.ugoe.cs.swe.bnftools.ebnf.ImportSection;
17import de.ugoe.cs.swe.bnftools.ebnf.MergeEntry;
[9]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
[17]34        private boolean lastWasSectionHeading=false;
35       
[9]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
[11]53        protected void visitBefore(EtsiBnf node) {
[9]54                buf.append("grammar " + node.getName());
55                if (node.getType() != null)
56                        buf.append(node.getType());
57                buf.append(";");
[13]58                buf.append("\n");
[9]59        }
60
[11]61        protected void visitAfter(EtsiBnf node) {
[9]62        }
63
[13]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       
[11]90        protected void visitBefore(Atom node) {
[9]91        }
92
[11]93        protected void visitAfter(Atom node) {
[9]94        }
95
[11]96        protected void visitBefore(Term node) {
[9]97        }
98
[11]99        protected void visitAfter(Term node) {
[12]100                if (!isLastElement())
[11]101                        buf.append(" ");
[9]102        }
103
[11]104        protected void visitBefore(DefinitionList node) {
[9]105        }
106
[11]107        protected void visitAfter(DefinitionList node) {
[9]108        }
109
[11]110        protected void visitBefore(ExtRule node) {
[9]111        }
112
[11]113        protected void visitAfter(ExtRule node) {
[9]114        }
115
[11]116        protected void visitBefore(GlobalCombinator node) {
[9]117        }
118
[11]119        protected void visitAfter(GlobalCombinator node) {
[9]120        }
121
[11]122        protected void visitBefore(GroupedSequence node) {
123                buf.append("(");
[9]124        }
125
[11]126        protected void visitAfter(GroupedSequence node) {
127                buf.append(")");
[9]128        }
129
[11]130        protected void visitBefore(HookCombinator node) {
[9]131        }
132
[11]133        protected void visitAfter(HookCombinator node) {
[9]134        }
135
[11]136        protected void visitBefore(Import node) {
[12]137                buf.append("import \"" + node.getImportURI() + "\";\n");
[9]138        }
139
[11]140        protected void visitAfter(Import node) {
[9]141        }
142
[11]143        protected void visitBefore(MergeRule node) {
144        }
145
146        protected void visitAfter(MergeRule node) {
147        }
148
149        protected void visitBefore(OptionalSequence node) {
[12]150                buf.append("[");
[11]151        }
152
153        protected void visitAfter(OptionalSequence node) {
[12]154                buf.append("]");
[11]155        }
156
157        protected void visitBefore(RepeatedSequence node) {
[12]158                buf.append("{");
[11]159        }
160
161        protected void visitAfter(RepeatedSequence node) {
[12]162                buf.append("}");
163                if (node.isMorethanonce())
164                        buf.append("+");
[11]165        }
166
167        protected void visitBefore(Rule node) {
[17]168                if (lastWasSectionHeading)
169                        buf.append("\n");
170               
171                lastWasSectionHeading=false;
172
[11]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) {
[12]190                buf.append(node.getRuleref().getName());
[11]191        }
192
193        protected void visitAfter(RuleReference node) {
194        }
195
196        protected void visitBefore(SectionHeading node) {
[17]197                if (!lastWasSectionHeading && !buf.substring(buf.length()-2).equals("\n\n"))
[13]198                        buf.append("\n");
199               
[17]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               
[13]205                buf.append(node.getSectionHeader());
[11]206        }
207
208        protected void visitAfter(SectionHeading node) {
[17]209//              buf.append("\n");
[11]210        }
211
212        protected void visitBefore(SingleDefinition node) {
213        }
214
215        protected void visitAfter(SingleDefinition node) {
[12]216                if (!isLastElement())
217                        buf.append(" | ");
218               
[11]219        }
220
221        protected void visitBefore(StringRule node) {
222                if (node.getLiteral() != null)
223                        buf.append("\"" + node.getLiteral() + "\"");
224                else if (node.getColon() != null)
[12]225                        buf.append("\"\"\"");
[11]226        }
227
228        protected void visitAfter(StringRule node) {
229        }
230
231       
232       
[9]233}
Note: See TracBrowser for help on using the repository browser.