Grouping
[This content is no longer valid. For the latest information on "M", "Quadrant", SQL Server Modeling Services, and the Repository, see the Model Citizen blog.]
You can group terms in a production together to control operator precedence in much the same way as you use grouping in arithmetic expressions. Grouping is done by surrounding terms in a production with parentheses, and any operations specified inside the parentheses are done first, before operations outside the parentheses are applied to the results of the inside operation.
Examples
Here are two examples using grouping.
"Not" and "Or"
This example controls the order in which the "not" (^) and "or" (|) operators are applied. The code says that when tokenizing the input character stream, the CharacterSimple
token is inserted if the current character is not any of the characters within the parentheses, assuming that no other token rule takes precedence.
token CharacterSimple
= ^(
'\u0027' // Single Quote
| '\u005C' // Backslash
| '\u000A' // New Line
| '\u000D' // Carriage Return
| '\u0085' // Next Line
| '\u2028' // Line Separator
| '\u2029' // Paragraph Separator
);
Operator Precedence
The following example is taken from the grammar that defines MGrammar itself.
token Identifier = '@'? Letter (Letter | Digit)*;
This rule states that when tokenizing the input character stream, an Identifier
token is inserted when the following is recognized in order:
An optional
‘@’
character.A
Letter
symbol (probably a token).0 to many instances of either the
Letter
or theDigit
symbol.
In this expression, the parentheses are used to ensure that the |
operator is applied to a Letter
or Digit
symbol before the *
is applied. The result is that as each input character is read, it is first tested by the |
operator, and if it passes the test, then the *
operator is evaluated.