How to ignore quoted sections? 
Author Message
 How to ignore quoted sections?

I want to match the comment in lines like:

Assign <variablename> <expression> [#<comment>]

Some examples of lines:

1) Assign a 12
2) Assign b "alpha" # comment
3) Assign c "# here is a quoted hash" # comment
4) Assign d Add a 12 # comment

Is there a simple solution for matching just the comment (match
nothing in example 1, match ` comment' in 2, 3 and 4)?

Thanks,
Mats



Sat, 01 Mar 2003 19:05:10 GMT  
 How to ignore quoted sections?


Quote:
> I want to match the comment in lines like:

> Assign <variablename> <expression> [#<comment>]

> Some examples of lines:

> 1) Assign a 12
> 2) Assign b "alpha" # comment
> 3) Assign c "# here is a quoted hash" # comment
> 4) Assign d Add a 12 # comment

> Is there a simple solution for matching just the comment (match
> nothing in example 1, match ` comment' in 2, 3 and 4)?

Based on your definitions - make your Field Separator a " character, and
only search the odd number fields for the "#"

Will not handle multi-line comments or potential escaping the "
character - ie "here is a \"comment\"" or what to do with a statement
like Assign c a #"here is a comment"

Aren't parsers fun?

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 02 Mar 2003 00:53:18 GMT  
 How to ignore quoted sections?

Quote:



>>I want to match the comment in lines like:

>> Assign <variablename> <expression> [#<comment>]

>> Some examples of lines:

>>1) Assign a 12
>>2) Assign b "alpha" # comment
>>3) Assign c "# here is a quoted hash" # comment
>>4) Assign d Add a 12 # comment

>>Is there a simple solution for matching just the comment (match
>>nothing in example 1, match ` comment' in 2, 3 and 4)?

>Based on your definitions - make your Field Separator a " character,
>and only search the odd number fields for the "#"

>Will not handle multi-line comments or potential escaping the "
>character - ie "here is a \"comment\"" or what to do with a statement
>like Assign c a #"here is a comment"

Get tricky. _IF_ I can assume that I don't have to catch syntax errors,
so lines line

Assign a \"don't forget quotes\"" # comment

would end with the double-quoted string beginning with ' # comment' and
continue onto subsequent lines, then replace troublesome text before
splitting into fields.

{
  gsub(/\\\\/, "<bs><bs>")
  gsub(/\\"/, "<bs><dq>")
  n = split($0, x, /"/)
  c = s = ""
  for (i = 1; i <= n; i += 2) {
    if (match(x[i], /#/) > 0) {
      c = substr($0, length(s) + RSTART)
      break
    } else {
      s = ((i > 1) ? s "\"" : "") x[i] ((n > i) ? "\"" x[i + 1] : "")
    }
  }
  delete x
  sub(/^ */, "", c)
  gsub(/<bs><dq>/, "\\\\\"", c)
  gsub(/<bs><bs>/, "\\\\\\\\", c)
  if (c) print FILENAME, FNR, c

Quote:
}

Sent via Deja.com http://www.deja.com/
Before you buy.


Sun, 02 Mar 2003 04:27:39 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Value clause in File section and Linkage section

2. Quote.py - Look up stock quotes

3. single-quoted string conversion to triple-quoted string

4. When is a quote not a quote?

5. quote quote reshape v

6. replace single quote to double quote with an example

7. emacs python-mode quote in triple quote font-lock problem

8. Quote.py - Look up stock quotes

9. Computational scientists ignoring and ignored by Ada

10. WhySmalltalk.com Jobs Section

11. Dummy section assembler directives

12. dummy section directives

 

 
Powered by phpBB® Forum Software