On this page:
2.1.1.1 Names
2.1.1.2 String Literals
2.1.1.3 Number Literals
2.1.1.4 Boolean Literals
2.1.1 Primitives and Literals

There are several different literal token types referred to in this documentation.

2.1.1.1 Names

Names in Pyret match the following regular expression:

^[_a-zA-Z][_a-zA-Z0-9]*(?:-+[_a-zA-Z0-9]+)*

The convention in Pyret is that kebab-case-names are used for names of values and fields, and TitleCaseNames are used for annotations.

That is, they start with an alphabetical character or an underscore, followed by any number of alphanumeric characters mixed with underscores and hyphens, ending in a non-hyphen. So, for example, the following are valid names (though not necessarily good style):

a a1 a-1 abc ABC a----------b a-_-_-_-__--b a--_ _a __

The following are not valid names:

_- -_ a- -a -a- -abc a1- a-1- a_1- α 1abc $abc

2.1.1.2 String Literals

‹string-expr›: STRING

Strings in Pyret come in several forms. First, they can be enclosed in double quotes:

"a string" "a string\" with escapes" "'single quotes' are allowed unescaped or \' escaped"

They can also be enclosed in single quotes:

'a string' 'a string\' with escapes' '"double quotes" are allowed unescaped or \" escaped'

String literals with single or double quotes must terminate by the end of the line:

"multi-line strings not allowed with double quotes"

Finally, multi-line string literals can be created by starting and ending them with three backticks (). For example:

``` This string spans multiple lines ```

Multi-line string literals strip all whitespace before the first non-whitespace character and after the last non-whitespace character. All whitespace at the beginning of intermediate lines is preserved.

2.1.1.3 Number Literals

‹num-expr›: NUMBER

Pyret has several types of number literals. The most traditional allows for decimal numbers, negation, and an exponent:

^[-+]?[0-9]+(?:\\.[0-9]+)?(?:[eE][-+]?[0-9]+)?

That is, an optional sign, then some number of digits, optionally followed by a decimal point and more digits, optionally followed by an exponent. These are valid number literals:

0.1 1 1e100 1.1e100 +1.1e100 -1.1e-100 1.1230e-0 10 19 19.0

Note that a number literal cannot start with a decimal point; some leading digits are required. These are not number literals:

.1 1.1.1 1.+1 0.1+100

This first kind of number literal represents an exact number, or Exactnum. Number literals can also be prefixed with a tilde, to indicate that the number is an approximation, or a Roughnum. So these are all valid rough number literals:

~0.1 ~1 ~1e100 ~1.1e100 ~+1.1e100 ~-1.1e-100 ~1.1230e-0 ~10 ~19 ~19.0

And these are not valid:

~.1 ~1.1.1 ~1.+1 ~0.1+100

Finally, numbers can be written as exact ratios of whole numbers:

^[-+]?[0-9]+/[0-9]+

These numbers are interpreted as Exactnums. These are valid rational literals:

1/2 -1/2 +1/4 1234/9 0/1234

It is a syntax error to use zero as the denominator in a fraction literal. These are not valid rational literals:

1+1/2 -1/0 1.1/9 1/-3

2.1.1.4 Boolean Literals

‹bool-expr›: true | false

Boolean literals are the lowercase words true and false.