1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-28 16:23:15 +00:00
TiddlyWiki5/editions/dev/tiddlers/from tw5.com/moduletypes/WikiRuleModules.tid
2021-01-21 16:57:12 +00:00

33 lines
2.4 KiB
Plaintext

created: 20130825214700000
modified: 20210121054620146
tags: dev moduletypes
title: WikiRuleModules
WikiRuleModules have a `module-type` of `wikirule`, and may have following types: `pragma`, `block`, `inline`. Modules of these types encapsulate the logic of individual parsing rules used by the WikiParser engine. For example, `entity.js` rule module identifies references to HTML entities by matching the pattern `&<chars>;`.
Pragma rules are applied at the start of a block of text, and cover definitions and declarations that affect the parsing of the rest of the text. Block rules are only applied at the beginning of a block of wikitext, while inline rules can appear anywhere. The only current example of a pragma rule is for macro definitions.
Examples of block rules:
* Headings
* Tables
* Lists
Examples of inline rules:
* Entities
* HTML tags
* Wiki links
Parser rule modules extend the `$tw.WikiParserRule` class. This is done by instantiating the class and then copying the exports of the rule module onto the instance. In this way, the parser rule can override the base behaviour of the `$tw.WikiParserRule` class. In particular, the base class incorporates logic for using regular expressions to match parse rules but this logic could be overridden by a parse rule that wanted to, say, use `indexOf()` instead of regular expressions.
The standard methods and properties of parser rules are as follows:
* `name`: a string containing the name of this parse rule
* `types`: an object that defines the module's type. For example, `exports.types = {block: true};`
* `init(parser)`: initialisation function called immediately after the constructor with a pointer back to the parser containing this rule
* `findNextMatch(pos)`: returns the position of the next match after the specified position
* `parse()`: parses the most recent match, returning an array of the generated parse tree nodes. Pragma rules don't return parse tree nodes but instead modify the parser object directly (for example, to add local macro definitions)
The built in parser rules use regular expression matching. Such rules can take advantage of the implementation of `findNextMatch()` in the base `$tw.WikiRule` class by ensuring that their `init()` method creates a `matchRegExp` property containing the regular expression to match. The `match` property contains the details of the match for use in the `parse()` method.