How to ignore the delimiter when it comments or quotes

I am writing a parser to find string concatenation expressions. I have a number of lines that are enclosed in parentheses, arising mainly from a function call.

For example, ("one"+"two"+"three") -> ("one"|"two"|"three") is a simple case, and I can handle it.

More complex is (null, "one"+"two"+"three", null) -> (null, "one"|"two"|"three", null) , but I can parse it with boost::tokenizer .

(null, "one"+"two"+"three,four", 1 /* third parameter can be: 1, 2, 3 */) , in such a complex example, I suggest parsing with boost::spirit , but I need help in writing some rules for him.

Further:

It seems that escaped_list_separator from boost::tokenizer is what I need. But I have one problem:

  using namespace std; using namespace boost; string s = "Field 1,\"putting quotes around fields, allows commas\",Field 3"; tokenizer<escaped_list_separator<char> > tok(s,escaped_list_separator<char>("", ",", "\"")); for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end();++beg){ cout <<"~~~"<< *beg << "\n"; } 

removes " for me. You can save quotes in the output like this

 Field 1 "putting quotes around fields, allows commas" Field 3 
+4
source share
1 answer

Basically, you can use operator- with matching characters:

  rule = '"' >> (char_ - '"') >> '"'; 

Also see operator ~ to invert the encoding.

If you are interested in speeding up quotes inside quotes, as well as possibly in style comments at the same time, I recommend looking at my answer here:

Display (partially) quoted cells in CSV files, including escaped quotation marks inside strings.

Other items of interest:

+2
source

Source: https://habr.com/ru/post/1414084/


All Articles