Bracket parser 2
Introduction¶
A lite-weight delimiter parser.
- Author: Rex
- Object
Live demoes¶
Usage¶
Install plugin¶
Load minify file¶
- Load plugin (minify file) in preload stage
scene.load.plugin('rexbracketparser2plugin', 'https://raw.githubusercontent.com/rexrainbow/phaser3-rex-notes/master/dist/rexbracketparser2plugin.min.js', true);
- Add parser object
var parser = scene.plugins.get('rexbracketparser2plugin').add(config);
Import plugin¶
- Install rex plugins from npm
npm i phaser3-rex-plugins
- Install plugin in configuration of game
import BracketParserPlugin from 'phaser3-rex-plugins/plugins/bracketparser2-plugin.js'; var config = { // ... plugins: { global: [{ key: 'rexBracketParser', plugin: BracketParserPlugin, start: true }, // ... ] } // ... }; var game = new Phaser.Game(config);
- Add parser object
var parser = scene.plugins.get('rexBracketParser').add(config);
Import class¶
- Install rex plugins from npm
npm i phaser3-rex-plugins
- Import class
import BracketParser from 'phaser3-rex-plugins/plugins/bracketparser2.js';
- Add parser object
var parser = new BracketParser(config);
Create instance¶
var parser = scene.plugins.get('rexBracketParser').add({
// delimiters: '<>', // or ['<', '>']
// valueConvert: true,
// translateTagNameCallback: undefined,
});
delimiters
: String of left-delimiter and right-delimiter.- A single string with 2 characters. Default value is
'<>'
. - A array with 2 strings
- A single string with 2 characters. Default value is
valueConvert
: A callback to convert values.true
: Use defaule value converter. Values will be converted to number (include hex number string like '0xFF'), boolean, null, or string.false
, ornull
: Bypass string value.- Function object:
function(s) { return s; }
translateTagNameCallback
: Callback to translate tag-nameundefined
: Use original tag-name. Default behavior.- A function, return new tag name.
function(tagName) { return newTagName; }
Tag and content¶
Assume that left-delimiter and right-delimiter is '<>'
- Start-tag :
<TAG>
- Start-tag with payload :
or
<TAG param0=value0 param1=value1>
< TAG param0=value0 param1=value1 >
- payload will be
{param0:value0, param1:value1}
value
: IfvalueConvert
istrue
,- String with
'...'
, or"..."
- JSON array with
[...]
- JSON object with
{...}
- Number
- Boolean
- null
- String
- String with
- payload will be
- End-tag :
<\TAG>
- End-tag with payload
or
<\TAG param0=value0 param1=value1>
< \TAG param0=value0 param1=value1 >
- payload will be
{param0:value0, param1:value1}
value
: IfvalueConvert
istrue
,- String with
'...'
, or"..."
- JSON array with
[...]
- JSON object with
{...}
- Number
- Boolean
- null
- String
- String with
- payload will be
- Content : Any string outside of tag-start, or tag-end.
Start parsing¶
parser.start(text);
These events will be emitted under this method.
Pause¶
- Pause
parser.pause();
- Pause until event
parser.pauseUntilEvent(eventEmitter, eventName);
- Will invoke
eventEmitter.once(eventName, function() { parser.next(); })
- Will invoke
Invoke this method during tag-start,tag-end, or content events to suspend parsing.
Resume¶
parser.next();
Skip any-tag-start/any-tag-end event¶
parser.skipEvent();
When getting a tag-start, or a tag-end event, parser will emitts
- Start-tag :
'+TAG'
, then'+'
- End-tag :
'-TAG'
, then'-'
Invoke this medthod under '+TAG'
, or '-TAG'
event to skip '+'
, or '-'
event.
Status¶
- Is parsing
var isRunning = parser.isRunning;
true
: Has remainder charactersfalse
: After parsing last character
- Is paused
var isPaused = parser.isPaused;
Events¶
Tags/Content¶
- Get a specific tag-start
- Start-tag with payload :
'<TAG param0=value0 param1=value1>'
parser.on('+' + TagName, function(payload){ /* ... */ });
payload
:{param0:value0, param1:value1, ...}
- Start-tag with payload :
- Get any-tag-start
- Start-tag with payload :
'<TAG param0=value0 param1=value1>'
parser.on('+', function(tagName, payload){ /* ... */ });
payload
:{param0:value0, param1:value1, ...}
- Start-tag with payload :
- Get a specific tag-end
- End-tag with payload :
'<\TAG param0=value0 param1=value1>'
parser.on('-' + TagName, function(payload){ /* ... */ });
payload
:{param0:value0, param1:value1, ...}
- End-tag with payload :
- Get a content
parser.on('content', function(content){ /* ... */ });
- Previous tag-start :
parser.lastTagStart
{ name: '', payload: {} }
- Previous tag-start :
- Get any-tag-end
- End-tag with payload :
'<TAG param0=value0 param1=value1>'
parser.on('-', function(tagName, payload){ /* ... */ });
payload
:{param0:value0, param1:value1, ...}
- Previous tag-start :
parser.lastTagStart
{ name: '', payload: {} }
- Previous Content :
parser.lastContent
- End-tag with payload :
Control flow¶
- Parsing start
parser.on('start', function(){ /* ... */ });
- Parsing end
parser.on('complete', function(){ /* ... */ });
- On pause
parser.on('pause', function(){ /* ... */ });
- On resume
parser.on('resume', function(){ /* ... */ });
Compare with bracket-parser¶
Tag format in
- bracket-parser:
<TAG=value0,value1>
- Carried parameters is an array, more compactly.
- bracket-parser2:
or
<TAG param0=value0 param1=value1>
< TAG param0=value0 param1=value1 >
- Carried parameters is a dictionary, more clearly.