public ElementToken(int index, RangeList rangeList) : base(rangeList) { _startIndex = index; _rangeList = rangeList; Tokens = new LinkedList<ParseToken>(); Attributes = new Dictionary<string, string>(); }
public static RootToken Parse(string text) { var root = XElement.Parse(text, LoadOptions.PreserveWhitespace); var rlist = new RangeList(root.Value); var rn = new RootToken(root, rlist); //ParamParser.Parse(rn); RemoveWhitespaces(rn, rlist); RemoveFormating(rn); return rn; }
static ParseToken Create(XNode node, int index, RangeList list) { if (node is XText) { var len = ((XText)node).Value.Length; return new TextToken(index, len, list); } if (node is XElement) { var el = (XElement)node; var en = new ElementToken(index, list); ParseElement(en, el, index, list); return en; } throw new NotSupportedException(node.NodeType.ToString()); }
static void ParseElement(ElementToken en, XElement el, int index, RangeList list) { // parse name & attributes en.Name = el.Name.ToString(); foreach (var attr in el.Attributes()) { en.Attributes[attr.Name.ToString()] = attr.Value; } // parse nodes foreach (var node in el.Nodes()) { var pn = Create(node, index, list); en.Tokens.AddLast(pn); index += pn.Length; } }
static void ClearParamsInPre(ElementToken token, RangeList list) { foreach (var node in token.Tokens) { if (!(node is ElementToken)) continue; var el = (ElementToken)node; if (el.Name != "params") { ClearParamsInPre(el, list); } else { var i1 = el.StartIndex; var i2 = el.EndIndex; // clear range ClearRange(i1, i2, list); } } }
static void ClearRange(int i1, int i2, RangeList list) { if (i1 == i2) return; var str = list.GetText(i1, i2 - i1); // find seq-s of whitespace var regex = new Regex("\\s+"); var ms = regex.Matches(str); foreach (Match m in ms) { var j1 = i1 + m.Index; var j2 = j1 + m.Length; list.Chars[j1] = ' '; for (var i = j1 + 1; i < j2; i++) { list.Chars[i] = '\0'; } } }
static int Clear(int i1, ElementToken root, RangeList list) { foreach (var node in root.Tokens) { var i2 = node.StartIndex; if (!(node is ElementToken)) continue; var el = (ElementToken)node; if (el.Name != "pre") { i1 = Clear(i1, el, list); } else { ClearParamsInPre(el, list); // clear range ClearRange(i1, i2, list); i1 = el.EndIndex; } } return i1; }
static void RemoveWhitespaces(RootToken rt, RangeList list) { //var i1 = Clear(rt.StartIndex, rt, list); //ClearRange(i1, rt.EndIndex, list); }
private TextToken(RangeList rangeList) : base(rangeList) { }
public TextToken(int index, int len, RangeList rangeList) : base(rangeList) { Range = _rangeList.AddRange(index, len); }
public RootToken(XElement root, RangeList rangeList) : base(0, rangeList) { ParseElement(this, root, 0, _rangeList); }
protected ParseToken(RangeList rangeList) { _rangeList = rangeList; Tokens = new LinkedList<ParseToken>(); }
protected ParseToken(RangeList rangeList) { _rangeList = rangeList; Tokens = new LinkedList <ParseToken>(); }