regexdelphigoogle-code-prettify

google code prettify Delphi comment


I'm trying to get the google code prettify working showing something similar to the Delphi IDE and ran into a problem with the comments

This is a comment:

{text in here}

This is a compiler directive (not a comment):

{$text in here}

Does anyone know how to change the comment section below so that if the character after { is the $ character then it is not a comment

This is the lang-Pascal.js that I've been using

PR.registerLangHandler(
    PR.createSimpleLexer(
        [ // shortcutStylePatterns
          // 'single-line-string'
          [PR.PR_STRING,        /^(?:\'(?:[^\\\'\r\n]|\\.)*(?:\'|$))/, null, '\''],
          // Whitespace
          [PR.PR_PLAIN,         /^\s+/, null, ' \r\n\t\xA0']
        ],
        [ // fallthroughStylePatterns
          // A cStyleComments comment (* *) or {}
          [PR.PR_COMMENT,       /^(?:\/\/[^\r\n]*)|\(\*[\s\S]*?(?:\*\)|$)|^\{[\s\S]*?(?:\}|$)/, null],
          //[PR.PR_COMMENT,       /^\(\*[\s\S]*?(?:\*\)|$)|^\{[\s\S]*?(?:\}|$)/, null],
          [PR.PR_KEYWORD,       /^(?:TRY|EXCEPT|PUBLIC|PRIVATE|CLASS|PROPERTY|ABSOLUTE|AND|ARRAY|ASM|ASSEMBLER|BEGIN|CASE|CONST|CONSTRUCTOR|DESTRUCTOR|DIV|DO|DOWNTO|ELSE|END|EXTERNAL|FOR|FORWARD|FUNCTION|GOTO|IF|IMPLEMENTATION|IN|INLINE|INTERFACE|INTERRUPT|LABEL|MOD|NOT|OBJECT|ON|OF|OR|PACKED|PROCEDURE|PROGRAM|RECORD|REPEAT|SET|SHL|SHR|STRICT|THEN|TO|TYPE|UNIT|UNTIL|USES|VAR|VIRTUAL|WHILE|WITH|XOR)\b/i, null],
          [PR.PR_LITERAL,       /^(?:true|false|self|nil)/i, null],
          [PR.PR_PLAIN,         /^[a-z][a-z0-9]*/i, null],
          // Literals .0, 0, 0.0 0E13
          [PR.PR_LITERAL,       /^(?:\$[a-f0-9]+|(?:\d+(?:\.\d*)?|\.\d+)(?:e[+\-]?\d+)?)/i,  null, '0123456789'],
          [PR.PR_PUNCTUATION,   /^.[^\s\w\.$@\'\/]*/, null]
        ]),
    ['pascal']);

Solution

  • Since the syntax is JS (and the file is lang-Pascal.js), I also assume the regex is ECMAScript 5 compliant.

    A very simple fix is to add a (?!\$) negative lookahead after {:

    ^(?:\/\/[^\r\n]*)|\(\*[\s\S]*?(?:\*\)|$)|^\{(?!\$)[\s\S]*?(?:\}|$) 
                                                ^^^^^
    

    See the regex demo

    This lookahead will fail the match if there is a $ after a literal { that is at the beginning of the string/line.