Skip to content

Discuss: Custom token callbacks, or mode value property? #1133

Closed
@foo123

Description

@foo123

Hello, i'm playing around with highlight.js (9.2.0) and want to integrate with a -grammar add-on (following previous work on syntax-highlighting, for example here) which enables to syntax-highlight code by defining a grammar specification for the language (e.g in BNF form).

i have already made some integration code (to be uploaded here), but so far, in order to use the grammar parser and integrate with hljs core highlighter some mode boilerplate code (for example multiple modes inside contains and dummy ;lexemesRe, beginRe, endRe functions). While it would be easier (and more flexible) if there was some property that allowed a callback or even a static value with the lexeme (i,e token) to be directly available from the mode itself (and passed directly to highlighter to be wrapped in <span>[token value]</span> for highlight).

To be more explicit, consider the fragment from hljs highlight method below:

// ..
    var mode_buffer = '';
    var relevance = 0;
    try {
      var match, count, index = 0;
      while (true) {
        top.terminators.lastIndex = index;
        // it seems the only way is to override mode.terminators.exec
       // in order to hook here with the token
       // maybe accepting an already parsed value/callback would make all this more flexibe
      // e.g if ( mode.value ) count = processLexeme(value.substr(index, match.index - index), mode.value());
      // or sth like that
        match = top.terminators.exec(value);
        if (!match)
          break;
        count = processLexeme(value.substr(index, match.index - index), match[0]);
        index = match.index + count;
      }
      processLexeme(value.substr(index));
      for(current = top; current.parent; current = current.parent) { // close dangling modes
        if (current.className) {
          result += '</span>';
        }
      }
      return {
        relevance: relevance,
        value: result,
        language: name,
        top: top
      };
    } catch (e) {

// ..

Nikos

Metadata

Metadata

Assignees

No one assigned

    Labels

    parserpluginSpecific plugin or plugin discussion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions