root/trunk/x42view.net/Helpers.TokenParser.cs

Revision 484, 1.6 kB (checked in by phill, 1 year ago)

o Can now load and play embedded animations or those from a ST style animation.cfg file.

Line 
1 using System;
2 using System.Collections.Generic;
3 using System.Text.RegularExpressions;
4
5 namespace x42view
6 {
7         partial class Helpers
8         {
9                 /// <summary>
10                 /// Parses Quake 3 style ParseExt-based scripts.
11                 /// </summary>
12                 public sealed class TokenParser : IEnumerable<string>
13                 {
14                         private static Regex rxCommentStripper = new Regex( @"(?://.*)|(?:/\*(?:(?:.|\r\n|\n)*?)\*/)", RegexOptions.Multiline | RegexOptions.Compiled );
15                         private static Regex rxTokenParser = new Regex( @"[\s\r\n]*((?:""(?'2'.*?)(?:""|\Z))|(?:(?'3'[^\s\r\n]+?)(?:[\x00-\x20]|\Z)))", RegexOptions.Multiline | RegexOptions.Compiled );
16
17                         private string text;
18
19                         public TokenParser( string text )
20                         {
21                                 if( text == null )
22                                         throw new ArgumentNullException( "text" );
23
24                                 this.text = StripComments( text );
25                         }
26
27                         public static string StripComments( string text )
28                         {
29                                 if( text == null )
30                                         throw new ArgumentNullException( "text" );
31
32                                 return rxCommentStripper.Replace( text, "" );
33                         }
34
35                         #region IEnumerable<string> Members
36
37                         public IEnumerator<string> GetEnumerator()
38                         {
39                                 for( Match m = rxTokenParser.Match( text );
40                                         m.Success; m = m.NextMatch() )
41                                 {
42                                         string s;
43
44                                         if( m.Groups[2].Value != string.Empty )
45                                                 s = m.Groups[2].Value;
46                                         else
47                                                 s = m.Groups[3].Value;
48
49                                         if( s[0] == '\0' )
50                                                 yield break;
51
52                                         yield return s;
53                                 }
54                         }
55
56                         #endregion
57
58                         #region IEnumerable Members
59
60                         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
61                         {
62                                 return GetEnumerator();
63                         }
64
65                         #endregion
66                 }
67         }
68 }
Note: See TracBrowser for help on using the browser.