Skip to content

Commit

Permalink
v.1.0.1
Browse files Browse the repository at this point in the history
* fix anyChar issue in generate src
* some changes for composer groups and wordboundary in analyzer
  • Loading branch information
Nikos M committed Dec 17, 2016
1 parent 825aa95 commit 2a2edd4
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 69 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A generic, simple & intuitive **Regular Expression Analyzer & Composer**
Java, C/C++, ActionScript (TODO)


**Regex v.1.0.0**
**Regex v.1.0.1**


**see also:**
Expand Down
32 changes: 25 additions & 7 deletions src/js/Regex.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
* Regex
* @version: 1.0.0
* @version: 1.0.1
*
* A simple & generic Regular Expression Analyzer & Composer for PHP, Python, Node/XPCOM/JS, Java, C/C++, ActionScript
* https://github.com/foo123/RegexAnalyzer
Expand All @@ -23,7 +23,7 @@ else if ( !(name in root) ) /* Browser/WebWorker/.. */
/* module name */ "Regex",
/* module factory */ function ModuleFactory__Regex( undef ){
"use strict";
var __version__ = "1.0.0",
var __version__ = "1.0.1",

PROTO = 'prototype', OP = Object[PROTO], AP = Array[PROTO],
Keys = Object.keys, to_string = OP.toString, HAS = OP.hasOwnProperty,
Expand Down Expand Up @@ -861,7 +861,7 @@ var rnd = function( a, b ){ return Math.round((b-a)*Math.random()+a); },
}
else
{
ret.src += !node.flags.MatchStart && !node.flags.MatchEnd ? (ESC+node.val) : (''+node.val);
ret.src += node.flags.MatchAnyChar || node.flags.MatchStart || node.flags.MatchEnd ? (''+node.val) : (ESC+node.val);
}
}
else if ( T_STRING === type )
Expand Down Expand Up @@ -918,6 +918,10 @@ var rnd = function( a, b ){ return Math.round((b-a)*Math.random()+a); },
{
ret[inNegativeCharGroup?"positive":"negative"][ '\\s' ] = 1;
}
else if ('B' === part)
{
ret[inNegativeCharGroup?"positive":"negative"][ '\\b' ] = 1;
}
else
{
ret[peek][ESC + part] = 1;
Expand Down Expand Up @@ -1732,6 +1736,12 @@ Analyzer[PROTO] = {
cases = concat(cases, ['_'].concat(character_range('0', '9')).concat(character_range('a', 'z')).concat(character_range('A', 'Z')));
}

else if ('\\b' === c)
{
delete p[c];
cases[ specialChars['b'] ] = 1;
}

else if ('\\.' === c)
{
delete p[c];
Expand Down Expand Up @@ -1999,14 +2009,22 @@ Composer[PROTO] = {
return this.alternate();
},

group: function( opts ) {
group: function( opts, v ) {
var self = this, type = T_GROUP, fl = '';
opts = opts || {};
if ( !!opts['name'] )
if ( is_string(opts) )
{
fl = opts; opts = {};
opts[fl] = v; fl = '';
}
else
{
opts = opts || {};
}
if ( !!opts['name'] || !!opts['named'] )
{
self.g++;
self.grp[self.g] = self.g;
self.grp[opts.name] = self.g;
self.grp[opts.name||opts.named] = self.g;
}
else if ( (true === opts['lookahead']) || (false === opts['lookahead']) )
{
Expand Down
4 changes: 2 additions & 2 deletions src/js/Regex.min.js

Large diffs are not rendered by default.

36 changes: 27 additions & 9 deletions src/php/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
*
* Regex
* @version: 1.0.0
* @version: 1.0.1
*
* A simple & generic Regular Expression Analyzer & Composer for PHP, Python, Node/XPCOM/JS, Java, C/C++, ActionScript
* https://github.com/foo123/RegexAnalyzer
Expand Down Expand Up @@ -131,7 +131,7 @@ public function toObject( )
// http://php.net/manual/en/reference.pcre.pattern.syntax.php
class RegexAnalyzer
{
const VERSION = "1.0.0";
const VERSION = "1.0.1";
public static $BSPACES = null;
public static $SPACES = null;
public static $PUNCTS = null;
Expand Down Expand Up @@ -801,7 +801,7 @@ private static function reduce_src( $ret, &$node, &$state )
}
else
{
$ret->src .= empty($node->flags->MatchStart) && empty($node->flags->MatchEnd) ? (Regex::ESC.$node->val) : (''.$node->val);
$ret->src .= isset($node->flags->MatchAnyChar) || isset($node->flags->MatchStart) || isset($node->flags->MatchEnd) ? (''.$node->val) : (Regex::ESC.$node->val);
}
}
elseif ( Regex::T_STRING === $type )
Expand Down Expand Up @@ -857,6 +857,10 @@ private static function reduce_peek( $ret, &$node, &$state )
{
$ret[$inNegativeCharGroup?"positive":"negative"][ '\\s' ] = 1;
}
elseif ('B' === $part)
{
$ret[$inNegativeCharGroup?"positive":"negative"][ '\\b' ] = 1;
}
else
{
$ret[$peek][Regex::ESC . $part] = 1;
Expand Down Expand Up @@ -1689,6 +1693,12 @@ public function peek( )
));
}

else if ('\\b' === $c)
{
unset( $p[$c] );
$cases[ $this->specialChars['b'] ] = 1;
}

else if ('\\.' === $c)
{
unset( $p[$c] );
Expand Down Expand Up @@ -1726,7 +1736,7 @@ public function peek( )
class RegexComposer
{

const VERSION = "1.0.0";
const VERSION = "1.0.1";

public $re = null;
private $g = 0;
Expand Down Expand Up @@ -1971,15 +1981,23 @@ public function either( )
return $this->alternate( );
}

public function group( $opts=array() )
public function group( $opts=array(), $v=null )
{
$type = Regex::T_GROUP; $fl = '';
$opts = (array)$opts;
if ( isset($opts['name']) && strlen($opts['name']) )
if ( is_string($opts) )
{
$fl = $opts; $opts = array();
$opts[$fl] = $v; $fl = '';
}
else
{
$opts = (array)$opts;
}
if ( (isset($opts['name']) && strlen($opts['name'])) || (isset($opts['named']) && strlen($opts['named'])) )
{
$this->g++;
$this->grp[$this->g] = $this->g;
$this->grp[$opts['name']] = $this->g;
$this->grp[isset($opts['name']) ? $opts['name'] : $opts['named']] = $this->g;
}
elseif ( isset($opts['lookahead']) && ((true === $opts['lookahead']) || (false === $opts['lookahead'])) )
{
Expand Down Expand Up @@ -2092,7 +2110,7 @@ public function endOfInput( )
}
class Regex
{
const VERSION = "1.0.0";
const VERSION = "1.0.1";
const T_SEQUENCE = 1;
const T_ALTERNATION = 2;
const T_GROUP = 4;
Expand Down
25 changes: 19 additions & 6 deletions src/python/Regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
##
#
# Regex
# @version: 1.0.0
# @version: 1.0.1
#
# A simple & generic Regular Expression Analyzer & Composer for PHP, Python, Node/XPCOM/JS, Java, C/C++, ActionScript
# https://github.com/foo123/Analyzer
Expand Down Expand Up @@ -638,7 +638,7 @@ def reduce_src( ret, node, state ):
if 'BackReference' in node.flags:
ret['src'] += ESC+node.val
else:
ret['src'] += ESC+node.val if ('MatchStart' not in node.flags) and ('MatchEnd' not in node.flags) else( ''+node.val)
ret['src'] += (''+node.val) if ('MatchAnyChar' in node.flags) or ('MatchStart' in node.flags) or ('MatchEnd' in node.flags) else (ESC+node.val)
elif T_STRING == type:
ret['src'] += esc_re(node.val, ESC) if state['escaped'] else node.val

Expand Down Expand Up @@ -675,6 +675,8 @@ def reduce_peek( ret, node, state ):
ret["positive" if inNegativeCharGroup else "negative"][ '\\w' ] = 1
elif 'S' == part:
ret["positive" if inNegativeCharGroup else "negative"][ '\\s' ] = 1
elif 'B' == part:
ret["positive" if inNegativeCharGroup else "negative"][ '\\b' ] = 1
else:
ret[peek][ESC + part] = 1
elif T_STRING == type:
Expand Down Expand Up @@ -1255,7 +1257,7 @@ def analyze_re( re_obj ):
# http://php.net/manual/en/reference.pcre.pattern.syntax.php
# A simple regular expression analyzer
class Analyzer:
VERSION = "1.0.0"
VERSION = "1.0.1"

def __init__(self, re=None, delim='/'):
self.ast = None
Expand Down Expand Up @@ -1481,6 +1483,11 @@ def peek( self ):
del p[c]
cases = concat(cases, ['_'] + character_range('0', '9') + character_range('a', 'z') + character_range('A', 'Z'))

elif '\\b' == c:

del p[c]
cases[ _G.specialChars['b'] ] = 1


elif '\\.' == c:

Expand Down Expand Up @@ -1518,7 +1525,7 @@ def getArgs( args ):

# A simple regular expression composer
class Composer:
VERSION = "1.0.0"
VERSION = "1.0.1"

def __init__( self ):
self.re = None
Expand Down Expand Up @@ -1683,9 +1690,15 @@ def alternate( self ):
def either( self ):
return self.alternate()

def group( self, opts=dict() ):
def group( self, opts=dict(), v=None ):
type = T_GROUP
fl = ''
if is_string(opts):
fl = opts
opts = {}
opts[fl] = v
fl = ''

if ('name' in opts) and len(str(opts['name'])):
self.g += 1
self.grp[str(self.g)] = self.g
Expand Down Expand Up @@ -1767,7 +1780,7 @@ class Regex:
Regular Expressipn Analyzer and Composer for Python
https://github.com/foo123/Analyzer
"""
VERSION = "1.0.0"
VERSION = "1.0.1"
Node = Node
Analyzer = Analyzer
Composer = Composer
Expand Down
32 changes: 16 additions & 16 deletions test/js/output.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Regex.VERSION = 1.0.0
Regex.VERSION = 1.0.1
Testing Regex.Composer
===============================================================
Partial : [_a-z][_a-z0-9]*
Expand Down Expand Up @@ -115,43 +115,43 @@ Regular Expression Minimum / Maximum Length :
Regular Expression Sample Match Strings :
[
{
"sample": "DCCefghDCCe",
"sample": "cFGHc",
"match": "yes",
"groups": {
"1": "DCCe",
"named_group": "DCCe"
"1": "c",
"named_group": "c"
}
},
{
"sample": "dBFGHdB",
"sample": "cDEfghcDE",
"match": "yes",
"groups": {
"1": "dB",
"named_group": "dB"
"1": "cDE",
"named_group": "cDE"
}
},
{
"sample": "EbcfghEbc",
"sample": "EDcFGHEDc",
"match": "yes",
"groups": {
"1": "Ebc",
"named_group": "Ebc"
"1": "EDc",
"named_group": "EDc"
}
},
{
"sample": "cbfghcb",
"sample": "eBFGHeB",
"match": "yes",
"groups": {
"1": "cb",
"named_group": "cb"
"1": "eB",
"named_group": "eB"
}
},
{
"sample": "dAfghdA",
"sample": "cBFGHcB",
"match": "yes",
"groups": {
"1": "dA",
"named_group": "dA"
"1": "cB",
"named_group": "cB"
}
}
]
Expand Down
Loading

0 comments on commit 2a2edd4

Please sign in to comment.