This is the BNF that describes the Katydid grammar.
Whitespace can contain a comment or one or more tabs, spaces or newlines.
Comments can be block of line comments.
Examples
Throughout the rest of this specification the white spaces have been omitted, for the sake of brevity. Please see the BNF for the full specification, which includes all the allowable white spaces.
An identifier starts with an english alphabet letter or underscore, but can be followed by an interleaved sequence of english alphabet letters, underscores and numbers.
Examples
All literals have been kept as close as possible to their golang syntax counterparts.
There are three possible representations of an integer literal: decimal, octal and hexadecimal. Any of these representations can be prefixed with a - indicating a negative number. Only octal and hexadecimal must be enclosed in parentheses and prefixed with the word int. Decimal may also be enclosed and prefixed, but it is not required.
Examples
An unsigned integer literal is an integer literal which is always enclosed in parentheses and prefixed with the word uint.
The integer literal should not include the negative sign.
Examples
Floating point numbers only need enclosing parentheses and a prefix of the word double, if no decimals or exponent are provided.
Examples
Strings in backticks and double quotes are supported.
Bytes can be composed into comma separated list of bytes, enclosed in curly braces and prefixed by []byte. Each byte can be a character or an integer literal.
Examples
A boolean value is a lowercase word: true or false.
Examples
A variable is a dollar followed by a type name. It specifies which Value method on the parser will be called.
Examples
A terminal is a variable or any literal.
An expression is either a terminal, list or function.
A list of expressions is a comma separated list of expressions.
A list is zero or more expressions that are enclosed by curly braces and prefixed by a ListType.
Examples
A built in function is a well defined prefix followed by an expression.
Examples
A name expression can be:
- any name indicated by an underscore;
- a specific name, specified by a literal or an identifier;
- anything except the specified name expression, which is specified using an exclamation mark and enclosing parentheses;
- a list of alternative name expressions, which is specified with grouping parentheses and delimited with pipe characters.
The identifier is used as a shorthand for some strings.
Examples
A Pattern is the union of all pattern types.
The syntactical difference between a Pattern and DepthPattern will be discussed later. This pattern is equivalent to, but not the same as the one specified in the BNF. This was done for the sake of keeping it simple, please see the BNF for a more accurate representation.
ZAny specifies zero or more of anything, very much like .* in a regular expression.
A tree node consists of a name and a pattern.
A DepthPattern has enough syntactical clues to indicate that, given a name expression, a tree node is specified. Other patterns require a colon between the name expression and the pattern.
Examples
A LeafNode applies a function, that returns a boolean, to a value. A value is a tree node without any children, which can be some type of number, a string, bytes or a boolean. These functions can be composed with other functions, see the built in functions or you can even add your own functions. Some of the built in functions also have shorthand version.
Examples
Empty is used internally to indicate that the pattern has been satisfied. It is also useful in the specification of XML trees, or to indicate an empty structure or list.
Concat is used to describe an ordered sequence of patterns. This is done by concatenating 2 or more patterns together.
Examples
Interleave is used to describe an unordered list of patterns.
Examples
Contains only cares about satisfying one pattern in sequence of nodes. It is a shorthand for [*, Pattern, *].
Examples
Or specifies a list of alternative patterns.
Examples
And specifies a list of intersecting patterns.
Examples
ZeroOrMore specifies a repeated pattern that repeats zero or more times.
Examples
Optional is shorthand for (Pattern|<empty>).
Examples
Not is used to take the complement of a pattern.
Examples
Reference is used as an in place reference to a predefined pattern or pattern declaration.
Examples
Reference is A grammar consists of a list of pattern declarations. A pattern declaration consists of an name and a pattern. A grammar can start with one pattern which does not have a name. It is implied that this first pattern’s name is main. as an in place reference to a predefined pattern or pattern declaration.
Examples
Dragons can view the BNF source here.