blob: 86eb1e24520151b51d7714e74cf2132cf3e08904 (
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
|
// Copyright (c) 2013-2021 Bruyère Jean-Philippe <jp_bruyere@hotmail.com>
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
using System;
using Crow.Text;
using System.Collections.Generic;
using System.Diagnostics;
namespace Crow
{
public class XmlSource {
public readonly string Source;
Token[] tokens;
SyntaxNode RootNode;
public Token[] Tokens => tokens;
public XmlSource (string _source) {
Source = _source;
Tokenizer tokenizer = new Tokenizer();
tokens = tokenizer.Tokenize (Source);
SyntaxAnalyser syntaxAnalyser = new SyntaxAnalyser (this);
Stopwatch sw = Stopwatch.StartNew ();
syntaxAnalyser.Process ();
sw.Stop();
/*foreach (Token t in Tokens)
Console.WriteLine ($"{t,-40} {Source.AsSpan(t.Start, t.Length).ToString()}");
syntaxAnalyser.Root.Dump();*/
Console.WriteLine ($"Syntax Analysis done in {sw.ElapsedMilliseconds}(ms) {sw.ElapsedTicks}(ticks)");
foreach (SyntaxException ex in syntaxAnalyser.Exceptions)
Console.WriteLine ($"{ex}");
RootNode = syntaxAnalyser.Root;
}
public Token FindTokenIncludingPosition (int pos) {
if (pos == 0 || tokens == null || tokens.Length == 0)
return default;
int idx = Array.BinarySearch (tokens, 0, tokens.Length, new Token () {Start = pos});
return idx == 0 ? tokens[0] : idx < 0 ? tokens[~idx - 1] : tokens[idx - 1];
}
public SyntaxNode FindNodeIncludingPosition (int pos) {
if (RootNode == null)
return null;
if (!RootNode.Contains (pos))
return null;
return RootNode.FindNodeIncludingPosition (pos);
}
public T FindNodeIncludingPosition<T> (int pos) {
if (RootNode == null)
return default;
if (!RootNode.Contains (pos))
return default;
return RootNode.FindNodeIncludingPosition<T> (pos);
}
}
}
|