Highlighting OCaml attributed expressions? #3415
Replies: 1 comment 2 replies
-
Yes, and no. It's impossible to count arbitrarily nested expressions with JS regexes, that's correct. However, it is possible to count until a finite maximum depth (e.g. 4 to 8), which is typically good enough for syntax highlighting in practice. We do this trick in a few languages (e.g. C#). However, this trick as a cost: large regular expressions. This can be a problem because:
I also want to point out another you could do. Prism doesn't actually require regexes to do that text matching, it only requires something that behaves like a regex (see #2595). So you could write an interface RegExpLike {
global: true
lastIndex: number;
exec(value: string): RegExpExecArray | null;
} You can then use the class like a regex: Prism.languages.ocaml = {
// ...
'attribute': {
pattern: new OCamlAttribute(),
inside: { /* ... */ }
}
}; You can also implement this as a single function using the However, there are downsides to this approach as well:
I hope this helps. |
Beta Was this translation helpful? Give feedback.
-
I want to highlight OCaml code that contains attbirutes in a specific way. Given a program like this:
I want:
where the attribute
[@x ]
as well as the parentheses around it are removed and the attributed expression(if true then (2 + 3) * 4 else 5)
is highlighted in a specific color. Since an attributed expression can contain arbitrarily large expressions with possibly matching parentheses, and it is impossible to detect matching parentheses using regular expressions, I assume it is impossible to do such highlighting using prism.js. Is this correct? (If there is any way, please let me know.) Thanks in advance.Beta Was this translation helpful? Give feedback.
All reactions