LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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

Reply
 
Search this Thread
Old 08-29-2012, 05:37 PM   #1
deskt0plinux
Member
 
Registered: May 2008
Distribution: ubuntu debian knoppix centos puppy
Posts: 31
Blog Entries: 2

Rep: Reputation: 1
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?
 
Old 08-31-2012, 04:58 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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)
 
Old 09-01-2012, 02:19 AM   #3
deskt0plinux
Member
 
Registered: May 2008
Distribution: ubuntu debian knoppix centos puppy
Posts: 31
Blog Entries: 2

Original Poster
Rep: Reputation: 1
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.
 
Old 09-03-2012, 12:00 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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?
 
Old 09-03-2012, 10:49 AM   #5
deskt0plinux
Member
 
Registered: May 2008
Distribution: ubuntu debian knoppix centos puppy
Posts: 31
Blog Entries: 2

Original Poster
Rep: Reputation: 1
First of all, thanks for the reply!

Quote:
Originally Posted by David the H. View Post
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!
Attached Images
File Type: png single-line-quotes-fine.png (25.0 KB, 5 views)
File Type: png multiple-lines-quotes-broken.png (9.0 KB, 5 views)
 
Old 09-07-2012, 10:39 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.
 
Old 09-08-2012, 01:34 AM   #7
deskt0plinux
Member
 
Registered: May 2008
Distribution: ubuntu debian knoppix centos puppy
Posts: 31
Blog Entries: 2

Original Poster
Rep: Reputation: 1
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).
 
  


Reply

Tags
multiline, nano, regex, string, syntax


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash - String replace at the end with a regex myro Linux - General 7 09-27-2011 04:37 PM
Multiline regex? grob115 Programming 6 11-12-2010 09:22 AM
[SOLVED] regex match string from start to find unique combinations fukawi2 Programming 6 02-11-2010 05:32 PM
Perl Regex multiline match issues adymcc Linux - General 2 03-31-2008 09:45 AM
MySQL: search for pluralized keywords using RegEx? MicahCarrick Programming 1 09-26-2006 08:50 PM


All times are GMT -5. The time now is 08:46 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration