• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2Some portions:
3Copyright (c) 2008, Yahoo! Inc. All rights reserved.
4Code licensed under the BSD License:
5http://developer.yahoo.net/yui/license.txt
6version: 2.5.1
7*/
8
9// create org.antlr module
10if (typeof org == "undefined" || !org) {
11    var org = {};
12}
13if (typeof org.antlr == "undefined" || !org.antlr) {
14    /**
15     * The org.antlr global namespace object.  If antlr is already defined, the
16     * existing antlr object will not be overwritten so that defined
17     * namespaces are preserved.
18     * @namespace org.antlr
19     */
20    org.antlr = {};
21}
22
23/**
24 * The global JavaScript object.
25 */
26org.antlr.global = (function() {
27    return this;
28}).call(null);
29
30/**
31 * Returns the namespace specified and creates it if it doesn't exist.
32 *
33 * Be careful when naming packages. Reserved words may work in some browsers
34 * and not others. For instance, the following will fail in Safari:
35 * <pre>
36 * org.antlr.namespace("really.long.nested.namespace");
37 * </pre>
38 * This fails because "long" is a future reserved word in ECMAScript
39 *
40 * @static
41 * @param  {Array.<String>} arguments 1-n namespaces to create
42 * @return {Object}  A reference to the last namespace object created
43 * @example
44 * org.antlr.namespace("org.antlr.property.package");
45 */
46org.antlr.namespace = function() {
47    var a=arguments, o=null, i, j, d;
48    for (i=0; i<a.length; i=i+1) {
49        d=a[i].split(".");
50        o=org.antlr.global;
51
52        // ANTLR is implied, so it is ignored if it is included
53        for (j=0; j<d.length; j=j+1) {
54            o[d[j]]=o[d[j]] || {};
55            o=o[d[j]];
56        }
57    }
58
59    return o;
60};
61
62/**
63 * org.antlr.env is used to keep track of what is known about the library and
64 * the browsing environment
65 * @namespace org.antlr.env
66 */
67org.antlr.env = org.antlr.env || {};
68
69/**
70 * Do not fork for a browser if it can be avoided.  Use feature detection when
71 * you can.  Use the user agent as a last resort.  org.antlr.env.ua stores a
72 * version number for the browser engine, 0 otherwise.  This value may or may
73 * not map to the version number of the browser using the engine.  The value is
74 * presented as a float so that it can easily be used for boolean evaluation
75 * as well as for looking for a particular range of versions.  Because of this,
76 * some of the granularity of the version info may be lost (e.g., Gecko 1.8.0.9
77 * reports 1.8).
78 * @namespace org.antlr.env.ua
79 */
80org.antlr.env.ua = function() {
81    var o= /** @lends org.antlr.env.ua */ {
82
83        /**
84         * Internet Explorer version number or 0.  Example: 6
85         * @property ie
86         * @type float
87         */
88        ie:0,
89
90        /**
91         * Opera version number or 0.  Example: 9.2
92         * @property opera
93         * @type float
94         */
95        opera:0,
96
97        /**
98         * Gecko engine revision number.  Will evaluate to 1 if Gecko
99         * is detected but the revision could not be found. Other browsers
100         * will be 0.  Example: 1.8
101         * <pre>
102         * Firefox 1.0.0.4: 1.7.8   <-- Reports 1.7
103         * Firefox 1.5.0.9: 1.8.0.9 <-- Reports 1.8
104         * Firefox 2.0.0.3: 1.8.1.3 <-- Reports 1.8
105         * Firefox 3 alpha: 1.9a4   <-- Reports 1.9
106         * </pre>
107         * @property gecko
108         * @type float
109         */
110        gecko:0,
111
112        /**
113         * AppleWebKit version.  KHTML browsers that are not WebKit browsers
114         * will evaluate to 1, other browsers 0.  Example: 418.9.1
115         * <pre>
116         * Safari 1.3.2 (312.6): 312.8.1 <-- Reports 312.8 -- currently the
117         *                                   latest available for Mac OSX 10.3.
118         * Safari 2.0.2:         416     <-- hasOwnProperty introduced
119         * Safari 2.0.4:         418     <-- preventDefault fixed
120         * Safari 2.0.4 (419.3): 418.9.1 <-- One version of Safari may run
121         *                                   different versions of webkit
122         * Safari 2.0.4 (419.3): 419     <-- Tiger installations that have been
123         *                                   updated, but not updated
124         *                                   to the latest patch.
125         * Webkit 212 nightly:   522+    <-- Safari 3.0 precursor (with native SVG
126         *                                   and many major issues fixed).
127         * 3.x yahoo.com, flickr:422     <-- Safari 3.x hacks the user agent
128         *                                   string when hitting yahoo.com and
129         *                                   flickr.com.
130         * Safari 3.0.4 (523.12):523.12  <-- First Tiger release - automatic update
131         *                                   from 2.x via the 10.4.11 OS patch
132         * Webkit nightly 1/2008:525+    <-- Supports DOMContentLoaded event.
133         *                                   yahoo.com user agent hack removed.
134         *
135         * </pre>
136         * http://developer.apple.com/internet/safari/uamatrix.html
137         * @property webkit
138         * @type float
139         */
140        webkit: 0,
141
142        /**
143         * The mobile property will be set to a string containing any relevant
144         * user agent information when a modern mobile browser is detected.
145         * Currently limited to Safari on the iPhone/iPod Touch, Nokia N-series
146         * devices with the WebKit-based browser, and Opera Mini.
147         * @property mobile
148         * @type string
149         */
150        mobile: null,
151
152        /**
153         * Adobe AIR version number or 0.  Only populated if webkit is detected.
154         * Example: 1.0
155         * @property air
156         * @type float
157         */
158        air: 0,
159
160        /**
161         * Is this the Rhino interpreter?
162         * @property rhino
163         * @type Boolean
164         */
165        rhino: false
166
167    };
168
169    var ua, m;
170
171    try {
172        ua = navigator.userAgent;
173
174        // Modern KHTML browsers should qualify as Safari X-Grade
175        if ((/KHTML/).test(ua)) {
176            o.webkit=1;
177        }
178        // Modern WebKit browsers are at least X-Grade
179        m=ua.match(/AppleWebKit\/([^\s]*)/);
180        if (m&&m[1]) {
181            o.webkit=parseFloat(m[1]);
182
183            // Mobile browser check
184            if (/ Mobile\//.test(ua)) {
185                o.mobile = "Apple"; // iPhone or iPod Touch
186        } else {
187            m=ua.match(/NokiaN[^\/]*/);
188            if (m) {
189                o.mobile = m[0]; // Nokia N-series, ex: NokiaN95
190            }
191        }
192
193        m=ua.match(/AdobeAIR\/([^\s]*)/);
194        if (m) {
195            o.air = m[0]; // Adobe AIR 1.0 or better
196        }
197
198        }
199
200        if (!o.webkit) { // not webkit
201            // @todo check Opera/8.01 (J2ME/MIDP; Opera Mini/2.0.4509/1316; fi; U; ssr)
202            m=ua.match(/Opera[\s\/]([^\s]*)/);
203            if (m&&m[1]) {
204                o.opera=parseFloat(m[1]);
205                m=ua.match(/Opera Mini[^;]*/);
206            if (m) {
207                o.mobile = m[0]; // ex: Opera Mini/2.0.4509/1316
208            }
209            } else { // not opera or webkit
210                m=ua.match(/MSIE\s([^;]*)/);
211                if (m&&m[1]) {
212                    o.ie=parseFloat(m[1]);
213                } else { // not opera, webkit, or ie
214                    m=ua.match(/Gecko\/([^\s]*)/);
215                    if (m) {
216                        o.gecko=1; // Gecko detected, look for revision
217                        m=ua.match(/rv:([^\s\)]*)/);
218                        if (m&&m[1]) {
219                            o.gecko=parseFloat(m[1]);
220                        }
221                    }
222                }
223            }
224        }
225    } catch(e) {
226        // ignore this if we're not in a browser
227    }
228
229    try {
230        if (typeof window=="undefined" && typeof loadClass!="undefined") {
231            o.rhino = true;
232        }
233    } catch(e) {}
234
235
236    return o;
237}();
238
239/**
240 * JavaScript runtime library code.
241 * @name org.antlr.runtime
242 * @namespace
243 */
244/**
245 * JavaScript runtime library tree parser code.
246 * @name org.antlr.runtime.tree
247 * @namespace
248 */
249org.antlr.namespace("org.antlr.runtime.tree");
250
251/**
252 * Provides the language utilites and extensions used by the library
253 * @namespace org.antlr.lang
254 */
255org.antlr.lang = org.antlr.lang || /** @lends org.antlr.lang */ {
256    /**
257     * Determines whether or not the provided object is an array.
258     * Testing typeof/instanceof/constructor of arrays across frame
259     * boundaries isn't possible in Safari unless you have a reference
260     * to the other frame to test against its Array prototype.  To
261     * handle this case, we test well-known array properties instead.
262     * properties.
263     * @param {any} o The object being testing
264     * @return {boolean} the result
265     */
266    isArray: function(o) {
267        if (o) {
268           var l = org.antlr.lang;
269           return l.isNumber(o.length) && l.isFunction(o.splice);
270        }
271        return false;
272    },
273
274    /**
275     * Determines whether or not the provided object is a boolean
276     * @param {any} o The object being testing
277     * @return {boolean} the result
278     */
279    isBoolean: function(o) {
280        return typeof o === 'boolean';
281    },
282
283    /**
284     * Determines whether or not the provided object is a function
285     * @param {any} o The object being testing
286     * @return {boolean} the result
287     */
288    isFunction: function(o) {
289        return typeof o === 'function';
290    },
291
292    /**
293     * Determines whether or not the provided object is null
294     * @param {any} o The object being testing
295     * @return {boolean} the result
296     */
297    isNull: function(o) {
298        return o === null;
299    },
300
301    /**
302     * Determines whether or not the provided object is a legal number
303     * @param {any} o The object being testing
304     * @return {boolean} the result
305     */
306    isNumber: function(o) {
307        return typeof o === 'number' && isFinite(o);
308    },
309
310    /**
311     * Determines whether or not the provided object is of type object
312     * or function
313     * @param {any} o The object being testing
314     * @return {boolean} the result
315     */
316    isObject: function(o) {
317return (o && (typeof o === 'object' || org.antlr.lang.isFunction(o))) || false;
318    },
319
320    /**
321     * Determines whether or not the provided object is a string
322     * @param {any} o The object being testing
323     * @return {boolean} the result
324     */
325    isString: function(o) {
326        return typeof o === 'string';
327    },
328
329    /**
330     * Determines whether or not the provided object is undefined
331     * @param {any} o The object being testing
332     * @return {boolean} the result
333     */
334    isUndefined: function(o) {
335        return typeof o === 'undefined';
336    },
337
338    /**
339     * IE will not enumerate native functions in a derived object even if the
340     * function was overridden.  This is a workaround for specific functions
341     * we care about on the Object prototype.
342     * @param {Function} r  the object to receive the augmentation
343     * @param {Function} s  the object that supplies the properties to augment
344     * @private
345     */
346    _IEEnumFix: function(r, s) {
347        if (org.antlr.env.ua.ie) {
348            var add=["toString", "valueOf"], i;
349            for (i=0;i<add.length;i=i+1) {
350                var fname=add[i],f=s[fname];
351                if (org.antlr.lang.isFunction(f) && f!=Object.prototype[fname]) {
352                    r[fname]=f;
353                }
354            }
355        }
356    },
357
358    /**
359     * Utility to set up the prototype, constructor and superclass properties to
360     * support an inheritance strategy that can chain constructors and methods.
361     * Static members will not be inherited.
362     *
363     * @method extend
364     * @static
365     * @param {Function} subc   the object to modify
366     * @param {Function} superc the object to inherit
367     * @param {Object} [overrides]  additional properties/methods to add to the
368     *                              subclass prototype.  These will override the
369     *                              matching items obtained from the superclass
370     *                              if present.
371     */
372    extend: function(subc, superc, overrides) {
373        if (!superc||!subc) {
374            throw new Error("org.antlr.lang.extend failed, please check that " +
375                            "all dependencies are included.");
376        }
377        var F = function() {};
378        F.prototype=superc.prototype;
379        subc.prototype=new F();
380        subc.prototype.constructor=subc;
381        subc.superclass=superc.prototype;
382        if (superc.prototype.constructor == Object.prototype.constructor) {
383            superc.prototype.constructor=superc;
384        }
385
386        if (overrides) {
387            for (var i in overrides) {
388                subc.prototype[i]=overrides[i];
389            }
390
391            org.antlr.lang._IEEnumFix(subc.prototype, overrides);
392        }
393    },
394
395    /**
396     * Applies all properties in the supplier to the receiver if the
397     * receiver does not have these properties yet.  Optionally, one or
398     * more methods/properties can be specified (as additional
399     * parameters).  This option will overwrite the property if receiver
400     * has it already.  If true is passed as the third parameter, all
401     * properties will be applied and _will_ overwrite properties in
402     * the receiver.
403     *
404     * @param {Function} r  the object to receive the augmentation
405     * @param {Function} s  the object that supplies the properties to augment
406     * @param {Array.<String>|boolean}  [arguments] zero or more properties
407     *        methods  to augment the receiver with.  If none specified,
408     *        everything in the supplier will be used unless it would
409     *        overwrite an existing property in the receiver. If true
410     *        is specified as the third parameter, all properties will
411     *        be applied and will overwrite an existing property in
412     *        the receiver
413     */
414    augmentObject: function(r, s) {
415        if (!s||!r) {
416            throw new Error("Absorb failed, verify dependencies.");
417        }
418        var a=arguments, i, p, override=a[2];
419        if (override && override!==true) { // only absorb the specified properties
420            for (i=2; i<a.length; i=i+1) {
421                r[a[i]] = s[a[i]];
422            }
423        } else { // take everything, overwriting only if the third parameter is true
424            for (p in s) {
425                if (override || !r[p]) {
426                    r[p] = s[p];
427                }
428            }
429
430            org.antlr.lang._IEEnumFix(r, s);
431        }
432    },
433
434    /**
435     * Same as org.antlr.lang.augmentObject, except it only applies prototype properties
436     * @see org.antlr.lang.augmentObject
437     * @param {Function} r  the object to receive the augmentation
438     * @param {Function} s  the object that supplies the properties to augment
439     * @param {Array.<String>|boolean}  [arguments] zero or more properties methods
440     *        to augment the receiver with.  If none specified, everything
441     *        in the supplier will be used unless it would overwrite an existing
442     *        property in the receiver.  if true is specified as the third
443     *        parameter, all properties will be applied and will overwrite an
444     *        existing property in the receiver
445     */
446    augmentProto: function(r, s) {
447        if (!s||!r) {
448            throw new Error("Augment failed, verify dependencies.");
449        }
450        //var a=[].concat(arguments);
451        var a=[r.prototype,s.prototype];
452        for (var i=2;i<arguments.length;i=i+1) {
453            a.push(arguments[i]);
454        }
455        org.antlr.lang.augmentObject.apply(this, a);
456    },
457
458    /**
459     * Returns a new object containing all of the properties of
460     * all the supplied objects.  The properties from later objects
461     * will overwrite those in earlier objects.
462     * @param arguments {Object*} the objects to merge
463     * @return the new merged object
464     */
465    merge: function() {
466        var o={}, a=arguments;
467        for (var i=0, l=a.length; i<l; i=i+1) {
468            org.antlr.lang.augmentObject(o, a[i], true);
469        }
470        return o;
471    },
472
473    /**
474     * A convenience method for detecting a legitimate non-null value.
475     * Returns false for null/undefined/NaN, true for other values,
476     * including 0/false/''
477     * @param o {any} the item to test
478     * @return {boolean} true if it is not null/undefined/NaN || false
479     */
480    isValue: function(o) {
481        var l = org.antlr.lang;
482return (l.isObject(o) || l.isString(o) || l.isNumber(o) || l.isBoolean(o));
483    },
484
485    /** @namespace org.antlr.lang.array Array convenience methods. */
486    array: /** @lends org.antlr.lang.array */ {
487        /**
488         * Retrieve the last element of an array. Throws an error if a is not
489         * an array or empty.
490         * @param a {Array} the array stack to peek in
491         * @return the last element of the array
492         */
493         peek: function(a) {
494            if (!org.antlr.lang.isArray(a)) {
495                throw new Error("org.antlr.lang.array.peek: a is not an array.");
496            }
497            var l = a.length;
498            if (l<=0) {
499                throw new Error("org.antlr.lang.array.peek: a is empty.");
500            }
501            return a[l-1];
502        }
503    }
504};
505