Click here to see the visualized instance of UniversalExpressionParser.IParseExpressionResult
- This is another example demonstrating that the parsed expression can have multiple prefix and postfix custom expressions items applied to the same regular expression item parsed from "F1(x:T1, y:T2, z:T3)". .. sourcecode:: :linenos: // The expression below ("::metadata {...}") is parsed to a prefix custom expression item and added to list of prefixes of regular // expression item parsed from F1(x:T1, y:T2, z:T3) ::metadata {description: "F1 demoes regular function expression item to which multiple prefix and postfix custom expression items are added."} // ::types[T1,T2] is also parsed to a prefix custom expression item and added to list of prefixes of regular // expression item parsed from F1(x:T1, y:T2, z:T3) ::types[T1,T2] F1(x:T1, y:T2, z:T3) // The postfix custom expression item parsed from "where T1:int where T2:double whereend" is added to list of postfixes of regular expression // parsed from "F1(x:T1, y:T2, z:T3)". where T1:int,class where T2:double whereend // The postfix custom expression item parsed from "where T3 : T1 whereend " is also added to list of postfixes of regular expression // parsed from "F1(x:T1, y:T2, z:T3)". where T3 : T1 whereend { // This code block will be added as a postfix to expression item parsed from "F1(x:T1, y:T2, z:T3)". } .. raw:: htmlClick here to see the visualized instance of UniversalExpressionParser.IParseExpressionResult
Implementing Custom Expression Parsers ====================================== For examples of custom expression item parsers look at some examples in demo project **UniversalExpressionParser.DemoExpressionLanguageProviders**. The following demo implementations of **UniversalExpressionParser.ExpressionItems.Custom.ICustomExpressionItemParserByKeywordId** might be useful when implementing custom expression parses: - UniversalExpressionParser.DemoExpressionLanguageProviders.CustomExpressions.WhereCustomExpressionItemParserBase - UniversalExpressionParser.DemoExpressionLanguageProviders.CustomExpressions.PragmaCustomExpressionItemParser - UniversalExpressionParser.DemoExpressionLanguageProviders.CustomExpressions.MetadataCustomExpressionItemParser Also, these custom expression parser implementations demonstrate how to use the helper class **UniversalExpressionParser.IParseExpressionItemContext** that is passed as a parameter to method **DoParseCustomExpressionItem(IParseExpressionItemContext context,...)** in **UniversalExpressionParser.ExpressionItems.Custom.CustomExpressionItemParserByKeywordId** to parse the text at current position, as well as how to report errors, if any. - To add a new custom expression parser, one needs to implement an interface **UniversalExpressionParser.ExpressionItems.Custom.ICustomExpressionItemParser** and make sure the property **CustomExpressionItemParsers** in interface **UniversalExpressionParser.IExpressionLanguageProvider** includes an instance of the implemented parser class. - In most cases the default implementation **UniversalExpressionParser.ExpressionItems.Custom.AggregateCustomExpressionItemParser** of **UniversalExpressionParser.ExpressionItems.Custom.ICustomExpressionItemParser** can be used to initialize the list of all custom expression parers that will be used by **Universal Expression Parser**. **UniversalExpressionParser.ExpressionItems.Custom.AggregateCustomExpressionItemParser** has a dependency on **IEnumerable<ICustomExpressionItemParserByKeywordId>** (injected into constructor). - Using a single instance of **AggregateCustomExpressionItemParser** in property **CustomExpressionItemParsers** in interface **UniversalExpressionParser.IExpressionLanguageProvider** instead of multiple custom expression parsers in this property improves the performance. **AggregateCustomExpressionItemParser** keeps internally a mapping from keyword Id to all the instances of **UniversalExpressionParser.ExpressionItems.Custom.ICustomExpressionItemParserByKeywordId** injected in constructor. When the parser executes the method **TryParseCustomExpressionItem(...,IReadOnlyListClick here to see the definition of interface UniversalExpressionParser.IParseExpressionItemContext