Demonstration of applying optics to do interesting things with AST's

AST Syntax Highlighting with Optics

Would it be possible, and feasible, to express syntax highlighting rules via optics?

Current Achievment

Given an Abstract Syntax Tree stub of this form:

let ast = Root(block: .blockquote(.table(.code("Hello"))))

We can express getting to the inline .code inside a .table inside a .blockquote from the document root like so:

let deepReach = Root.blockLens
  >>> /BlockToken.blockquote
  >>> /BlockToken.table
  >>> /InlineToken.code

let value = deepReach.tryGet(ast) // → "Hello"

References and Inspiration

GitHub

View Github