1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Pattern Calculator

Discussion in 'Script Tools' started by bi0, Jan 2, 2019.

  1. bi0

    bi0 Active Member

    Joined:
    Mar 26, 2017
    Messages:
    26
    Likes Received:
    11
    Skript Pattern Calculator

    The pattern calculator allows you to show all possible inputs for a given skript pattern.

    [​IMG]

    This tool is mainly useful for developers to check whether a pattern provides all the combinations that they would like. It is also here to raise some awareness on how quickly the amount of possible combinations grows if you add too many optional or choice elements in your pattern. Remember that the skript parser tries every possible combination of your pattern when it parses a skript (this is somewhat exaggerated, please read the next section for more informations)!

    TIP: To get an estimate of how good your pattern is you should only check the beginning of it until the first required element (either a choice group or a string which is not in an optional group)!


    Some notes on skript patterns


    When skript parses a script file it tries to match each line in the script against all registered patterns one after the other (there is some memoization going on but we ignore that for now). As soon as skript finds the right pattern it continues to the next line. But if the line does not contain a valid pattern it tries first all registered patterns until it runs out of options and concludes that there has to be an error in the line.

    This means that a pattern should fail fast on invalid input, allowing the skript parser to skip to the next pattern. And it should quickly parse correct patterns. Lets look at some examples to get an intuition for this:

    Code (Skript):
    1. [(all [[of] the]|the)] permissions (from|of) %players
    This pattern checks first if your code starts with "all", "all of the", "all the" or "the" and then continues with the "permissions ..." part. If it doesn't it checks if your code starts with "permission" and then goes on from there.
    This pattern is not that bad because there are only 10 possible combinations how you can write it. The issue is that the beginning of the pattern is the complex part. So the parser tries really hard to match the beginning of the pattern, this is NOT fail fast!

    Lets look at another pattern:
    Code (Skript):
    1. split %text% (at|using|by) [[the] delimiter] %text%
    This pattern just checks if the code starts with "split", if it doesn't it already knows that the code cannot possibly match the pattern! A good example of fail fast. Note that there are still 9 different inputs this pattern can match. But most of its complexity is towards the end of the pattern! This is better because once we know that the pattern begins with "split" we can be pretty sure that the code will match this pattern (in fact there is only one other possible expression that starts with "split" in skript and all its addons combined!).


    Some guidelines
    • Try to keep your patterns simple. Fewer combinations is better!
    • Avoid too many possible beginnings of your pattern (but remember that the priority should be to make the pattern as convenient to use as possible)
    • Don't use regexes at the beginning of your pattern!
    • Don't use many chained optional elements like: [x][y][z]
    • Prefer registering many simple patterns instead of a single complicated one (this makes reading your documentation also a lot easier!)
    • Avoid `%object%` elements in your pattern because then skript will have to match against all possible patterns again!

    Finally, take all this with a grain of a salt and don't spend too much time trying to optimise your pattern. Better be productive and add nice features to your addon.


    Further information
    You can find a list of all patterns registered on SkriptHub ranked by their combinations count HERE. But remember that many combinations does not necessarily mean that the pattern is bad! It is the complexity at the beginning that is most important! The first expression in the list has only two possible beginnings ("world creator" and "creator")! But number 10 on the list has 33!
     
Loading...