aboutsummaryrefslogtreecommitdiffstatsci
path: root/CrowIDE/src/SourceEditor/XMLParser.cs
blob: 59601a75cfff25df296bd9aae5e87294ef392ca7 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using System;
using Crow;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Linq;

namespace Crow.Coding
{
	public class XMLParser : BufferParser
	{
		public new enum TokenType {
			Unknown = BufferParser.TokenType.Unknown,
			WhiteSpace = BufferParser.TokenType.WhiteSpace,
			NewLine = BufferParser.TokenType.NewLine,
			LineComment = BufferParser.TokenType.LineComment,
			BlockCommentStart = BufferParser.TokenType.BlockCommentStart,
			BlockComment = BufferParser.TokenType.BlockComment,
			BlockCommentEnd = BufferParser.TokenType.BlockCommentEnd,
			ElementName = BufferParser.TokenType.Type,
			AttributeName = BufferParser.TokenType.Identifier,
			ElementClosing = BufferParser.TokenType.StatementEnding,
			Affectation = BufferParser.TokenType.Affectation,
			AttributeValueOpening = BufferParser.TokenType.StringLitteralOpening,
			AttributeValueClosing = BufferParser.TokenType.StringLitteralClosing,
			AttributeValue = BufferParser.TokenType.StringLitteral,
			XMLDecl = BufferParser.TokenType.Preprocessor,
			ElementStart = 50,
			ElementEnd = 51,
		}

		public enum States
		{
			init,       //first statement of prolog, xmldecl should only apear in this state
			prolog,     //misc before doctypedecl
			InternalSubset,    //doctype declaration subset
			ExternalSubsetInit,
			ExternalSubset,
			BlockComment,
			DTDEnd,//doctype finished
			XML,//normal xml
			StartTag,//inside start tag
			Content,//after start tag with no closing slash
			EndTag
		}

		#region CTOR
		public XMLParser (CodeBuffer _buffer) : base(_buffer) {}
		#endregion

		enum Keywords
		{
			DOCTYPE,
			ELEMENT,
			ATTLIST,
			ENTITY,
			NOTATION
		}

		States curState = States.init;

		#region Regular Expression for validity checks
		//private static Regex rxValidChar = new Regex("[\u0020-\uD7FF]");
		private static Regex rxValidChar = new Regex(@"\u0009|\u000A|\u000D|[\u0020-\uD7FF]|[\uE000-\uFFFD]");   //| [\u10000-\u10FFFF] unable to set those plans
		private static Regex rxNameStartChar = new Regex(@":|[A-Z]|_|[a-z]|[\u00C0-\u00D6]|[\u00D8-\u00F6]|[\u00F8-\u02FF]|[\u0370-\u037D]|[\u037F-\u1FFF]|[\u200C-\u200D]|[\u2070-\u218F]|[\u2C00-\u2FEF]|[\u3001-\uD7FF]|[\uF900-\uFDCF]|[\uFDF0-\uFFFD]"); // | [\u10000-\uEFFFF]
		private static Regex rxNameChar = new Regex(@":|[A-Z]|_|[a-z]|[\u00C0-\u00D6]|[\u00D8-\u00F6]|[\u00F8-\u02FF]|[\u0370-\u037D]|[\u037F-\u1FFF]|[\u200C-\u200D]|[\u2070-\u218F]|[\u2C00-\u2FEF]|[\u3001-\uD7FF]|[\uF900-\uFDCF]|[\uFDF0-\uFFFD]|-|\.|[0-9]|\u00B7|[\u0300-\u036F]|[\u203F-\u2040]");//[\u10000-\uEFFFF]|
		private static Regex rxDecimal = new Regex(@"[0-9]+");
		private static Regex rxHexadecimal = new Regex(@"[0-9a-fA-F]+");
		private static Regex rxAttributeValue = new Regex(@"[^<]");
		private static Regex rxEntityValue = new Regex(@"[^<]");
		private static Regex rxPubidChar = new Regex(@"\u0020|\u000D|\u000A|[a-zA-Z0-9]|[-\(\)\+\,\./:=\?;!\*#@\$_%]");
		#endregion

		#region Character ValidityCheck
		public bool nextCharIsValidCharStartName
		{
			get { return rxNameStartChar.IsMatch(new string(new char[]{Peek()})); }
		}
		public bool nextCharIsValidCharName
		{
			get { return rxNameChar.IsMatch(new string(new char[]{Peek()})); }
		}
		#endregion

		public override void SetLineInError (ParserException ex)
		{
			base.SetLineInError (ex);
			//buffer[ex.Line].Tokens.EndingState = (int)States.init;
		}

		public override void ParseCurrentLine ()
		{
			Debug.WriteLine (string.Format("parsing line:{0}", currentLine));
			CodeLine cl = buffer [currentLine];
			cl.Tokens = new List<Token> ();

			//retrieve current parser state from previous line
			if (currentLine > 0)
				curState = (States)buffer[currentLine - 1].EndingState;
			else
				curState = States.init;

			States previousEndingState = (States)cl.EndingState;


			while (! eol) {
				SkipWhiteSpaces ();

				if (eol)
					break;

				if (Peek () == '\n') {
					if (currentTok != TokenType.Unknown)
						throw new ParserException (currentLine, currentColumn, "Unexpected end of line");
					Read ();
					eol = true;
					continue;
				}

				if (curState == States.BlockComment) {
					if (currentTok != TokenType.Unknown)
						Debugger.Break ();

					currentTok.Start = CurrentPosition;
					currentTok.Type = (BufferParser.TokenType)TokenType.BlockComment;
					currentTok += ReadLineUntil ("-->");
					if (Peek (3) == "-->") {
						readToCurrTok (3);
						curState = States.XML;
					}
					saveAndResetCurrentTok ();
					continue;
				}

				switch (Peek()) {
				case '<':
					readToCurrTok (true);
					switch (Peek()) {
					case '?':
						if (curState != States.init)
							throw new ParserException (currentLine, currentColumn, "xml decl may appear only on first line");
						readToCurrTok ();
						currentTok += ReadLineUntil ("?>");
						if (Peek (2) != "?>")
							throw new ParserException (currentLine, currentColumn, "expecting '?>'");
						readToCurrTok (2);
						saveAndResetCurrentTok (TokenType.XMLDecl);
						curState = States.prolog;
						break;
					case '!':
						readToCurrTok ();
						switch (Peek()) {
						case '-':
							readToCurrTok ();
							if (Peek () != '-')
								throw new ParserException (currentLine, currentColumn, "Expecting comment start tag");
							readToCurrTok ();
							currentTok += ReadLineUntil ("--");
							if (Peek (3) == "-->") {
								readToCurrTok (3);
							}else
								curState = States.BlockComment;
							saveAndResetCurrentTok (TokenType.BlockComment);
							break;
						default:
							throw new ParserException (currentLine, currentColumn, "error");
						}
						break;
					default:
						if (!(curState == States.Content || curState == States.XML || curState == States.init || curState == States.prolog))
							throw new ParserException (currentLine, currentColumn, "Unexpected char: '<'");
						if (Peek () == '/') {
							curState = States.EndTag;
							readToCurrTok ();
							saveAndResetCurrentTok (TokenType.ElementEnd);
						} else {
							curState = States.StartTag;
							saveAndResetCurrentTok (TokenType.ElementStart);
						}

						if (!nextCharIsValidCharStartName)
							throw new ParserException (currentLine, currentColumn, "Expected element name");

						readToCurrTok (true);
						while (nextCharIsValidCharName)
							readToCurrTok ();

						saveAndResetCurrentTok (TokenType.ElementName);
						break;
					}
					break;
				case '/':
					if (curState != States.StartTag)
						throw new ParserException (currentLine, currentColumn, "Unexpected char: '/'");
					readToCurrTok (true);
					if (Peek () != '>')
						throw new ParserException (currentLine, currentColumn, "Expecting '>'");
					readAndResetCurrentTok (TokenType.ElementEnd);

					curState = States.XML;
					break;
				case '>':
					readAndResetCurrentTok (TokenType.ElementClosing, true);
					switch (curState) {
					case States.EndTag:
						curState = States.XML;
						break;
					case States.StartTag:
						curState = States.Content;
						break;
					default:
						throw new ParserException (currentLine, currentColumn, "Unexpected char: '>'");
					}
					break;
				default:
					switch (curState) {
					case States.StartTag:
						if (!nextCharIsValidCharStartName)
							throw new ParserException (currentLine, currentColumn, "Expected attribute name");
						readToCurrTok (true);
						while (nextCharIsValidCharName)
							readToCurrTok ();
						saveAndResetCurrentTok (TokenType.AttributeName);

						SkipWhiteSpaces ();

						if (Peek () != '=')
							throw new ParserException (currentLine, currentColumn, "Expecting: '='");
						readAndResetCurrentTok (TokenType.Affectation, true);

						SkipWhiteSpaces ();

						char openAttVal = Peek ();
						if (openAttVal != '"' && openAttVal != '\'')
							throw new ParserException (currentLine, currentColumn, "Expecting attribute value enclosed either in '\"' or in \"'\"");
						readAndResetCurrentTok (TokenType.AttributeValueOpening, true);

						currentTok.Start = CurrentPosition;
						currentTok.Content = ReadLineUntil (new string (new char[]{ openAttVal }));
						saveAndResetCurrentTok (TokenType.AttributeValue);

						if (Peek () != openAttVal)
							throw new ParserException (currentLine, currentColumn, string.Format ("Expecting {0}", openAttVal));
						readAndResetCurrentTok (TokenType.AttributeValueClosing, true);
						break;
					default:
						throw new ParserException (currentLine, currentColumn, "unexpected char: " + Peek ());
					}
					break;
				}
			}

			if (cl.EndingState != (int)curState && currentLine < buffer.LineCount - 1)
				buffer [currentLine + 1].Tokens = null;

			cl.EndingState = (int)curState;
		}
		public override void SyntaxAnalysis ()
		{
			RootNode = new Node () { Name = "RootNode", Type="Root" };

			Node currentNode = RootNode;

			for (int i = 0; i < buffer.LineCount; i++) {
				CodeLine cl = buffer[i];
				if (cl.Tokens == null)
					continue;
				cl.SyntacticNode = null;

				int tokPtr = 0;
				while (tokPtr < cl.Tokens.Count) {
					switch ((XMLParser.TokenType)cl.Tokens [tokPtr].Type) {
					case TokenType.ElementStart:
						tokPtr++;
						Node newElt = new Node () { Name = cl.Tokens [tokPtr].Content, StartLine = cl };
						currentNode.AddChild (newElt);
						currentNode = newElt;
						if (cl.SyntacticNode == null)
							cl.SyntacticNode = newElt;
						break;
					case TokenType.ElementEnd:
						tokPtr++;
						if (tokPtr < cl.Tokens.Count) {
							if ((XMLParser.TokenType)cl.Tokens [tokPtr].Type == TokenType.ElementName &&
								cl.Tokens [tokPtr].Content != currentNode.Name)
								throw new ParserException (currentLine, currentColumn, "Closing tag mismatch");
						}
						currentNode.EndLine = cl;
						currentNode = currentNode.Parent;
						break;
					case TokenType.ElementClosing:
						//currentNode = currentNode.Parent;
						break;
					default:
						break;
					}
					tokPtr++;
				}
			}
		}
	}
}