3

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']);
J...
  • 30,968
  • 6
  • 66
  • 143
Dangas56
  • 849
  • 1
  • 8
  • 32
  • 1
    I've added the regex tag. You should also add a tag for the specific regex flavor used. While it's not set up for your use specifically, you should be able to modify `(?:[/]{2}[^\r\n]*)|^(?:\(\*[\s\w\d]+\*\))|(?:({[^$][\s\w]+}))` to work for you; it matches the `//`, `{}`, and `(* *)` comment markers (including those that are multi-line), but excludes `{$ }`. – Ken White Jun 22 '16 at 17:01

1 Answers1

1

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563