LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   multiline string regex for nanorc syntax not proper even with start/end keywords (https://www.linuxquestions.org/questions/linux-software-2/multiline-string-regex-for-nanorc-syntax-not-proper-even-with-start-end-keywords-4175424657/)

deskt0plinux 08-29-2012 05:37 PM

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?

Snark1994 08-31-2012 04:58 AM

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)

deskt0plinux 09-01-2012 02:19 AM

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.

David the H. 09-03-2012 12:00 AM

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?

deskt0plinux 09-03-2012 10:49 AM

2 Attachment(s)
First of all, thanks for the reply!

Quote:

Originally Posted by David the H. (Post 4771094)
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!

David the H. 09-07-2012 10:39 AM

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.

deskt0plinux 09-08-2012 01:34 AM

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).


All times are GMT -5. The time now is 12:11 AM.