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 com.sun.xml.internal.bind.v2.schemagen.xmlschema.Import;
|
---|
7 |
|
---|
8 | import de.ugoe.cs.swe.bnftools.ebnf.Atom;
|
---|
9 | import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList;
|
---|
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.MergeRule;
|
---|
16 | import de.ugoe.cs.swe.bnftools.ebnf.OptionalSequence;
|
---|
17 | import de.ugoe.cs.swe.bnftools.ebnf.RepeatedSequence;
|
---|
18 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
19 | import de.ugoe.cs.swe.bnftools.ebnf.RuleCombinator;
|
---|
20 | import de.ugoe.cs.swe.bnftools.ebnf.RuleReference;
|
---|
21 | import de.ugoe.cs.swe.bnftools.ebnf.SectionHeading;
|
---|
22 | import de.ugoe.cs.swe.bnftools.ebnf.SingleDefinition;
|
---|
23 | import de.ugoe.cs.swe.bnftools.ebnf.StringRule;
|
---|
24 | import de.ugoe.cs.swe.bnftools.ebnf.Term;
|
---|
25 | import de.ugoe.cs.swe.bnftools.visitor.EbnfVisitor;
|
---|
26 |
|
---|
27 | public class EbnfFormatterVisitor extends EbnfVisitor {
|
---|
28 | private StringBuffer buf;
|
---|
29 | private FormatterConfig config;
|
---|
30 |
|
---|
31 | public EbnfFormatterVisitor(EObject rootNode, FormatterConfig config) {
|
---|
32 | super(rootNode);
|
---|
33 | this.config = config;
|
---|
34 | buf = new StringBuffer();
|
---|
35 | }
|
---|
36 |
|
---|
37 | public EbnfFormatterVisitor(FormatterConfig config) {
|
---|
38 | this.config = config;
|
---|
39 | buf = new StringBuffer();
|
---|
40 | }
|
---|
41 |
|
---|
42 | public StringBuffer getBuf() {
|
---|
43 | return buf;
|
---|
44 | }
|
---|
45 |
|
---|
46 | // -----------------------------------------------------------------------------
|
---|
47 |
|
---|
48 | protected void visitBefore(EtsiBnf node) {
|
---|
49 | buf.append("grammar " + node.getName());
|
---|
50 | if (node.getType() != null)
|
---|
51 | buf.append(node.getType());
|
---|
52 | buf.append(";");
|
---|
53 | buf.append("\n\n");
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected void visitAfter(EtsiBnf node) {
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected void visitBefore(Atom node) {
|
---|
60 | }
|
---|
61 |
|
---|
62 | protected void visitAfter(Atom node) {
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected void visitBefore(Term node) {
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected void visitAfter(Term node) {
|
---|
69 | SingleDefinition parent = (SingleDefinition) node.eContainer();
|
---|
70 | int index = parent.getTerms().indexOf(node);
|
---|
71 | if ((parent.getTerms().size() > 0) && (index < parent.getTerms().size()-1))
|
---|
72 | buf.append(" ");
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected void visitBefore(DefinitionList node) {
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected void visitAfter(DefinitionList node) {
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected void visitBefore(ExtRule node) {
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected void visitAfter(ExtRule node) {
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected void visitBefore(GlobalCombinator node) {
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected void visitAfter(GlobalCombinator node) {
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected void visitBefore(GroupedSequence node) {
|
---|
94 | buf.append("(");
|
---|
95 | }
|
---|
96 |
|
---|
97 | protected void visitAfter(GroupedSequence node) {
|
---|
98 | buf.append(")");
|
---|
99 | }
|
---|
100 |
|
---|
101 | protected void visitBefore(HookCombinator node) {
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected void visitAfter(HookCombinator node) {
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected void visitBefore(Import node) {
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected void visitAfter(Import node) {
|
---|
111 | }
|
---|
112 |
|
---|
113 | protected void visitBefore(MergeRule node) {
|
---|
114 | }
|
---|
115 |
|
---|
116 | protected void visitAfter(MergeRule node) {
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected void visitBefore(OptionalSequence node) {
|
---|
120 | }
|
---|
121 |
|
---|
122 | protected void visitAfter(OptionalSequence node) {
|
---|
123 | }
|
---|
124 |
|
---|
125 | protected void visitBefore(RepeatedSequence node) {
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected void visitAfter(RepeatedSequence node) {
|
---|
129 | }
|
---|
130 |
|
---|
131 | protected void visitBefore(Rule node) {
|
---|
132 | if (node.getRulenumber() > 0)
|
---|
133 | buf.append(node.getRulenumber() + ". ");
|
---|
134 |
|
---|
135 | buf.append(node.getName() + " ::= ");
|
---|
136 | }
|
---|
137 |
|
---|
138 | protected void visitAfter(Rule node) {
|
---|
139 | buf.append(";\n");
|
---|
140 | }
|
---|
141 |
|
---|
142 | protected void visitBefore(RuleCombinator node) {
|
---|
143 | }
|
---|
144 |
|
---|
145 | protected void visitAfter(RuleCombinator node) {
|
---|
146 | }
|
---|
147 |
|
---|
148 | protected void visitBefore(RuleReference node) {
|
---|
149 | }
|
---|
150 |
|
---|
151 | protected void visitAfter(RuleReference node) {
|
---|
152 | }
|
---|
153 |
|
---|
154 | protected void visitBefore(SectionHeading node) {
|
---|
155 | }
|
---|
156 |
|
---|
157 | protected void visitAfter(SectionHeading node) {
|
---|
158 | }
|
---|
159 |
|
---|
160 | protected void visitBefore(SingleDefinition node) {
|
---|
161 | }
|
---|
162 |
|
---|
163 | protected void visitAfter(SingleDefinition node) {
|
---|
164 | }
|
---|
165 |
|
---|
166 | protected void visitBefore(StringRule node) {
|
---|
167 | if (node.getLiteral() != null)
|
---|
168 | buf.append("\"" + node.getLiteral() + "\"");
|
---|
169 | else if (node.getColon() != null)
|
---|
170 | buf.append(" " + node.getColon());
|
---|
171 | }
|
---|
172 |
|
---|
173 | protected void visitAfter(StringRule node) {
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 |
|
---|
178 | }
|
---|