Skip to content

Instantly share code, notes, and snippets.

@kaleb
Last active December 10, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaleb/4447112 to your computer and use it in GitHub Desktop.
Save kaleb/4447112 to your computer and use it in GitHub Desktop.
The plan is to use this in a html5 form pattern attribute masker http://zaach.github.com/jison/try/ http://jsfiddle.net/kaleb/aWpec/
%{
/*
* code
*/
function xerox(que, quanto) {
var ary = [];
while(quanto--) ary.push(que);
return ary;
}
%}
%lex
%{
/************\
* code block *
\************/
%}
/**
* lex definitions
*/
CHAR_CLASS "\\c"[A-Z]|"\\"[dDfnrsStvwW]|"["[^\]]*"]"
%%
/***********\
* lex rules *
\***********/
"{" return '{'
"}" return '}'
"(" return '('
")" return ')'
"?" return '?'
[0-9]+ return 'NUM'
{CHAR_CLASS}|"." yytext = RegExp(yytext); return 'CLASS'
. return 'CHAR'
<<EOF>> return 'EOF'
%%
/**
* lex user code; is this supported in jison?
*/
/lex
%start regex
%%
regex : rx EOF { console.log($$); return $$; };
rx : { $$ = []; }
| rx CHAR { $$.push($CHAR); }
| rx class { $$ = $$.concat($class); }
| rx sub { $$ = $$.concat($2); }
;
class : CLASS { $$ = [$CLASS]; }
| CLASS "{" NUM "}" { $$ = xerox($CLASS, $NUM); }
;
sub : "(" rx ")" "?" { $$ = [$rx]; }
;
<input type="tel" pattern="\d{3}-\d{3}-\d{4}" required="" />
<input type="submit" />
(function($, win, undef){
var n = 'pattern', // name of plugin; subject to change
def = 'initialize', // default method name
defaults = { // Default plugin options
//TODO
},
methods = { // Default plugin methods
/**
* HTML5 pattern attribute polyfill with basic masking capabilities
*
* @return jQuery object
*/
initialize: function(options){
return this.each(function(i, el){
var $this = $(el),
data = $this.data(n),
// Extend the default options
settings = $.extend({}, defaults, options || {}),
pattern = this.pattern;
if(!data){
// Initialize data object
//TODO
// Set data object
$this.data(n, {
//TODO
});
}
console.log(el);
});
},
/**
* Dispose of the pattern features on specified objects
* @return jQuery object
*/
dispose: function(){
return this.each(function(i, el){
var $this = $(el);
$this.removeData(n);
$.error('Method `dispose` has yet to be implemented.');
});
}
};
// expose functionality
$[n] = {
options: defaults,
fn: methods
};
$.fn[n] = function(method) {
if(methods[method]){
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if(typeof method === 'object' || !method) {
return methods[def].apply(this, arguments);
} else {
$.error('Method `' + method + '` does not exist on jQuery.mask');
}
};
})(jQuery, this, undefined);
$('[pattern]').pattern();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment