PDA

View Full Version : Qtextedit parsing multiple lines as in an IDE



qtnewbie6019
16th July 2015, 19:52
I'm trying to write my own code-editor, I figure its a good way to learn pyQt. I am using a qtextedit, in which i can write code(it's not real code, more pseudo code). Each line represents ending in a semi-colon represents some command e.g.


PSEUDO->FWD->90;
PSEUDO->STOP;
PSEUDO->RIGHT 90;
PSEUDO->FWD 10;

These are relatively easy to read, as the user presses the [ENTER] the current line is read, parsed and checked for errors so the following


PSEUDO->RIGHT -pi/2

would generate an error because the line doesn't end in a semi-colon and the value following RIGHT needs to be a number.(my editor, my rules).All this I have more or less got working.

I would like to know how to do multiple lines though. I am familiar with editors such as Eclipse,sublime or visual studio which handle muliple lines very well, in my case


PSEUDO->DO:
FWD->90
RIGHT->45
FWD->10
LEFT->55
FWD->50
STOP;

Should all be read in and treated as one statement, starting at the keyword PSEUDO and ending at the semi-colon. However the following should be read as 3 separate statements.


PSEUDO->DO:
FWD->90
RIGHT->45
FWD->10
LEFT->55
FWD->50
STOP;

PSEUDO->DO:
FWD->90
RIGHT->45
STOP;

PSEUDO->BACK 10;

My question how can I go about reading multiple lines as described above from QTextEditor as discreet statements. Should I do the parse/check whenever I press the [ENTER] key for a new line?

I'm using python2.7,pyQT, and QTextEdit. But if there are any Qt samples I'll do my best to follow.

My first idea is using an anchor, i.e whenever the word PSEUDO-> is written I drop an anchor just before the word, and then when a semi-colon(;) is typed. I read backwards from the semi-colon to the last anchor.


Is this a good suggestion?
Can anchors be used in this way?
What do editors like QtCreator,Eclipse,Sublime or even Visual Studio do to solve this kind of problem

ChrisW67
18th July 2015, 06:14
Your syntax makes the semicolon the end-of-statement mark.
Naive approach assuming the statements are processed as entered:

Keep a statement buffer.
When the user presses Enter copy the line onto the end of the buffer.
Look at the last non-space in the buffer, if it is ';' then process the buffer and empty it.

If terminating semicolons can appear mid-line then you have to process only the whole statements in the buffer and keep whatever remains afterwards.

qtnewbie6019
20th July 2015, 20:05
Just curious, but could I use a regular expression, I am not fully familiar to QRegEx or QRegularExpression, but could I use them in some way to determine when its sensible to parse a statement? If so any suggestions on how I could go about doing so?