Match Mode Pattern "P."


I want to introduce a new pattern “P.” which should be colorized with the link class.

Hence I wrote this line:
function tokenBase(stream, state) {
var ch =;
if (ch == ‘"’ || ch == “’”) {
state.tokenize = tokenString(ch);
return state.tokenize(stream, state);
} else if (ch == “.” && stream.match(/^\d[\d_]*(?:[eE][±]?[\d_]+)?/)) {
return ret(“number”, “number”);
} else if (ch == “.” && stream.match("…")) {
return ret(“spread”, “meta”);
} else if (/[[]{}(),;:.]/.test(ch)) {
return ret(ch);
} else if (ch == “=” &&">")) {
return ret("=>", “operator”);
} else if (stream.match(“P”)) {
** return ret(“link”, “link”);**


} else if (stream.match("P.")){
			return ret("atom", "atom");

But if I write now in the editor P. -> no change happens?


The "P" will always match before the "P." is even tried, so you’ll never reach the branch that returns "atom".

Ok. But how to solve now this issue with the P. Pattern? I’ve to mark P. with a different color…

As well as:


I mangaged it to fix the SW CS… fixed pattern. But it’s still not working with P. and RS_* where I do not know how the pattern ends…

This really seems more of a basic programming problem than a CodeMirror problem. Switching the if clauses for P and P. might help, but I think you’ll have a hard time writing a language mode by trial-and-error editing of an existing mode.

Thanks Marijn for your reply.

I know that I’m going to have a hard time :frowning: … But I’m not able to escape - since I need it for my job.

As I told you, I managed now to fix the static pattern - but I’ve still my issues with the X. Notation and RS_* where I do not know how to color the literals which I do not know yet - in fact I just want to colorize words which are starting with RS_*…

If you support I can sent you an excellent bottle of Austrian white vine :wink:

You can pass a regular expression like /RS_\w+/ to match to match any word starting with RS_. But I have no idea what your other problem is and, wine or no wine, I’m not really prepared to coach you though this.


that’s totally fine for me. I just need to know how to put this RegEx in the mode correctly.

My first try was:

	if (stream.match(/RS_\w+/)) {
			return ret("oliv", "oliv");

But this didn’t work out…



I managed now to match the pattern with:

		var regex = new RegExp("(RS_\\w+)", "g");
		if (stream.match(regex)) {
			return ret("oliv", "oliv");

The return ret (…) will be triggered. The String within the editor starts to change color, but in the end it’s red again. Could it be that it will be overloaded by the JS Mode afterwards?