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