Results 1 to 12 of 12

Thread: parse line to detect valid Qt expression

  1. #1
    Join Date
    Nov 2013
    Posts
    8
    Thanked 5 Times in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default parse line to detect valid Qt expression

    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 ...

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: parse line to detect valid Qt expression

    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:
    Qt Code:
    1. #include <iostream>
    2.  
    3. int
    4.  
    5. main(){
    6. int
    7. i =
    8. 9
    9. ;
    10. i
    11. +=
    12. 1;
    13. std::cout
    14. <<
    15. i <<
    16. "\n";
    17. return
    18. 0;
    19. }
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. int main(){
    2. i <<
    3. return 0;
    4. }
    To copy to clipboard, switch view to plain text mode 
    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. 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).

  3. #3
    Join Date
    Nov 2013
    Posts
    8
    Thanked 5 Times in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: parse line to detect valid Qt expression

    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

  4. #4
    Join Date
    Sep 2009
    Location
    Poland, Cracow
    Posts
    34
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: parse line to detect valid Qt expression

    Define that all lines starting with for example:
    Qt Code:
    1. //=
    To copy to clipboard, switch view to plain text mode 
    are review comments and everything else is C++ code. Then write all your review comments with your defined prefix and the problem is solved.

  5. #5
    Join Date
    Nov 2013
    Posts
    8
    Thanked 5 Times in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: parse line to detect valid Qt expression

    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.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: parse line to detect valid Qt expression

    "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?

  7. #7
    Join Date
    Nov 2013
    Posts
    8
    Thanked 5 Times in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: parse line to detect valid Qt expression

    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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: parse line to detect valid Qt expression

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Nov 2013
    Posts
    8
    Thanked 5 Times in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: parse line to detect valid Qt expression

    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

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: parse line to detect valid Qt expression

    Here is a simple example svn diff:
    Qt Code:
    1. svn diff -r r1400 main.cpp
    2. Index: main.cpp
    3. ===================================================================
    4. --- main.cpp (revision 1400)
    5. +++ main.cpp (working copy)
    6. @@ -1,12 +1,10 @@
    7. -#include <QApplication>
    8. -#include <QSettings>
    9.  
    10. #include "mainwindow.h"
    11. #include "util/pluginmanager.h"
    12.  
    13. -#include "A/adialog.h"
    14. -#include "B/bdialog.h"
    15. -#include "C/cdialog.h"
    16. +#include <QApplication>
    17. +#include <QSettings>
    18. +#include <QDir>
    19.  
    20. int main(int argc, char *argv[])
    21. {
    22. @@ -41,14 +39,5 @@
    23. MainWindow w;
    24. w.show();
    25.  
    26. -// ADialog d;
    27. -// d.show();
    28. -
    29. -// BDialog d;
    30. -// d.show();
    31. -
    32. -// CDialog d;
    33. -// d.show();
    34. -
    35. return app.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 
    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

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: parse line to detect valid Qt expression

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Nov 2013
    Posts
    8
    Thanked 5 Times in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: parse line to detect valid Qt expression

    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.

Similar Threads

  1. QtXml parse error at line 1 column line 1
    By vinod sharma in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 8th September 2012, 05:02
  2. .lib is not a valid Qt plugin
    By rickrvo in forum Qt for Embedded and Mobile
    Replies: 11
    Last Post: 21st May 2011, 23:18
  3. No valid Qt version set?
    By RoryWalsh in forum Qt Tools
    Replies: 4
    Last Post: 21st October 2009, 08:02
  4. valid a path name
    By klaus1111 in forum Newbie
    Replies: 3
    Last Post: 23rd July 2006, 15:07
  5. How to detect new line?
    By whoops.slo in forum Newbie
    Replies: 6
    Last Post: 6th January 2006, 17:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.