[61] | 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 | private final static String ruleReferencedOneDescription = "The rule is only referenced by one other rule";
|
---|
| 29 |
|
---|
| 30 | private final static String passthroughRuleDescription = "The rule is a passthrough rule";
|
---|
| 31 |
|
---|
| 32 | private final static String unreferencedPassthroughRuleDescription = "The rule is an unreferenced passthrough rule";
|
---|
| 33 |
|
---|
| 34 | private final static String unusedRuleDescription = "The rule is not referenced anywhere";
|
---|
| 35 |
|
---|
| 36 | private final static String equalAlternativeDescription = "The rule contains equal alternatives";
|
---|
| 37 |
|
---|
| 38 | private final static String duplicateRulesDescription = "The rule is a duplicate";
|
---|
| 39 |
|
---|
| 40 | private 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 | this.warning(EbnfValidator.ruleReferencedOneDescription, EbnfPackage.Literals.RULE__NAME);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * Checks if a a rule has the same definition as another rule e.g.:
|
---|
| 71 | * a ::= "test"
|
---|
| 72 | * b ::= "test"
|
---|
| 73 | * (Problem: does not check if there is a permutation)
|
---|
| 74 | */
|
---|
| 75 | @Check
|
---|
| 76 | public void checkDuplicateRules(final Rule rule) {
|
---|
| 77 | EObject _eContainer = rule.eContainer();
|
---|
| 78 | EObject _eContainer_1 = _eContainer.eContainer();
|
---|
| 79 | EtsiBnf etsiBnf = ((EtsiBnf) _eContainer_1);
|
---|
| 80 | DefinitionList _definitionList = rule.getDefinitionList();
|
---|
| 81 | ICompositeNode definitionList = NodeModelUtils.findActualNodeFor(_definitionList);
|
---|
| 82 | String _text = definitionList.getText();
|
---|
| 83 | String _trim = _text.trim();
|
---|
| 84 | String rightHandSideText = _trim.replaceAll("[ \t\n\r]", "");
|
---|
| 85 | List<Rule> allRules = EbnfAnalysisUtils.getAllRules(etsiBnf);
|
---|
| 86 | for (final Rule currentRule : allRules) {
|
---|
| 87 | boolean _notEquals = (!Objects.equal(currentRule, rule));
|
---|
| 88 | if (_notEquals) {
|
---|
| 89 | DefinitionList _definitionList_1 = currentRule.getDefinitionList();
|
---|
| 90 | ICompositeNode currentRuleDefinitionList = NodeModelUtils.findActualNodeFor(_definitionList_1);
|
---|
| 91 | String _text_1 = currentRuleDefinitionList.getText();
|
---|
| 92 | String _trim_1 = _text_1.trim();
|
---|
| 93 | String currentRuleRightHandSideText = _trim_1.replaceAll("[ \t\n\r]",
|
---|
| 94 | "");
|
---|
| 95 | boolean _equals = currentRuleRightHandSideText.equals(rightHandSideText);
|
---|
| 96 | if (_equals) {
|
---|
| 97 | String _name = currentRule.getName();
|
---|
| 98 | String _plus = ((EbnfValidator.duplicateRulesDescription + " with rule \"") + _name);
|
---|
| 99 | String _plus_1 = (_plus +
|
---|
| 100 | "\" (Line ");
|
---|
| 101 | ICompositeNode _findActualNodeFor = NodeModelUtils.findActualNodeFor(currentRule);
|
---|
| 102 | int _startLine = _findActualNodeFor.getStartLine();
|
---|
| 103 | String _plus_2 = (_plus_1 + Integer.valueOf(_startLine));
|
---|
| 104 | String description = (_plus_2 + ")");
|
---|
| 105 | this.warning(description, EbnfPackage.Literals.RULE__NAME);
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * Checks if a Rule got the same Name as another Rule, e.g.:
|
---|
| 113 | * a ::= "foo"
|
---|
| 114 | * a ::= "bar"
|
---|
| 115 | */
|
---|
| 116 | @Check
|
---|
| 117 | public void checkNameIsUnique(final Rule rule) {
|
---|
| 118 | EObject _eContainer = rule.eContainer();
|
---|
| 119 | EObject _eContainer_1 = _eContainer.eContainer();
|
---|
| 120 | final EtsiBnf bnf = ((EtsiBnf) _eContainer_1);
|
---|
| 121 | List<Rule> _allRules = EbnfAnalysisUtils.getAllRules(bnf);
|
---|
| 122 | for (final Rule r : _allRules) {
|
---|
| 123 | String _name = rule.getName();
|
---|
| 124 | String _name_1 = r.getName();
|
---|
| 125 | boolean _equals = _name.equals(_name_1);
|
---|
| 126 | if (_equals) {
|
---|
| 127 | boolean _equals_1 = r.equals(rule);
|
---|
| 128 | boolean _not = (!_equals_1);
|
---|
| 129 | if (_not) {
|
---|
| 130 | ICompositeNode _findActualNodeFor = NodeModelUtils.findActualNodeFor(r);
|
---|
| 131 | int _startLine = _findActualNodeFor.getStartLine();
|
---|
| 132 | String _plus = (EbnfValidator.nonUniqueNameDescription + Integer.valueOf(_startLine));
|
---|
| 133 | this.error(_plus,
|
---|
| 134 | EbnfPackage.Literals.RULE__NAME);
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /**
|
---|
| 141 | * Checks if a Rule, except for the #1 is not referenced, e.g.:
|
---|
| 142 | * a::= b
|
---|
| 143 | * b::="foo"
|
---|
| 144 | * c ::= "bar"
|
---|
| 145 | */
|
---|
| 146 | @Check
|
---|
| 147 | public void checkUnusedRule(final Rule rule) {
|
---|
| 148 | List<RuleReference> references = EbnfAnalysisUtils.findReferences(rule);
|
---|
| 149 | boolean _and = false;
|
---|
| 150 | int _size = references.size();
|
---|
| 151 | boolean _equals = (_size == 0);
|
---|
| 152 | if (!_equals) {
|
---|
| 153 | _and = false;
|
---|
| 154 | } else {
|
---|
| 155 | int _rulenumber = rule.getRulenumber();
|
---|
| 156 | boolean _notEquals = (_rulenumber != 1);
|
---|
| 157 | _and = _notEquals;
|
---|
| 158 | }
|
---|
| 159 | if (_and) {
|
---|
| 160 | this.warning(EbnfValidator.unusedRuleDescription, EbnfPackage.Literals.RULE__NAME);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /**
|
---|
| 165 | * Checks if a rule got two equal alternatives, e.g.:
|
---|
| 166 | * a ::= b | "foo" | b
|
---|
| 167 | * (Problem:ignores whitespaces in literals)
|
---|
| 168 | */
|
---|
| 169 | @Check
|
---|
| 170 | public void checkEqualAlternative(final Rule rule) {
|
---|
| 171 | DefinitionList definitionList = rule.getDefinitionList();
|
---|
| 172 | List<SingleDefinition> singleDefinitions = definitionList.getSingleDefinition();
|
---|
| 173 | for (final SingleDefinition sDef1 : singleDefinitions) {
|
---|
| 174 | for (final SingleDefinition sDef2 : singleDefinitions) {
|
---|
| 175 | boolean _equals = sDef1.equals(sDef2);
|
---|
| 176 | boolean _not = (!_equals);
|
---|
| 177 | if (_not) {
|
---|
| 178 | ICompositeNode _findActualNodeFor = NodeModelUtils.findActualNodeFor(sDef1);
|
---|
| 179 | String _text = _findActualNodeFor.getText();
|
---|
| 180 | String _trim = _text.trim();
|
---|
| 181 | String d1 = _trim.replaceAll("[ \t\n\r]", "");
|
---|
| 182 | ICompositeNode _findActualNodeFor_1 = NodeModelUtils.findActualNodeFor(sDef2);
|
---|
| 183 | String _text_1 = _findActualNodeFor_1.getText();
|
---|
| 184 | String _trim_1 = _text_1.trim();
|
---|
| 185 | String d2 = _trim_1.replaceAll("[ \t\n\r]", "");
|
---|
| 186 | boolean _equals_1 = d1.equals(d2);
|
---|
| 187 | if (_equals_1) {
|
---|
| 188 | this.warning(EbnfValidator.equalAlternativeDescription, EbnfPackage.Literals.RULE__NAME, EbnfValidator.equalAlternativeDescription);
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | /**
|
---|
| 196 | * Checks if a rule gets just passed through, e.g.:
|
---|
| 197 | * a ::= b | "literal"
|
---|
| 198 | * b ::= c
|
---|
| 199 | * c ::= "foo.bar"
|
---|
| 200 | */
|
---|
| 201 | @Check
|
---|
| 202 | public void checkPassthroughRule(final Rule rule) {
|
---|
| 203 | List<RuleReference> references = EbnfAnalysisUtils.findReferences(rule);
|
---|
| 204 | boolean _isPassthroughRule = EbnfAnalysisUtils.isPassthroughRule(rule);
|
---|
| 205 | if (_isPassthroughRule) {
|
---|
| 206 | int _size = references.size();
|
---|
| 207 | boolean _equals = (_size == 0);
|
---|
| 208 | if (_equals) {
|
---|
| 209 | this.warning(EbnfValidator.unreferencedPassthroughRuleDescription, EbnfPackage.Literals.RULE__NAME);
|
---|
| 210 | } else {
|
---|
| 211 | this.warning(EbnfValidator.passthroughRuleDescription, EbnfPackage.Literals.RULE__NAME, EbnfValidator.passthroughRuleDescription);
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 | }
|
---|