1 | package de.ugoe.cs.swe.bnftools.analysis;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.List;
|
---|
5 |
|
---|
6 | import org.eclipse.xtext.resource.IReferenceDescription;
|
---|
7 | import org.eclipse.xtext.resource.IResourceDescription;
|
---|
8 | import org.eclipse.xtext.resource.IResourceDescriptions;
|
---|
9 |
|
---|
10 | import de.ugoe.cs.swe.bnftools.ebnf.Atom;
|
---|
11 | import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList;
|
---|
12 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
13 | import de.ugoe.cs.swe.bnftools.ebnf.RuleReference;
|
---|
14 | import de.ugoe.cs.swe.bnftools.ebnf.SingleDefinition;
|
---|
15 | import de.ugoe.cs.swe.bnftools.ebnf.StringRule;
|
---|
16 | import de.ugoe.cs.swe.bnftools.ebnf.Term;
|
---|
17 |
|
---|
18 | public class EbnfAnalysisUtils {
|
---|
19 |
|
---|
20 | //----------------------------------------------------------------------------------------------------
|
---|
21 |
|
---|
22 | static public boolean isPassthroughRule(Rule rule) {
|
---|
23 | DefinitionList definitionList = rule.getDefinitionList();
|
---|
24 | if (definitionList == null)
|
---|
25 | return false;
|
---|
26 |
|
---|
27 | if (rule.getDefinitionList().getSingleDefinition().size() == 1) {
|
---|
28 | SingleDefinition element = rule.getDefinitionList().getSingleDefinition().get(0);
|
---|
29 | if (element.getTerms().size() == 1) {
|
---|
30 | Term first = element.getTerms().get(0);
|
---|
31 | if (first.getTermAtom() != null) {
|
---|
32 | Atom atom = (Atom) first.getTermAtom();
|
---|
33 | if (atom.getAtomRuleReference() != null)
|
---|
34 | return true;
|
---|
35 | }
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | return false;
|
---|
40 | }
|
---|
41 |
|
---|
42 | //----------------------------------------------------------------------------------------------------
|
---|
43 |
|
---|
44 | static public RuleReference getPassthroughRuleReference(Rule rule) {
|
---|
45 | DefinitionList definitionList = rule.getDefinitionList();
|
---|
46 | if (definitionList == null)
|
---|
47 | return null;
|
---|
48 |
|
---|
49 | if (rule.getDefinitionList().getSingleDefinition().size() == 1) {
|
---|
50 | SingleDefinition element = rule.getDefinitionList().getSingleDefinition().get(0);
|
---|
51 | if (element.getTerms().size() == 1) {
|
---|
52 | Term first = element.getTerms().get(0);
|
---|
53 | if (first.getTermAtom() != null) {
|
---|
54 | Atom atom = (Atom) first.getTermAtom();
|
---|
55 | if (atom.getAtomRuleReference() != null)
|
---|
56 | return atom.getAtomRuleReference();
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | return null;
|
---|
62 | }
|
---|
63 |
|
---|
64 | //----------------------------------------------------------------------------------------------------
|
---|
65 |
|
---|
66 | static public boolean isTokenRule(Rule rule) {
|
---|
67 | DefinitionList definitionList = rule.getDefinitionList();
|
---|
68 | if (definitionList == null)
|
---|
69 | return false;
|
---|
70 |
|
---|
71 | if (rule.getDefinitionList().getSingleDefinition().size() == 1) {
|
---|
72 | SingleDefinition element = rule.getDefinitionList().getSingleDefinition().get(0);
|
---|
73 | if (element.getTerms().size() == 1) {
|
---|
74 | Term first = element.getTerms().get(0);
|
---|
75 | Atom atom = first.getTermAtom();
|
---|
76 | if (atom != null && atom.getAtomStringRule() != null) {
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | return false;
|
---|
83 | }
|
---|
84 |
|
---|
85 | //----------------------------------------------------------------------------------------------------
|
---|
86 |
|
---|
87 | static public StringRule getTokenRuleTerminal(Rule rule) {
|
---|
88 | DefinitionList definitionList = rule.getDefinitionList();
|
---|
89 | if (definitionList == null)
|
---|
90 | return null;
|
---|
91 |
|
---|
92 | if (rule.getDefinitionList().getSingleDefinition().size() == 1) {
|
---|
93 | SingleDefinition element = rule.getDefinitionList().getSingleDefinition().get(0);
|
---|
94 | if (element.getTerms().size() == 1) {
|
---|
95 | Term first = element.getTerms().get(0);
|
---|
96 | if ((first.getTermAtom() != null) && (first.getTermAtom().getAtomStringRule() != null)) {
|
---|
97 | StringRule stringRule = first.getTermAtom().getAtomStringRule();
|
---|
98 | return stringRule;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | return null;
|
---|
104 | }
|
---|
105 |
|
---|
106 | //----------------------------------------------------------------------------------------------------
|
---|
107 |
|
---|
108 | static public List<Rule> findReferences(Rule rule, IResourceDescriptions resourceDescriptions) {
|
---|
109 | List<Rule> ruleReferences = new ArrayList<Rule>();
|
---|
110 |
|
---|
111 | final String ruleURIF = rule.eResource().getURIFragment(rule);
|
---|
112 |
|
---|
113 | for (IResourceDescription resourceDescription : resourceDescriptions
|
---|
114 | .getAllResourceDescriptions()) {
|
---|
115 | if (!resourceDescription.getURI().equals(rule.eResource().getURI()))
|
---|
116 | continue;
|
---|
117 |
|
---|
118 | for (IReferenceDescription referenceDescription : resourceDescription
|
---|
119 | .getReferenceDescriptions()) {
|
---|
120 | if (ruleURIF.equals(referenceDescription.getTargetEObjectUri()
|
---|
121 | .fragment())) {
|
---|
122 | ruleReferences.add((Rule) rule.eResource().getEObject(referenceDescription.getTargetEObjectUri().fragment()));
|
---|
123 | }
|
---|
124 |
|
---|
125 | }
|
---|
126 | }
|
---|
127 | return ruleReferences;
|
---|
128 | }
|
---|
129 |
|
---|
130 | }
|
---|