### Start Development Environment
Source: https://github.com/mgm-tp/a12-expression/blob/main/README.md
Start the development server and TypeScript compilers in watch mode. This is the recommended way to begin development.
```sh
pnpm start
```
--------------------------------
### Install Dependencies with pnpm
Source: https://github.com/mgm-tp/a12-expression/blob/main/README.md
Install all project dependencies and link local packages using pnpm. This command is necessary before building the project.
```sh
pnpm install
```
--------------------------------
### Expression AST Example
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0400_modules.adoc
This example shows the Abstract Syntax Tree (AST) generated from a sample expression string. The AST represents the structure of the expression, including fields, strings, and groups.
```json
{
"type": "root",
"children": [
{
"type": "group",
"name": "PersonalInfo",
"children": [
{
"type": "field",
"name": "LastName"
},
{
"type": "string",
"content": " "
},
{
"type": "field",
"name": "FirstName"
}
]
}
]
}
```
--------------------------------
### Install expression-core NPM Package
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0200_installation.adoc
Use this command to install the expression-core package from NPM. This command installs ES5 CommonJS modules.
```bash
npm install @com.mgmtp.a12.expression/expression-core
```
--------------------------------
### Compile All Packages
Source: https://github.com/mgm-tp/a12-expression/blob/main/README.md
Build all packages within the repository after installing dependencies. This command compiles the project's code.
```sh
pnpm compile
```
--------------------------------
### Main Usage Example
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0300_usage.adoc
This snippet demonstrates the main usage of the Expression package. It requires document, documentModel, localizer, and fieldFormatter to create an Expression resolver.
```typescript
import {
Expression,
ExpressionResolver,
ExpressionContext,
} from "@mgm/a12-expression";
// Assume these are provided by the A12 environment
const document = {};
const documentModel = {};
const localizer = {};
const fieldFormatter = (value: any) => String(value);
// Create an Expression context
const context: ExpressionContext = {
document,
documentModel,
localizer,
fieldFormatter,
};
// Create an Expression resolver
const resolver = new ExpressionResolver(context);
// Example usage of the resolver
const expression = new Expression("user.name");
const value = resolver.resolve(expression);
console.log(value);
```
--------------------------------
### MultilingualTextValue Example
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0500_language.adoc
Defines localized text values using locale codes and corresponding strings, enclosed in parentheses and separated by commas.
```bash
(en: "women", de: "frauen", fr: "femmes")
```
--------------------------------
### ExpressionInterpreter HTML Output Example
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0400_modules.adoc
Demonstrates the HTML output generated by the ExpressionInterpreter.format function when processing a given AST. The output is typically a structured HTML string.
```html
```
--------------------------------
### StringValue Example
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0500_language.adoc
Represents a string literal enclosed in double quotes.
```bash
"Fullname:"
```
--------------------------------
### FieldValue Example
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0500_language.adoc
References a field within the current scope, enclosed in square brackets. Only available within GroupOperation.
```bash
[FirstName]
```
--------------------------------
### GroupOperation with Custom Delimiter
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0500_language.adoc
Concatenates results of repeatable groups using a specified delimiter. This example uses ', ' to separate multiple addresses.
```bash
kontext (PersonalInfo) {
kontext (Address, delimiter = ", ") {
[City] " (" [Country] ")"
}
}
```
--------------------------------
### Markdown Formatting in Expressions
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0500_language.adoc
This example shows how to use Markdown syntax within expressions for text formatting like bold and italics. Markdown characters must be wrapped in double quotes.
```expression
"**Bold Text**"
"*Italic Text*"
"~~StrikeThrough~~
"My name is: **" [FirstName] "**" " " *" [LastName] *"
```
--------------------------------
### GroupOperation Basic Usage
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0500_language.adoc
Accesses nested document model elements. The example shows accessing FirstName and LastName, and then City and Country within a nested Address group.
```bash
kontext (PersonalInfo) {
[FirstName] " " [LastName]
" from "
kontext (Address) {
[City] "," [Country]
}
}
```
--------------------------------
### Run Unit Tests
Source: https://github.com/mgm-tp/a12-expression/blob/main/README.md
Execute unit tests across all packages in the repository. This command verifies the correctness of the code.
```sh
pnpm test
```
--------------------------------
### Run Linting
Source: https://github.com/mgm-tp/a12-expression/blob/main/README.md
Execute linting checks on the entire project. This command helps maintain code quality and consistency.
```sh
pnpm lint
```
--------------------------------
### ExpressionBuilder.build
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0400_modules.adoc
The ExpressionBuilder module exposes the `build` function to create an abstract syntax tree (AST) from a raw expression string. It accepts the expression string and an optional context object.
```APIDOC
## ExpressionBuilder.build
### Description
Builds an abstract syntax tree (AST) from a raw expression string. This is useful for customizations.
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
* **expression** (string) - Required - The string expression to be parsed.
* **context** (object) - Optional - Contains additional information for the builder.
```
--------------------------------
### Format Code
Source: https://github.com/mgm-tp/a12-expression/blob/main/README.md
Automatically fix linting issues and format the code according to project standards. This command ensures code style consistency.
```sh
pnpm format
```
--------------------------------
### CaseOperation Syntax
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0500_language.adoc
Provides branching logic based on field comparison. Supports equality ('=') and inequality ('!=') operators.
```bash
case [] { }
```
--------------------------------
### Clean Build Artifacts
Source: https://github.com/mgm-tp/a12-expression/blob/main/README.md
Remove all build artifacts from the project. Use this command to clean the project before a fresh build.
```sh
pnpm clean
```
--------------------------------
### ExpressionBuilder API
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0400_modules.adoc
The ExpressionBuilder module exposes the build function for creating an Abstract Syntax Tree (AST) from a raw string expression. It takes the expression string and an optional context object.
```typescript
/**
* Builds an AST from a raw expression string.
*
* @param expression The expression string to parse.
* @param context Optional context information for the builder.
* @returns The generated AST.
*/
export function build(expression: string, context?: unknown): AST {
// Implementation omitted for brevity
return { type: "root", children: [] };
}
```
--------------------------------
### ExpressionInterpreter Parameters
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0400_modules.adoc
Defines the parameters accepted by the ExpressionInterpreter module for formatting an AST into HTML. It includes the AST itself and optional configuration for output.
```typescript
interface Parameters {
ast: AST;
// Optional: wrap output by a span instead of div
asSpan?: boolean;
// Optional: additional CSS class name
className?: string;
}
```
--------------------------------
### ExpressionInterpreter.format
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0400_modules.adoc
The ExpressionInterpreter module transforms an expression AST into an HTML string. It takes a single parameter object conforming to the Interpreter.Parameters interface.
```APIDOC
## ExpressionInterpreter.format
### Description
Transforms an expression AST into an HTML string.
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
* **(object)** (Interpreter.Parameters) - Required - An object containing parameters for formatting the AST into HTML. The specific fields depend on the `Interpreter.Parameters` interface.
### Response
#### Success Response (200)
* **(string)** - The HTML string representation of the expression.
### Response Example
```html
```
```
--------------------------------
### ExpressionOutput Component
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0400_modules.adoc
The ExpressionOutput is a React component that wraps the ExpressionInterpreter. It is recommended for integrating HTML output into applications, providing a safer alternative to React's `dangerouslySetInnerHTML`.
```APIDOC
## ExpressionOutput Component
### Description
A React component that renders HTML output from an expression AST, wrapping the `ExpressionInterpreter`.
### Props
* **(Interpreter.Parameters)** - Required - Same parameters as `ExpressionInterpreter.format`.
* **asSpan** (boolean) - Optional - If true, wraps the output in a `span` element instead of the default `div`.
* **className** (string) - Optional - Additional CSS class name to apply to the wrapper element.
```
--------------------------------
### Expression with Conditional Title
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0500_language.adoc
This expression demonstrates how to conditionally add a title based on a boolean field. It reuses the document model from the GroupOperation section.
```expression
kontext (PersonalInfo) {
case [IsMale] = "true" { "Mr." }
case [IsMale] != "true" { "Mrs." }
[LastName]
}
```
--------------------------------
### Expression Language Grammar
Source: https://github.com/mgm-tp/a12-expression/blob/main/documentation/src/0500_language.adoc
This is the ANTLR grammar definition for the Expression language, used by the ANTLR library to generate the lexer and parser.
```g4
grammar Expression;
//
// Parser rules
//
expression
: value
| operation
;
operation
: LPAREN expression RPAREN
| NOT expression
| expression BINOP expression
| expression QUESTION expression COLON expression
| expression CASE expression LCURLY expression RCURLY
| expression IS expression
| expression ISNOT expression
| expression EQ expression
| expression NEQ expression
| expression GT expression
| expression GTE expression
| expression LT expression
| expression LTE expression
;
value
: STRING
| NUMBER
| BOOLEAN
| NULL
| field
| list
| kontext
;
field
: LBRACKET ID RBRACKET
;
list
: LBRACKET value (COMMA value)* RBRACKET
;
kontext
: KONTEXT ID LCURLY expression RCURLY
;
//
// Lexer rules
//
STRING
: '"' ( ESCAPED_STRING | . )* '"'
;
fragment
ESCAPED_STRING
: '\\' ( '"' | '\\' )
;
NUMBER
: SIGNED_NUMBER
| UNSIGNED_NUMBER
;
fragment
SIGNED_NUMBER
: (PLUS | MINUS) UNSIGNED_NUMBER
;
fragment
UNSIGNED_NUMBER
: INT ( (DOT INT?)? | DOT INT )
;
fragment
INT
: [0-9]+
;
BOOLEAN
: 'true' | 'false'
;
NULL
: 'null'
;
BINOP
: EQ | NEQ | GT | GTE | LT | LTE | IS | ISNOT
;
EQ
: '='
;
NEQ
: '!='
;
GT
: '>'
;
GTE
: '>='
;
LT
: '<'
;
LTE
: '<='
;
IS
: 'is'
;
ISNOT
: 'is not'
;
NOT
: '!'
;
QUESTION
: '?'
;
COLON
: ':'
;
CASE
: 'case'
;
KONTEXT
: 'kontext'
;
LPAREN
: '('
;
RPAREN
: ')'
;
LCURLY
: '{'
;
RCURLY
: '}'
;
LBRACKET
: '['
;
RBRACKET
: ']'
;
COMMA
: ','
;
PLUS
: '+'
;
MINUS
: '-'
;
DOT
: '.'
;
WS
: ( ' ' | '\t' | '\r' | '\n' )+ -> skip
;
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.