-
Notifications
You must be signed in to change notification settings - Fork 14
Syntax patterns
The real power of Skript comes from its intuitive design, mainly featuring the feature-rich syntax that feels really close to plain English. To achieve this, skript-parser has a lot of functionalities in place to create 'syntax patterns'. Each pattern can consist of multiple elements that interact with each other. The user will make the final decision on how he will use the different patterns in his code.
This means that the developer can create multiple possibilities, while the user can choose which to use and how to use it. Both parties are happy!
If you want to learn how the patterns are actually matched against each other, together with a briefer explanation of the different pattern elements, refer to 'Pattern matching'.
Developers can choose between multiple types of elements. These elements together will form the pattern. The user input will be matched, line by line, against the stored elements. Each element has its own strategy when matching. If a match fails, the parser moves on to the next pattern.
Text elements consist of a plain text instance. The matched string must contain every character of this text instance, otherwise, the match will fail.
Suppose we have the following inputs with the pattern attacker
:
attcker # fail
attacker # success
An optional group consists of a simple element, or multiple, that can be omitted if desired by the user. It usually serves as a way to give more variety to the syntax, though it can be used to add optional parameters. It is indicated by square brackets (ex: [optional]
) around the optional elements.
Suppose we have the following inputs with the pattern [the] attacker
:
the attacker # success
attacker # success
the attcker # fail
As one can see, a popular use is the addition of optional definite and indefinite articles as a cosmetic feature to the code. Users writing an application can opt for the more English-friendly syntax with articles.
Choice groups are indicated by two parentheses, and each choice is separated by a pipe |
(ex: (choice|option)
). When writing an application, either choice can be written. It also usually serves as a way to give more variety to the syntax.
Suppose we have the following inputs with the pattern [the] (attack|strike) damage
:
the attack damage # success
strike damage # success
Note that developers have the option to use optional and choice groups mixed together. The following patterns are all valid:
[the] [(attack|strike)] damage
-
[the] ([absolute] attack|strike) damage
Thus, elements are quite flexible and allow developers to gain a lot of interaction with the user.
Regex elements are indicated by angled brackets (ex: <regex>
). The enclosed text must represent a regex pattern that will be tested at parse time. This is useful when the other pattern elements can't represent the desired syntax properly (usually when the desired result can only be modelled by regex quantifiers). This should only be used if it must be, as skript-parser handles the other pattern elements much more efficiently.
Especially be careful when using all-around regex patterns like <.+>
at the start of your syntax pattern. Make sure to design the other elements around your regex pattern, and not vice-versa. The parser especially likes text elements as a reference point!
These are