Changeset 49 in default for trunk/de.ugoe.cs.swe.bnftools.ebnf
- Timestamp:
- 12/01/10 15:15:50 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/de.ugoe.cs.swe.bnftools.ebnf/src/de/ugoe/cs/swe/bnftools/validation/EbnfJavaValidator.java
r19 r49 3 3 import java.util.ArrayList; 4 4 import java.util.List; 5 import java.util.Stack; 5 6 6 7 import org.eclipse.emf.common.util.EList; 7 8 import org.eclipse.emf.common.util.URI; 9 import org.eclipse.emf.ecore.EObject; 8 10 import org.eclipse.emf.ecore.resource.Resource; 9 11 import org.eclipse.xtext.parsetree.AbstractNode; … … 336 338 337 339 @Check(CheckType.EXPENSIVE) 338 public void checkSubruleDuplicates( Rule rule) {340 public void checkSubruleDuplicates(EtsiBnf bnf) { 339 341 if (!checkSubruleDuplicates) 340 342 return; 341 343 342 //TODO: currently compares complete rules and not subrules 343 344 for (int i=0; i < bnf.getBnfEntry().size(); i++) { 345 BnfEntry currentBnfEntry = bnf.getBnfEntry().get(i); 346 if ((currentBnfEntry.getRule() != null) && (currentBnfEntry.getRule().getDefinitionList() != null)) { 347 348 CompositeNode ruleParserNode = NodeUtil.getNodeAdapter(currentBnfEntry.getRule().getDefinitionList()).getParserNode(); 349 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 379 380 String trimmedRuleText = processedRuleText.replaceAll("[\t\n\r]", " ").replaceAll("[ ]+", " "); 381 System.out.println("rule: " + trimmedRuleText); 382 383 // System.out.println(trimmedRuleText); 384 String[] parts = trimmedRuleText.split(" "); 385 386 for (int j=0; j <= parts.length; j++) { 387 for (int k=j; k <= parts.length; k++) { 388 if (isBalancedPairs(parts, j, k) && (k-j > 1)) { 389 String balancedSubString = createString(parts, j, k); 390 System.out.println(balancedSubString); 391 } 392 393 // combined.append(parts[k]); 394 // combined.append(" "); 395 } 396 } 397 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 422 } 423 424 private boolean isBalancedPairs(String[] parts, int start, int end) { 425 Stack<String> pairStack = new Stack<String>(); 426 427 if (start == end) 428 return false; 429 430 if (parts[start].contains("|")) 431 return false; 432 433 if (parts[start].contains("}")) 434 return false; 435 436 if (parts[start].contains("}+")) 437 return false; 438 439 if (parts[start].contains("]")) 440 return false; 441 442 if (parts[end-1].contains("|")) 443 return false; 444 445 for (int i=start; i < end; i++) { 446 String currentPart = parts[i]; 447 if (currentPart.equals("(") || currentPart.equals("{") || currentPart.equals("[")) { 448 pairStack.push(currentPart); 449 } 450 451 if (!pairStack.isEmpty()) { 452 if (currentPart.equals(")") && pairStack.peek().equals("(")) { 453 pairStack.pop(); 454 } else if (currentPart.equals("}") && pairStack.peek().equals("{")) { 455 pairStack.pop(); 456 } else if (currentPart.equals("}+") && pairStack.peek().equals("{")) { 457 pairStack.pop(); 458 } else if (currentPart.equals("]") && pairStack.peek().equals("[")) { 459 pairStack.pop(); 460 } 461 } else { 462 if (currentPart.equals(")") || currentPart.equals("}") || currentPart.equals("}+") || currentPart.equals("]")) { 463 return false; 464 } 465 } 466 } 467 468 if (pairStack.empty()) 469 return true; 470 471 return false; 472 473 } 474 475 private String createString(String[] parts, int start, int end) { 476 StringBuffer result = new StringBuffer(); 477 478 for (int i=start; i < end; i++) { 479 result.append(parts[i]); 480 result.append(" "); 481 } 482 483 return result.toString().trim(); 344 484 } 345 485
Note: See TracChangeset
for help on using the changeset viewer.