1

Just to start off, I'm using regexr.com as reference (writing the highlighting in Javascript). And a very important thing is: I'm not writing syntax highlighting for Javascript. I'm writing for a language called gLUA, which is a very strict type of language, example code can be found here.

I have a project I'm using PrismJS for, with its syntax highlighting features. I had to make my own with regex (code below), and now I'm stuck at one part, namely two selectors clashing one another. Syntax highlight looks like this with both rules.

If I remove the ( from the punctuation regex selection, everything turns out fine (except the parenthesis, that is...)

I need the (?=\() selection for the function, or else I'm going to have a really bad time working around it. I've already spend several hours figuring it out, so I'd rather not poke on it. Are there any other workarounds? I love the gray color on the parentheses...

// DONE
'directive': /@(name.*|model.*|inputs|outputs|persist|trigger|autoupdate)/,

//DONE
'type': /\b(angle|array|bone|complex|entity|matrix[24]?|number|quaternion|ranger|string|table|vector[24]?|wirelink)(?!\()\b/ig,

//DONE
'keyword': /\b((else)?if|else|for(each)?|while|break|continue|local|switch|case|default|function|return)\b/g,

//ERROR - CLASHING WITH FUNCTION BECAUSE OF "("
'punctuation': /[()\[\]{};:,]/,

//DONE
'operator': /[-+=*/%]/,

//DONE WITH MARGIN OF ERROR - SINGLE POUND CONSUMES NEXT LINE IF NO COMMENT IS ADDED AFTER POUND
'comment': /#([^\[].*?|\[[\s\S]*?]#)\b/g,

//DONE WITH MARGIN OF ERROR
'variable': /\b[A-Z]\w*/,

//DONE
'constant': /_[A-Z0-9_]+/,

//ERROR - CLASHING WITH THE PUNCTUATION "(" BECAUSE OF SELECTION (?=\() IN REGEX
'function': /\b(?!(else)?if|else|for(each)?|while|break|continue|local|switch|case|default|function|return)([a-z]\w*)(?=\()/,

//DONE
'number': /\b[-+]?[0-9]*\.?[0-9]+\b/,

//DONE
'string': {
    pattern: /("|')(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/ig,
    greedy: true
}
Revolution
  • 11
  • 1
  • 3
  • don't know about the whole thing, but `...keyword... (?=\()` doesn't look right to me. – georg Jun 01 '17 at 12:14
  • Sharp eye there, the `'keyword': ...` shouldn't have `(?=\()`, but rather just a `\b` so that `case`, `break` and `local` can be highlighted too – Revolution Jun 01 '17 at 12:49
  • It is much more complex than that: a function does not need to have parentheses following it, as functions can be passed around, just like other objects. It's only when you *invoke* one that there will be parentheses (and even then you can [invoke functions without using parentheses](https://stackoverflow.com/questions/35949554/invoking-a-function-without-parentheses)). BTW, such parentheses can have white space around them. – trincot Jun 01 '17 at 13:10
  • I've forgot to mention an important thing - what I'm writing syntax highlighting for is a very strict language where: 1. you cannot pass around functions, 2. Variables must be defined when declared, and they must start with a capital letter, 3. this language has far less functionality than your good old Javascript. The only thing I'm struggling with is the regex for `functions` clash with `punctuation` – Revolution Jun 01 '17 at 13:14

0 Answers0