use strict;
my $str = '/**
* @file Error base class
* @since 0.1.5
*/
/*#ifndef(UMD)*/
"use strict";
/*global _gpfExtend*/ // gpf.extend
/*global _gpfIgnore*/ // Helper to remove unused parameter warning
/*global _gpfObjectForEach*/ // Similar to [].forEach but for objects
/*global _gpfStringCapitalize*/ // Capitalize the string
/*global _gpfStringReplaceEx*/ // String replacement using dictionary map
/*exported _gpfErrorDeclare*/ // Declare new gpf.Error names
/*#endif*/
/**
* GPF Error class
*
* @constructor
* @alias gpf.Error
* @since 0.1.5
*/
var _GpfError = gpf.Error = function () {};
_GpfError.prototype = new Error();
_gpfExtend(_GpfError.prototype, /** @lends gpf.Error.prototype */ {
constructor: _GpfError,
/**
* Error code
*
* @readonly
* @since 0.1.5
*/
code: 0,
/**
* Error name
*
* @readonly
* @since 0.1.5
*/
name: "Error",
/**
* Error message
*
* @readonly
* @since 0.1.5
*/
message: "",
/**
* Build message by substituting context variables
*
* @param {Object} context Dictionary of named keys
* @since 0.1.5
*/
_buildMessage: function (context) {
var replacements;
if (context) {
replacements = {};
_gpfObjectForEach(context, function (value, key) {
replacements["{" + key + "}"] = value.toString();
});
this.message = _gpfStringReplaceEx(this.message, replacements);
}
}
});
function _gpfErrorFactory (code, name, message) {
function NewErrorClass (context) {
this._buildMessage(context);
}
NewErrorClass.prototype = new _GpfError();
_gpfExtend(NewErrorClass.prototype, {
code: code,
name: name,
message: message
});
// constructor can\'t be enumerated with wscript
NewErrorClass.prototype.constructor = NewErrorClass;
_GpfError[_gpfStringCapitalize(name)] = NewErrorClass;
return function (context) {
throw new NewErrorClass(context);
};
}
/**
* Generates an error class
*
* @param {Number} code Error code
* @param {String} name Error name
* @param {String} message Error message
* @return {Function} New error class
* @gpf:closure
* @since 0.1.5
*/
function _gpfGenenerateErrorFunction (code, name, message) {
var result = _gpfErrorFactory(code, name, message);
result.CODE = code;
result.NAME = name;
result.MESSAGE = message;
return result;
}
// Last allocated error code
var _gpfLastErrorCode = 0;
/**
* Declare error messages.
* Each source declares its own errors.
*
* @param {String} source Source name
* @param {Object} dictionary Dictionary of error name to message
* @since 0.1.5
*/
function _gpfErrorDeclare (source, dictionary) {
_gpfIgnore(source);
_gpfObjectForEach(dictionary, function (message, name) {
var code = ++_gpfLastErrorCode;
gpf.Error["CODE_" + name.toUpperCase()] = code;
gpf.Error[name] = _gpfGenenerateErrorFunction(code, name, message);
});
}
_gpfErrorDeclare("error", {
/**
* ### Summary
*
* Method or function is not implemented
*
* ### Description
*
* This error is used to flag methods or functions that are not yet implemented.
* @since 0.1.5
*/
notImplemented:
"Not implemented",
/**
* ### Summary
*
* Method is abstract
*
* ### Description
*
* This error is used to implement abstract methods. Mostly used for interfaces.
* @since 0.1.5
*/
abstractMethod:
"Abstract method",
/**
* ### Summary
*
* An assertion failed
*
* ### Description
*
* This error is triggered when an assertion fails
*
* @see {@link gpf.assert}
* @see {@link gpf.asserts}
* @since 0.1.5
*/
assertionFailed:
"Assertion failed: {message}",
/**
* ### Summary
*
* Method or function was called with an invalid parameter
*
* ### Description
*
* This error is used when a parameter is invalid
* @since 0.1.5
*/
invalidParameter:
"Invalid parameter"
});
';
my $regex = qr/_gpfErrorDeclare\("([a-zA-Z\\]+)", {\n((?:.*\n)*)\s*}\)/p;
if ( $str =~ /$regex/ ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html