Override style in special cases in SQL mode for keywords and builtin words

I defined my own SQL MIME types so that SQL functions like min, max, ucase are identified as keywords (cm-keyword style will be applied; in my case it’s uppercased and purple) and SQL keywords like select, from, order as builtin (cm-builtin style will be applied; in my case it’s uppercased and darkblue).
However in Access ASC is both a SQL function (it returns the ASCII code for a char) and a “SQL keyword” (for sorting a record set ascending) as can be seen in following example:

Notice that ASC, in the query above, is in both cases identified as builtin. I want the first ASC (in the SELECT) to be identified as a SQL function and therefore cm-keyword.
How can I override the style that when ASC(…) is being typed cm-keyword is used and when ASC without (…) is typed cm-builtin is used?
The solution of course still needs to work if copy/paste is done or when the text changes for whatever reason.

I managed to make it myself. I’ll document it here for future generations :slight_smile:

In sql.js I changed the last lines of the parsing in tokenBase:

} else {
  stream.eatWhile(/^[_\w\d]/);
  var word = stream.current().toLowerCase();
  // dates (standard SQL syntax)
  // ref: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-literals.html
  if (dateSQL.hasOwnProperty(word) && (stream.match(/^( )+'[^']*'/) || stream.match(/^( )+"[^"]*"/)))
    return "number";
  if (atoms.hasOwnProperty(word)) return "atom";
  if (builtin.hasOwnProperty(word)) {
    if ((stream.peek() == "(") && (keywords.hasOwnProperty(word))) {
      return "keyword";
    }
    else {
      return "builtin";
    }
  }
  if (keywords.hasOwnProperty(word)) {
    return "keyword";
  }
  if (client.hasOwnProperty(word)) return "string-2";
  return null;
}

My only question is if this is the proper way to do it. In my solution I don’t like the fact that I am changing the innerworking’s of the parser. Is there a cleaner/better way?

1 Like

I like the way you handled this special keyword. Have you made additional changes to sql.js to work with Access/Jet SQL? I want to use CodeMirror within an Access add-in to edit queries.