aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/material-parser.js
blob: c078c3af6541e7e36e7e2c05e8710b7a3d295762 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
</copyright> */

var MaterialParser = function MaterialParser(theStr) {

    this._strBuffer = theStr;

    this.nextValue = function (prop, endKeyArg, advanceBufferArg) {
        if (!this._strBuffer) return;

        // make the 2 & 3rd argument optional.  default is to advance the string
        var endKey = "\n", advanceBuffer = true;
        if (endKeyArg) {
            endKey = endKeyArg;
        }

        if (advanceBufferArg) {
            advanceBuffer = advanceBufferArg;
        }

        var iStart = this._strBuffer.indexOf(prop);
        if (iStart < 0) return;

        var iEnd = this._strBuffer.indexOf(endKey, iStart);
        if (iEnd < 0) throw new Error("property " + prop + " improperly terminated: " + this._strBuffer);

        iStart += prop.length;
        var nChars = iEnd - iStart;
        var rtnStr = this._strBuffer.substr(iStart, nChars);

        if (advanceBuffer) {
            this._strBuffer = this._strBuffer.substr(iEnd + endKey.length);
        }

        return rtnStr;
    };

    this.nextToken = function () {
        if (!this._strBuffer) return;

        // find the limits
        var index = this._strBuffer.search(/\S/); 	// first non-whitespace character
        if (index > 0) this._strBuffer = this._strBuffer.slice(index);
        index = this._strBuffer.search(/\s/); 	// first whitespace character marking the end of the token

        var token;
        if (index > 0) {
            token = this._strBuffer.slice(0, index);
            this._strBuffer = this._strBuffer.slice(index);
        }

        return token;
    };

    this.advancePastToken = function (token) {
        var index = this._strBuffer.indexOf(token);
        if (index < 0) {
            console.log("could not find token: " + token + " in string: " + this._strBuffer);
        } else {
            this._strBuffer = this._strBuffer.substr(index + token.length);
        }
    };
};

if (typeof exports === "object") {
    exports.MaterialParser = MaterialParser;
}