Welcome to mirror list, hosted at ThFree Co, Russian Federation.

StringHelper.cs « scanner « ilasm « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e1e13e02b07325a4958d7c90a96a35cf1bde83ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// StringHelper.cs
// Author: Sergey Chaban (serge@wildwestsoftware.com)

using System;
using System.Text;

namespace Mono.ILASM {

	/// <summary>
	/// </summary>
	internal class StringHelper : StringHelperBase {

		private static readonly string idChars = "_$@?`";

		/// <summary>
		/// </summary>
		/// <param name="host"></param>
		public StringHelper (ILTokenizer host) : base (host)
		{
		}


		/// <summary>
		/// </summary>
		/// <returns></returns>
		public override bool Start (char ch)
		{
			mode = Token.UNKNOWN;

			if (Char.IsLetter (ch) || idChars.IndexOf (ch) != -1) {
				mode = Token.ID;
			} else if (ch == '\'') {
				mode = Token.SQSTRING;
			} else if (ch == '"') {
				mode = Token.QSTRING;
			}

			return (mode != Token.UNKNOWN);
		}


		private static bool IsIdChar (int c)
		{
			char ch = (char) c;
			return (Char.IsLetterOrDigit(ch) || idChars.IndexOf (ch) != -1);
		}

		/// <summary>
		/// </summary>
		/// <returns></returns>
		public override string Build ()
		{
			if (mode == Token.UNKNOWN) return String.Empty;
			int ch = 0;

			ILReader reader = host.Reader;

			StringBuilder idsb = new StringBuilder ();
			if (mode == Token.SQSTRING || mode == Token.QSTRING) {
				int term = (mode == Token.SQSTRING) ? '\'' : '"';
				reader.Read (); // skip quote
				for (ch = reader.Read (); ch != -1; ch = reader.Read ()) {
					if (ch == term) {
						break;
					}

					if (ch == '\\') {
						ch = reader.Read ();

						/*
						 * Long string can be broken across multiple lines
						 * by using '\' as the last char in line.
						 * Any white space chars between '\' and the first
						 * char on the next line are ignored.
						 */
						if (ch == '\n') {
							reader.SkipWhitespace ();
							continue;
						}

						int escaped = Escape (ch);
						if (escaped == -1) {
                                                        reader.Unread (ch);
                                                        ch = '\\';
                                                } else {
                                                        ch = escaped;
						}
					}

					idsb.Append((char)ch);
				}
			} else { // ID
				while ((ch = reader.Read ()) != -1) {
					if (IsIdChar (ch)) {
						idsb.Append ((char) ch);
					} else {
						reader.Unread (ch);
						break;
					}
				}
			}
			return idsb.ToString ();
		}




		/// <summary>
		/// </summary>
		/// <param name="ch"></param>
		/// <returns></returns>
		public static int Escape (int ch)
		{
			int res = -1;

			if (ch >= '0' && ch <='7') {
				//TODO : octal code
			} else {
				int id = "abfnrtv\"'\\".IndexOf ((char)ch);
				if (id != -1) {
					res = "\a\b\f\n\r\t\v\"'\\" [id];
				}
			}

			return res;
		}

	}


}