1 | package com.framsticks.framclipse.internal.parser;
|
---|
2 |
|
---|
3 | import org.eclipse.jface.text.IDocument;
|
---|
4 |
|
---|
5 | public abstract class ElementWithOffset extends SimpleNode
|
---|
6 | {
|
---|
7 |
|
---|
8 | private int beginOffset;
|
---|
9 | private int endOffset;
|
---|
10 |
|
---|
11 | public ElementWithOffset(int i) {
|
---|
12 | super(i);
|
---|
13 | }
|
---|
14 |
|
---|
15 |
|
---|
16 | public ElementWithOffset(FramclipseNonScriptParser p, int i) {
|
---|
17 | super(p, i);
|
---|
18 | }
|
---|
19 |
|
---|
20 |
|
---|
21 | public int getBeginOffset() {
|
---|
22 | return beginOffset;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public void setBeginOffset(int offset) {
|
---|
26 | this.beginOffset = offset;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public int getEndOffset() {
|
---|
30 | return endOffset;
|
---|
31 | }
|
---|
32 |
|
---|
33 | public void setEndOffset(int offset) {
|
---|
34 | this.endOffset = offset;
|
---|
35 | }
|
---|
36 |
|
---|
37 | static boolean withinInterval(int val, int loInclusive, int hiInclusive)
|
---|
38 | {
|
---|
39 | if(val >= loInclusive && val <= hiInclusive)
|
---|
40 | return true;
|
---|
41 | return false;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public Node getElementForOffset(int offset)
|
---|
45 | {
|
---|
46 | if(offset < beginOffset || offset > endOffset)
|
---|
47 | return null;
|
---|
48 |
|
---|
49 | if(children != null)
|
---|
50 | {
|
---|
51 | for(int k = 0; k < children.length; ++k)
|
---|
52 | {
|
---|
53 | if(children[k] instanceof ElementWithOffset)
|
---|
54 | {
|
---|
55 | ElementWithOffset child = (ElementWithOffset)children[k];
|
---|
56 | if(withinInterval(offset, child.beginOffset, child.endOffset))
|
---|
57 | return child.getElementForOffset(offset);
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | return this;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public boolean isEquivalent(ElementWithOffset element, IDocument document)
|
---|
66 | {
|
---|
67 | if(!this.getClass().toString().equals(element.getClass().toString()))
|
---|
68 | return false;
|
---|
69 |
|
---|
70 | if(jjtGetNumChildren() != element.jjtGetNumChildren())
|
---|
71 | return false;
|
---|
72 |
|
---|
73 | if(getEndOffset() - getBeginOffset() != element.getEndOffset() - element.getBeginOffset())
|
---|
74 | return false;
|
---|
75 |
|
---|
76 | if(children != null)
|
---|
77 | for(int k = 0; k < children.length; ++k)
|
---|
78 | if(children[k] instanceof ElementWithOffset)
|
---|
79 | if(!(element.children[k] instanceof ElementWithOffset) ||
|
---|
80 | !((ElementWithOffset)children[k]).isEquivalent((ElementWithOffset)element.children[k], document))
|
---|
81 | return false;
|
---|
82 |
|
---|
83 | return true;
|
---|
84 | }
|
---|
85 |
|
---|
86 | }
|
---|