/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set et sw=2 ts=2: */ /*************************************************************************** * parse.cc * * Thu Mar 24 12:07:16 CET 2011 * Copyright 2011 Bent Bisballe Nyeng * deva@aasimon.org ****************************************************************************/ /* * This file is part of Pracro. * * Pracro is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Pracro is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Pracro; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #include "parse.h" #include #include #include typedef enum { none, indesc, indescex, inatt, inmeth, inmethex, inpar, inret, } state_t; static QString convert(QString in) { QString out; bool linkme = false; QStringList tokens = in.split(QRegExp("\\s+")); QStringList::iterator i = tokens.begin(); while(i != tokens.end()) { if(linkme) { QString link = *i; if(i->right(1) == ".") link = link.left(link.length() - 1); if(link.indexOf("(") != -1 && link.indexOf(")") != -1 ) { // method link out += ""+link+""; } else { // tag link out += ""+link+""; } if(i->right(1) == ".") out += "."; out += " "; i++; linkme = false; continue; } if(*i == "@see") { linkme = true; i++; continue; } out += *i + " "; i++; } return out; } Doc parse(QString filename) { Doc doc; doc.screenshot = false; doc.container = false; QFileInfo fi(filename); QString name = fi.fileName(); doc.name = name.left(name.length() - 2); // Strip ".h" state_t state = none; fprintf(stderr, "Processing file: %s\n", filename.toStdString().c_str()); QFile file(filename); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { fprintf(stderr, "Could not open %s for reading.\n", filename.toStdString().c_str()); return doc; } bool running = false; QByteArray data; while(!file.atEnd()) { QString line = file.readLine(); if(line.right(1) == "\n") line = line.left(line.length() - 1); if(line.contains("*/")) running = false; if(running) { line.remove(QRegExp("^[ \t]*[*][ \t]")); if(state == indesc && line.left(1) == "@") state = none; if(state == indescex && line.left(1) == "@") state = none; if(state == inatt && line.left(1) == "@") state = none; if(state == inpar && line.left(1) == "@") state = inmeth; if(state == inret && line.left(1) == "@") state = inmeth; if(state == inmethex && line.left(1) == "@") state = inmeth; if(state == none && doc.title == "") { doc.title = line; continue; } if(state == none && line.left(4) == "@tag") { doc.tag = line.right(line.length() - line.indexOf(" ") - 1); doc.name = doc.tag; continue; } if(state == none && line.left(8) == "@extends") { doc.extends = line.right(line.length() - line.indexOf(" ") - 1); continue; } if(state == none && line.left(11) == "@screenshot") { doc.screenshot = true; continue; } if(state == none && line.left(10) == "@container") { doc.container = true; continue; } if(state == none && line.left(1) != "@" && doc.description == "") { state = indesc; } if(state == indesc) { doc.description += line + "\n"; continue; } if((state == none || state == indesc || state == inmeth) && line.left(8) == "@example") { Example e; e.name = line.right(line.length() - 8); if(state == none || state == indesc) { doc.examples.push_back(e); state = indescex; } else { Method &m = doc.methods[doc.methods.size() - 1]; m.examples.push_back(e); state = inmethex; } continue; } if(state == indescex || state == inmethex) { Example *e; if(state == indescex) { e = &doc.examples[doc.examples.size() - 1]; } else { Method &m = doc.methods[doc.methods.size() - 1]; e = &m.examples[m.examples.size() - 1]; } e->code += line + "\n"; continue; } if(state == none && line.left(4) == "@att") { Attribute att; att.name = line.mid(5, line.indexOf(" ", 5) - 5); att.description = line.right(line.length() - line.indexOf(" ", 6) - 1) + "\n"; doc.attributes.push_back(att); state = inatt; continue; } if(state == inatt) { Attribute &a = doc.attributes[doc.attributes.size() - 1]; a.description += line + "\n"; continue; } if((state == none || state == inmeth) && line.left(7) == "@method") { Method meth; meth.name = line.right(line.length() - 7 - 1); doc.methods.push_back(meth); state = inmeth; continue; } if(state == inmeth && line.left(1) != "@") { Method &m = doc.methods[doc.methods.size() - 1]; m.description += line + "\n"; continue; } if(state == inmeth && line.left(6) == "@param") { Method &m = doc.methods[doc.methods.size() - 1]; Parameter par; par.name = line.mid(7, line.indexOf(" ", 7) - 7); par.description = line.right(line.length() - line.indexOf(" ", 7) - 1) + "\n"; m.parameters.push_back(par); state = inpar; continue; } if(state == inpar) { Method &m = doc.methods[doc.methods.size() - 1]; Parameter &p = m.parameters[m.parameters.size() - 1]; p.description += line + "\n"; continue; } if(state == inmeth && line.left(7) == "@return") { Method &m = doc.methods[doc.methods.size() - 1]; m.returns = line.right(line.length() - line.indexOf(" ", 7) - 1) + "\n"; state = inret; continue; } if(state == inret) { Method &m = doc.methods[doc.methods.size() - 1]; m.returns += line + "\n"; continue; } } if(line == "/***") running = true; } // Convert all @see to links doc.description = convert(doc.description); QVector::iterator i = doc.attributes.begin(); while(i != doc.attributes.end()) { i->description = convert(i->description); i++; } QVector::iterator j = doc.methods.begin(); while(j != doc.methods.end()) { j->description = convert(j->description); j++; } return doc; }