File indexing completed on 2024-05-05 17:15:17

0001 /**********************************************************************************
0002 *   Copyright (C) 2003 by Jeroen Wijnhout (Jeroen.Wijnhout@kdemail.net)           *
0003 *                 2005-2007 by Holger Danielsson (holger.danielsson@versanet.de)  *
0004 *                 2006-2022 by Michel Ludwig (michel.ludwig@kdemail.net)          *
0005 ***********************************************************************************/
0006 
0007 /***************************************************************************
0008  *                                                                         *
0009  *   This program is free software; you can redistribute it and/or modify  *
0010  *   it under the terms of the GNU General Public License as published by  *
0011  *   the Free Software Foundation; either version 2 of the License, or     *
0012  *   (at your option) any later version.                                   *
0013  *                                                                         *
0014  ***************************************************************************/
0015 
0016 #include "parser.h"
0017 
0018 #include <QStringList>
0019 
0020 #include "parserthread.h"
0021 
0022 namespace KileParser {
0023 
0024 StructureViewItem::StructureViewItem(const QString &title, uint line, uint column, int type, int level, uint startline, uint startcol,
0025                                      const QString &pix, const QString &folder)
0026     :  title(title),
0027        line(line),
0028        column(column),
0029        type(type),
0030        level(level),
0031        startline(startline),
0032        startcol(startcol),
0033        pix(pix),
0034        folder(folder)
0035 {
0036 }
0037 
0038 StructureViewItem::~StructureViewItem()
0039 {
0040 }
0041 
0042 ParserInput::ParserInput(const QUrl &url)
0043     : url(url)
0044 {
0045 }
0046 
0047 ParserInput::~ParserInput()
0048 {
0049 }
0050 
0051 ParserOutput::~ParserOutput()
0052 {
0053     for(StructureViewItem *item : structureViewItems) {
0054         delete(item);
0055     }
0056 }
0057 
0058 Parser::Parser(ParserThread *parserThread, QObject *parent) :
0059     QObject(parent),
0060     m_parserThread(parserThread)
0061 {
0062 }
0063 
0064 Parser::~Parser()
0065 {
0066 }
0067 
0068 QString Parser::processTextline(const QString &line, TodoResult &todo)
0069 {
0070     static QRegExp reComments("[^\\\\](%.*$)");
0071     QString s = line;
0072     todo.type = -1;
0073     if(!s.isEmpty()) {
0074         // remove comment lines
0075         if(s[0] == '%') {
0076             searchTodoComment(s,0,todo);
0077             s.clear();
0078         }
0079         else {
0080             //remove escaped \ characters
0081             s.replace("\\\\", "  ");
0082 
0083             //remove comments
0084             int pos = s.indexOf(reComments);
0085             if(pos != -1) {
0086                 searchTodoComment(s, pos,todo);
0087                 s = s.left(reComments.pos(1));
0088             }
0089         }
0090     }
0091     return s;
0092 }
0093 
0094 void Parser::searchTodoComment(const QString &s, uint startpos, TodoResult &todo)
0095 {
0096     static QRegExp reTodoComment("\\b(TODO|FIXME)\\b(:|\\s)?\\s*(.*)");
0097 
0098     if(s.indexOf(reTodoComment, startpos) != -1) {
0099         todo.type = (reTodoComment.cap(1) == "TODO") ? KileStruct::ToDo : KileStruct::FixMe;
0100         todo.colTag = reTodoComment.pos(1);
0101         todo.colComment = reTodoComment.pos(3);
0102         todo.comment = reTodoComment.cap(3).trimmed();
0103     }
0104 }
0105 
0106 // match a { with the corresponding }
0107 // pos is the position of the {
0108 QString Parser::matchBracket(const QStringList& textLines, QChar obracket, int &l, int &pos)
0109 {
0110     QChar cbracket;
0111     if(obracket == '{') {
0112         cbracket = '}';
0113     }
0114     else if(obracket == '[') {
0115         cbracket = ']';
0116     }
0117     else if(obracket == '(') {
0118         cbracket = ')';
0119     }
0120 
0121     QString line, grab = "";
0122     int count=0;
0123     ++pos;
0124 
0125     TodoResult todo;
0126     while(l < textLines.size()) {
0127         line = processTextline(textLines[l], todo);
0128         int len = line.length();
0129         for (int i = pos; i < len; ++i) {
0130             if(line[i] == '\\' && (line[i+1] == obracket || line[i+1] == cbracket)) {
0131                 ++i;
0132             }
0133             else if(line[i] == obracket) {
0134                 ++count;
0135             }
0136             else if(line[i] == cbracket) {
0137                 --count;
0138                 if (count < 0) {
0139                     pos = i;
0140                     return grab;
0141                 }
0142             }
0143 
0144             grab += line[i];
0145         }
0146         ++l;
0147         pos = 0;
0148     }
0149 
0150     return QString();
0151 }
0152 
0153 QString Parser::getTextLine(const QStringList& textLines, int line)
0154 {
0155     if(line < 0 || line >= textLines.size()) {
0156         return QString();
0157     }
0158     return textLines[line];
0159 }
0160 
0161 }
0162