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, basically it is a copy from the system 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:
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!