1 package net.sf.plb4jedit.plb;
2
3 import java.util.Iterator;
4 import java.util.List;
5
6 import javax.swing.tree.DefaultMutableTreeNode;
7
8 import org.gjt.sp.jedit.Buffer;
9 import org.gjt.sp.jedit.EditPane;
10 import org.gjt.sp.util.Log;
11
12 import sidekick.SideKickCompletion;
13 import sidekick.SideKickParsedData;
14 import sidekick.SideKickParser;
15 import errorlist.DefaultErrorSource;
16
17 /***
18 * parses a PLB-source file and extends SideKickParser to view structure (I know there is no .... )
19 * in the StructureBrowser tree. <br/>
20 * The main parsing is done in PlbSource, which parses and gives access to the information we need:
21 * variables, labels, includes and a ResourceManager, which add the possibility to recursively look in
22 * all includes used by a program.
23 *
24 * TODO: port to SideKickPlugin v.0.3/jEdit 4.2 which supports finer integration of code-completion.
25 * @author Peter Schaefer
26 */
27 public class PlbParser extends SideKickParser {
28 /***
29 * constructs a PlbParser object.
30 * @param name
31 */
32 public PlbParser(String name) {
33 super(name);
34 }
35
36 // taken from super class
37 public boolean supportsCompletion() {
38 return false;
39 }
40
41 // taken from super class
42 public String getDelayCompletionTriggers() {
43 return "abcdefghijklmnopqrstuvwxyz#";
44 }
45
46 // taken from super class
47 public SideKickCompletion complete(EditPane editPane, int caret) {
48 Log.log(
49 Log.DEBUG,
50 this,
51 "complete("
52 + editPane.getTextArea().getCaretPosition()
53 + ","
54 + caret);
55 return new PlbCompletion(
56 editPane.getView(),
57 SideKickParsedData.getParsedData(editPane.getView()));
58 }
59
60 // taken from super class
61 public SideKickParsedData parse(
62 Buffer buffer,
63 DefaultErrorSource errorSource) {
64 Log.log(Log.DEBUG, this, "parse");
65 String[] text;
66 try {
67 buffer.readLock();
68
69 text = new String[buffer.getLineCount()];
70 for (int i = 0; i < buffer.getLineCount(); i++) {
71 text[i] = buffer.getLineText(i);
72 }
73 } finally {
74 buffer.readUnlock();
75 }
76
77 if (text.length == 0)
78 return new SideKickParsedData(buffer.getName());
79
80 PlbSource source = new PlbSource(text, buffer.getPath());
81 Log.log(Log.DEBUG, this, "build source name");
82 StringBuffer name = new StringBuffer(buffer.getName());
83 if (!source.isInclude()) {
84 name.append(" " + source.getVersion());
85 } else if (source.isIoInclude()) {
86 name.append(" (io)");
87 } else if (source.isRecordInclude()) {
88 name.append(" (rcd)");
89 } else if (source.isMiscInclude()) {
90 name.append(" (misc)");
91 }
92 Log.log(Log.DEBUG, this, "build parsed data");
93 PlbParsedData data = new PlbParsedData(name.toString());
94 data.setPlbSource(source);
95 // TODO which structure properly ???
96 List varList = source.getVariables();
97
98 List incList = source.getIncludes();
99
100 List labelList = source.getLabels();
101
102 int varStart;
103 int varEnd;
104 int incStart;
105 int incEnd;
106 int labelStart;
107 int labelEnd;
108 if (!source.isInclude()) {
109 varStart = 1;
110 varEnd = 1;
111 incStart = 2;
112 incEnd = 2;
113 labelStart = 3;
114 labelEnd = source.getNumberOfLines();
115 } else if (source.isRecordInclude()) {
116 varStart = 3;
117 varEnd = source.getNumberOfLines();
118 incStart = 1;
119 incEnd = 1;
120 labelStart = 2;
121 labelEnd = 2;
122 } else { // io or misc
123 varStart = 1;
124 varEnd = 1;
125 incStart = 2;
126 incEnd = 2;
127 labelStart = 3;
128 labelEnd = source.getNumberOfLines();
129 }
130
131 Log.log(Log.DEBUG, this, "build tree");
132
133 DefaultMutableTreeNode child =
134 new DefaultMutableTreeNode(
135 new PlbAsset(
136 "Vars (" + varList.size() + ")",
137 varStart,
138 varEnd));
139 data.root.add(child);
140 addList(child, varList);
141
142 child =
143 new DefaultMutableTreeNode(
144 new PlbAsset(
145 "Includes (" + incList.size() + ")",
146 incStart,
147 incEnd));
148 data.root.add(child);
149 addList(child, incList);
150
151 child =
152 new DefaultMutableTreeNode(
153 new PlbAsset(
154 "Labels (" + labelList.size() + ")",
155 labelStart,
156 labelEnd));
157 data.root.add(child);
158 addList(child, labelList);
159
160 return data;
161 }
162
163 private void addList(DefaultMutableTreeNode root, List list) {
164 for (Iterator it = list.iterator(); it.hasNext();) {
165 root.add(
166 new DefaultMutableTreeNode(
167 new PlbAsset((SourceLine) it.next())));
168 }
169 }
170
171 }
This page was automatically generated by Maven