Changeset 50 in default
- Timestamp:
- 12/01/10 16:02:05 (14 years ago)
- Location:
- trunk/de.ugoe.cs.swe.bnftools.ebnf/src/de/ugoe/cs/swe/bnftools/validation
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/de.ugoe.cs.swe.bnftools.ebnf/src/de/ugoe/cs/swe/bnftools/validation/EbnfJavaValidator.java
r49 r50 2 2 3 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.HashSet; 6 import java.util.Iterator; 4 7 import java.util.List; 8 import java.util.Set; 5 9 import java.util.Stack; 6 10 … … 341 345 if (!checkSubruleDuplicates) 342 346 return; 343 347 348 HashMap<String, DuplicateEntry> dupesMap = new HashMap<String, DuplicateEntry>(); 349 Set<String> taggedEntries = new HashSet<String>(); 350 351 // build dupes map 344 352 for (int i=0; i < bnf.getBnfEntry().size(); i++) { 345 353 BnfEntry currentBnfEntry = bnf.getBnfEntry().get(i); … … 348 356 CompositeNode ruleParserNode = NodeUtil.getNodeAdapter(currentBnfEntry.getRule().getDefinitionList()).getParserNode(); 349 357 String ruleText = ruleParserNode.serialize().trim(); 350 ruleText = ruleText + " "; 351 String processedRuleText = ruleText.replaceAll("([^\"])\\(([^\"])", "$1 ( $2"); // ( case 1 352 processedRuleText = processedRuleText.replaceAll("\\(([^\"])", " ( $1"); // ( case 2 353 processedRuleText = processedRuleText.replaceAll("([^\"])\\(\"", "$1 ( \""); // ( case 3 354 355 processedRuleText = processedRuleText.replaceAll("([^\"])\\)([^\"])", "$1 ) $2"); // ) case 1 356 processedRuleText = processedRuleText.replaceAll("\\)([^\"])", " ) $1"); // ) case 2 357 processedRuleText = processedRuleText.replaceAll("\"\\)([^\"])", "\" ) $1"); // ) case 3 358 359 processedRuleText = processedRuleText.replaceAll("([^\"])\\{([^\"])", "$1 { $2"); // { case 1 360 processedRuleText = processedRuleText.replaceAll("\\{([^\"])", " { $1"); // { case 2 361 processedRuleText = processedRuleText.replaceAll("([^\"])\\{\"", "$1 { \""); // { case 3 362 363 processedRuleText = processedRuleText.replaceAll("([^\"])\\}([^\\+\"])", "$1 } $2"); // } case 1 364 processedRuleText = processedRuleText.replaceAll("\\}([^\\+\"])", " } $1"); // } case 2 365 processedRuleText = processedRuleText.replaceAll("\"\\}([^\\+\"])", "\" } $1"); // } case 3 366 367 processedRuleText = processedRuleText.replaceAll("([^\"])\\}\\+([^\"])", "$1 }\\+ $2"); // }+ case 1 368 processedRuleText = processedRuleText.replaceAll("\\}\\+([^\"])", " }\\+ $1 "); // }+ case 2 369 processedRuleText = processedRuleText.replaceAll("\"\\}\\+([^\"])", "\" }\\+ $1 "); // }+ case 3 370 371 processedRuleText = processedRuleText.replaceAll("([^\"])\\[([^\"])", "$1 [ $2"); // [ case 1 372 processedRuleText = processedRuleText.replaceAll("\\[([^\"])", " [ $1"); // [ case 2 373 processedRuleText = processedRuleText.replaceAll("([^\"])\\[\"", "$1 [ \""); // [ case 3 374 375 processedRuleText = processedRuleText.replaceAll("([^\"])\\]([^\"])", "$1 ] $2"); // ] case 1 376 processedRuleText = processedRuleText.replaceAll("\\]([^\"])", " ] $1"); // ] case 2 377 processedRuleText = processedRuleText.replaceAll("\"\\]([^\"])", "\" ] $1"); // ] case 3 378 358 String processedRuleText = textSpacer(ruleText); 379 359 380 360 String trimmedRuleText = processedRuleText.replaceAll("[\t\n\r]", " ").replaceAll("[ ]+", " "); 381 System.out.println("rule: " + trimmedRuleText);382 361 383 // System.out.println(trimmedRuleText);384 362 String[] parts = trimmedRuleText.split(" "); 385 363 … … 388 366 if (isBalancedPairs(parts, j, k) && (k-j > 1)) { 389 367 String balancedSubString = createString(parts, j, k); 390 System.out.println(balancedSubString); 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 } 391 379 } 392 393 // combined.append(parts[k]);394 // combined.append(" ");395 380 } 396 381 } 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 += ")"; 397 408 398 System.out.println("---------------------------------------------"); 399 400 401 402 403 } 404 } 405 406 407 408 409 // System.out.println("rule name: " + rule.getName()); 410 // for (int i=0; i < definitionLists.size(); i++) { 411 // DefinitionList currentDefList = definitionLists.get(i); 412 // 413 // CompositeNode currentRuleDefinitionList = NodeUtil.getNodeAdapter(currentDefList).getParserNode(); 414 // String currentRuleDefinitionListText = currentRuleDefinitionList.serialize().trim(); 415 // String trimmedCurrentRuleDefinitionListText = currentRuleDefinitionListText.replaceAll("[ \t\n\r]", ""); 416 // System.out.println(trimmedCurrentRuleDefinitionListText); 417 //// System.out.println(" -> " + currentRuleDefinitionListText); 418 // 419 // 420 // } 421 409 if (i+1 < dupeEntry.getRules().size()) 410 warningText += ", "; 411 412 } 413 414 for (int i=0; i < dupeEntry.getRules().size(); i++) { 415 Rule rule = dupeEntry.getRules().get(i); 416 CompositeNode parserNode = NodeUtil.getNodeAdapter(rule).getParserNode(); 417 warning(warningText, parserNode.getElement(), 1); 418 } 419 } 420 421 } 422 423 private String textSpacer(String ruleText) { 424 ruleText = ruleText + " "; 425 String processedRuleText = ruleText.replaceAll("([^\"])\\(([^\"])", "$1 ( $2"); // ( case 1 426 processedRuleText = processedRuleText.replaceAll("\\(([^\"])", " ( $1"); // ( case 2 427 processedRuleText = processedRuleText.replaceAll("([^\"])\\(\"", "$1 ( \""); // ( case 3 428 429 processedRuleText = processedRuleText.replaceAll("([^\"])\\)([^\"])", "$1 ) $2"); // ) case 1 430 processedRuleText = processedRuleText.replaceAll("\\)([^\"])", " ) $1"); // ) case 2 431 processedRuleText = processedRuleText.replaceAll("\"\\)([^\"])", "\" ) $1"); // ) case 3 432 433 processedRuleText = processedRuleText.replaceAll("([^\"])\\{([^\"])", "$1 { $2"); // { case 1 434 processedRuleText = processedRuleText.replaceAll("\\{([^\"])", " { $1"); // { case 2 435 processedRuleText = processedRuleText.replaceAll("([^\"])\\{\"", "$1 { \""); // { case 3 436 437 processedRuleText = processedRuleText.replaceAll("([^\"])\\}([^\\+\"])", "$1 } $2"); // } case 1 438 processedRuleText = processedRuleText.replaceAll("\\}([^\\+\"])", " } $1"); // } case 2 439 processedRuleText = processedRuleText.replaceAll("\"\\}([^\\+\"])", "\" } $1"); // } case 3 440 441 processedRuleText = processedRuleText.replaceAll("([^\"])\\}\\+([^\"])", "$1 }\\+ $2"); // }+ case 1 442 processedRuleText = processedRuleText.replaceAll("\\}\\+([^\"])", " }\\+ $1 "); // }+ case 2 443 processedRuleText = processedRuleText.replaceAll("\"\\}\\+([^\"])", "\" }\\+ $1 "); // }+ case 3 444 445 processedRuleText = processedRuleText.replaceAll("([^\"])\\[([^\"])", "$1 [ $2"); // [ case 1 446 processedRuleText = processedRuleText.replaceAll("\\[([^\"])", " [ $1"); // [ case 2 447 processedRuleText = processedRuleText.replaceAll("([^\"])\\[\"", "$1 [ \""); // [ case 3 448 449 processedRuleText = processedRuleText.replaceAll("([^\"])\\]([^\"])", "$1 ] $2"); // ] case 1 450 processedRuleText = processedRuleText.replaceAll("\\]([^\"])", " ] $1"); // ] case 2 451 processedRuleText = processedRuleText.replaceAll("\"\\]([^\"])", "\" ] $1"); // ] case 3 452 return processedRuleText; 422 453 } 423 454
Note: See TracChangeset
for help on using the changeset viewer.