Reputation: 135
Just started working on the assignment (code in Perl) for syntax highlighter. I need to match comments, keywords and strings and highlight them in different colours.
Can anyone give me a hint where to start with? I've just got only two weeks for this.
I use regex to split every line of the file into tokens. For example
$my string = 'my @array = (56, "string", 56.5758);';
my @b = $string =~ /([A-Za-z0-9'"\._\@\#\$]+)/g;
that gives:
my
@array
56
'string'
56.5758
then I can start analysing them. I've got the following questions:
1. How can I put back these tokens back to the string? I mean I've got spaces, parentheses, ";" and other characters that used to be between tokens. How can I do again?
'<keyword>my</keyword> @array = (56, <string>"string"<string>, 56.5758);
2. What' the best way to write highlighter? Just using regex and try to model all different situations?
Any help guys would be much appreciated. I have not progress much so far.
Many thanks
Upvotes: 0
Views: 102
Reputation: 98398
Do not try to use regex; use PPI. Any attempt to parse perl is fraught with peril; PPI has solved that problem for almost all perl code likely to be encountered.
There's still plenty of code to write to make a syntax highlighter from it; you should check if you can use it. (And if you can't, I think poorly of your instructor for mandating such a task; much better to have made the assignment to write a syntax highlighter for a something that can be represented in BNF or EBNF such as C or Python or sudoers.)
Upvotes: 8