3

I have already written a non-trivial custom language in OCaml. I know this way purely in JavaScript to make Monaco Editor register a new language. However, I don't know how to leverage the language defined in OCaml while registering it in Monaco Editor.

For example, we have already defined tokens in OCaml, how could we not define those tokens again inside monaco.languages.setMonarchTokensProvider?

I make a small example here: https://github.com/chengtie/mylang-monaco-editor. A very simple language is defined in OCaml under mylang/, where there are INT and BOOL. In example/index.js, we have the following code to extend Monaco Editor and to achieve a basic highlighting. I feel it is NOT good to define those tokens twice.

monaco.languages.register({ id: 'mySpecialLanguage' });

// Register a tokens provider for the language
monaco.languages.setMonarchTokensProvider('mySpecialLanguage', {
  tokenizer: {
    root: [
      [/[0-9:]+/, "INT"],
      [/FALSE|TRUE/, "BOOL"],
    ]
  }
});

// Define a new theme that contains only rules that match this language
monaco.editor.defineTheme('myCoolTheme', {
  base: 'vs',
  inherit: false,
  rules: [
    { token: 'INT', foreground: '008800' },
    { token: 'BOOL', foreground: 'ff0000' },
  ]
});

Does anyone know how to link the language defined in OCaml to Monaco Editor?

SoftTimur
  • 5,630
  • 38
  • 140
  • 292

0 Answers0