Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 835 | chris | 1 | package com.gebauz.bauzoid.parser; |
| 2 | |||
| 3 | public class Preprocessor |
||
| 4 | { |
||
| 5 | // Constants======================================================================================== |
||
| 6 | |||
| 7 | // Embedded Types=================================================================================== |
||
| 8 | |||
| 9 | // Members========================================================================================== |
||
| 10 | |||
| 11 | // Methods========================================================================================== |
||
| 12 | |||
| 13 | public static String stripComments(String str) |
||
| 14 | { |
||
| 15 | return stripComments(str, "//", "/*","*/"); |
||
| 16 | } |
||
| 17 | |||
| 18 | |||
| 19 | public static String stripComments(String str, String lineCommentBegin, String commentBegin, String commentEnd) |
||
| 20 | { |
||
| 21 | String result = ""; |
||
| 22 | int pos = 0; |
||
| 23 | |||
| 24 | while (pos < str.length()) |
||
| 25 | { |
||
| 26 | if (!lineCommentBegin.isEmpty() && isNextString(str, pos, lineCommentBegin)) |
||
| 27 | { |
||
| 28 | // skip until end of line |
||
| 29 | while ((pos < str.length()) && (!isNextString(str, pos, "\n"))) |
||
| 30 | { |
||
| 31 | pos++; |
||
| 32 | } |
||
| 33 | } |
||
| 34 | else if (!commentBegin.isEmpty() && !commentEnd.isEmpty() && isNextString(str, pos, commentBegin)) |
||
| 35 | { |
||
| 36 | // skip until commentEnd encountered |
||
| 37 | while (pos < str.length()) |
||
| 38 | { |
||
| 39 | if (isNextString(str, pos, commentEnd)) |
||
| 40 | { |
||
| 41 | // skip over commentEnd and then exit loop |
||
| 42 | pos += commentEnd.length(); |
||
| 43 | break; |
||
| 44 | } |
||
| 45 | |||
| 46 | pos++; |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | result = result + str.charAt(pos); |
||
| 51 | pos++; |
||
| 52 | } |
||
| 53 | |||
| 54 | return result; |
||
| 55 | } |
||
| 56 | |||
| 57 | public static boolean isNextString(String str, int pos, String substr) |
||
| 58 | { |
||
| 59 | for (int i = 0; i < substr.length(); i++) |
||
| 60 | { |
||
| 61 | if ((pos + i) >= str.length()) |
||
| 62 | return false; |
||
| 63 | |||
| 64 | if (substr.charAt(i) != str.charAt(pos + i)) |
||
| 65 | return false; |
||
| 66 | } |
||
| 67 | |||
| 68 | return true; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** Trim whitespaces from the begin and end of the string. */ |
||
| 72 | public static String trim(String str, char[] whitespaces) |
||
| 73 | { |
||
| 74 | int begin = 0; |
||
| 75 | int end = str.length()-1; |
||
| 76 | |||
| 77 | while (begin < str.length()) |
||
| 78 | { |
||
| 79 | for (char c : whitespaces) |
||
| 80 | { |
||
| 81 | if (str.charAt(begin) == c) |
||
| 82 | { |
||
| 83 | begin++; |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | // no whitespace -> break; |
||
| 89 | break; |
||
| 90 | } |
||
| 91 | |||
| 92 | while (end >= 0) |
||
| 93 | { |
||
| 94 | for (char c : whitespaces) |
||
| 95 | { |
||
| 96 | if (str.charAt(end) == c) |
||
| 97 | { |
||
| 98 | end--; |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | // no whitespace -> break; |
||
| 104 | end++; |
||
| 105 | break; |
||
| 106 | } |
||
| 107 | |||
| 108 | if (begin >= end) |
||
| 109 | return ""; |
||
| 110 | |||
| 111 | return str.substring(begin, end); |
||
| 112 | } |
||
| 113 | |||
| 114 | public static String escapeString(String src) |
||
| 115 | { |
||
| 116 | StringBuilder result = new StringBuilder(); |
||
| 117 | |||
| 118 | int i = 0; |
||
| 119 | while (i < src.length()) |
||
| 120 | { |
||
| 121 | char c = src.charAt(i); |
||
| 122 | i++; |
||
| 123 | |||
| 124 | if (c == '\\') |
||
| 125 | { |
||
| 126 | // ecape character |
||
| 127 | char next = src.charAt(i); |
||
| 128 | i++; |
||
| 129 | switch(next) |
||
| 130 | { |
||
| 131 | case '\\': |
||
| 132 | // escaped backslash |
||
| 133 | result.append('\\'); |
||
| 134 | break; |
||
| 135 | case 'u': |
||
| 136 | // escape Unicode character |
||
| 137 | String codeStr = src.substring(i, i+4); |
||
| 138 | i += 4; |
||
| 139 | Integer code = Integer.decode("0x" + codeStr); |
||
| 140 | result.append(Character.toChars(code)); |
||
| 141 | break; |
||
| 142 | case 'n': |
||
| 143 | result.append('\n'); |
||
| 144 | break; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | else |
||
| 148 | { |
||
| 149 | result.append(c); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | return result.toString(); |
||
| 154 | } |
||
| 155 | |||
| 156 | // Getters/Setters================================================================================== |
||
| 157 | |||
| 158 | } |