Prefixes

Prefixes are one or more expression items that precede some other expression item, and are added to the list in property Prefixes in interface UniversalExpressionParser.ExpressionItems.IComplexExpressionItem for the expression item that follows the list of prefix expression items.

Note

Prefixes are supported only if the value of property SupportsPrefixes in interface UniversalExpressionParser.IExpressionLanguageProvider is true.

Currently Universal Expression Parser supports two types of prefixes:

1) Nameless brace as prefixes

Square or round braces expressions items without names (i.e. expression items that are parsed to UniversalExpressionParser.ExpressionItems.IBracesExpressionItem with property NamedExpressionItem equal to null) that precede another expression item (e.g., another braces expression, a literal, a code block, text expression item, numeric value expression item, etc) are parsed as prefixes and are added to expression item they precede.

  • In the example below the braces expression items parsed from “[NotNull, ItemNotNull]” and “(Attribute(“MarkedFunction”))” will be added as prefixes to expression item parsed from “F1(x, x2)”.

1[NotNull, ItemNotNull](Attribute("MarkedFunction")) F1(x, x2)
2{
3    // This code block will be added to expression item parsed from F1(x:T1, y:T2, z: T3) as a postfix.
4    retuens [x1, x2, x3];
5}

Click here to see the visualized instance of UniversalExpressionParser.IParseExpressionResult

2) Custom expressions as prefixes

If custom expression items of type UniversalExpressionParser.ExpressionItems.Custom.ICustomExpressionItem with property CustomExpressionItemCategory equal to UniversalExpressionParser.ExpressionItems.Custom.CustomExpressionItemCategory.Prefix are added as prefixes to expression item they precede.

Note

List of prefixes can include both nameless brace expression items as well as custom expression items, placed in any order.

  • In the example below, the expression items “::types[T1,T2]” and “::types[T3]” are parsed to custom expression items of type UniversalExpressionParser.DemoExpressionLanguageProviders.CustomExpressions.IGenericTypeDataExpressionItem, and are added as prefixes to braces expression item parsed from “F1(x:T1, y:T2, z: T3)”.

Note

For more details on custom expression items see section Custom Expression Item Parsers.

1::types[T1,T2] ::types[T3] F1(x:T1, y:T2, z: T3)
2{
3    // This code block will be added to expression item parsed from F1(x:T1, y:T2, z: T3) as a postfix.
4}

Click here to see the visualized instance of UniversalExpressionParser.IParseExpressionResult

Note

The list of prefixes can include both types of prefixes at the same time (i.e., braces and custom expression items).

  • Here is an example of prefixes used to model c# like attributes for classes and methods:

 1// [TestFixture] and [Attribute("IntegrationTest")] are added as prefixes to literal MyTests.
 2[TestFixture]
 3[Attribute("IntegrationTest")]
 4// public and class are added as keywords to MyTests
 5public class MyTests
 6{
 7    // Brace expression items [SetupMyTests], [Attribute("This is a demo of multiple prefixes")]
 8    // and custom expression item starting with ::metadata and ending with } are added as prefixes to
 9    // expression SetupMyTests()
10    [TestSetup]
11    [Attribute("This is a demo of multiple prefixes")]
12    ::metadata {
13        Description: "Demo of custom expression item parsed to
14                        UniversalExpressionParser.DemoExpressionLanguageProviders.CustomExpressions.IMetadataCustomExpressionItem
15                        used in prefixes list of expression parsed from 'SetupMyTests()'";
16        SomeMetadata: 1
17    }
18    // public and static are added as keywords to expression SetupMyTests().
19    public static SetupMyTests() : void
20    {
21        // Do some test setup here
22    }
23}

Click here to see the visualized instance of UniversalExpressionParser.IParseExpressionResult

Note

The list of prefixes can include both types of prefixes at the same time (i.e., braces and custom expression items).

  • Below is an example of using prefixes with different expression item types:

 1// Prefixes added to a literal "x".
 2[NotNull] [Attribute("Marker")] x;
 3
 4// Prefixes added to named round braces. [NotNull] [Attribute("Marker")] will be added
 5// to prefixes in braces expression item parsed from "f1(x1)"
 6[NotNull] [Attribute("Marker")] f1(x1);
 7
 8// Prefixes added to unnamed round braces. [NotNull] [Attribute("Marker")] will be added
 9// to prefixes in braces expression item parsed from "(x1)"
10[NotNull] [Attribute("Marker")] (x1);
11
12// Prefixes added to named square braces. [NotNull] [Attribute("Marker")] will be added
13// to prefixes in named braces expression item parsed from "m1[x1]"
14[NotNull] [Attribute("Marker")] m1[x1];
15
16// Prefixes added to unnamed square braces. [NotNull] [Attribute("Marker")] will be added
17// to prefixes in braces expression item parsed from "[x1]".
18[NotNull] [Attribute("Marker")] [x1];
19
20// Prefixes added to code block.
21// Custom prefix expression item "::types[T1,T2]" will be added to list of prefixes in code block expression item
22// parsed from "{var i = 12;}".
23// Note, if we replace "::types[T1,T2]" to unnamed braces, then the unnamed braces will be used as a postfix for
24// code block.
25::types[T1,T2] {var i = 12;};
26
27// Prefixes added to custom expression item parsed from "::pragma x".
28// [Attribute("Marker")] will be added to list of prefixes in custom expression item
29// parsed from "::pragma x".
30[Attribute("Marker")] ::pragma x;
31
32// Prefixes added text expression item.
33// [Attribute("Marker")] will be added to list of prefixes in text expression item
34// parsed from "Some text".
35[Attribute("Marker")] "Some text";
36
37// Prefixes added to numeric value item.
38// [Attribute("Marker")] will be added to list of prefixes in numeric value expression item
39// parsed from "0.5e-3.4".
40[Attribute("Marker")] 0.5e-3.4;

Click here to see the visualized instance of UniversalExpressionParser.IParseExpressionResult