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();
    

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();
    

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);
    
    • expressionString :
      • Number : 1, 1.5, 0xf.
      • Variable : a, 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)
  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);