Lexer Directives

Lexer directives start with percent (:%).

Lexer directives are designed to switch the language grammar to another predefined one. For example, let's consider a user-defined grammar named my_yaml:

:a : :%my_yaml( 
- name
- password
)
\< a 
# Prints ['name', 'password']

Schematically, U will execute the following steps:

%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#ffffff', 'mainBkg': '#cde9f4'}}}%% flowchart LR S(Parse with U <br/>':a : :%') --> A(Find grammar named<br/><my_yaml>) --> B("Parse with <my_yaml> <br/>'(- name...)'") --> E(Parse with U <br/>'\< a ...') style A fill:#FFFFFF,stroke:#333 style S fill:#FFFFFF,stroke:#333 style E fill:#FFFFFF,stroke:#333

Here's what happens:

  1. U lexer read ':a : ', then the aperture ':%'
  2. U lexer switches the language grammar to my_yaml
  3. The code following the directive until ')'is parsed according to the rules of the my_yaml grammar.
  4. While parsing, my_yaml uses U compiler to create an array with strings 'name', and 'password'
  5. Once parsing with my_yaml is complete, the lexer can switch back to the original grammar or another specified one.