logo
down
shadow

c++ why */ in quoted string ends a multiline comment


c++ why */ in quoted string ends a multiline comment

By : user2948220
Date : November 14 2020, 04:48 PM
To fix the issue you can do That's simply because there is no string inside your comment.
Comment contents have no structure: IIRC they are not even tokenized. The only special character sequence inside a multiline comment is */, and that's it.
code :


Share : facebook icon twitter icon
How to match a comment unless it's in a quoted string?

How to match a comment unless it's in a quoted string?


By : user1773359
Date : March 29 2020, 07:55 AM
I hope this helps you . Instead of using a parser that parses an entire Java source file, or writing something yourself that parses only those parts you're interested in, you could use some 3rd party tool like ANTLR.
ANTLR has the ability to define only those tokens you are interested in (and of course the tokens that can mess up your token-stream like multi-line comments and String- and char literals). So you only need to define a lexer (another word for tokenizer) that correctly handles those tokens.
code :
lexer grammar FuzzyJavaLexer;

options{filter=true;}

SingleLineComment
  :  '//' ~( '\r' | '\n' )*
  ;

MultiLineComment
  :  '/*' .* '*/'
  ;

StringLiteral
  :  '"' ( '\\' . | ~( '"' | '\\' ) )* '"'
  ;

CharLiteral
  :  '\'' ( '\\' . | ~( '\'' | '\\' ) )* '\''
  ;
java -cp antlr-3.2.jar org.antlr.Tool FuzzyJavaLexer.g
import org.antlr.runtime.*;

public class FuzzyJavaLexerTest {
    public static void main(String[] args) throws Exception {
        String source = 
            "class Test {                                 \n"+
            "  String s = \" ... \\\" // no comment \";   \n"+
            "  /*                                         \n"+
            "   * also no comment: // foo                 \n"+
            "   */                                        \n"+
            "  char quote = '\"';                         \n"+
            "  // yes, a comment, finally!!!              \n"+
            "  int i = 0; // another comment              \n"+
            "}                                            \n";
        System.out.println("===== source =====");
        System.out.println(source);
        System.out.println("==================");
        ANTLRStringStream in = new ANTLRStringStream(source);
        FuzzyJavaLexer lexer = new FuzzyJavaLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        for(Object obj : tokens.getTokens()) {
            Token token = (Token)obj;
            if(token.getType() == FuzzyJavaLexer.SingleLineComment) {
                System.out.println("Found a SingleLineComment on line "+token.getLine()+
                        ", starting at column "+token.getCharPositionInLine()+
                        ", text: "+token.getText());
            }
        }
    }
}
javac -cp .:antlr-3.2.jar *.java
// *nix/MacOS
java -cp .:antlr-3.2.jar FuzzyJavaLexerTest
// Windows
java -cp .;antlr-3.2.jar FuzzyJavaLexerTest
===== source =====
class Test {                                 
  String s = " ... \" // no comment ";   
  /*                                         
   * also no comment: // foo                 
   */                                        
  char quote = '"';                         
  // yes, a comment, finally!!!              
  int i = 0; // another comment              
}                                            

==================
Found a SingleLineComment on line 7, starting at column 2, text: // yes, a comment, finally!!!              
Found a SingleLineComment on line 8, starting at column 13, text: // another comment  
C# filter multiline double quoted string with Regular expression

C# filter multiline double quoted string with Regular expression


By : Mostafa Kamel
Date : March 29 2020, 07:55 AM
it helps some times Use RegexOptions.SingleLine instead.
From the docs:
With pyparsing, how do you parse a quoted string that ends with a backslash

With pyparsing, how do you parse a quoted string that ends with a backslash


By : Adil Khan
Date : March 29 2020, 07:55 AM
I wish this helpful for you I'm trying to use pyparsing to parse quoted strings under the following conditions: , What is it about this code that is not working for you?
code :
from pyparsing import *

s = r"foo = 'ab\'cd\\'"  # <--- IMPORTANT - use a raw string literal here

ident = Word(alphas)
strValue = QuotedString("'", escChar='\\')
strAssign = ident + '=' + strValue

results = strAssign.parseString(s)
print results.asList() # displays repr form of each element

for r in results:
    print r # displays str form of each element

# count the backslashes
backslash = '\\'
print results[-1].count(backslash)
['foo', '=', "ab'cd\\\\"]
foo
=
ab'cd\\
2
import re
strValue.setParseAction(lambda t: re.sub(r'\\(.)', r'\g<1>', t[0]))
Remove \n from multiline quoted string in PyParsing

Remove \n from multiline quoted string in PyParsing


By : user3397269
Date : March 29 2020, 07:55 AM
wish help you to fix your issue This isn't really what the escChar argument is for, it is to indicate how to escape embedded characters that would normally be quote delimiters.
This is more what I would see as best handled with a parse action, which is a parse-time callback that can modify the tokens right after they are parsed, but before they are returned to the caller. Here is your code as a console session, adding the parse action remove_newlines to qs:
code :
>>> text = """PROPERTY PName "Multiline quoted 
... string" ;"""
>>> import pyparsing as pp

>>> qs = pp.QuotedString('"', multiline=True)

>>> qs.searchString(text)
([(['Multiline quoted \nstring'], {})], {})

>>> def remove_newlines(t):
...     t[0] = t[0].replace('\n', '')
...     
>>> qs.addParseAction(remove_newlines)

>>> qs.searchString(text)
([(['Multiline quoted string'], {})], {})
Parse CSV with quoted multiline string

Parse CSV with quoted multiline string


By : Banshidhar Pradhan
Date : March 29 2020, 07:55 AM
it should still fix some issue It should work just fine unless there's some bug in the library or misconfiguration. However it might be the case that your password has an unescaped/unclosed quoted field and this not being written properly.
If that's the situation, give univocity-parsers a try as it has many different options to handle unescaped/unclosed quotes properly. It is also 4 times faster than Commons CSV.
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org