parsing.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. ktigcc - TIGCC IDE for KDE
  3. Copyright (C) 2006 Kevin Kofler
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. // This file handles parsing of source files for the function list and for
  17. // completion purposes.
  18. #include "parsing.h"
  19. #include "ktigcc.h"
  20. #include <qstringlist.h>
  21. #include <qtextcodec.h>
  22. #include <kprocio.h>
  23. #include <kmessagebox.h>
  24. #include <unistd.h>
  25. SourceFileFunctions getCFunctions(const QString &text)
  26. {
  27. // Parse C using Exuberant Ctags (http://ctags.sourceforge.net).
  28. SourceFileFunctions result;
  29. write_temp_file("parser_temp_source.c",text,0);
  30. {
  31. // The QTextCodec has to be passed explicitly, or it will default to
  32. // ISO-8859-1 regardless of the locale, which is just broken.
  33. KProcIO procio(QTextCodec::codecForLocale());
  34. // Use MergedStderr instead of Stderr so the messages get ordered
  35. // properly.
  36. procio.setComm(static_cast<KProcess::Communication>(
  37. KProcess::Stdout|KProcess::MergedStderr));
  38. procio.setWorkingDirectory(tempdir);
  39. procio<<"ctags"<<"-f"<<"-"<<"-n"<<"-u"<<"-h"<<".h"<<"--language-force=C"
  40. <<"--C-kinds=pf"<<"--fields=k"<<"parser_temp_source.c";
  41. if (!procio.start()) {
  42. delete_temp_file("parser_temp_source.c");
  43. KMessageBox::error(0,"Could not run ctags.\nThis feature requires "
  44. "Exuberant Ctags, which can be obtained from: "
  45. "http://ctags.sourceforge.net");
  46. return result;
  47. }
  48. QString line;
  49. int ret;
  50. while ((ret=procio.readln(line))>=0 || procio.isRunning()) {
  51. if (ret>=0) {
  52. QStringList columns=QStringList::split('\t',line,TRUE);
  53. QString identifier=columns[0];
  54. QString linenoString=columns[2];
  55. int semicolonPos=linenoString.find(';');
  56. if (semicolonPos>=0) linenoString.truncate(semicolonPos);
  57. int lineno=linenoString.toInt()-1;
  58. QString kind=columns[3];
  59. SourceFileFunctions::Iterator it=result.find(identifier);
  60. if (kind=="p") {
  61. if (it==result.end())
  62. result.append(SourceFileFunction(identifier,lineno,-1));
  63. } else if (kind=="f") {
  64. if (it==result.end())
  65. result.append(SourceFileFunction(identifier,-1,lineno));
  66. else
  67. (*it).implementationLine=lineno;
  68. } else qWarning("Invalid result from ctags.");
  69. } else usleep(1000);
  70. }
  71. }
  72. delete_temp_file("parser_temp_source.c");
  73. return SourceFileFunctions(); // Not implemented yet.
  74. }
  75. SourceFileFunctions getASMFunctions(const QString &text)
  76. {
  77. // Parse ASM by hand.
  78. QStringList lines=QStringList::split('\n',text,TRUE);
  79. unsigned lineno=0;
  80. SourceFileFunctions result;
  81. for (QStringList::Iterator it=lines.begin(); it!=lines.end(); ++it,++lineno) {
  82. QString line=*it;
  83. if (line.isEmpty()) continue;
  84. QString identifier;
  85. unsigned col=0, l=line.length();
  86. while (col<l) {
  87. QChar c=line[col++];
  88. if ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||(c>='0'&&c<='9')||c=='_'||c=='$')
  89. identifier.append(c);
  90. else
  91. break;
  92. }
  93. if (line[col-1]==':') result.append(SourceFileFunction(identifier,-1,lineno));
  94. }
  95. return result;
  96. }