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