[3743] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3743] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[1703] | 24 | using System.Text;
|
---|
[1776] | 25 | using System.Text.RegularExpressions;
|
---|
[1703] | 26 |
|
---|
| 27 | namespace HeuristicLab.Persistence.Auxiliary {
|
---|
| 28 |
|
---|
[3016] | 29 | /// <summary>
|
---|
| 30 | /// Error during type name parsing, thrown by <see cref="TypeNameParser"/>.
|
---|
| 31 | /// </summary>
|
---|
[1776] | 32 | public class ParseError : Exception {
|
---|
[3016] | 33 |
|
---|
| 34 | /// <summary>
|
---|
| 35 | /// Initializes a new instance of the <see cref="ParseError"/> class.
|
---|
| 36 | /// </summary>
|
---|
| 37 | /// <param name="message">The message.</param>
|
---|
[1776] | 38 | public ParseError(string message) : base(message) { }
|
---|
| 39 | }
|
---|
[1703] | 40 |
|
---|
[3004] | 41 | /// <summary>
|
---|
| 42 | /// Parse a .NET type name using the following grammar:
|
---|
| 43 | ///
|
---|
[3016] | 44 | /// <code>
|
---|
[3004] | 45 | /// TypeSpec := SimpleTypeSpec '&'?
|
---|
| 46 | ///
|
---|
| 47 | /// SimpleTypeSpec := (IDENTIFIER '.')*
|
---|
| 48 | /// (IDENTIFIER '+')*
|
---|
| 49 | /// IDENTIFIER
|
---|
| 50 | /// ( '`\d+[' Generics ']' )?
|
---|
| 51 | /// (\*|\[(\d+\.\.\d+|\d+\.\.\.|(|\*)(,(|\*))*)\])*
|
---|
| 52 | /// (',\s*' IDENTIFIER (',\s*' AssemblyProperty)* )?
|
---|
| 53 | ///
|
---|
| 54 | /// Generics := '[' SimpleTypeSpec ']' (',[' SimpleTypeSpec ']')
|
---|
| 55 | ///
|
---|
| 56 | /// AssemblyProperty := 'Version=' Version
|
---|
| 57 | /// | 'PublicKey(Token)?=[a-fA-F0-9]+'
|
---|
| 58 | /// | 'Culture=[a-zA-F0-9]+'
|
---|
| 59 | ///
|
---|
| 60 | /// Version := \d+\.\d+\.\d+\.\d+
|
---|
| 61 | ///
|
---|
| 62 | /// IDENTIFIER = [_a-zA-Z][_a-ZA-Z0-9]*
|
---|
[3016] | 63 | /// </code>
|
---|
[3004] | 64 | /// </summary>
|
---|
[1776] | 65 | public class TypeNameParser {
|
---|
| 66 |
|
---|
| 67 | /*
|
---|
| 68 | TypeSpec := SimpleTypeSpec '&'?
|
---|
| 69 |
|
---|
| 70 | SimpleTypeSpec := (IDENTIFIER '.')*
|
---|
| 71 | (IDENTIFIER '+')*
|
---|
| 72 | IDENTIFIER
|
---|
| 73 | ( '`\d+[' Generics ']' )?
|
---|
| 74 | (\*|\[(\d+\.\.\d+|\d+\.\.\.|(|\*)(,(|\*))*)\])*
|
---|
| 75 | (',\s*' IDENTIFIER (',\s*' AssemblyProperty)* )?
|
---|
| 76 |
|
---|
| 77 | Generics := '[' SimpleTypeSpec ']' (',[' SimpleTypeSpec ']')
|
---|
| 78 |
|
---|
| 79 | AssemblyProperty := 'Version=' Version
|
---|
| 80 | | 'PublicKey(Token)?=[a-fA-F0-9]+'
|
---|
| 81 | | 'Culture=[a-zA-F0-9]+'
|
---|
| 82 |
|
---|
[2859] | 83 | Version := \d+\.\d+\.\d+\.\d+
|
---|
[1776] | 84 |
|
---|
[2737] | 85 | IDENTIFIER = [_a-zA-Z][_a-ZA-Z0-9]*
|
---|
[1776] | 86 | */
|
---|
| 87 |
|
---|
| 88 |
|
---|
[3004] | 89 | private class Token {
|
---|
[1776] | 90 | private static Dictionary<string, string> tokens =
|
---|
| 91 | new Dictionary<string, string> {
|
---|
[2737] | 92 | {"-", "DASH"},
|
---|
[1776] | 93 | {"&", "AMPERSAND"},
|
---|
| 94 | {".", "DOT"},
|
---|
| 95 | {"+", "PLUS"},
|
---|
| 96 | {",", "COMMA"},
|
---|
| 97 | {"[", "OPEN_BRACKET"},
|
---|
| 98 | {"]", "CLOSE_BRACKET"},
|
---|
| 99 | {"*", "ASTERISK"},
|
---|
| 100 | {" ", "SPACE"},
|
---|
| 101 | {"=", "EQUALS"},
|
---|
| 102 | {"`", "BACKTICK"} };
|
---|
| 103 | private static Regex NumberRegex = new Regex("^\\d+$");
|
---|
[2859] | 104 | private static Regex IdentifierRegex = new Regex("^[_a-zA-Z][_a-zA-Z0-9]*$");
|
---|
| 105 | private static Regex TokenRegex = new Regex("[-&.+,\\[\\]* =`]|[a-f0-9]+|\\d+|[_a-zA-Z][_a-zA-Z0-9]*");
|
---|
[1776] | 106 | public string Name { get; private set; }
|
---|
| 107 | public string Value { get; private set; }
|
---|
[2859] | 108 | public bool IsIdentifier { get; private set; }
|
---|
[1776] | 109 | public int? Number { get; private set; }
|
---|
[1779] | 110 | public int Position { get; private set; }
|
---|
| 111 | private Token(string value, int pos) {
|
---|
| 112 | Position = pos;
|
---|
[1776] | 113 | if (tokens.ContainsKey(value)) {
|
---|
| 114 | Name = tokens[value];
|
---|
| 115 | } else if (NumberRegex.IsMatch(value)) {
|
---|
| 116 | Number = int.Parse(value);
|
---|
| 117 | } else {
|
---|
| 118 | Value = value;
|
---|
[2859] | 119 | IsIdentifier = IdentifierRegex.IsMatch(value);
|
---|
[1776] | 120 | }
|
---|
| 121 | }
|
---|
[4068] | 122 | public static IEnumerable<Token> Tokenize(string s) {
|
---|
[1779] | 123 | int pos = 0;
|
---|
[1776] | 124 | foreach (Match m in TokenRegex.Matches(s)) {
|
---|
[1779] | 125 | yield return new Token(m.Value, pos);
|
---|
| 126 | pos += m.Length;
|
---|
[1776] | 127 | }
|
---|
| 128 | }
|
---|
| 129 | public override string ToString() {
|
---|
| 130 | if (Name != null)
|
---|
| 131 | return "Token(" + Name + ")";
|
---|
| 132 | if (Value != null)
|
---|
| 133 | return "\"" + Value + "\"";
|
---|
| 134 | if (Number != null)
|
---|
| 135 | return Number.ToString();
|
---|
| 136 | return "<empty>";
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[1779] | 140 | private Queue<Token> tokens;
|
---|
| 141 | private int Position {
|
---|
| 142 | get {
|
---|
| 143 | if (tokens.Count == 0)
|
---|
| 144 | return -1;
|
---|
| 145 | else
|
---|
| 146 | return tokens.Peek().Position;
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
[1776] | 149 |
|
---|
| 150 | private TypeNameParser(string s) {
|
---|
| 151 | tokens = new Queue<Token>(Token.Tokenize(s));
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[3016] | 154 | /// <summary>
|
---|
| 155 | /// Parses the specified typename string as obtained by
|
---|
| 156 | /// <c>System.Object.GetType().FullName"</c>.
|
---|
| 157 | /// </summary>
|
---|
| 158 | /// <param name="s">The typename string.</param>
|
---|
| 159 | /// <returns>A <see cref="TypeName"/> representing the type name.</returns>
|
---|
[1779] | 160 | public static TypeName Parse(string s) {
|
---|
[1776] | 161 | TypeNameParser p = new TypeNameParser(s);
|
---|
[1779] | 162 | try {
|
---|
| 163 | return p.TransformTypeSpec();
|
---|
[4068] | 164 | }
|
---|
| 165 | catch (ParseError x) {
|
---|
[1779] | 166 | if (p.Position > 0)
|
---|
| 167 | throw new ParseError(String.Format(
|
---|
| 168 | "Could not parse typename: {0}\n\"{1}====>{2}<===={3}",
|
---|
| 169 | x.Message,
|
---|
| 170 | s.Substring(0, p.Position),
|
---|
| 171 | s[p.Position],
|
---|
| 172 | s.Substring(p.Position, s.Length - p.Position)));
|
---|
| 173 | else
|
---|
| 174 | throw new ParseError(String.Format(
|
---|
| 175 | "Could not parse typenname \"{0}\" at end of input: {1}",
|
---|
| 176 | s,
|
---|
| 177 | x.Message));
|
---|
| 178 | }
|
---|
[1776] | 179 | }
|
---|
| 180 |
|
---|
[1779] | 181 | private TypeName TransformTypeSpec() {
|
---|
| 182 | TypeName t = TransformSimpleTypeSpec();
|
---|
| 183 | t.IsReference = ConsumeToken("AMPERSAND");
|
---|
| 184 | return t;
|
---|
[1776] | 185 | }
|
---|
| 186 |
|
---|
[1779] | 187 | private TypeName TransformSimpleTypeSpec() {
|
---|
[1776] | 188 | List<string> nameSpace = new List<string>();
|
---|
| 189 | nameSpace.Add(ConsumeIdentifier());
|
---|
| 190 | while (ConsumeToken("DOT"))
|
---|
| 191 | nameSpace.Add(ConsumeIdentifier());
|
---|
[1779] | 192 | List<string> className = new List<string>();
|
---|
[1776] | 193 | if (nameSpace.Count > 0) {
|
---|
[1779] | 194 | className.Add(nameSpace[nameSpace.Count - 1]);
|
---|
[1776] | 195 | nameSpace.RemoveAt(nameSpace.Count - 1);
|
---|
| 196 | }
|
---|
| 197 | while (ConsumeToken("PLUS"))
|
---|
[1779] | 198 | className.Add(ConsumeIdentifier());
|
---|
| 199 | TypeName typeName = new TypeName(
|
---|
| 200 | string.Join(".", nameSpace.ToArray()),
|
---|
| 201 | string.Join("+", className.ToArray()));
|
---|
[1776] | 202 | if (ConsumeToken("BACKTICK")) {
|
---|
[1779] | 203 | int nGenericArgs = ConsumeNumber();
|
---|
| 204 | if (ConsumeToken("OPEN_BRACKET") &&
|
---|
| 205 | CanConsumeToken("OPEN_BRACKET")) {
|
---|
| 206 | typeName.GenericArgs.AddRange(TransformGenerics());
|
---|
| 207 | ConsumeToken("CLOSE_BRACKET", true);
|
---|
| 208 | }
|
---|
[1776] | 209 | }
|
---|
| 210 | StringBuilder pointerOrArray = new StringBuilder();
|
---|
| 211 | while (true) {
|
---|
| 212 | if (ConsumeToken("ASTERSIK")) {
|
---|
| 213 | pointerOrArray.Append("*");
|
---|
| 214 | } else if (ConsumeToken("OPEN_BRACKET")) {
|
---|
[1779] | 215 | pointerOrArray.Append('[');
|
---|
[1776] | 216 | ParseDimension(pointerOrArray);
|
---|
| 217 | while (ConsumeToken("COMMA")) {
|
---|
| 218 | pointerOrArray.Append(",");
|
---|
| 219 | ParseDimension(pointerOrArray);
|
---|
| 220 | }
|
---|
| 221 | ConsumeToken("CLOSE_BRACKET", true);
|
---|
[1779] | 222 | pointerOrArray.Append(']');
|
---|
[1776] | 223 | } else {
|
---|
| 224 | break;
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
[1779] | 227 | typeName.MemoryMagic = pointerOrArray.ToString();
|
---|
[1776] | 228 | if (ConsumeComma()) {
|
---|
[1779] | 229 | StringBuilder sb = new StringBuilder();
|
---|
| 230 | sb.Append(ConsumeIdentifier());
|
---|
| 231 | while (CanConsumeToken("DOT") ||
|
---|
| 232 | CanConsumeToken("DASH") ||
|
---|
| 233 | CanConsumeNumber() ||
|
---|
| 234 | CanConsumeIdentifier()) {
|
---|
| 235 | if (ConsumeToken("DOT"))
|
---|
| 236 | sb.Append('.');
|
---|
| 237 | else if (ConsumeToken("DASH"))
|
---|
| 238 | sb.Append('-');
|
---|
| 239 | else if (CanConsumeNumber())
|
---|
| 240 | sb.Append(ConsumeNumber());
|
---|
| 241 | else
|
---|
| 242 | sb.Append(ConsumeIdentifier());
|
---|
| 243 | }
|
---|
| 244 | typeName.AssemblyName = sb.ToString();
|
---|
[1776] | 245 | while (ConsumeComma()) {
|
---|
[1779] | 246 | KeyValuePair<string, string> property =
|
---|
| 247 | TransformAssemblyProperty();
|
---|
[6210] | 248 | typeName.AssemblyAttributes.Add(property.Key, property.Value);
|
---|
[1776] | 249 | }
|
---|
| 250 | }
|
---|
[1779] | 251 | return typeName;
|
---|
[1776] | 252 | }
|
---|
| 253 |
|
---|
| 254 | private void ParseDimension(StringBuilder sb) {
|
---|
| 255 | if (ConsumeToken("ASTERSIK")) {
|
---|
| 256 | sb.Append("*");
|
---|
| 257 | } else if (CanConsumeNumber()) {
|
---|
| 258 | sb.Append(ConsumeNumber());
|
---|
| 259 | ConsumeToken("DOT", true);
|
---|
| 260 | ConsumeToken("DOT", true);
|
---|
| 261 | if (ConsumeToken("DOT")) {
|
---|
| 262 | sb.Append("...");
|
---|
| 263 | } else {
|
---|
| 264 | sb.Append("..").Append(ConsumeNumber());
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[1779] | 269 | private IEnumerable<TypeName> TransformGenerics() {
|
---|
[1776] | 270 | ConsumeToken("OPEN_BRACKET", true);
|
---|
[1779] | 271 | yield return TransformSimpleTypeSpec();
|
---|
[1776] | 272 | ConsumeToken("CLOSE_BRACKET", true);
|
---|
| 273 | while (ConsumeToken("COMMA")) {
|
---|
| 274 | ConsumeToken("OPEN_BRACKET", true);
|
---|
[1779] | 275 | yield return TransformSimpleTypeSpec();
|
---|
[1776] | 276 | ConsumeToken("CLOSE_BRACKET", true);
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[1779] | 280 | private KeyValuePair<string, string> TransformAssemblyProperty() {
|
---|
[1776] | 281 | if (ConsumeIdentifier("Version")) {
|
---|
| 282 | ConsumeToken("EQUALS", true);
|
---|
[1779] | 283 | return new KeyValuePair<string, string>(
|
---|
| 284 | "Version",
|
---|
| 285 | TransformVersion());
|
---|
[1776] | 286 | } else if (ConsumeIdentifier("PublicKey")) {
|
---|
[1795] | 287 | return ConsumeAssignment("PublicKey");
|
---|
[1776] | 288 | } else if (ConsumeIdentifier("PublicKeyToken")) {
|
---|
[1795] | 289 | return ConsumeAssignment("PublicKeyToken");
|
---|
[1776] | 290 | } else if (ConsumeIdentifier("Culture")) {
|
---|
[1795] | 291 | return ConsumeAssignment("Culture");
|
---|
[1776] | 292 | } else if (ConsumeIdentifier("Custom")) {
|
---|
[1795] | 293 | return ConsumeAssignment("Custom");
|
---|
[1776] | 294 | } else {
|
---|
| 295 | throw new ParseError(String.Format(
|
---|
| 296 | "Invalid assembly property \"{0}\"",
|
---|
| 297 | tokens.Peek().ToString()));
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
[1795] | 300 |
|
---|
| 301 | private KeyValuePair<string, string> ConsumeAssignment(string name) {
|
---|
| 302 | ConsumeToken("EQUALS", true);
|
---|
[2859] | 303 | return new KeyValuePair<string, string>(name, ConsumeToken());
|
---|
[1795] | 304 | }
|
---|
| 305 |
|
---|
[1776] | 306 | private string TransformVersion() {
|
---|
[1779] | 307 | StringBuilder version = new StringBuilder();
|
---|
| 308 | version.Append(ConsumeNumber());
|
---|
[1776] | 309 | ConsumeToken("DOT");
|
---|
[1779] | 310 | version.Append('.').Append(ConsumeNumber());
|
---|
[1776] | 311 | ConsumeToken("DOT");
|
---|
[1779] | 312 | version.Append('.').Append(ConsumeNumber());
|
---|
[1776] | 313 | ConsumeToken("DOT");
|
---|
[1779] | 314 | version.Append('.').Append(ConsumeNumber());
|
---|
| 315 | return version.ToString();
|
---|
[1776] | 316 | }
|
---|
| 317 |
|
---|
| 318 | private bool CanConsumeNumber() {
|
---|
| 319 | if (tokens.Count == 0)
|
---|
| 320 | return false;
|
---|
| 321 | return tokens.Peek().Number != null;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | private int ConsumeNumber() {
|
---|
| 325 | if (tokens.Count == 0)
|
---|
| 326 | throw new ParseError("End of input while expecting number");
|
---|
| 327 | if (tokens.Peek().Number != null)
|
---|
| 328 | return (int)tokens.Dequeue().Number;
|
---|
| 329 | throw new ParseError(string.Format(
|
---|
| 330 | "Number expected, found \"{0}\" instead",
|
---|
| 331 | tokens.Peek().ToString()));
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | private bool ConsumeIdentifier(string value) {
|
---|
| 335 | if (tokens.Count == 0)
|
---|
| 336 | return false;
|
---|
[2859] | 337 | if (tokens.Peek().Value == value && tokens.Peek().IsIdentifier) {
|
---|
[1776] | 338 | tokens.Dequeue();
|
---|
| 339 | return true;
|
---|
| 340 | } else {
|
---|
| 341 | return false;
|
---|
| 342 | }
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[1779] | 345 | private bool CanConsumeIdentifier() {
|
---|
| 346 | return tokens.Count > 0 && tokens.Peek().Value != null;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
[1776] | 349 | private string ConsumeIdentifier() {
|
---|
| 350 | if (tokens.Count == 0)
|
---|
| 351 | throw new ParseError("End of input while expecting identifier");
|
---|
[2859] | 352 | if (tokens.Peek().Value != null && tokens.Peek().IsIdentifier)
|
---|
[1776] | 353 | return tokens.Dequeue().Value;
|
---|
| 354 | throw new ParseError(String.Format(
|
---|
| 355 | "Identifier expected, found \"{0}\" instead",
|
---|
| 356 | tokens.Peek().ToString()));
|
---|
| 357 | }
|
---|
| 358 |
|
---|
[2859] | 359 | private string ConsumeToken() {
|
---|
| 360 | if (tokens.Count == 0)
|
---|
| 361 | throw new ParseError("End of input while expecting token");
|
---|
| 362 | if (tokens.Peek().Value != null)
|
---|
| 363 | return tokens.Dequeue().Value;
|
---|
| 364 | throw new ParseError(String.Format(
|
---|
| 365 | "Token expected, found \"{0}\" instead",
|
---|
| 366 | tokens.Peek().ToString()));
|
---|
| 367 | }
|
---|
| 368 |
|
---|
[1776] | 369 | private bool ConsumeComma() {
|
---|
| 370 | if (ConsumeToken("COMMA")) {
|
---|
| 371 | while (ConsumeToken("SPACE")) ;
|
---|
| 372 | return true;
|
---|
| 373 | } else {
|
---|
| 374 | return false;
|
---|
| 375 | }
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | private bool ConsumeToken(string name) {
|
---|
| 379 | return ConsumeToken(name, false);
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[1779] | 382 | private bool CanConsumeToken(string name) {
|
---|
| 383 | if (tokens.Count == 0)
|
---|
| 384 | return false;
|
---|
| 385 | if (tokens.Peek().Name == name)
|
---|
| 386 | return true;
|
---|
| 387 | return false;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
[1776] | 390 | private bool ConsumeToken(string name, bool force) {
|
---|
| 391 | if (tokens.Count == 0)
|
---|
| 392 | if (force)
|
---|
| 393 | throw new ParseError(String.Format(
|
---|
| 394 | "end of input while expecting token \"{0}\"",
|
---|
| 395 | name));
|
---|
| 396 | else
|
---|
| 397 | return false;
|
---|
| 398 | if (tokens.Peek().Name == name) {
|
---|
| 399 | tokens.Dequeue();
|
---|
| 400 | return true;
|
---|
| 401 | } else if (force) {
|
---|
| 402 | throw new ParseError(String.Format(
|
---|
| 403 | "expected \"{0}\" found \"{1}\"",
|
---|
| 404 | name,
|
---|
| 405 | tokens.Peek().ToString()));
|
---|
| 406 | } else {
|
---|
| 407 | return false;
|
---|
| 408 | }
|
---|
| 409 | }
|
---|
| 410 | }
|
---|
[1703] | 411 | } |
---|