# Help wanted on understanding how it applies to syntax highlighting

**URL:** <https://discuss.codemirror.net/t/help-wanted-on-understanding-how-it-applies-to-syntax-highlighting/2274>\
**Category:** Lezer\
**Created:** [January 22, 2020, 2:34pm UTC](https://discuss.codemirror.net/t/help-wanted-on-understanding-how-it-applies-to-syntax-highlighting/2274 "2020-01-22T14:34:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![qbane](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/qbane/32/703_2.png) [@qbane](https://discuss.codemirror.net/u/qbane)\
**Post date:** [January 22, 2020, 2:34pm UTC](https://discuss.codemirror.net/t/help-wanted-on-understanding-how-it-applies-to-syntax-highlighting/2274/1 "2020-01-22T14:34:26Z")

</div>

I created a small repository for learning how Lezer works, but I felt that there is a large gap from parsing the document to put it into real use cases. So I came here to ask for clarifications.

The repo: [https://github.com/andy0130tw/try-lezer](https://github.com/andy0130tw/try-lezer), basically it is a copy from the [system guide](https://lezer.codemirror.net/docs/guide/).

I know I can invoke `iterate` on the root tree that implements `SubTree` interface. So the following code snippet produces the following verbose, in comment:

```auto
import {parser as parserExpr} from './parsers/expr'
const nodeTypeRepr = type => `${type.id}:${type.name}`

const tree = parserExpr.parse('(8+9)*17+20*20')

tree.iterate({
  // from: 0,
  // to: tree.length,
  enter: (type, start, end) => {
    console.log('enter', nodeTypeRepr(type), start, end)
  },
  leave: (type, start, end) => {
    console.log('leave', nodeTypeRepr(type), start, end)
  },
})

/* Output:
enter 4:BinaryExpression 0 14                                                                             
enter 4:BinaryExpression 0 8                                                                              
enter 4:BinaryExpression 0 5                                                                              
enter 4:BinaryExpression 1 4                                                                              
enter 3:Number 1 2
leave 3:Number 1 2
enter 3:Number 3 4
leave 3:Number 3 4
leave 4:BinaryExpression 1 4
leave 4:BinaryExpression 0 5
enter 3:Number 6 8
leave 3:Number 6 8
leave 4:BinaryExpression 0 8
enter 4:BinaryExpression 9 14
enter 3:Number 9 11
leave 3:Number 9 11
enter 3:Number 12 14
leave 3:Number 12 14
leave 4:BinaryExpression 9 14
leave 4:BinaryExpression 0 14
leave 4:BinaryExpression 0 14 
*/

```

My question is, how I can make use of this to do syntax highlighting, or, pretty printing, etc.? If I would like to tackle with this myself, my approach should definitely be focusing on the leaf nodes only, for instance, producing a list of (type, position) pairs. But it turns out that no such functionality is in store. I wonder which part is wrong in my thoughts. Thanks!

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)\
**Post date:** [January 22, 2020, 2:52pm UTC](https://discuss.codemirror.net/t/help-wanted-on-understanding-how-it-applies-to-syntax-highlighting/2274/2 "2020-01-22T14:52:00Z")

</div>

You can see [this code](https://github.com/codemirror/codemirror.next/blob/732d88fd127a1f24120c70473f5571654b857ea4/highlight/src/highlight.ts#L272) that does highlighting based on a tree. Basically it just emits spans based on the innermost styled node that covers a given range of code (though there’s also a bunch of subtle stuff going on related to style matching and inherited styles).

Using this for pretty-printing is a whole different thing, and I haven’t really thought about that.

---

<div class="post-metadata">

**Author:** ![nikku](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/nikku/32/693_2.png) [@nikku](https://discuss.codemirror.net/u/nikku)\
**Post date:** [January 26, 2020, 8:39pm UTC](https://discuss.codemirror.net/t/help-wanted-on-understanding-how-it-applies-to-syntax-highlighting/2274/3 "2020-01-26T20:39:36Z")

</div>

I’ve implemented the approach @marijn mentions [in a small app](https://nikku.github.io/feel-playground/) and it works perfectly fine.

First I iterate over a lezer tree and collect all tokens that are worth syntax highlighting. Each token is a `{start, end, tokenType}` element (cf. [here](https://github.com/nikku/feel-playground/blob/master/src/App.svelte#L236)).

For each token worth highlighting I insert spans with `mark-{tokenType}` as a class name. In my case, using `CodeMirror@5` `CodeMirror#markText(...)` does the job for me (cf. [here](https://github.com/nikku/feel-playground/blob/master/src/App.svelte#L142)).

The performance is pretty bad, as I’m creating hierarchies of `<span/>` elements. If I need to highlight the function call `foo(a)`, the result will be something like:

```auto
<span class="mark-function">
  <span class="mark-keyword">
    foo
  </span>
  (
  <span class="mark-parameters">
    <span class="mark-name">
      a
    </span>
  </span>
  )
</span>

```

A maybe cheaper way is to have one level of `<span>` only and make nested tokens inherit parent marks. We essentially wrap each token with only one `span` and compose the classes instead:

```auto
<span class="mark-function mark-keyword">
  foo
</span>
<span class="mark-function">
  (
</span>
<span class="mark-function mark-parameters mark-name">
  a
</span>
<span class="mark-function">
  )
</span>

```

---

<div class="post-metadata">

**Author:** ![qbane](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/qbane/32/703_2.png) [@qbane](https://discuss.codemirror.net/u/qbane)\
**Post date:** [February 1, 2020, 8:19pm UTC](https://discuss.codemirror.net/t/help-wanted-on-understanding-how-it-applies-to-syntax-highlighting/2274/4 "2020-02-01T20:19:35Z")

</div>

Thanks. With these concrete examples, I think I understand better now. As @nikku pointed out, it is generally not effective to use `tree.iterate` to re-form a tree. More efforts are required (than I had imagined), like maintaining a stack, flatten nested tokens, to have the job done.
