Using Pegs in Janet
a day ago
- #Janet
- #PEGs
- #Parsing
- Janet uses parser expression grammars (PEGs) instead of regular expressions.
- PEGs in Janet are defined using associative data structures with named rules.
- Rules can refer to other rules, including recursively, and can run arbitrary functions.
- A simplified HTML subset can be parsed using sequences, choices, captures, and back-references.
- The :main rule is the starting point for parsing in Janet.
- Rules like :tagged, :open-tag, :close-tag, and :value are defined to parse HTML tags and content.
- Match-time captures (cmt) and back-references are used to ensure tag names match in opening and closing tags.
- The :value rule handles nested tags and untagged content using the any and + combinators.
- The :untagged rule matches characters that are not part of a tag.
- Captures can be structured into data using replace and struct functions.
- PEGs can be compiled for better performance using peg/compile.
- Example usage shows parsing HTML strings into structured data.