Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 806 | chris | 1 | using System; |
| 2 | using System.Collections.Generic; |
||
| 3 | using System.Linq; |
||
| 4 | using System.Text; |
||
| 5 | using System.Threading.Tasks; |
||
| 6 | |||
| 7 | namespace BauzoidNET.parser |
||
| 8 | { |
||
| 9 | public class Preprocessor |
||
| 10 | { |
||
| 11 | public static string stripComments(string str) |
||
| 12 | { |
||
| 13 | return stripComments(str, "//", "/*", "*/"); |
||
| 14 | } |
||
| 15 | |||
| 16 | |||
| 17 | public static string stripComments(string str, string lineCommentBegin, string commentBegin, string commentEnd) |
||
| 18 | { |
||
| 19 | String result = ""; |
||
| 20 | int pos = 0; |
||
| 21 | |||
| 22 | while (pos < str.Length) |
||
| 23 | { |
||
| 24 | if ((lineCommentBegin.Length != 0) && isNextString(str, pos, lineCommentBegin)) |
||
| 25 | { |
||
| 26 | // skip until end of line |
||
| 27 | while ((pos < str.Length) && (!isNextString(str, pos, "\n"))) |
||
| 28 | { |
||
| 29 | pos++; |
||
| 30 | } |
||
| 31 | } |
||
| 32 | else if ((commentBegin.Length != 0) && (commentEnd.Length != 0) && isNextString(str, pos, commentBegin)) |
||
| 33 | { |
||
| 34 | // skip until commentEnd encountered |
||
| 35 | while (pos < str.Length) |
||
| 36 | { |
||
| 37 | if (isNextString(str, pos, commentEnd)) |
||
| 38 | { |
||
| 39 | // skip over commentEnd and then exit loop |
||
| 40 | pos += commentEnd.Length; |
||
| 41 | break; |
||
| 42 | } |
||
| 43 | |||
| 44 | pos++; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | result = result + str[pos]; |
||
| 49 | pos++; |
||
| 50 | } |
||
| 51 | |||
| 52 | return result; |
||
| 53 | } |
||
| 54 | |||
| 55 | public static bool isNextString(string str, int pos, string substr) |
||
| 56 | { |
||
| 57 | for (int i = 0; i < substr.Length; i++) |
||
| 58 | { |
||
| 59 | if ((pos + i) >= str.Length) |
||
| 60 | return false; |
||
| 61 | |||
| 62 | if (substr[i] != str[pos + i]) |
||
| 63 | return false; |
||
| 64 | } |
||
| 65 | |||
| 66 | return true; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** Trim whitespaces from the begin and end of the string. */ |
||
| 70 | public static string trim(string str, char[] whitespaces) |
||
| 71 | { |
||
| 72 | int begin = 0; |
||
| 73 | int end = str.Length-1; |
||
| 74 | |||
| 75 | while (begin < str.Length) |
||
| 76 | { |
||
| 77 | for (int i = 0; i < whitespaces.Length; i++) |
||
| 78 | { |
||
| 79 | char c = whitespaces[i]; |
||
| 80 | if (str[begin] == c) |
||
| 81 | { |
||
| 82 | begin++; |
||
| 83 | continue; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | // no whitespace -> break; |
||
| 88 | break; |
||
| 89 | } |
||
| 90 | |||
| 91 | while (end >= 0) |
||
| 92 | { |
||
| 93 | for (int i = 0; i < whitespaces.Length; i++) |
||
| 94 | { |
||
| 95 | char c = whitespaces[i]; |
||
| 96 | if (str[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[i]; |
||
| 122 | i++; |
||
| 123 | |||
| 124 | if (c == '\\') |
||
| 125 | { |
||
| 126 | // ecape character |
||
| 127 | char next = src[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 | int code = Convert.ToInt32("0x" + codeStr, 16); |
||
| 141 | //result.Append(Character.toChars(code)); |
||
| 142 | result.Append(Convert.ToChar(code)); |
||
| 143 | break; |
||
| 144 | case 'n': |
||
| 145 | result.Append('\n'); |
||
| 146 | break; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | else |
||
| 150 | { |
||
| 151 | result.Append(c); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | return result.ToString(); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |