1 | /**
|
---|
2 | * generated by Xtext
|
---|
3 | */
|
---|
4 | package de.ugoe.cs.swe.bnftools.validation;
|
---|
5 |
|
---|
6 | import com.google.common.base.Objects;
|
---|
7 | import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList;
|
---|
8 | import de.ugoe.cs.swe.bnftools.ebnf.EbnfPackage;
|
---|
9 | import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf;
|
---|
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 | import de.ugoe.cs.swe.bnftools.validation.AbstractEbnfValidator;
|
---|
14 | import de.ugoe.cs.swe.bnftools.validation.EbnfAnalysisUtils;
|
---|
15 | import java.util.List;
|
---|
16 | import org.eclipse.emf.ecore.EObject;
|
---|
17 | import org.eclipse.xtext.nodemodel.ICompositeNode;
|
---|
18 | import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
|
---|
19 | import org.eclipse.xtext.validation.Check;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Custom validation rules.
|
---|
23 | *
|
---|
24 | * see http://www.eclipse.org/Xtext/documentation.html#validation
|
---|
25 | */
|
---|
26 | @SuppressWarnings("all")
|
---|
27 | public class EbnfValidator extends AbstractEbnfValidator {
|
---|
28 | public final static String ruleReferencedOneDescription = "The rule is only referenced by one other rule";
|
---|
29 |
|
---|
30 | public final static String passthroughRuleDescription = "The rule is a passthrough rule";
|
---|
31 |
|
---|
32 | public final static String unreferencedPassthroughRuleDescription = "The rule is an unreferenced passthrough rule";
|
---|
33 |
|
---|
34 | public final static String unusedRuleDescription = "The rule is not referenced anywhere";
|
---|
35 |
|
---|
36 | public final static String equalAlternativeDescription = "The rule contains equal alternatives";
|
---|
37 |
|
---|
38 | public final static String duplicateRulesDescription = "The rule is a duplicate";
|
---|
39 |
|
---|
40 | public final static String nonUniqueNameDescription = "The rule has the same Name as the Rule in Line ";
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Checks if a rule is only referenced by one other Rule, e.g.:
|
---|
44 | * a ::= b
|
---|
45 | * b ::= "foo"
|
---|
46 | */
|
---|
47 | @Check
|
---|
48 | public void checkReferencedOnlyOnce(final Rule rule) {
|
---|
49 | boolean _isTokenRule = EbnfAnalysisUtils.isTokenRule(rule);
|
---|
50 | if (_isTokenRule) {
|
---|
51 | return;
|
---|
52 | }
|
---|
53 | List<RuleReference> references = EbnfAnalysisUtils.findReferences(rule);
|
---|
54 | boolean _and = false;
|
---|
55 | int _size = references.size();
|
---|
56 | boolean _equals = (_size == 1);
|
---|
57 | if (!_equals) {
|
---|
58 | _and = false;
|
---|
59 | } else {
|
---|
60 | int _rulenumber = rule.getRulenumber();
|
---|
61 | boolean _notEquals = (_rulenumber != 1);
|
---|
62 | _and = _notEquals;
|
---|
63 | }
|
---|
64 | if (_and) {
|
---|
65 | String _name = rule.getName();
|
---|
66 | this.warning(EbnfValidator.ruleReferencedOneDescription, EbnfPackage.Literals.RULE__NAME, EbnfValidator.ruleReferencedOneDescription, _name);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Checks if a a rule has the same definition as another rule e.g.:
|
---|
72 | * a ::= "test"
|
---|
73 | * b ::= "test"
|
---|
74 | * (Problem: does not check if there is a permutation)
|
---|
75 | */
|
---|
76 | @Check
|
---|
77 | public void checkDuplicateRules(final Rule rule) {
|
---|
78 | EObject _eContainer = rule.eContainer();
|
---|
79 | EObject _eContainer_1 = _eContainer.eContainer();
|
---|
80 | EtsiBnf etsiBnf = ((EtsiBnf) _eContainer_1);
|
---|
81 | DefinitionList _definitionList = rule.getDefinitionList();
|
---|
82 | ICompositeNode definitionList = NodeModelUtils.findActualNodeFor(_definitionList);
|
---|
83 | String _text = definitionList.getText();
|
---|
84 | String _trim = _text.trim();
|
---|
85 | String rightHandSideText = _trim.replaceAll("[ \t\n\r]", "");
|
---|
86 | List<Rule> allRules = EbnfAnalysisUtils.getAllRules(etsiBnf);
|
---|
87 | for (final Rule currentRule : allRules) {
|
---|
88 | boolean _notEquals = (!Objects.equal(currentRule, rule));
|
---|
89 | if (_notEquals) {
|
---|
90 | DefinitionList _definitionList_1 = currentRule.getDefinitionList();
|
---|
91 | ICompositeNode currentRuleDefinitionList = NodeModelUtils.findActualNodeFor(_definitionList_1);
|
---|
92 | String _text_1 = currentRuleDefinitionList.getText();
|
---|
93 | String _trim_1 = _text_1.trim();
|
---|
94 | String currentRuleRightHandSideText = _trim_1.replaceAll("[ \t\n\r]",
|
---|
95 | "");
|
---|
96 | boolean _equals = currentRuleRightHandSideText.equals(rightHandSideText);
|
---|
97 | if (_equals) {
|
---|
98 | String _name = currentRule.getName();
|
---|
99 | String _plus = ((EbnfValidator.duplicateRulesDescription + " with rule \"") + _name);
|
---|
100 | String _plus_1 = (_plus +
|
---|
101 | "\" (Line ");
|
---|
102 | ICompositeNode _findActualNodeFor = NodeModelUtils.findActualNodeFor(currentRule);
|
---|
103 | int _startLine = _findActualNodeFor.getStartLine();
|
---|
104 | String _plus_2 = (_plus_1 + Integer.valueOf(_startLine));
|
---|
105 | String description = (_plus_2 + ")");
|
---|
106 | this.warning(description, EbnfPackage.Literals.RULE__NAME);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Checks if a Rule got the same Name as another Rule, e.g.:
|
---|
114 | * a ::= "foo"
|
---|
115 | * a ::= "bar"
|
---|
116 | */
|
---|
117 | @Check
|
---|
118 | public void checkNameIsUnique(final Rule rule) {
|
---|
119 | EObject _eContainer = rule.eContainer();
|
---|
120 | EObject _eContainer_1 = _eContainer.eContainer();
|
---|
121 | final EtsiBnf bnf = ((EtsiBnf) _eContainer_1);
|
---|
122 | List<Rule> _allRules = EbnfAnalysisUtils.getAllRules(bnf);
|
---|
123 | for (final Rule r : _allRules) {
|
---|
124 | String _name = rule.getName();
|
---|
125 | String _name_1 = r.getName();
|
---|
126 | boolean _equals = _name.equals(_name_1);
|
---|
127 | if (_equals) {
|
---|
128 | boolean _equals_1 = r.equals(rule);
|
---|
129 | boolean _not = (!_equals_1);
|
---|
130 | if (_not) {
|
---|
131 | ICompositeNode _findActualNodeFor = NodeModelUtils.findActualNodeFor(r);
|
---|
132 | int _startLine = _findActualNodeFor.getStartLine();
|
---|
133 | String _plus = (EbnfValidator.nonUniqueNameDescription + Integer.valueOf(_startLine));
|
---|
134 | this.error(_plus,
|
---|
135 | EbnfPackage.Literals.RULE__NAME);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Checks if a Rule, except for the #1 is not referenced, e.g.:
|
---|
143 | * a::= b
|
---|
144 | * b::="foo"
|
---|
145 | * c ::= "bar"
|
---|
146 | */
|
---|
147 | @Check
|
---|
148 | public void checkUnusedRule(final Rule rule) {
|
---|
149 | List<RuleReference> references = EbnfAnalysisUtils.findReferences(rule);
|
---|
150 | boolean _and = false;
|
---|
151 | int _size = references.size();
|
---|
152 | boolean _equals = (_size == 0);
|
---|
153 | if (!_equals) {
|
---|
154 | _and = false;
|
---|
155 | } else {
|
---|
156 | int _rulenumber = rule.getRulenumber();
|
---|
157 | boolean _notEquals = (_rulenumber != 1);
|
---|
158 | _and = _notEquals;
|
---|
159 | }
|
---|
160 | if (_and) {
|
---|
161 | String _name = rule.getName();
|
---|
162 | this.warning(EbnfValidator.unusedRuleDescription, EbnfPackage.Literals.RULE__NAME, EbnfValidator.unusedRuleDescription, _name);
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Checks if a rule got two equal alternatives, e.g.:
|
---|
168 | * a ::= b | "foo" | b
|
---|
169 | * (Problem:ignores whitespaces in literals)
|
---|
170 | */
|
---|
171 | @Check
|
---|
172 | public void checkEqualAlternative(final Rule rule) {
|
---|
173 | DefinitionList definitionList = rule.getDefinitionList();
|
---|
174 | List<SingleDefinition> singleDefinitions = definitionList.getSingleDefinition();
|
---|
175 | for (final SingleDefinition sDef1 : singleDefinitions) {
|
---|
176 | for (final SingleDefinition sDef2 : singleDefinitions) {
|
---|
177 | boolean _equals = sDef1.equals(sDef2);
|
---|
178 | boolean _not = (!_equals);
|
---|
179 | if (_not) {
|
---|
180 | ICompositeNode _findActualNodeFor = NodeModelUtils.findActualNodeFor(sDef1);
|
---|
181 | String _text = _findActualNodeFor.getText();
|
---|
182 | String _trim = _text.trim();
|
---|
183 | String d1 = _trim.replaceAll("[ \t\n\r]", "");
|
---|
184 | ICompositeNode _findActualNodeFor_1 = NodeModelUtils.findActualNodeFor(sDef2);
|
---|
185 | String _text_1 = _findActualNodeFor_1.getText();
|
---|
186 | String _trim_1 = _text_1.trim();
|
---|
187 | String d2 = _trim_1.replaceAll("[ \t\n\r]", "");
|
---|
188 | boolean _equals_1 = d1.equals(d2);
|
---|
189 | if (_equals_1) {
|
---|
190 | this.warning(EbnfValidator.equalAlternativeDescription, EbnfPackage.Literals.RULE__NAME, EbnfValidator.equalAlternativeDescription);
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Checks if a rule gets just passed through, e.g.:
|
---|
199 | * a ::= b | "literal"
|
---|
200 | * b ::= c
|
---|
201 | * c ::= "foo.bar"
|
---|
202 | */
|
---|
203 | @Check
|
---|
204 | public void checkPassthroughRule(final Rule rule) {
|
---|
205 | List<RuleReference> references = EbnfAnalysisUtils.findReferences(rule);
|
---|
206 | boolean _isPassthroughRule = EbnfAnalysisUtils.isPassthroughRule(rule);
|
---|
207 | if (_isPassthroughRule) {
|
---|
208 | int _size = references.size();
|
---|
209 | boolean _equals = (_size == 0);
|
---|
210 | if (_equals) {
|
---|
211 | this.warning(EbnfValidator.unreferencedPassthroughRuleDescription, EbnfPackage.Literals.RULE__NAME);
|
---|
212 | } else {
|
---|
213 | this.warning(EbnfValidator.passthroughRuleDescription, EbnfPackage.Literals.RULE__NAME, EbnfValidator.passthroughRuleDescription);
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|