Dodo doc > Syntax > Literal Values > Pattern Literals


Pattern literals

Regular expressions

A regular expression is used to match a text string against a pattern. They are enclosed in forward slashes. The backslash is used to escape special characters in the regular expression or to start a command. The special characters are:

\	escape character
( open grouping expression
) close grouping expression
[ open character alternatives
] close character alternatives
^ beginning of line
$ end of line
| alternative
* repetition
+ non-empty repetition
? option
{ open repetition count
} close repetition count
. wildcard
/ end of regular expression

Examples:

/file[0-9]{3}\.txt/
/^(CA\$H)*$/
Rationale: regular expressions are complex enough to master, so dodo sticks to the standards to preserve the programmer's sanity.

Structured pattern

A pattern can be a single value, a tuple, a list or a map containing patterns. In the case of a map, only the values can be patterns. The expression matches if it is structured the same way as the pattern and has matching values.

Examples:

prettyHouse
(-1.0, 3.3, false)
[["a", "b"], ["c"], [\t]]
[firstName: /^F/, lastName: /^N[ae]/]
Rationale: structured patterns are helpful in many contexts. Without them, the expression would have to be broken down into its constituents before matching.

Variable pattern

Any expression can be matched to a variable name prefixed with "$". A new variable which is created for the match. If the variable name is an underscore "_", any value matches it.

In a list, the last item can be a variable pattern followed by "*" to denote any number of values. The associated variable is a list with all the remaining items.

Examples:

$person
[x, $tail*]
_
Rationale: the ability to create a variable for a match makes it much easier to extract values from an expression.

Extended variable pattern

An extended variable pattern starts with a question mark. It has three parts, all optional: the variable, the type and the state. At least one part should be specified. The different parts are separated with a forward slash.

The first part contains a variable pattern optionally followed by a list of attribute values. The second part is the same, but it matches the type of the expression. The third part matches the abstract state of the expression.

Examples:

?$house(wallColour: green)
?_(size: $fruitSize)/Fruit
?//at_rest
Rationale: the extended variable pattern helps deconstructing a value to match specific attributes. It offers an unified mechanism for testing attributes, type and abstract state.

Intervals

An interval defines a start value, an end value or both. The values must be Ordered (for pattern matching). An interval can also be used in a loop and in an index, where the values must be Enumerable. The start and end values must be of the same type if both are defined.

The bounds are included in the interval.

Examples:

1...10
...100
-0.8...

If the type of the values is Arithmetic the interval can be prefixed with a span size instead of specifying both a start and an end value. The span size is added to the start value or deducted from the end value to find the other bound. However, unlike an explicit start or end value, the calculated bound is excluded from the interval.

Similarly, when used in a loop or an index it is possible to prefix the interval with a range size instead of specifying both a start and an end value.

The span size is written with a star before the value and with braces around. There should be no space between the opening brace and star.

Examples:

{*4} 6...  # matches the span of values 6, 7, 8 and 9
[for i in {*4} 6...] # ranges over 6, 7, 8 and 9
Rationale: Matching against an interval often requires less typing than comparing values using "<=" and ">=". There are two ways to define an interval, either specify the start and end value (bounds) or specify just one bound and give a size. The former is the most widely used and has simple syntax, but dodo also allows the latter for convenience.

^ 2.4.5. Tuples

v 3. Functions and variables