Parsing Section in Text
Sometimes we want to parse a single braces expression at specific location in text (i.e., an expression starting with “(” or “[” and ending in “)” or “]” correspondingly) or single code block expression (i.e., an expression starting with UniversalExpressionParser.IExpressionLanguageProvider.CodeBlockStartMarker and ending in UniversalExpressionParser.IExpressionLanguageProvider.CodeBlockEndMarker). In these scenarios, we want the parser to stop right after fully parsing the braces or code block expression.
The interface UniversalExpressionParser.IExpressionParser has two methods for doing just that.
The methods for parsing single braces or code block expression are UniversalExpressionParser.IParseExpressionResult ParseBracesExpression(string expressionLanguageProviderName, string expressionText, IParseExpressionOptions parseExpressionOptions) and UniversalExpressionParser.IParseExpressionResult ParseCodeBlockExpression(string expressionLanguageProviderName, string expressionText, IParseExpressionOptions parseExpressionOptions), and are demonstrated in sub-sections below.
The parsed expression of type UniversalExpressionParser.IParseExpressionResult returned by these methods has a property int PositionInTextOnCompletion { get; } that stores the position in text, after the parsing is complete (i.e., the position after closing brace or code block end marker).
Example of parsing single braces expression
Below is an an SQLite table definition in which we want to parse only the braces expression (SALARY > 0 AND SALARY > MAX_SALARY/2 AND f1(SALARY) < f2(MAX_SALARY)), and stop parsing right after the closing brace ‘)’.
1CREATE TABLE COMPANY(
2 ID INT PRIMARY KEY NOT NULL,
3 MAX_SALARY REAL,
4 /* The parser will only parse expression
5 (SALARY > 0 AND SALARY > MAX_SALARY/2 AND f1(SALARY)<f2(MAX_SALARY)) and will stop right after the
6 closing round brace ')' of in this expression. */
7 AVG_SALARY REAL
8 CHECK(SALARY > 0 AND
9 SALARY > MAX_SALARY/2 AND
10 f1(SALARY) < f2(MAX_SALARY)),
11 ADDRESS CHAR(50)
12);
Click here to see the visualized instance of UniversalExpressionParser.IParseExpressionResult
The method ParseBracesAtCurrentPosition(string expression, int positionInText) in class UniversalExpressionParser.Tests.Demos.ParseSingleBracesExpressionAtPositionDemo (shown below) demonstrates how to parse the braces expression (SALARY > 0 AND SALARY > MAX_SALARY/2 AND f1(SALARY) < f2(MAX_SALARY)), by passing the position of opening brace in parameter positionInText.
Click here to see definition of class UniversalExpressionParser.Tests.Demos.ParseSingleBracesExpressionAtPositionDemo
Here is square braces expression [f1()+m1[], f2{++i;}] between texts ‘any text before braces’ and ‘any text after braces…’, which can also be parsed using the code in class UniversalExpressionParser.Tests.Demos.ParseSingleBracesExpressionAtPositionDemo.
1any text before braces[f1()+m1[], f2
2{
3 ++i;
4}]any text after braces including more braces expressions that will not be parsed
Click here to see the visualized instance of UniversalExpressionParser.IParseExpressionResult
Example of parsing single code block expression
Below is a text with code block expression {f1(f2()+m1[], f2{++i;})} between texts ‘any text before code block’ and ‘any text after code block…’ that we want to parse.
1any text before braces[f1()+m1[], f2
2{
3 ++i;
4}]any text after braces including more braces expressions that will not be parsed
Click here to see the visualized instance of UniversalExpressionParser.IParseExpressionResult
The method IParseExpressionResult ParseCodeBlockExpressionAtCurrentPosition(string expression, int positionInText) in class UniversalExpressionParser.Tests.Demos.ParseSingleCodeBlockExpressionAtPositionDemo demonstrates how to parse the single code block expression {f1(f2()+m1[], f2{++i;})}, by passing the position of code block start marker ‘{‘ in parameter positionInText.
Click here to see definition of class UniversalExpressionParser.Tests.Demos.ParseSingleCodeBlockExpressionAtPositionDemo