1

I've built some new Ace Editor Modes for my custom language (JMS Message representations) with a sophisticated state machine. Now it would be great to reuse that syntax highlighting also to create the errors. Is that possible ?

In other words, let's say my syntax highlighting creates 'invalid' tokens and I want to use the line number of that token to flag an error and then do something like this: https://github.com/ajaxorg/ace/wiki/Syntax-validation

The simplest format is the HEX format:

this.$rules = {
    "start": [
        { regex: /[!#].*$/, token: "comment" },
        { regex: /^0x[0-9a-f]+:/, token: "constant" }, // hex offset
        { regex: /(?:[0-9a-fA-F]{4} |[0-9a-fA-F]{2} )/, token: "constant.numeric" }, // hex value
        { regex: /[\S ]{1,16}$/, token: "string" }, // printable value
        { regex: "\\s+", token: "text" },
        { defaultToken: "invalid" }
    ]
};

And let's say the editor created this state with an invalid token in line 4:

enter image description here

Is there a (preferably easy) way to get to the line numbers of my invalid tokens ? Or to reuse my $rules state machine for syntax checking ?

1 Answer 1

2

Found it - I must admit, Ace Editor is really good stuff. Always works as expected.

What works for me, after computing the tokens of the document with the rules state machine, I iterate through all tokens and find the once that are 'invalid' and then set annotations on those lines. Initially simply 'Syntax error' but different types of 'invalid' could mean different things in the future. This way I only have to write the validation syntax validation once.

aceEditor.session.on('change', function(delta) {

  var sess = aceEditor.session;
  sess.clearAnnotations();
  
  var invalids = [];
  for( var row=0;row<sess.getLength();row++ ) {
    
    var tokens = sess.getTokens(row);
    if( !tokens ) continue;
    
    for( var t=0;t<tokens.length;t++ ) {
    
      if( tokens[t].type==="invalid" ) {
      
        invalids.push({ row: row, column: 0, text: "Syntax error", type: "error" });
      }
    }
  }
  sess.setAnnotations( invalids );
});

There might be a smarter way to do this (maybe an onToken(type,row,column) function somewhere ?), but above works for me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.