1 | package de.ugoe.cs.swe.bnftools.ui.views.syntaxdiagram;
|
---|
2 |
|
---|
3 | import org.eclipse.emf.ecore.EObject;
|
---|
4 | import org.eclipse.xtext.parsetree.NodeAdapter;
|
---|
5 |
|
---|
6 | import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList;
|
---|
7 | import de.ugoe.cs.swe.bnftools.ebnf.GroupedSequence;
|
---|
8 | import de.ugoe.cs.swe.bnftools.ebnf.OptionalSequence;
|
---|
9 | import de.ugoe.cs.swe.bnftools.ebnf.RepeatedSequence;
|
---|
10 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
11 | import de.ugoe.cs.swe.bnftools.ebnf.RuleReference;
|
---|
12 | import de.ugoe.cs.swe.bnftools.ebnf.SingleDefinition;
|
---|
13 |
|
---|
14 | public class SyntaxDiagramCreator {
|
---|
15 |
|
---|
16 | private NodeAdapter node;
|
---|
17 |
|
---|
18 | public SyntaxDiagramCreator(NodeAdapter node) {
|
---|
19 | this.node = node;
|
---|
20 | }
|
---|
21 |
|
---|
22 | public SyntaxDiagramFigure createSyntaxDiagram() {
|
---|
23 | if (node == null)
|
---|
24 | return new SyntaxDiagramFigure();
|
---|
25 |
|
---|
26 | EObject ruleRoot = (EObject) node.getParserNode().getElement();
|
---|
27 |
|
---|
28 | SyntaxDiagramFigure syntaxDiagramFigure = create(ruleRoot);
|
---|
29 |
|
---|
30 | return syntaxDiagramFigure;
|
---|
31 | }
|
---|
32 |
|
---|
33 | private SyntaxDiagramFigure create(EObject o) {
|
---|
34 | if (o instanceof Rule) {
|
---|
35 | SyntaxDiagramFigure f = create(o.eContents().get(0));
|
---|
36 | return f;
|
---|
37 | } else if (o instanceof DefinitionList) {
|
---|
38 | if (o.eContents().size() == 1) {
|
---|
39 | SyntaxDiagramFigure f = create(o.eContents().get(0));
|
---|
40 | return f;
|
---|
41 | } else {
|
---|
42 | AlternativeFigure af = new AlternativeFigure();
|
---|
43 | for (int i=0; i < o.eContents().size();i++) {
|
---|
44 | SyntaxDiagramFigure f = create(o.eContents().get(i));
|
---|
45 | if (f != null)
|
---|
46 | af.addSyntaxFigure(f);
|
---|
47 | }
|
---|
48 | return af;
|
---|
49 | }
|
---|
50 | } else if (o instanceof SingleDefinition) {
|
---|
51 | SyntaxDiagram outer = new SyntaxDiagram();
|
---|
52 | for (int i=0; i < o.eContents().size();i++) {
|
---|
53 | SyntaxDiagramFigure f = create(o.eContents().get(i));
|
---|
54 | if (f != null)
|
---|
55 | outer.addSyntaxFigure(f);
|
---|
56 | }
|
---|
57 | outer.finish();
|
---|
58 | return outer;
|
---|
59 | } else if (o instanceof GroupedSequence) {
|
---|
60 | SyntaxDiagram syntaxDiagram = new SyntaxDiagram();
|
---|
61 | for (int i=0; i < o.eContents().size();i++) {
|
---|
62 | SyntaxDiagramFigure f = create(o.eContents().get(i));
|
---|
63 | if (f != null)
|
---|
64 | syntaxDiagram.addSyntaxFigure(f);
|
---|
65 | }
|
---|
66 | return syntaxDiagram;
|
---|
67 | } else if (o instanceof OptionalSequence) {
|
---|
68 | SyntaxDiagram syntaxDiagram = new SyntaxDiagram();
|
---|
69 | for (int i=0; i < o.eContents().size();i++) {
|
---|
70 | SyntaxDiagramFigure f = create(o.eContents().get(i));
|
---|
71 | if (f != null)
|
---|
72 | syntaxDiagram.addSyntaxFigure(f);
|
---|
73 | }
|
---|
74 | OptionFigure af = new OptionFigure(syntaxDiagram);
|
---|
75 | syntaxDiagram.finish();
|
---|
76 | return af;
|
---|
77 | } else if (o instanceof RepeatedSequence) {
|
---|
78 | RepeatedSequence e = (RepeatedSequence) o;
|
---|
79 | SyntaxDiagram syntaxDiagram = new SyntaxDiagram();
|
---|
80 | for (int i=0; i < o.eContents().size();i++) {
|
---|
81 | SyntaxDiagramFigure f = create(o.eContents().get(i));
|
---|
82 | if (f != null)
|
---|
83 | syntaxDiagram.addSyntaxFigure(f);
|
---|
84 | }
|
---|
85 | if (e.isMorethanonce()) {
|
---|
86 | RepetitionFigure rf = new RepetitionFigure(syntaxDiagram);
|
---|
87 | syntaxDiagram.finish();
|
---|
88 | return rf;
|
---|
89 | } else {
|
---|
90 | OptionRepetitionFigure rf = new OptionRepetitionFigure(syntaxDiagram);
|
---|
91 | syntaxDiagram.finish();
|
---|
92 | return rf;
|
---|
93 | }
|
---|
94 | } else if (o instanceof de.ugoe.cs.swe.bnftools.ebnf.StringRule) {
|
---|
95 | de.ugoe.cs.swe.bnftools.ebnf.StringRule e = (de.ugoe.cs.swe.bnftools.ebnf.StringRule) o;
|
---|
96 | if (e.getColon() != null)
|
---|
97 | return new TerminalFigure(e.getColon());
|
---|
98 | else
|
---|
99 | return new TerminalFigure(e.getLiteral());
|
---|
100 |
|
---|
101 | } else if (o instanceof RuleReference) {
|
---|
102 | RuleReference e = (RuleReference) o;
|
---|
103 | return new NonTerminalFigure(e.getRuleref().getName());
|
---|
104 | } else {
|
---|
105 | return create(o.eContents().get(0));
|
---|
106 | }
|
---|
107 |
|
---|
108 | // return null;
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|