1 using System;
2 using System.Collections;
3 using System.IO;
4
5 namespace soulcm.scm2html
6 {
7 public string MakeFilePath(const string& fileName, const System.Lex.Token& token)
8 {
9 ustring s;
10 const uchar* p = token.match.begin;
11 const uchar* e = token.match.end;
12 if (p != e && *p == '<')
13 {
14 ++p;
15 }
16 while (p != e && *p != '>')
17 {
18 s.Append(*p, 1);
19 ++p;
20 }
21 if (p != e && *p == '>')
22 {
23 ++p;
24 }
25 if (p != e)
26 {
27 throw Exception("invalid file path at " + fileName + ":" + ToString(token.line) + " : \'" + ToUtf8(token.match.ToString()) + "\'");
28 }
29 return ToUtf8(s);
30 }
31
32 public ustring ParseStringLiteral(const string& fileName, const System.Lex.Token& token)
33 {
34 ustring stringLiteral;
35 const uchar* p = token.match.begin;
36 const uchar* e = token.match.end;
37 if (p != e && *p == '\"')
38 {
39 ++p;
40 while (p != e && *p != '\r' && *p != '\n' && *p != '\"')
41 {
42 if (*p == '\\')
43 {
44 ++p;
45 stringLiteral.Append(ParseEscape(fileName, p, e, token), 1);
46 }
47 else
48 {
49 stringLiteral.Append(*p, 1);
50 ++p;
51 }
52 }
53 if (p != e && *p == '\"')
54 {
55 ++p;
56 }
57 if (p != e)
58 {
59 throw Exception("invalid string literal at " + fileName + ":" + ToString(token.line) + ": " + ToUtf8(token.match.ToString()));
60 }
61 }
62 else
63 {
64 throw Exception("invalid string literal at " + fileName + ":" + ToString(token.line) + ": " + ToUtf8(token.match.ToString()));
65 }
66 return stringLiteral;
67 }
68
69 public uchar ParseCharLiteral(const string& fileName, const System.Lex.Token& token)
70 {
71 uchar charLit = '\0';
72 const uchar* p = token.match.begin;
73 const uchar* e = token.match.end;
74 bool first = true;
75 if (p != e && *p == '\'')
76 {
77 ++p;
78 while (p != e && *p != '\r' && *p != '\n' && *p != '\'')
79 {
80 if (*p == '\\')
81 {
82 ++p;
83 if (first)
84 {
85 charLit = ParseEscape(fileName, p, e, token);
86 first = false;
87 }
88 else
89 {
90 throw Exception("invalid character literal at " + fileName + ":" + ToString(token.line) + ": " + ToUtf8(token.match.ToString()));
91 }
92 }
93 else
94 {
95 if (first)
96 {
97 charLit = *p;
98 first = false;
99 }
100 else
101 {
102 throw Exception("invalid character literal at " + fileName + ":" + ToString(token.line) + ": " + ToUtf8(token.match.ToString()));
103 }
104 ++p;
105 }
106 }
107 if (p != e && *p == '\'')
108 {
109 ++p;
110 }
111 if (p != e)
112 {
113 throw Exception("invalid character literal at " + fileName + ":" + ToString(token.line) + ": " + ToUtf8(token.match.ToString()));
114 }
115 }
116 else
117 {
118 throw Exception("invalid character literal at " + fileName + ":" + ToString(token.line) + ": " + ToUtf8(token.match.ToString()));
119 }
120 return charLit;
121 }
122
123 public CharSet ParseCharSet(const string& fileName, const System.Lex.Token& token, const ustring& str)
124 {
125 CharSet set;
126 const uchar* p = str.Chars();
127 const uchar* e = str.Chars() + str.Length();
128 bool inverse = false;
129 if (p != e && *p == '[')
130 {
131 ++p;
132 if (p != e && *p == '^')
133 {
134 set.SetInverse();
135 ++p;
136 }
137 while (p != e - 1)
138 {
139 uchar first = *p;
140 ++p;
141 uchar last = first;
142 if (p != e - 1)
143 {
144 if (*p == '-')
145 {
146 ++p;
147 if (p != e - 1)
148 {
149 last = *p;
150 ++p;
151 }
152 else
153 {
154 System.Parsing.Range range;
155 range.first = cast<int>(first);
156 range.last = cast<int>(first);
157 set.AddRange(range);
158 first = '-';
159 last = '-';
160 }
161 }
162 }
163 System.Parsing.Range range;
164 range.first = cast<int>(first);
165 range.last = cast<int>(last);
166 set.AddRange(range);
167 }
168 if (p != e && *p == ']')
169 {
170 ++p;
171 }
172 if (p != e)
173 {
174 throw Exception("invalid character set literal at " + fileName + ":" + ToString(token.line) + ": " + ToUtf8(token.match.ToString()));
175 }
176 }
177 else
178 {
179 throw Exception("invalid character set literal at " + fileName + ":" + ToString(token.line) + ": " + ToUtf8(token.match.ToString()));
180 }
181 return set;
182 }
183 }