[Qt-interest] Syntax Highlighter State Stack

Neel Basu neel.basu.z at gmail.com
Tue Dec 30 19:23:41 CET 2008


First of all Thanks for your comments.
actually I want my Plan to be perfect e.g. I don't want to hear/realize there is
design flaw in my code when I am 80% finished with my code.

On Tuesday 30 Dec 2008 11:35:39 pm Robert Hairgrove wrote:
> Neel Basu wrote:
> > I am Making a Syntax Highlighter for PHP
> > there are a lot of states such as
> > 		enum State{
> > 			NormalState = -1,
> > 			InPHPCodeBlock,
> > 			InFunction,
> > 			InClass,
> > 			InMultiComment,
> > 			InCurlyBraces
> > 		};
> > Now Its possible that some code block is in InMultiComment which is in InFnction which is in InPHPCodeBlock
> > It is not possible to keep informations about these states in a single integer variable.
> > (Is it ??)
> > So I thought to keep a vector for this enum values which will be used as a StateStack
> > 
> > Now the question is am I on the right Track ?? or there are easier or safer or more standard alternatives ??
> > 
> > Neel
> 
> If you define your enum values like this:
> 
>   		enum State{
>   			NormalState = 0,
>   			InPHPCodeBlock = 1,
>   			InFunction = 2,
>   			InClass = 4,
>   			InMultiComment = 8,
>   			InCurlyBraces = 16
>   		};
> 
> then you could keep them in an unsigned integer by OR'ing the values 
> together. However, it would be impossible to keep the information as to 
> the order of the values, i.e. if you had to unwind the "stack".

Ya I've thought about this plan before. But the problem is Is it possible to check
InClass exists or not while I am in a InFunction Block.
and I do always need to check wheather I am in a InPHPCodeBlock or not
cause all PHP Code stays in <?php (Open) and ?> (Close)
but the first thing I'd confess that I never did OR'ing before.
and I havn't found any docs on that (although I know Its easy stuff)
and as I am unfamiliar I felt scared on QFlag()

> The STL has std::stack<>. If you really need LIFO semantics, this is 
> probably a good choice. Otherwise, look into std::deque<> which is 
> insertable at both ends. I've never programmed syntax highlighting 
> before, so I can't make any recommendations here.

Ya thats the think I thought however I planned to use std::vector instead
cause in std::stack I'd not be able to do search operation. e.g. I've only top() in stack
however I can search for a particular State in vector and vector have push_back() pop_back() operations. 

I was just taking openions cause as Qt's QSyntaxHighlighter keeps track of States in two integers
I thought there might be some alternatives in Qt's own way.

Thanks
Neel



More information about the Qt-interest-old mailing list