Debian Codegraph @

plugins/symbolbrowser/symbols/parsers/parser_perl.cpp

raw symbols SCIP
1 /***************************************************************************
2 A SymbolBrowser Perl plugin for JuffEd editor.
3
4 Copyright: 2011 Aleksander Zaitsev <[email protected]>
5 Some of the code based on python parser by Alexander Sokoloff <[email protected]>
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License.
9 version 2 as published by the Free Software Foundation.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ***************************************************************************/
20
21 #ifndef PARSER_PERL
22 #define PARSER_PERL
23
24 #include <QDebug>
25 #include "../common.h"
26 #include "vstring.h"
27 #include "parser.h"
28 #include "../symbol.h"
29
30 //#define SHOW_IMPORTS
31 //#define SHOW_VARIABLES
32
33
34 /*****************************************************************************
35 *****************************************************************************/
36 class PerlSymbol: public Symbol {
37 public:
38 PerlSymbol(const Symbol::SymbolType type=SymbolUnknown, const QString &name="", Symbol *parent = NULL): Symbol(type, name, parent) {};
39 int indent() const { return mIndent; }
40 void setIndent(int value) { mIndent = value; };
41
42 private:
43 int mIndent;
44 };
45
46
47
48 class Parser_Perl: public Parser {
49
50 public:
51 Parser_Perl();
52
53 protected:
54 void parse();
55
56 private:
57 QList<PerlSymbol*> nestingSymbols;
58 PerlSymbol* getParent(int indent);
59 //void checkParent(int indent, const vString *parent);
60 //void addNestingSymbol(PerlSymbol *symbol);
61
62 bool isIdentifierFirstCharacter (char c) const;
63 bool isIdentifierCharacter (int c) const;
64
65 const char *skipString (const char *cp);
66 const char *skipEverything (const char *cp);
67 const char *skipIdentifier (const char *cp);
68 const char *findDefinitionOrClass (const char *cp);
69 const char *skipSpace (const char *cp);
70 const char *parseIdentifier (const char *cp, QString *identifier) const;
71
72 PerlSymbol* makeClass (const char *cp, Symbol *parent);
73 PerlSymbol* makeFunction (const char *cp, QString *def, Symbol *parent);
74
75 QString parseArgs();
76
77 //const char *findVariable(const char *line);
78 int calcIndent(const char* cp);
79 };
80
81
82 /*****************************************************************************
83 *****************************************************************************/
84 Parser_Perl::Parser_Perl() {
85
86 }
87
88
89
90 /*****************************************************************************
91 *****************************************************************************/
92 bool Parser_Perl::isIdentifierFirstCharacter (char c) const
93 {
94 return (bool) (isalpha (c) || c == '_');
95 }
96
97
98 /*****************************************************************************
99 *****************************************************************************/
100 bool Parser_Perl::isIdentifierCharacter (int c) const
101 {
102 return (bool) (isalnum (c) || c == '_');
103 }
104
105 /****************************************************************************/
106 int Parser_Perl::calcIndent(const char *line)
107 {
108 int indent = 0;
109
110 while(true) {
111 switch (*line) {
112 case ' ':
113 indent+= 1;
114 break;
115
116 case '\t':
117 indent+= 8;
118 break;
119
120 default:
121 return indent;
122 }
123 line++;
124 }
125 }
126
127 /*****************************************************************************
128 Skip a single or double quoted string.
129 *****************************************************************************/
130 const char *Parser_Perl::skipString (const char *cp)
131 {
132 const char *start = cp;
133 int escaped = 0;
134 for (cp++; *cp; cp++)
135 {
136 if (escaped)
137 escaped--;
138 else if (*cp == '\\')
139 escaped++;
140 else if (*cp == *start)
141 return cp + 1;
142 }
143 return cp;
144 }
145
146
147 /*****************************************************************************
148 Skip everything up to an identifier start.
149 *****************************************************************************/
150 const char *Parser_Perl::skipEverything (const char *cp)
151 {
152 for (; *cp; cp++)
153 {
154 if (*cp == '"' || *cp == '\'')
155 {
156 cp = skipString(cp);
157 if (!*cp) break;
158 }
159 if (isIdentifierFirstCharacter ((int) *cp))
160 return cp;
161 }
162 return cp;
163 }
164
165
166 /*****************************************************************************
167 Skip an identifier.
168 *****************************************************************************/
169 const char *Parser_Perl::skipIdentifier (const char *cp)
170 {
171 while (isIdentifierCharacter ((int) *cp))
172 cp++;
173 return cp;
174 }
175
176
177 /*****************************************************************************
178 *****************************************************************************/
179 const char *Parser_Perl::findDefinitionOrClass (const char *cp)
180 {
181 while (*cp)
182 {
183 cp = skipEverything (cp);
184 if (!strncmp(cp, "sub", 3) || !strncmp(cp, "package", 7))
185 {
186 return cp;
187 }
188 cp = skipIdentifier (cp);
189 }
190 return NULL;
191 }
192
193
194 /*****************************************************************************
195 *****************************************************************************/
196 const char *Parser_Perl::skipSpace (const char *cp)
197 {
198 while (isspace ((int) *cp))
199 ++cp;
200 return cp;
201 }
202
203
204 /*****************************************************************************
205 Starting at ''cp'', parse an identifier into ''identifier''.
206 *****************************************************************************/
207 const char *Parser_Perl::parseIdentifier (const char *cp, QString *identifier) const
208 {
209 std::string str;
210 while (isIdentifierCharacter ((int)*cp))
211 {
212 str+=*cp;
213 ++cp;
214 }
215 identifier->append(str.c_str());
216 return cp;
217 }
218
219
220 /*****************************************************************************
221 Given a string with the contents of the line directly after the "class"
222 keyword, extract all necessary information and create a tag.
223 *****************************************************************************/
224 PerlSymbol* Parser_Perl::makeClass (const char *cp, Symbol *parent)
225 {
226 int lineNum = getSourceLineNumber()-1;
227 QString *name=new QString();
228 cp = parseIdentifier (cp, name);
229
230 Symbol * parentSymbol = (parent) ? parent : mSymbols;
231
232 PerlSymbol *symbol = new PerlSymbol(Symbol::SymbolClass, *name, parentSymbol);
233 symbol->setDetailedText(QString("%1").arg(*name).simplified());
234 symbol->setLine(lineNum);
235 if(name)delete name;
236 return symbol;
237 }
238
239
240 /*****************************************************************************
241 Given a string with the contents of a line directly after the "def" keyword,
242 extract all relevant information and create a symbol.
243 *****************************************************************************/
244 PerlSymbol* Parser_Perl::makeFunction (const char *cp, QString *def, Symbol *parent)
245 {
246 int lineNum = getSourceLineNumber()-1;
247 cp = parseIdentifier(cp, def);
248 QString args = parseArgs();
249 Symbol * parentSymbol = (parent) ? parent : mSymbols;
250 PerlSymbol *symbol = new PerlSymbol(Symbol::SymbolFunc, *def, parentSymbol);
251 symbol->setDetailedText(QString("%1 (%2)").arg(*def).arg(args).simplified());
252 symbol->setLine(lineNum);
253 def->clear();
254 return symbol;
255
256 }
257
258 /*****************************************************************************
259 *****************************************************************************/
260 QString Parser_Perl::parseArgs()
261 {
262 QString res;
263 const char *cp;
264 while ((cp=(const char*)fileReadLine())!=NULL)
265 {
266 cp = skipSpace (cp);
267 if(*cp=='\0')continue;
268 if(*cp=='#') continue;
269 if(strstr(cp,"{"))
270 if(!(strstr(cp,"shift") || strstr(cp,"@_")))
271 continue;
272 if(strstr(cp,"shift") || strstr(cp, "@_"))
273 {
274 if(strstr(cp,"$"))
275 cp=strstr(cp,"$");
276 else
277 {
278 if(strstr(cp,"@"))
279 {
280 if((strstr(cp,"@") != strstr(cp,"@_")))
281 cp=strstr(cp,"@");
282 else
283 cp=NULL;
284 }
285 else
286 cp=NULL;
287 }
288 if(cp)
289 {
290 printf("no $!!\n");
291 if(!res.isEmpty())res.append(", ");
292 while(*cp !='=' && *cp!='\0' && *cp!=')' && *cp!=';')
293 {
294 if(*cp!=' ')
295 res.append(*cp);
296 cp++;
297 }
298 }else
299 continue;
300 }
301 if(strstr(cp,"}"))break;
302 }
303 return res;
304 }
305
306
307 /*****************************************************************************
308
309 *****************************************************************************/
310
311
312
313 /*****************************************************************************
314 *****************************************************************************/
315
316
317 /*****************************************************************************
318 *****************************************************************************/
319 void Parser_Perl::parse()
320 {
321 QString continuation;
322 QString *name=new QString();
323 QString parent;
324 PerlSymbol *parentSymbol=NULL;
325
326 const char *line;
327 int line_skip = 0;
328 char const *longStringLiteral = NULL;
329
330 while ((line = (const char *) fileReadLine ()) != NULL)
331 {
332 const char *cp = line;
333 char const *keyword;
334 //int indent;
335 cp = skipSpace (cp);
336 if (*cp == '\0') /* skip blank line */
337 continue;
338 /* Skip comment if we are not inside a multi-line string. */
339 if (*cp == '#' && !longStringLiteral)
340 continue;
341 /* Deal with line continuation. */
342 if (!line_skip) continuation="";
343 continuation+=QString(line);
344 //vStringStripTrailing(continuation);
345
346
347 cp = line = continuation.toLatin1().data();
348 cp = skipSpace (cp);
349 line_skip = 0;
350
351 /* Deal with def and class keywords. */
352 keyword = findDefinitionOrClass (cp);
353 //printf("LINE_:%s\n",line);
354 if (keyword)
355 {
356 bool found = false;
357 bool is_class = false;
358
359 if (!strncmp (keyword, "sub ", 4) && isspace(keyword[3]))
360 {
361 cp = skipSpace (keyword + 4);
362 found = true;
363 }
364 else if (!strncmp (keyword, "package", 7) && isspace(keyword[7]))
365 {
366 cp = skipSpace (keyword + 7);
367 found = true;
368 is_class = true;
369 }
370
371 if (found)
372 {
373 PerlSymbol *symbol = NULL;
374
375 if (is_class)
376 {
377 symbol = makeClass (cp, 0);
378 parentSymbol=symbol;
379 }
380 else
381 symbol = makeFunction(cp, name, parentSymbol);
382 }
383 }
384
385
386 /* Find global and class variables */
387 #ifdef SHOW_VARIABLES
388 //char const *variable = findVariable(line);
389 char const* variable;
390 if (variable)
391 {
392 const char *start = variable;
393
394 vStringClear (name);
395 while (isIdentifierCharacter ((int) *start))
396 {
397 vStringPut (name, (int) *start);
398 ++start;
399 }
400 vStringTerminate (name);
401
402 PerlSymbol *symbol = NULL;
403 PerlSymbol *parentSymbol = getParent(indent);
404
405
406 symbol = new PerlSymbol(parentSymbol, vStringToQString(name));
407 symbol->setIndent(indent);
408 symbol->setLine(this->getSourceLineNumber());
409 symbol->setIconType(Symbol::IconVar);
410 }
411 #endif
412
413 /* Find and parse imports */
414 #ifdef SHOW_IMPORTS
415 parseImports(line);
416 #endif
417 }
418
419 /* Clean up all memory we allocated. */
420 if(name)delete name;
421 }
422
423 #endif // PARSER_PERL