Index: /trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfFormatterVisitor.java
===================================================================
--- /trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfFormatterVisitor.java	(revision 24)
+++ /trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfFormatterVisitor.java	(revision 25)
@@ -36,5 +36,5 @@
 	private StringBuffer buf;
 	private FormatterConfig config;
-	private int bufferPosition = 0;
+	private int bufferPositionFormattedTextNoWhitespaces = 0;
 	private int bufferPositionOriginalText = 0;
 	private int allCommentsPosition = 0;
@@ -46,5 +46,5 @@
 	private int bufferPositionFormattedText;
 	private String formattedText;
-	private String bufNoWhitespaces;
+	private String formattedTextNoWhitespaces;
 	private String originalTextNoWhitespaces;
 	
@@ -105,5 +105,5 @@
 	
 	private void skipWhitespacesOriginalText() {
-		while (isWhitespace(originalText.charAt(bufferPositionOriginalText))) {
+		while (bufferPositionOriginalText < originalText.length() && isWhitespace(originalText.charAt(bufferPositionOriginalText))) {
 			bufferPositionOriginalText++;
 		}
@@ -111,5 +111,5 @@
 
 	private void skipWhitespacesFormattedText(StringBuffer result) {
-		while (isWhitespace(formattedText.charAt(bufferPositionFormattedText))) {
+		while (bufferPositionFormattedText < formattedText.length() && isWhitespace(formattedText.charAt(bufferPositionFormattedText))) {
 			result.append(formattedText.substring(bufferPositionFormattedText, bufferPositionFormattedText+1));
 			bufferPositionFormattedText++;
@@ -175,18 +175,34 @@
 	
 	private void weaveComments() {
-		bufferPosition = 0;
+//		if (true) {
+//			StringBuffer result = new StringBuffer();
+//			result.append(buf.toString());
+//			buf = result;
+//			return;
+//		}
+		
 		bufferPositionOriginalText = 0;
+		bufferPositionFormattedTextNoWhitespaces = 0;
 		bufferPositionFormattedText = 0;
 		
 		StringBuffer result = new StringBuffer();
-		bufNoWhitespaces = buf.toString().replaceAll("[ \t\n\r]", "");
+		formattedTextNoWhitespaces = buf.toString().replaceAll("[ \t\n\r]", "");
 		formattedText = buf.toString();
 		
-		while (bufferPosition < bufNoWhitespaces.length()) {
+		while (bufferPositionFormattedTextNoWhitespaces < formattedTextNoWhitespaces.length()) {
 			skipWhitespacesOriginalText();
 			skipWhitespacesFormattedText(result);
-
-			if (bufNoWhitespaces.charAt(bufferPosition) != originalText.charAt(bufferPositionOriginalText)) {
-				if (isCommentNext(originalText, bufferPositionOriginalText)) {
+			
+			if (!(bufferPositionOriginalText < originalText.length()))
+				break;
+			
+			char formattedPositionNoWhitespaces = formattedTextNoWhitespaces.charAt(bufferPositionFormattedTextNoWhitespaces);
+			char originalPosition = originalText.charAt(bufferPositionOriginalText);
+
+			if (formattedPositionNoWhitespaces != originalPosition) {
+				if (formattedPositionNoWhitespaces == ';') { // formatted text always outputs the optional semicolon, skip it if necessary
+					bufferPositionFormattedTextNoWhitespaces++;
+					bufferPositionFormattedText++;
+				} else if (isCommentNext(originalText, bufferPositionOriginalText)) {
 					LeafNode currentComment = allComments.get(allCommentsPosition); 
 					if (currentComment.getTotalOffset() == bufferPositionOriginalText) {
@@ -194,4 +210,8 @@
 							int newLinesCount = scanBackNewlinesCount(originalText, bufferPositionOriginalText-1);
 							if (newLinesCount > 0) {
+								if (scanBackNewlinesCount(result.toString(), result.toString().length()-1) == 0) {
+									result.append("\n\n");
+								}
+								
 								result.append(currentComment.getText());
 								result.append("\n");
@@ -216,11 +236,14 @@
 						allCommentsPosition++;
 					}
+				} else { // disaster handling: return original unformatted text!
+					buf = new StringBuffer();
+					buf.append(originalText);
+					return;
 				}
 			} else {
 				result.append(formattedText.substring(bufferPositionFormattedText, bufferPositionFormattedText+1));
-
 				bufferPositionOriginalText++;
 				bufferPositionFormattedText++;
-				bufferPosition++;
+				bufferPositionFormattedTextNoWhitespaces++;
 			}
 		}
