PDA

View Full Version : parse line to detect valid Qt expression



ghassenus
12th December 2013, 21:14
hi
i want to parse a text file line by line and for each line i want to detect if it's a valid Qt/C++ syntax or it's just a simple text line
someone have a little idea ?
i try to use the internal debugger for this but with no success until now ...

stampede
12th December 2013, 23:00
parse a text file line by line and for each line i want to detect if it's a valid Qt/C++ syntax
This issue is a lot harder than you probably think. A simple example - this is a perfectly valid c++ program:


#include <iostream>

int

main(){
int
i =
9
;
i
+=
1;
std::cout
<<
i <<
"\n";
return
0;
}

Looks kinda weird, but it is correct c++, so you should mark the line "i <<" as "valid".
Now, if you parse this program line-by-line, how would you know that expression "i <<" is valid c++ ?
It could be a part of some incorrect program, like this:


int main(){
i <<
return 0;
}

This code won't compile, so you should mark the line "i <<" as invalid now.
As you can see, validity of particular expression depends on the context and line-by-line approach is far from perfect for this task.

I don't know what you want to achieve, but in general you will need a C++ parser - a quick search in our favourite search engine (startpage, of course:)) will direct you to this post (http://stackoverflow.com/questions/526797/good-tools-for-creating-a-c-c-parser-analyzer). Or you could create c++ parser yourself, but this is far from being trivial. Simple hint - drop the line-by-line approach, start thinking about tokens (you'll probably want to preprocess the input as well...)

Maybe you can get away from all that scary grammars, parsers, tokenizers and syntax analyzers - by simply trying to compile the source and check for errors in the compiler output. QProcess is your friend here (and -S switch in gcc, I don't know about other compilers).

ghassenus
13th December 2013, 14:23
thank you stampede for your help
i don't really try to parse a c++ file, i know it's very hard, what i need is to create a review tool, so we have a text file containing a code and a comment review lines so i want to separate them. For this i need only to check if a the line have a c++ syntax or it's just a common text line.I have seen a js script that highlight a c++ text here (http://highlightjs.org/) so i'll try to use it with Qt script tools to do this, i hope that is not so hard that i think

googie
13th December 2013, 16:33
Define that all lines starting with for example:
//=are review comments and everything else is C++ code. Then write all your review comments with your defined prefix and the problem is solved.

ghassenus
13th December 2013, 18:16
not in my case
in fact the review comments are not integrated in the code itself it's add to the svn diff file, and i can't force a specific review style for the user
so like you see it's a little bit more complicated

ChrisW67
14th December 2013, 03:55
"svn diff" does not add "review comments" to anything: its the difference between two source files that only might be C++.
You say you're not processing the source code file itself.
What is the source of the data you are "parsing" then?

ghassenus
16th December 2013, 15:59
you'r right
what we do is take the svn diff "file" and add comment on it
example:


+ QString Something;
- quint32 role =1;

style : variable name start with lowercase

+ Somethig = tr("show message");
- role = MESSAGE_TYPE_WARNING;

this an example of review on the svn diff file the comment line is inserted manually

wysota
16th December 2013, 16:18
From the above I see the following rule: if the line starts with "+" or "-", it is a line of code, otherwise it is a comment.

ghassenus
16th December 2013, 20:10
not always some lines are unchanged and appears in the diff file without any sign. for this i try to identify if one line is a Qt syntax or not

ChrisW67
16th December 2013, 23:06
Here is a simple example svn diff:


svn diff -r r1400 main.cpp
Index: main.cpp
================================================== =================
--- main.cpp (revision 1400)
+++ main.cpp (working copy)
@@ -1,12 +1,10 @@
-#include <QApplication>
-#include <QSettings>

#include "mainwindow.h"
#include "util/pluginmanager.h"

-#include "A/adialog.h"
-#include "B/bdialog.h"
-#include "C/cdialog.h"
+#include <QApplication>
+#include <QSettings>
+#include <QDir>

int main(int argc, char *argv[])
{
@@ -41,14 +39,5 @@
MainWindow w;
w.show();

-// ADialog d;
-// d.show();
-
-// BDialog d;
-// d.show();
-
-// CDialog d;
-// d.show();
-
return app.exec();
}

The best I could suggest is that, between lines starting "@@", any line starting with one or more spaces, '+' or '-' is a source line. Any other non-whitespace in the first column indicates a comment. You still have to guess what to do with blank lines. IMHO it is far easier to herd the cats making the comments than to try and pull completely arbitrary text from the middle of other arbitrary text (which could be C++, XML, , QML...). You would have to train them to start in column 1 and never with a space, + or -.

Another approach: keep (or recreate) the original diff file and diff it against the commented diff file. Any line starting "+" is a comment line. Any line starting "-" is an idiot reviewer ;)

wysota
17th December 2013, 07:21
Another approach is to make sure reviews are separate commits to the repository and mark those commits with some qualified comment (or put them in a separate branch). Then it is very simple to extract everything that is needed.

ghassenus
17th December 2013, 18:20
Hummm... interesting approach thank's ChrisW67 i'll explore that keeping in mind that i can't "train" my customers.
wysota : i don't have any access to the svn server and each customer has his own configuration.