Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-29-2012, 05:37 PM
|
#1
|
|
Member
Registered: May 2008
Distribution: ubuntu debian knoppix centos puppy
Posts: 31
Rep:
|
multiline string regex for nanorc syntax not proper even with start/end keywords
For some basic editing on the server in PHP, using most common php.nanorc's, for multiline strings with lots of concatenation of strings and variables, the color highlighting in nano breaks down.
Example code:
Code:
$foo = '<table width="100%" cellpadding="1" cellspacing="1" class="class1"> '.
'<tr class="class2">'.
'<td colspan="5">FOO</td>'.
....
I tried this:
Code:
color yellow "('[^\']*')|(\"[^\"]*\")"
and this:
Code:
color yellow start="\"" end="\""
color yellow start="\'" end="\'"
and this:
Code:
color yellow "<[^= ]*>" ""(\.|[^"])*""
Each of those fail for multiline strings when both types of quotes single and double are used.
Now this type of code is common and needed in PHP scripting.
With the start/end approach I tried to see if it works matching double quotes with double quotes and single with single, so that at least there would be some uniformity.
But it turns out that if the start and end characters are the same, as I tried, it simply takes the character as the start every time and thus I get one never ending string right to the end of the file.
Shouldn't this behave in a stack kind of way - if it encounters a start quote, and then a next quote, then it is taken as an end quote, and so on?
Any regex that fixes this?
|
|
|
|
08-31-2012, 04:58 AM
|
#2
|
|
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,624
|
You're doing serious editing in nano? Not to start a flame war of any kind, but... I would seriously use either emacs or vim, or another editor. I personally don't like emacs that much, but it has far more editing features that nano, and both vim and emacs have proper syntax highlighting (and automatic indenting and lots of other features that nano doesn't have)
|
|
|
|
09-01-2012, 02:19 AM
|
#3
|
|
Member
Registered: May 2008
Distribution: ubuntu debian knoppix centos puppy
Posts: 31
Original Poster
Rep:
|
Well, actually I use nano mainly to make one-line changes on files on the server occasionally. But even so the default white on black / black on white was a bit too bland for my liking. So I did some digging about nanorc, and then some experimenting.
For web development proper, since I come from a Windows background, I use Eclipse PDT with its Remote System plugin or sometimes just Konqueror (with its built-in FTP) + Kate, or Geany.
Emacs is on my to-do list, but it seems to take some getting used to.
|
|
|
|
09-03-2012, 12:00 AM
|
#4
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
Could you explain or display exactly how it "breaks down"? And how you want it to behave? Perhaps by posting the code with some color tags. A few extra examples might help too.
If I'm understanding your description, the issue is related to nested quotes and/or tags, and nesting is one thing that regex just isn't capable of handling easily. It may be difficult or even impossible to design a rule that can work in all instances.
I also assume that nanorc doesn't treat newlines as special for the purposes of syntax highlighting?
|
|
|
|
09-03-2012, 10:49 AM
|
#5
|
|
Member
Registered: May 2008
Distribution: ubuntu debian knoppix centos puppy
Posts: 31
Original Poster
Rep:
|
First of all, thanks for the reply!
Quote:
Originally Posted by David the H.
Could you explain or display exactly how it "breaks down"? And how you want it to behave? Perhaps by posting the code with some color tags. A few extra examples might help too.
|
The most common case, as shown in the attached pics, is that of a SQL query built by string concatenation with arrays members, etc. (I know that's a *bad* thing to do, it's not code I write or maintain - it's just some code that shows the error ... )
There are all manners of quoting, escaping quotes, double + single alternated, etc.
It's manageable when the entire thing is on one line.
But the moment you split the SQL over multiple lines, all the highlighting goes haywire.
This also happens with the tagsoup that is HTML + JS + CSS + PHP-tags (eg <?=$foo['this']['that'];?> )
Quote:
|
If I'm understanding your description, the issue is related to nested quotes and/or tags, and nesting is one thing that regex just isn't capable of handling easily. It may be difficult or even impossible to design a rule that can work in all instances.
|
Oh. I see. That is part of the problem.
The other part is that it doesnt treat a string as a single string when it is spread over multiple lines.
Quote:
|
I also assume that nanorc doesn't treat newlines as special for the purposes of syntax highlighting?
|
Hmmm. I couldnt find mention of "\n" in the relevant file : php2.nanorc
Nor in ~/.nanorc or anywhere as such.
Code:
# PHP Syntax Highlighting
syntax "php2" "\.php[2345s~]?$"
## function names
color brightblue "(.*)\("
## literals
color black "\$[a-zA-Z_0-9$]*|[=!<>]"
## keywords
color brightgreen "(var|class|function|echo|case|break|default|exit|switch|if|else|elseif|@|while)\s"
color brightgreen "[.,{}();]"
## strings
## color yellow "('[^\']*')|(\"[^\"]*\")"
##color yellow start="\"" end="\""
##color yellow start="\'" end="\'"
color red "<[^= ]*>" ""(\.|[^"])*""
## comments
color brightgreen "(#.*|//.*)$"
color brightgreen start="/\*" end="\*/"
## php tags
color brightblue "(<\?(php)?|\?>)"
## php-escaped html
color black start="\?>" end="<\?(php)?"
Thanks in advance!
|
|
|
|
09-07-2012, 10:39 AM
|
#6
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
Sorry, I don't know much about php syntax, and your screenshots aren't helping to make it clearer. AFAICT, the highlighting in them seems reasonably good. What exactly are the starting and ending points, or keywords, or whatever, that are not being detected correctly?
Please give me a relatively simple example of code where the highlighting goes wrong, and how you actually want your rule to highlight it. You can use your original example above with some color tags around them, if you want.
Reading through man nanorc, it appears that you have to use the separate start= and end= flags for a rule to span multiple lines.
|
|
|
|
09-08-2012, 01:34 AM
|
#7
|
|
Member
Registered: May 2008
Distribution: ubuntu debian knoppix centos puppy
Posts: 31
Original Poster
Rep:
|
Thanks for your reply.
Here is a fair attempt at showing the colours except for the background color of the editor (which is white in my nano) ...
The query-string formation statement when on a single line is like this:
Code:
$updateQuestion = mysql_query("UPDATE d3_boxes SET content='".$_POST['content']."', cat_id='".$cat_id."' WHERE box_id='".$_POST['q_id']."
The same query-string formation statement when on multiple lines is like this:
Code:
$updateQuestion = mysql_query("UPDATE d3_boxes
SET
content='".$_POST['content']."',
cat_id='".$cat_id."'
WHERE
box_id='".$_POST['q_id']."'") or die (mysql_error());
Quote:
|
Reading through man nanorc, it appears that you have to use the separate start= and end= flags for a rule to span multiple lines.
|
You nailed it! If you use the same regex for start and end, it just takes the first instance of the end regex as the start regex !!
From http://man.he.net/man5/nanorc (and from man nanorc)
Code:
color fgcolor,bgcolor start="sr" end="er"
Display expressions which start with the extended regular expression
sr and end with the extended regular expression er with foreground
color fgcolor and background color bgcolor, at least one of which
must be specified. This allows syntax highlighting to span multiple
lines. Note that all subsequent instances of sr after an initial sr
is found will be highlighted until the first instance of er.
Now, we could probably tell the nano authors to add a condition to check if the er is the same as sr and if it is, then to treat it as a closing regex. But practially, that means handling nesting and that would be complexity, for which I think they would suggest vi/emacs, as almost everyone does (from my experience).
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:32 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|