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 ...
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 ...
This issue is a lot harder than you probably think. A simple example - this is a perfectly valid c++ program:parse a text file line by line and for each line i want to detect if it's a valid Qt/C++ syntax
Looks kinda weird, but it is correct c++, so you should mark the line "i <<" as "valid".Qt Code:
#include <iostream> int main(){ int i = 9 ; i += 1; std::cout << i << "\n"; return 0; }To copy to clipboard, switch view to plain text mode
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:
This code won't compile, so you should mark the line "i <<" as invalid now.Qt Code:
int main(){ i << return 0; }To copy to clipboard, switch view to plain text mode
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. 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).
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 so i'll try to use it with Qt script tools to do this, i hope that is not so hard that i think
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.
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
Last edited by ghassenus; 13th December 2013 at 18:23.
"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?
you'r right
what we do is take the svn diff "file" and add comment on it
example:
this an example of review on the svn diff file the comment line is inserted manually+ QString Something;
- quint32 role =1;
style : variable name start with lowercase
+ Somethig = tr("show message");
- role = MESSAGE_TYPE_WARNING;
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.
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
Here is a simple example svn diff:
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 -.Qt Code:
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(); }To copy to clipboard, switch view to plain text mode
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
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.
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.
Bookmarks