Skip to content

[matlab] CPD: Parse error with backslashes in character vectors/string literals #2258

Closed

Description

@chrite

Description:
I tried using cpd on Windows 10 in a medium large code base, but got a (wrong?) Lexical error immediately. I think MATLAB's weird sometimes-escape-character \ isn't handled properly:

net.sourceforge.pmd.lang.ast.TokenMgrError: Lexical error in file Untitled.m at line 3, column 56.  Encountered: "$" (36), after : ""
        at net.sourceforge.pmd.lang.matlab.ast.MatlabParserTokenManager.getNextToken(MatlabParserTokenManager.java:1996)
        at net.sourceforge.pmd.lang.matlab.MatlabTokenManager.getNextToken(MatlabTokenManager.java:31)
        at net.sourceforge.pmd.cpd.token.internal.BaseTokenFilter.getNextToken(BaseTokenFilter.java:30)
        at net.sourceforge.pmd.cpd.internal.JavaCCTokenizer.tokenize(JavaCCTokenizer.java:40)
        at net.sourceforge.pmd.cpd.CPD.addAndThrowLexicalError(CPD.java:144)
        at net.sourceforge.pmd.cpd.CPD.add(CPD.java:139)
        at net.sourceforge.pmd.cpd.CPD.add(CPD.java:108)
        at net.sourceforge.pmd.cpd.CPD.add(CPD.java:70)
        at net.sourceforge.pmd.cpd.CPD.addDirectory(CPD.java:80)
        at net.sourceforge.pmd.cpd.CPD.addRecursively(CPD.java:65)
        at net.sourceforge.pmd.cpd.CPDCommandLineInterface.addSourcesFilesToCPD(CPDCommandLineInterface.java:134)
        at net.sourceforge.pmd.cpd.CPDCommandLineInterface.addSourceFilesToCPD(CPDCommandLineInterface.java:114)
        at net.sourceforge.pmd.cpd.CPDCommandLineInterface.main(CPDCommandLineInterface.java:87)
        at net.sourceforge.pmd.cpd.CPD.main(CPD.java:179)

--ignore-literals doesn't help as a work-around either...

Code Sample demonstrating the issue:

names = {'hallo_welt', 'hello_world'};
texEscape = strrep(names, '_', '\_');

or

nameUnit = 'hello [world]';
name = strtrim(regexprep(nameUnit, '\s\[.*\]$', ''));

Activity

  1. added
    a:bug
    on Jan 28, 2020
  2. added
    needs:pmd7-revalidation
    and removed
    needs:pmd7-revalidation
    on Apr 2, 2024
  3. added
    in:cpd
    on Oct 29, 2026
  4. added this to the 7.29.0 milestone on Oct 29, 2026
  5. added a commit that references this issue on Oct 29, 2026
    e3975e2
  6. changed the title [-][matlab] [cpd] Parse error with backslashes in string literals[/-] [+][matlab] CPD: Parse error with backslashes in string literals[/+] on Oct 29, 2026
  7. changed the title [-][matlab] CPD: Parse error with backslashes in string literals[/-] [+][matlab] CPD: Parse error with backslashes in character vectors/string literals[/+] on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:bugin:cpd

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions