[5] | 1 | package de.ugoe.cs.swe.bnftools.validation; |
---|
| 2 | |
---|
| 3 | import java.util.ArrayList; |
---|
[50] | 4 | import java.util.HashMap; |
---|
| 5 | import java.util.HashSet; |
---|
| 6 | import java.util.Iterator; |
---|
[5] | 7 | import java.util.List; |
---|
[50] | 8 | import java.util.Set; |
---|
[49] | 9 | import java.util.Stack; |
---|
[5] | 10 | |
---|
| 11 | import org.eclipse.emf.common.util.EList; |
---|
| 12 | import org.eclipse.emf.common.util.URI; |
---|
[49] | 13 | import org.eclipse.emf.ecore.EObject; |
---|
[5] | 14 | import org.eclipse.emf.ecore.resource.Resource; |
---|
| 15 | import org.eclipse.xtext.parsetree.AbstractNode; |
---|
| 16 | import org.eclipse.xtext.parsetree.CompositeNode; |
---|
| 17 | import org.eclipse.xtext.parsetree.LeafNode; |
---|
| 18 | import org.eclipse.xtext.parsetree.NodeUtil; |
---|
| 19 | import org.eclipse.xtext.resource.IResourceDescriptions; |
---|
| 20 | import org.eclipse.xtext.resource.XtextResource; |
---|
| 21 | import org.eclipse.xtext.resource.XtextResourceSet; |
---|
| 22 | import org.eclipse.xtext.validation.Check; |
---|
| 23 | import org.eclipse.xtext.validation.CheckType; |
---|
| 24 | |
---|
| 25 | import com.google.inject.Inject; |
---|
| 26 | |
---|
| 27 | import de.ugoe.cs.swe.bnftools.analysis.EbnfAnalysisUtils; |
---|
[19] | 28 | import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry; |
---|
[5] | 29 | import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList; |
---|
| 30 | import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf; |
---|
| 31 | import de.ugoe.cs.swe.bnftools.ebnf.ExtRule; |
---|
| 32 | import de.ugoe.cs.swe.bnftools.ebnf.Import; |
---|
| 33 | import de.ugoe.cs.swe.bnftools.ebnf.Rule; |
---|
| 34 | import de.ugoe.cs.swe.bnftools.ebnf.SingleDefinition; |
---|
| 35 | |
---|
| 36 | public class EbnfJavaValidator extends AbstractEbnfJavaValidator { |
---|
| 37 | |
---|
| 38 | @Inject |
---|
| 39 | IResourceDescriptions resourceDescriptions; |
---|
| 40 | |
---|
| 41 | public static final String ruleReferencedOneDescription = "The rule is only referenced by one other rule"; |
---|
| 42 | public static final String passthroughRuleDescription = "The rule is a passthrough rule"; |
---|
| 43 | public static final String unreferencedPassthroughRuleDescription = "The rule is an unreferenced passthrough rule"; |
---|
| 44 | public static final String unusedRuleDescription = "The rule is not referenced anywhere"; |
---|
| 45 | public static final String equalAlternativeDescription = "The rule contains equal alternatives"; |
---|
| 46 | public static final String duplicateRulesDescription = "The rule is a duplicate"; |
---|
| 47 | public static final String duplicateSubRulesDescription = "A part of rule is a duplicate"; |
---|
| 48 | public static final String packageConsistencyCheckRuleMissingDescription = "Rule has been removed or renamed in the updated grammar. The package rule is therefore now an additional rule instead of a replacement rule. The package rule might therefore be inconsistent with the updated core grammar."; |
---|
| 49 | public static final String packageConsistencyCheckRuleDifferentDescription = "Corresponding core grammar rule has been changed in updated grammar. The package rule might have become inconsistent with the updated core grammar."; |
---|
| 50 | public static final String packageConsistencyCheckCorrespondingRuleMissingDescription = "Corresponding rule is missing in core grammar. Rule has been manually altered in the delta grammar (delta grammar is outdated) or the /core and /update flags are somehow switched."; |
---|
| 51 | |
---|
| 52 | public static boolean checkReferencedOnlyOnce = false; |
---|
| 53 | public static boolean checkPassthroughRule = false; |
---|
| 54 | public static boolean checkUnusedRule = false; |
---|
| 55 | public static boolean checkEqualAlternative = false; |
---|
| 56 | public static boolean checkDuplicateRules = false; |
---|
| 57 | public static boolean checkSubruleDuplicates = false; |
---|
| 58 | public static boolean checkUpdatedGrammarConsistency = false; |
---|
| 59 | |
---|
| 60 | // ---------------------------------------------------------------------------------------------------- |
---|
| 61 | |
---|
| 62 | @Check(CheckType.EXPENSIVE) |
---|
| 63 | public void checkUpdateGrammarConsistency(EtsiBnf bnf) { |
---|
| 64 | if (!checkUpdatedGrammarConsistency) |
---|
| 65 | return; |
---|
| 66 | |
---|
| 67 | CompositeNode compNode = NodeUtil.getRootNode(bnf); |
---|
| 68 | XtextResourceSet set = new XtextResourceSet(); |
---|
| 69 | URI uri = compNode.getElement().eResource().getURI(); |
---|
| 70 | Iterable<AbstractNode> allNodes = NodeUtil.getAllContents(compNode); |
---|
| 71 | if(!bnf.getType().equals("/delta")) |
---|
| 72 | return; |
---|
| 73 | |
---|
| 74 | Resource coreRes = null; |
---|
| 75 | Resource updatedRes = null; |
---|
[17] | 76 | EList<Import> imports = bnf.getImportSection().getImports(); |
---|
[5] | 77 | |
---|
| 78 | for(int j=0; j<imports.size(); j++) { |
---|
| 79 | if(imports.get(j).getGrammarType().equals("core")) { |
---|
| 80 | String packageUri = uri.trimSegments(1).toPlatformString(true); |
---|
| 81 | String fullUri = packageUri + "/" +imports.get(j).getImportURI(); |
---|
| 82 | coreRes= set.getResource( URI.createPlatformResourceURI(fullUri, true), true); |
---|
| 83 | } |
---|
| 84 | if(imports.get(j).getGrammarType().equals("update")) { |
---|
| 85 | String packageUri = uri.trimSegments(1).toPlatformString(true); |
---|
| 86 | String fullUri = packageUri + "/" +imports.get(j).getImportURI(); |
---|
| 87 | updatedRes= set.getResource( URI.createPlatformResourceURI(fullUri, true), true); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | if( (coreRes==null) || (updatedRes==null)) |
---|
| 92 | return; |
---|
| 93 | |
---|
| 94 | XtextResource coreResource = null; |
---|
| 95 | XtextResource updatedResource = null; |
---|
| 96 | |
---|
| 97 | if(coreRes instanceof XtextResource) |
---|
| 98 | coreResource = (XtextResource) coreRes; |
---|
| 99 | if(updatedRes instanceof XtextResource) |
---|
| 100 | updatedResource = (XtextResource) updatedRes; |
---|
| 101 | |
---|
| 102 | /* |
---|
| 103 | * the idea: get the core grammar and the updated grammar |
---|
| 104 | * for each extension rule in the delta grammar, |
---|
| 105 | * find the corresponding rule in the updated grammar and the core grammar |
---|
| 106 | * if it doesn't exist in the updated grammar write the inconsistency |
---|
| 107 | * if it exists, compare to the rule in the core grammar |
---|
| 108 | */ |
---|
| 109 | for(AbstractNode node: allNodes) { |
---|
| 110 | if(node.getElement() instanceof ExtRule) { |
---|
| 111 | checkForConsistency(node, coreResource, updatedResource, compNode); |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | private boolean checkForConsistency(AbstractNode node, XtextResource coreResource, XtextResource updatedResource, CompositeNode compNode) { |
---|
| 117 | //updated grammar: |
---|
| 118 | AbstractNode updatedRule = null; |
---|
| 119 | AbstractNode coreRule = null; |
---|
| 120 | Rule currentRule = null; |
---|
| 121 | for(AbstractNode uNode : NodeUtil.getAllContents(updatedResource.getParseResult().getRootNode())) { |
---|
| 122 | if (uNode.getElement() instanceof Rule) { |
---|
| 123 | currentRule = (Rule) uNode.getElement(); |
---|
| 124 | if (currentRule.getName().equals(((ExtRule) node.getElement()).getName())) { |
---|
| 125 | updatedRule = uNode; |
---|
| 126 | break; |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | if(updatedRule == null) { |
---|
| 132 | warning(EbnfJavaValidator.packageConsistencyCheckRuleMissingDescription, node.getElement(), 1); |
---|
| 133 | return false; |
---|
| 134 | } else { //core grammar |
---|
| 135 | for(AbstractNode cNode : NodeUtil.getAllContents(coreResource.getParseResult().getRootNode())) { |
---|
| 136 | if(cNode.getElement() instanceof Rule) |
---|
| 137 | if(((Rule) cNode.getElement()).getName().equals(((ExtRule) node.getElement()).getName())) { |
---|
| 138 | coreRule = cNode; |
---|
| 139 | break; |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | if(coreRule == null){ |
---|
| 144 | warning(EbnfJavaValidator.packageConsistencyCheckCorrespondingRuleMissingDescription, node.getElement(), 1); |
---|
| 145 | return false; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | if (compareRules(coreRule, updatedRule)) { |
---|
| 149 | return true; |
---|
| 150 | } else { |
---|
| 151 | warning(EbnfJavaValidator.packageConsistencyCheckRuleDifferentDescription, node.getElement(), 1); |
---|
| 152 | return false; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | // returns true, if the rules are equal |
---|
| 159 | private boolean compareRules(AbstractNode cNode, AbstractNode uNode) { |
---|
| 160 | EList<LeafNode> cLeaves = removeWS(cNode.getLeafNodes()); |
---|
| 161 | EList<LeafNode> uLeaves = removeWS(uNode.getLeafNodes()); |
---|
| 162 | if (cLeaves.size() != uLeaves.size()) |
---|
| 163 | return false; |
---|
| 164 | |
---|
| 165 | for(int i=0; i < cLeaves.size(); i++) { |
---|
| 166 | if(!(cLeaves.get(i).serialize().equals(uLeaves.get(i).serialize()))) |
---|
| 167 | return false; |
---|
| 168 | |
---|
| 169 | } |
---|
| 170 | return true; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | private EList<LeafNode> removeWS(EList<LeafNode> leaves) { |
---|
| 174 | for(int i=0; i < leaves.size(); i++) { |
---|
| 175 | if(!(leaves.get(i).getGrammarElement() instanceof org.eclipse.xtext.impl.TerminalRuleImpl)) |
---|
| 176 | leaves.remove(i); |
---|
| 177 | } |
---|
| 178 | return leaves; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | // ---------------------------------------------------------------------------------------------------- |
---|
| 182 | |
---|
| 183 | @Check(CheckType.EXPENSIVE) |
---|
| 184 | public void checkReferencedOnlyOnce(Rule rule) { |
---|
| 185 | if (!checkReferencedOnlyOnce) |
---|
| 186 | return; |
---|
| 187 | |
---|
| 188 | if (EbnfAnalysisUtils.isTokenRule(rule)) |
---|
| 189 | return; |
---|
| 190 | |
---|
| 191 | List<Rule> references = EbnfAnalysisUtils.findReferences(rule, |
---|
| 192 | resourceDescriptions); |
---|
| 193 | |
---|
| 194 | if (references.size() == 1) { |
---|
| 195 | warning(EbnfJavaValidator.ruleReferencedOneDescription, 1, |
---|
| 196 | EbnfJavaValidator.ruleReferencedOneDescription); |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | // ---------------------------------------------------------------------------------------------------- |
---|
| 201 | |
---|
| 202 | @Check(CheckType.EXPENSIVE) |
---|
| 203 | public void checkPassthroughRule(Rule rule) { |
---|
| 204 | if (!checkPassthroughRule) |
---|
| 205 | return; |
---|
| 206 | |
---|
| 207 | List<Rule> references = EbnfAnalysisUtils.findReferences(rule, |
---|
| 208 | resourceDescriptions); |
---|
| 209 | |
---|
| 210 | if (EbnfAnalysisUtils.isPassthroughRule(rule)) { |
---|
| 211 | if (references.size() == 0) { |
---|
| 212 | warning( |
---|
| 213 | EbnfJavaValidator.unreferencedPassthroughRuleDescription, |
---|
| 214 | 1, |
---|
| 215 | EbnfJavaValidator.unreferencedPassthroughRuleDescription); |
---|
| 216 | } else { |
---|
| 217 | warning(EbnfJavaValidator.passthroughRuleDescription, 1, |
---|
| 218 | EbnfJavaValidator.passthroughRuleDescription); |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | // ---------------------------------------------------------------------------------------------------- |
---|
| 224 | |
---|
| 225 | @Check(CheckType.EXPENSIVE) |
---|
| 226 | public void checkUnusedRule(Rule rule) { |
---|
| 227 | if (!checkUnusedRule) |
---|
| 228 | return; |
---|
| 229 | |
---|
| 230 | List<Rule> references = EbnfAnalysisUtils.findReferences(rule, |
---|
| 231 | resourceDescriptions); |
---|
| 232 | |
---|
| 233 | if ((references.size() == 0) && (rule.getRulenumber() != 1)) |
---|
| 234 | warning(EbnfJavaValidator.unusedRuleDescription, 1, |
---|
| 235 | EbnfJavaValidator.unusedRuleDescription); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | // ---------------------------------------------------------------------------------------------------- |
---|
| 239 | |
---|
| 240 | private List<DefinitionList> collectDefinitionLists(CompositeNode o) { |
---|
| 241 | List<DefinitionList> list = new ArrayList<DefinitionList>(); |
---|
| 242 | |
---|
| 243 | for (int i = 0; i < o.getChildren().size(); i++) { |
---|
| 244 | AbstractNode child = o.getChildren().get(i); |
---|
| 245 | if (child.getElement() instanceof DefinitionList) { |
---|
| 246 | list.add((DefinitionList) child.getElement()); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | if (child instanceof CompositeNode) { |
---|
| 250 | list.addAll(collectDefinitionLists((CompositeNode) child)); |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | return list; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | // ---------------------------------------------------------------------------------------------------- |
---|
| 258 | |
---|
| 259 | @Check(CheckType.EXPENSIVE) |
---|
| 260 | public void checkEqualAlternative(Rule rule) { |
---|
| 261 | if (!checkEqualAlternative) |
---|
| 262 | return; |
---|
| 263 | |
---|
| 264 | // TODO: is not per rule, but global! |
---|
| 265 | |
---|
| 266 | // quadratic! |
---|
| 267 | CompositeNode startNode = NodeUtil.getNodeAdapter(rule).getParserNode(); |
---|
| 268 | |
---|
| 269 | List<DefinitionList> definitionLists = collectDefinitionLists(startNode); |
---|
| 270 | |
---|
| 271 | for (int k = 0; k < definitionLists.size(); k++) { |
---|
| 272 | |
---|
| 273 | EList<SingleDefinition> singleDefinitions = definitionLists.get(k) |
---|
| 274 | .getSingleDefinition(); |
---|
| 275 | for (int i = 0; i < singleDefinitions.size(); i++) { |
---|
| 276 | for (int j = 0; j < singleDefinitions.size(); j++) { |
---|
| 277 | if (i == j) |
---|
| 278 | continue; |
---|
| 279 | |
---|
| 280 | SingleDefinition upper = singleDefinitions.get(i); |
---|
| 281 | SingleDefinition lower = singleDefinitions.get(j); |
---|
| 282 | CompositeNode upperNode = NodeUtil.getNodeAdapter(upper) |
---|
| 283 | .getParserNode(); |
---|
| 284 | CompositeNode lowerNode = NodeUtil.getNodeAdapter(lower) |
---|
| 285 | .getParserNode(); |
---|
| 286 | |
---|
| 287 | String upperString = upperNode.serialize().trim() |
---|
| 288 | .replaceAll("[ \t\n\r]", ""); |
---|
| 289 | String lowerString = lowerNode.serialize().trim() |
---|
| 290 | .replaceAll("[ \t\n\r]", ""); |
---|
| 291 | |
---|
| 292 | if (lowerString.equals(upperString)) |
---|
| 293 | warning(EbnfJavaValidator.equalAlternativeDescription, |
---|
| 294 | 1, |
---|
| 295 | EbnfJavaValidator.equalAlternativeDescription); |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | // ---------------------------------------------------------------------------------------------------- |
---|
| 303 | |
---|
| 304 | @Check(CheckType.EXPENSIVE) |
---|
| 305 | public void checkDuplicateRules(Rule rule) { |
---|
| 306 | if (!checkDuplicateRules) |
---|
| 307 | return; |
---|
| 308 | |
---|
[19] | 309 | EtsiBnf etsiBnfNode = (EtsiBnf) rule.eContainer().eContainer(); |
---|
[5] | 310 | |
---|
[19] | 311 | CompositeNode definitionList = NodeUtil.getNodeAdapter(rule.getDefinitionList()).getParserNode(); |
---|
| 312 | |
---|
| 313 | String rightHandSideText = definitionList.serialize().trim() |
---|
[5] | 314 | .replaceAll("[ \t\n\r]", ""); |
---|
| 315 | |
---|
[19] | 316 | for (int i = 0; i < etsiBnfNode.getBnfEntry().size(); i++) { |
---|
| 317 | BnfEntry currentNode = etsiBnfNode.getBnfEntry().get(i); |
---|
| 318 | if (currentNode == null) |
---|
[5] | 319 | continue; |
---|
[19] | 320 | if (currentNode.getRule() == null) |
---|
[5] | 321 | continue; |
---|
[19] | 322 | if (currentNode.getRule() == rule) |
---|
[5] | 323 | continue; |
---|
[19] | 324 | |
---|
| 325 | Rule currentRule = currentNode.getRule(); |
---|
| 326 | CompositeNode currentRuleDefinitionList = NodeUtil.getNodeAdapter(currentRule.getDefinitionList()).getParserNode(); |
---|
| 327 | String currentRuleRightHandSideText = currentRuleDefinitionList.serialize().trim() |
---|
| 328 | .replaceAll("[ \t\n\r]", ""); |
---|
| 329 | |
---|
| 330 | if (currentRuleRightHandSideText.equals(rightHandSideText)) { |
---|
[5] | 331 | String description = EbnfJavaValidator.duplicateRulesDescription |
---|
[19] | 332 | + " with rule \"" |
---|
| 333 | + currentRule.getName() |
---|
| 334 | + "\" (Line " |
---|
| 335 | + NodeUtil.getNodeAdapter(currentRule).getParserNode().getLine() + ")"; |
---|
[5] | 336 | warning(description, 1, description); |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | // ---------------------------------------------------------------------------------------------------- |
---|
| 342 | |
---|
| 343 | @Check(CheckType.EXPENSIVE) |
---|
[49] | 344 | public void checkSubruleDuplicates(EtsiBnf bnf) { |
---|
[5] | 345 | if (!checkSubruleDuplicates) |
---|
| 346 | return; |
---|
[50] | 347 | |
---|
| 348 | HashMap<String, DuplicateEntry> dupesMap = new HashMap<String, DuplicateEntry>(); |
---|
| 349 | Set<String> taggedEntries = new HashSet<String>(); |
---|
| 350 | |
---|
| 351 | // build dupes map |
---|
[49] | 352 | for (int i=0; i < bnf.getBnfEntry().size(); i++) { |
---|
| 353 | BnfEntry currentBnfEntry = bnf.getBnfEntry().get(i); |
---|
| 354 | if ((currentBnfEntry.getRule() != null) && (currentBnfEntry.getRule().getDefinitionList() != null)) { |
---|
| 355 | |
---|
| 356 | CompositeNode ruleParserNode = NodeUtil.getNodeAdapter(currentBnfEntry.getRule().getDefinitionList()).getParserNode(); |
---|
| 357 | String ruleText = ruleParserNode.serialize().trim(); |
---|
[50] | 358 | String processedRuleText = textSpacer(ruleText); |
---|
[49] | 359 | |
---|
| 360 | String trimmedRuleText = processedRuleText.replaceAll("[\t\n\r]", " ").replaceAll("[ ]+", " "); |
---|
| 361 | |
---|
| 362 | String[] parts = trimmedRuleText.split(" "); |
---|
| 363 | |
---|
| 364 | for (int j=0; j <= parts.length; j++) { |
---|
| 365 | for (int k=j; k <= parts.length; k++) { |
---|
| 366 | if (isBalancedPairs(parts, j, k) && (k-j > 1)) { |
---|
| 367 | String balancedSubString = createString(parts, j, k); |
---|
[50] | 368 | DuplicateEntry mapEntry = dupesMap.get(balancedSubString); |
---|
| 369 | if (mapEntry == null) { |
---|
| 370 | mapEntry = new DuplicateEntry(); |
---|
| 371 | mapEntry.setSnippet(balancedSubString); |
---|
| 372 | mapEntry.addRule(currentBnfEntry.getRule()); |
---|
| 373 | dupesMap.put(balancedSubString, mapEntry); |
---|
| 374 | } else { |
---|
| 375 | mapEntry.addRule(currentBnfEntry.getRule()); |
---|
| 376 | dupesMap.put(balancedSubString, mapEntry); |
---|
| 377 | taggedEntries.add(balancedSubString); |
---|
| 378 | } |
---|
[49] | 379 | } |
---|
| 380 | } |
---|
| 381 | } |
---|
[50] | 382 | } |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | // create warnings |
---|
| 386 | |
---|
| 387 | Iterator<String> it = taggedEntries.iterator(); |
---|
| 388 | |
---|
| 389 | while (it.hasNext()) { |
---|
| 390 | String entry = it.next(); |
---|
| 391 | DuplicateEntry dupeEntry = dupesMap.get(entry); |
---|
| 392 | if (dupeEntry.getRules().size() < 2) |
---|
| 393 | continue; |
---|
| 394 | |
---|
| 395 | String warningText = ""; |
---|
| 396 | warningText += entry + " is duplicated in the following rules: "; |
---|
| 397 | for (int i=0; i < dupeEntry.getRules().size(); i++) { |
---|
| 398 | Rule rule = dupeEntry.getRules().get(i); |
---|
| 399 | CompositeNode parserNode = NodeUtil.getNodeAdapter(rule).getParserNode(); |
---|
| 400 | |
---|
| 401 | if (rule.getRulenumber() > 0) { |
---|
| 402 | String ruleNumber = Integer.toString(rule.getRulenumber()).replaceAll("[ \t\n\r]", ""); |
---|
| 403 | warningText += ruleNumber; |
---|
| 404 | } |
---|
| 405 | warningText += " (Line "; |
---|
| 406 | warningText += parserNode.getLine(); |
---|
| 407 | warningText += ")"; |
---|
[49] | 408 | |
---|
[50] | 409 | if (i+1 < dupeEntry.getRules().size()) |
---|
| 410 | warningText += ", "; |
---|
[49] | 411 | |
---|
| 412 | } |
---|
[50] | 413 | |
---|
[51] | 414 | Rule rule = dupeEntry.getRules().get(0); |
---|
| 415 | CompositeNode parserNode = NodeUtil.getNodeAdapter(rule).getParserNode(); |
---|
| 416 | warning(warningText, parserNode.getElement(), 1); |
---|
[49] | 417 | } |
---|
[5] | 418 | |
---|
[50] | 419 | } |
---|
| 420 | |
---|
| 421 | private String textSpacer(String ruleText) { |
---|
| 422 | ruleText = ruleText + " "; |
---|
| 423 | String processedRuleText = ruleText.replaceAll("([^\"])\\(([^\"])", "$1 ( $2"); // ( case 1 |
---|
| 424 | processedRuleText = processedRuleText.replaceAll("\\(([^\"])", " ( $1"); // ( case 2 |
---|
| 425 | processedRuleText = processedRuleText.replaceAll("([^\"])\\(\"", "$1 ( \""); // ( case 3 |
---|
| 426 | |
---|
| 427 | processedRuleText = processedRuleText.replaceAll("([^\"])\\)([^\"])", "$1 ) $2"); // ) case 1 |
---|
| 428 | processedRuleText = processedRuleText.replaceAll("\\)([^\"])", " ) $1"); // ) case 2 |
---|
| 429 | processedRuleText = processedRuleText.replaceAll("\"\\)([^\"])", "\" ) $1"); // ) case 3 |
---|
[49] | 430 | |
---|
[50] | 431 | processedRuleText = processedRuleText.replaceAll("([^\"])\\{([^\"])", "$1 { $2"); // { case 1 |
---|
| 432 | processedRuleText = processedRuleText.replaceAll("\\{([^\"])", " { $1"); // { case 2 |
---|
| 433 | processedRuleText = processedRuleText.replaceAll("([^\"])\\{\"", "$1 { \""); // { case 3 |
---|
[49] | 434 | |
---|
[50] | 435 | processedRuleText = processedRuleText.replaceAll("([^\"])\\}([^\\+\"])", "$1 } $2"); // } case 1 |
---|
| 436 | processedRuleText = processedRuleText.replaceAll("\\}([^\\+\"])", " } $1"); // } case 2 |
---|
| 437 | processedRuleText = processedRuleText.replaceAll("\"\\}([^\\+\"])", "\" } $1"); // } case 3 |
---|
[49] | 438 | |
---|
[50] | 439 | processedRuleText = processedRuleText.replaceAll("([^\"])\\}\\+([^\"])", "$1 }\\+ $2"); // }+ case 1 |
---|
| 440 | processedRuleText = processedRuleText.replaceAll("\\}\\+([^\"])", " }\\+ $1 "); // }+ case 2 |
---|
| 441 | processedRuleText = processedRuleText.replaceAll("\"\\}\\+([^\"])", "\" }\\+ $1 "); // }+ case 3 |
---|
[49] | 442 | |
---|
[50] | 443 | processedRuleText = processedRuleText.replaceAll("([^\"])\\[([^\"])", "$1 [ $2"); // [ case 1 |
---|
| 444 | processedRuleText = processedRuleText.replaceAll("\\[([^\"])", " [ $1"); // [ case 2 |
---|
| 445 | processedRuleText = processedRuleText.replaceAll("([^\"])\\[\"", "$1 [ \""); // [ case 3 |
---|
| 446 | |
---|
| 447 | processedRuleText = processedRuleText.replaceAll("([^\"])\\]([^\"])", "$1 ] $2"); // ] case 1 |
---|
| 448 | processedRuleText = processedRuleText.replaceAll("\\]([^\"])", " ] $1"); // ] case 2 |
---|
| 449 | processedRuleText = processedRuleText.replaceAll("\"\\]([^\"])", "\" ] $1"); // ] case 3 |
---|
| 450 | return processedRuleText; |
---|
[5] | 451 | } |
---|
| 452 | |
---|
[49] | 453 | private boolean isBalancedPairs(String[] parts, int start, int end) { |
---|
| 454 | Stack<String> pairStack = new Stack<String>(); |
---|
| 455 | |
---|
| 456 | if (start == end) |
---|
| 457 | return false; |
---|
| 458 | |
---|
| 459 | if (parts[start].contains("|")) |
---|
| 460 | return false; |
---|
| 461 | |
---|
| 462 | if (parts[start].contains("}")) |
---|
| 463 | return false; |
---|
| 464 | |
---|
| 465 | if (parts[start].contains("}+")) |
---|
| 466 | return false; |
---|
| 467 | |
---|
| 468 | if (parts[start].contains("]")) |
---|
| 469 | return false; |
---|
| 470 | |
---|
| 471 | if (parts[end-1].contains("|")) |
---|
| 472 | return false; |
---|
| 473 | |
---|
| 474 | for (int i=start; i < end; i++) { |
---|
| 475 | String currentPart = parts[i]; |
---|
| 476 | if (currentPart.equals("(") || currentPart.equals("{") || currentPart.equals("[")) { |
---|
| 477 | pairStack.push(currentPart); |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | if (!pairStack.isEmpty()) { |
---|
| 481 | if (currentPart.equals(")") && pairStack.peek().equals("(")) { |
---|
| 482 | pairStack.pop(); |
---|
| 483 | } else if (currentPart.equals("}") && pairStack.peek().equals("{")) { |
---|
| 484 | pairStack.pop(); |
---|
| 485 | } else if (currentPart.equals("}+") && pairStack.peek().equals("{")) { |
---|
| 486 | pairStack.pop(); |
---|
| 487 | } else if (currentPart.equals("]") && pairStack.peek().equals("[")) { |
---|
| 488 | pairStack.pop(); |
---|
| 489 | } |
---|
| 490 | } else { |
---|
| 491 | if (currentPart.equals(")") || currentPart.equals("}") || currentPart.equals("}+") || currentPart.equals("]")) { |
---|
| 492 | return false; |
---|
| 493 | } |
---|
| 494 | } |
---|
| 495 | } |
---|
| 496 | |
---|
| 497 | if (pairStack.empty()) |
---|
| 498 | return true; |
---|
| 499 | |
---|
| 500 | return false; |
---|
| 501 | |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | private String createString(String[] parts, int start, int end) { |
---|
| 505 | StringBuffer result = new StringBuffer(); |
---|
| 506 | |
---|
| 507 | for (int i=start; i < end; i++) { |
---|
| 508 | result.append(parts[i]); |
---|
| 509 | result.append(" "); |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | return result.toString().trim(); |
---|
| 513 | } |
---|
| 514 | |
---|
[5] | 515 | } |
---|