digia_expr
v1.0.0
Published
Expression evaluator for React Native - TypeScript port of digia_expr
Maintainers
Readme
Digia Expression Evaluator for React Native
A powerful and flexible expression evaluator for React Native and JavaScript, built on an extensible visitor pattern architecture.
Features
- ✅ Extensible Visitor Pattern - Create custom analyzers by extending
Visitor<T> - ✅ AST Access - Full access to the Abstract Syntax Tree
- ✅ Expression Parsing - Built-in scanner and parser
- ✅ TypeScript Support - Full type definitions included
- ✅ React Native Compatible - Works seamlessly in React Native apps
- ✅ Variables & Functions - Support for variables and function calls
- ✅ Property Access - Navigate nested properties with dot notation
Installation
npm install @digia/expr-rn
# or
yarn add @digia/expr-rnQuick Start
Basic Expression Parsing
import { createAST } from '@digia/expr-rn';
const ast = createAST('user.name');
console.log(ast);Creating Custom Visitors
import { Visitor, ASTProgram, ASTVariable, createAST } from '@digia/expr-rn';
// Custom visitor to collect variable names
class VariableCollectorVisitor extends Visitor<string[]> {
visitProgram(node: ASTProgram): string[] {
const vars: string[] = [];
for (const expr of node.body) {
vars.push(...expr.visit(this));
}
return vars;
}
visitASTVariable(node: ASTVariable): string[] {
return [node.name.lexeme];
}
visitASTGetExpr(node: ASTGetExpr): string[] {
return node.expr.visit(this);
}
// Implement other required methods...
}
// Use the visitor
const ast = createAST('user.name');
const collector = new VariableCollectorVisitor();
const variables = ast.visit(collector);
console.log(variables); // ['user']Collecting Property Access Paths
class PropertyPathVisitor extends Visitor<string[]> {
visitASTVariable(node: ASTVariable): string[] {
return [node.name.lexeme];
}
visitASTGetExpr(node: ASTGetExpr): string[] {
const basePaths = node.expr.visit(this);
return basePaths.map(base => `${base}.${node.name.lexeme}`);
}
// Implement other methods...
}
const ast = createAST('appState.user.profile.name');
const pathCollector = new PropertyPathVisitor();
const paths = ast.visit(pathCollector);
console.log(paths); // ['appState.user.profile.name']React Native Example
import React, { useState } from 'react';
import { View, Text, TextInput } from 'react-native';
import { createAST, Visitor, ASTProgram, ASTVariable } from '@digia/expr-rn';
class VariableExtractor extends Visitor<string[]> {
// Implement visitor methods...
}
function ExpressionAnalyzer() {
const [expression, setExpression] = useState('');
const [variables, setVariables] = useState<string[]>([]);
const analyzeExpression = (expr: string) => {
try {
const ast = createAST(expr);
const extractor = new VariableExtractor();
const vars = ast.visit(extractor);
setVariables(vars);
} catch (error) {
console.error('Parse error:', error);
}
};
return (
<View>
<TextInput
value={expression}
onChangeText={(text) => {
setExpression(text);
analyzeExpression(text);
}}
placeholder="Enter expression"
/>
<Text>Variables: {variables.join(', ')}</Text>
</View>
);
}API Reference
Core Functions
createAST(expression: string): ASTNode- Parse an expression into an AST
AST Node Types
ASTProgram- Root program nodeASTVariable- Variable referenceASTGetExpr- Property access (dot notation)ASTCallExpression- Function callASTNumberLiteral- Number literalASTStringLiteral- String literalASTStringExpression- String with embedded expressionsASTBooleanLiteral- Boolean literal
Visitor Pattern
Extend Visitor<T> where T is your return type:
abstract class Visitor<T> {
abstract visitProgram(node: ASTProgram): T;
abstract visitASTVariable(node: ASTVariable): T;
abstract visitASTGetExpr(node: ASTGetExpr): T;
abstract visitCallExpression(node: ASTCallExpression): T;
abstract visitASTNumberLiteral(node: ASTNumberLiteral): T;
abstract visitASTStringLiteral(node: ASTStringLiteral): T;
abstract visitASTStringExpression(node: ASTStringExpression): T;
}Use Cases
- Static Analysis - Analyze expressions to extract variable dependencies
- Code Generation - Transform expressions into different formats
- Validation - Validate expressions before evaluation
- Custom Evaluators - Build domain-specific expression evaluators
- Dependency Tracking - Track what data an expression accesses
License
MIT
Author
Digia Technology Private Limited
