Skip to content

Expression parser

Introduction

Parse expression string into function.
Parser is generated from jison

  • Author: Rex
  • Member of scene

Live demos

Usage

Sample code

Install plugin

Load minify file

  • Load plugin (minify file) in preload stage
    scene.load.plugin('rexexpressionparserplugin', 'https://raw.githubusercontent.com/rexrainbow/phaser3-rex-notes/master/dist/rexexpressionparserplugin.min.js', true);
    
  • Add parser
    var parser = scene.plugins.get('rexexpressionparserplugin').add();
    
  • Or, parse expression to function object.
    var f = scene.plugins.get('rexexpressionparserplugin').compile(expressionString);
    // var value = f(context);
    

Import plugin

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Install plugin in configuration of game
    import ExpressionParserPlugin from 'phaser3-rex-plugins/plugins/expressionparser-plugin.js';
    var config = {
        // ...
        plugins: {
            global: [{
                key: 'rexExpressionParserPlugin',
                plugin: ExpressionParserPlugin,
                start: true
            },
            // ...
            ]
        }
        // ...
    };
    var game = new Phaser.Game(config);
    
  • Add parser
    var parser = scene.plugins.get('rexExpressionParserPlugin').add();
    
  • Or, parse expression to function object.
    var f = scene.plugins.get('rexExpressionParserPlugin').compile(expressionString);
    // var value = f(context);
    

Import class

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Import class
    import ExpressionParser from 'phaser3-rex-plugins/plugins/expressionparser.js';
    
  • Add parser
    var parser = new ExpressionParser();
    

Create instance

var parser = scene.plugins.get('rexExpressionParserPlugin').add();

Execute

Compile then execute

  1. Compile expression string into function
    var f = parser.compile(expressionString);
    
    or
    var f = scene.plugins.get('rexExpressionParserPlugin').compile(expressionString);
    
    • expressionString :
      • Number : 1, 1.5, 0xf.
      • Variable : a, $a, _a, a.$b._c_, a['b'].c
      • Arithmetic : +, -, *, \, %, (, ), ex : '(a + b.c) * 3 + (2 % 3)'.
      • Boolean : >, <, >=, <=, ==, !=, &&, ||, ex '(a > 10) && (a < 30) || (b.c > c)'.
      • Condition : (cond)? v0:v1, ex'(a > b.c)? a:b.c'.
      • Custom method : randomInt(a, b.c).
      • String concat : 'Hello ' + name.
  2. Invoke function
    var value = f(context);
    
    • f : Function object from compiled result.
    • context : Varables used in expression.
      {
          a: 10,  // Number
          b: {c: 10},  // Objet with number property
          c: 20,
          randomInt(a, b) {  // Custom method
              return Math.floor(Math.random()*(b-a)+a);
          }
      }
      

Execute directly

var value = parser.exec(expressionString, context);

or

var value = parser.exec(f, context);

Custom method

  • Add method into parser instance
    var parser = scene.plugins.get('rexExpressionParserPlugin').add();
    parser.randomInt = function(a, b) {
        return Math.floor(Math.random()*(b-a)+a); 
    }
    // var value = parser.exec('randomInt(a, b)', {a:10, b:20});
    
  • Declare method into class of parser
    class MyParser extends ExpressionParser {
        randomInt(a, b) {
            return Math.floor(Math.random()*(b-a)+a); 
        }
    }
    var parser = new MyParser();
    // var value = parser.exec('randomInt(a, b)', {a:10, b:20});
    
  • Add method into context
    var context = {
        a: 10,
        b: 20,
        randomInt(a, b) {  // Custom method
            return Math.floor(Math.random()*(b-a)+a);
        }
    }
    var value = parser.exec('randomInt(a, b)', context);
    

Proxy as context

Proxy with has and get handlers could be a context.

For example, proxy scene data :

var context = new Proxy({}, {
    has(target, key) {
        return scene.data.has(key);
    },
    get(target, prop) {
        return scene.data.get(prop);
    }
})

or

var context = scene.plugins.get('rexExpressionParserPlugin').createProxyContext({
    has(target, key) {
        // return boolean
    },
    get(target, prop) {
        // return any;
    }
})