As of v0.7.0, the parser and the interpreter are separated.
This is the file structure and in which order to incorporate them:
The main functionality of the parser is to load in additional files (defined in your main file and also in that additional files*) and execute your main function after all the files were loaded. While loading, all registered parsers will parse "the shit" out of your files.
*like: index.gml loads floor.gml and floor.gml loads bedroom.gml and kitchen.gml. bedroom.gml also loads secondbathroom.gml...etc.
As mentioned, the parser structure can hold your own individual parsers. For that, you need to create a class and add the functions parseGML(json, rootPath) and clear() to it. Then you register your parser in the GMLParser module and load your files.
You can add all the standard GML-parsers: ROOMS, ITEMS, SOUNDS and PANELS. They are NOT added automatically so you can define your own data structures.
You can use the given rootPath for parsing relative paths out of the json and converting them to absolute paths.
You will get your data out of the parser you write. That means, you parse the json data into variables which you define in your parser.
Remember that all the json variable names will be converted to UPPERCASE letters!
Here is an example:
var myParser = function()
{
this.value1 = 0;
this.value2 = [];
this.parseGML(json, rootPath)
{
// get the data for value1 and value2 here. Fill your arrays here, etc.
// example:
if(__defined(json['VALUE1']) // check if the value is defined.
this.value1 = json['VALUE1'];
if(__defined(json['VALUE2'])
this.value2.push(json['VALUE2']);
}
}
Ok. This is our own parser which gets the value1 out of the global structure.
You need to go into your structures "by yourself". You ever get the whole file structure to the parser.
Value1 will be overwritten with each file that defines a value1.
Value2 will be collected with each file that defines a value2.
After the parser has loaded and parsed all files, there is just one value1 with the value given in the last loaded file.
But there may be more than one value2 because they were collected in an array. Just to note.
Now we load a GML file and get the value1 for example purposes.
// first we need to register the parser if it is not already registered.
GMLParser.addParser("MYPARSER", new myParser());
// then we load the "index" file, which may have references to other files.
// after all the files are loaded and parsed, the "main" function will be called.
PARSEGMLFILE("myFile.gml", new function()
{
console.log("all gml files loaded. do something with the data here.");
// and here is your value1, just get the parser with the name given above:
var value1 = GMLParser.getParser("MYPARSER").value1;
});
You can also define "items" of array members with their own parseGML function. Using Value2 like above would need you to make a new file for each value2. Rather use arrays with "items" (of value2-s). It would be added like this:
var item = function()
{
..
this.parseGML(json, rootPath)
{..}
}
var myParser = function()
{
this.itemArray = [];
this.parseGML = function(json, rootPath)
{
..
// here, ARRAY is an array with some items which have their own values.
if(__defined(json["ARRAY"]))
{
for(var i=0;i < json["ARRAY"].length;i++)
{
var itm= new item();
itm.parseGML(json["ARRAY"][i]);
this.itemArray.push(itm);
}
}
}
}