Skip to content

Commit 12ce28e

Browse files
authored
Regex matches operator (#1169)
* Regex matches operator support
1 parent 139797a commit 12ce28e

File tree

5 files changed

+1327
-1138
lines changed

5 files changed

+1327
-1138
lines changed

src/Lexer/TemplateLexer.php

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ class TemplateLexer
160160
'LOGOP' => '"<", "==" ... logical operator',
161161
'TLOGOP' => '"lt", "eq" ... logical operator; "is div by" ... if condition',
162162
'SCOND' => '"is even" ... if condition',
163+
'MATCHES' => '"matches" regex operator',
163164
);
164165

165166
/**
@@ -567,7 +568,7 @@ public function yy_r2_23()
567568
public function yylex3()
568569
{
569570
if (!isset($this->yy_global_pattern3)) {
570-
$this->yy_global_pattern3 = $this->replace("/\G(\\s*SMARTYrdel)|\G((SMARTYldel)SMARTYal)|\G([\"])|\G('[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|\G([$][0-9]*[a-zA-Z_]\\w*)|\G([$])|\G(\\s+is\\s+(not\\s+)?in\\s+)|\G(\\s+as\\s+)|\G(\\s+to\\s+)|\G(\\s+step\\s+)|\G(\\s+instanceof\\s+)|\G(\\s*([!=][=]{1,2}|[<][=>]?|[>][=]?|[&|]{2})\\s*)|\G(\\s+(eq|ne|neq|gt|ge|gte|lt|le|lte|mod|and|or|xor)\\s+)|\G(\\s+is\\s+(not\\s+)?(odd|even|div)\\s+by\\s+)|\G(\\s+is\\s+(not\\s+)?(odd|even))|\G([!]\\s*|not\\s+)|\G([(](int(eger)?|bool(ean)?|float|double|real|string|binary|array|object)[)]\\s*)|\G(\\s*[(]\\s*)|\G(\\s*[)])|\G(\\[\\s*)|\G(\\s*\\])|\G(\\s*[-][>]\\s*)|\G(\\s*[=][>]\\s*)|\G(\\s*[=]\\s*)|\G(([+]|[-]){2})|\G(\\s*([+]|[-])\\s*)|\G(\\s*([*]{1,2}|[%\/^&]|[<>]{2})\\s*)|\G([@])|\G(array\\s*[(]\\s*)|\G([#])|\G(\\s+[0-9]*[a-zA-Z_][a-zA-Z0-9_\-:]*\\s*[=]\\s*)|\G(([0-9]*[a-zA-Z_]\\w*)?(\\\\[0-9]*[a-zA-Z_]\\w*)+)|\G([0-9]*[a-zA-Z_]\\w*)|\G(\\d+)|\G([`])|\G([|][@]?)|\G([.])|\G(\\s*[,]\\s*)|\G(\\s*[;]\\s*)|\G([:]{2})|\G(\\s*[:]\\s*)|\G(\\s*[?]\\s*)|\G(0[xX][0-9a-fA-F]+)|\G(\\s+)|\G([\S\s])/isS");
571+
$this->yy_global_pattern3 = $this->replace("/\G(\\s*SMARTYrdel)|\G((SMARTYldel)SMARTYal)|\G([\"])|\G('[^'\\\\]*(?:\\\\.[^'\\\\]*)*')|\G([$][0-9]*[a-zA-Z_]\\w*)|\G([$])|\G(\\s+is\\s+(not\\s+)?in\\s+)|\G(\\s+matches\\s+)|\G(\\s+as\\s+)|\G(\\s+to\\s+)|\G(\\s+step\\s+)|\G(\\s+instanceof\\s+)|\G(\\s*([!=][=]{1,2}|[<][=>]?|[>][=]?|[&|]{2})\\s*)|\G(\\s+(eq|ne|neq|gt|ge|gte|lt|le|lte|mod|and|or|xor)\\s+)|\G(\\s+is\\s+(not\\s+)?(odd|even|div)\\s+by\\s+)|\G(\\s+is\\s+(not\\s+)?(odd|even))|\G([!]\\s*|not\\s+)|\G([(](int(eger)?|bool(ean)?|float|double|real|string|binary|array|object)[)]\\s*)|\G(\\s*[(]\\s*)|\G(\\s*[)])|\G(\\[\\s*)|\G(\\s*\\])|\G(\\s*[-][>]\\s*)|\G(\\s*[=][>]\\s*)|\G(\\s*[=]\\s*)|\G(([+]|[-]){2})|\G(\\s*([+]|[-])\\s*)|\G(\\s*([*]{1,2}|[%\/^&]|[<>]{2})\\s*)|\G([@])|\G(array\\s*[(]\\s*)|\G([#])|\G(\\s+[0-9]*[a-zA-Z_][a-zA-Z0-9_\-:]*\\s*[=]\\s*)|\G(([0-9]*[a-zA-Z_]\\w*)?(\\\\[0-9]*[a-zA-Z_]\\w*)+)|\G([0-9]*[a-zA-Z_]\\w*)|\G(\\d+)|\G([`])|\G([|][@]?)|\G([.])|\G(\\s*[,]\\s*)|\G(\\s*[;]\\s*)|\G([:]{2})|\G(\\s*[:]\\s*)|\G(\\s*[?]\\s*)|\G(0[xX][0-9a-fA-F]+)|\G(\\s+)|\G([\S\s])/isS");
571572
}
572573
if (!isset($this->dataLength)) {
573574
$this->dataLength = strlen($this->data);
@@ -662,119 +663,124 @@ public function yy_r3_8()
662663
public function yy_r3_10()
663664
{
664665

665-
$this->token = \Smarty\Parser\TemplateParser::TP_AS;
666+
$this->token = \Smarty\Parser\TemplateParser::TP_MATCHES;
666667
}
667668
public function yy_r3_11()
668669
{
669670

670-
$this->token = \Smarty\Parser\TemplateParser::TP_TO;
671+
$this->token = \Smarty\Parser\TemplateParser::TP_AS;
671672
}
672673
public function yy_r3_12()
673674
{
674675

675-
$this->token = \Smarty\Parser\TemplateParser::TP_STEP;
676+
$this->token = \Smarty\Parser\TemplateParser::TP_TO;
676677
}
677678
public function yy_r3_13()
678679
{
679680

680-
$this->token = \Smarty\Parser\TemplateParser::TP_INSTANCEOF;
681+
$this->token = \Smarty\Parser\TemplateParser::TP_STEP;
681682
}
682683
public function yy_r3_14()
684+
{
685+
686+
$this->token = \Smarty\Parser\TemplateParser::TP_INSTANCEOF;
687+
}
688+
public function yy_r3_15()
683689
{
684690

685691
$this->token = \Smarty\Parser\TemplateParser::TP_LOGOP;
686692
}
687-
public function yy_r3_16()
693+
public function yy_r3_17()
688694
{
689695

690696
$this->token = \Smarty\Parser\TemplateParser::TP_SLOGOP;
691697
}
692-
public function yy_r3_18()
698+
public function yy_r3_19()
693699
{
694700

695701
$this->token = \Smarty\Parser\TemplateParser::TP_TLOGOP;
696702
}
697-
public function yy_r3_21()
703+
public function yy_r3_22()
698704
{
699705

700706
$this->token = \Smarty\Parser\TemplateParser::TP_SINGLECOND;
701707
}
702-
public function yy_r3_24()
708+
public function yy_r3_25()
703709
{
704710

705711
$this->token = \Smarty\Parser\TemplateParser::TP_NOT;
706712
}
707-
public function yy_r3_25()
713+
public function yy_r3_26()
708714
{
709715

710716
$this->token = \Smarty\Parser\TemplateParser::TP_TYPECAST;
711717
}
712-
public function yy_r3_29()
718+
public function yy_r3_30()
713719
{
714720

715721
$this->token = \Smarty\Parser\TemplateParser::TP_OPENP;
716722
}
717-
public function yy_r3_30()
723+
public function yy_r3_31()
718724
{
719725

720726
$this->token = \Smarty\Parser\TemplateParser::TP_CLOSEP;
721727
}
722-
public function yy_r3_31()
728+
public function yy_r3_32()
723729
{
724730

725731
$this->token = \Smarty\Parser\TemplateParser::TP_OPENB;
726732
}
727-
public function yy_r3_32()
733+
public function yy_r3_33()
728734
{
729735

730736
$this->token = \Smarty\Parser\TemplateParser::TP_CLOSEB;
731737
}
732-
public function yy_r3_33()
738+
public function yy_r3_34()
733739
{
734740

735741
$this->token = \Smarty\Parser\TemplateParser::TP_PTR;
736742
}
737-
public function yy_r3_34()
743+
public function yy_r3_35()
738744
{
739745

740746
$this->token = \Smarty\Parser\TemplateParser::TP_APTR;
741747
}
742-
public function yy_r3_35()
748+
public function yy_r3_36()
743749
{
744750

745751
$this->token = \Smarty\Parser\TemplateParser::TP_EQUAL;
746752
}
747-
public function yy_r3_36()
753+
public function yy_r3_37()
748754
{
749755

750756
$this->token = \Smarty\Parser\TemplateParser::TP_INCDEC;
751757
}
752-
public function yy_r3_38()
758+
public function yy_r3_39()
753759
{
754760

755761
$this->token = \Smarty\Parser\TemplateParser::TP_UNIMATH;
756762
}
757-
public function yy_r3_40()
763+
public function yy_r3_41()
758764
{
759765

760766
$this->token = \Smarty\Parser\TemplateParser::TP_MATH;
761767
}
762-
public function yy_r3_42()
768+
public function yy_r3_43()
763769
{
764770

765771
$this->token = \Smarty\Parser\TemplateParser::TP_AT;
766772
}
767-
public function yy_r3_43()
773+
public function yy_r3_44()
768774
{
769775

770776
$this->token = \Smarty\Parser\TemplateParser::TP_ARRAYOPEN;
771777
}
772-
public function yy_r3_44()
778+
public function yy_r3_45()
773779
{
774780

775781
$this->token = \Smarty\Parser\TemplateParser::TP_HATCH;
776782
}
777-
public function yy_r3_45()
783+
public function yy_r3_46()
778784
{
779785

780786
// resolve conflicts with shorttag and right_delimiter starting with '='
@@ -786,73 +792,73 @@ public function yy_r3_45()
786792
$this->token = \Smarty\Parser\TemplateParser::TP_ATTR;
787793
}
788794
}
789-
public function yy_r3_46()
795+
public function yy_r3_47()
790796
{
791797

792798
$this->token = \Smarty\Parser\TemplateParser::TP_NAMESPACE;
793799
}
794-
public function yy_r3_49()
800+
public function yy_r3_50()
795801
{
796802

797803
$this->token = \Smarty\Parser\TemplateParser::TP_ID;
798804
}
799-
public function yy_r3_50()
805+
public function yy_r3_51()
800806
{
801807

802808
$this->token = \Smarty\Parser\TemplateParser::TP_INTEGER;
803809
}
804-
public function yy_r3_51()
810+
public function yy_r3_52()
805811
{
806812

807813
$this->token = \Smarty\Parser\TemplateParser::TP_BACKTICK;
808814
$this->yypopstate();
809815
}
810-
public function yy_r3_52()
816+
public function yy_r3_53()
811817
{
812818

813819
$this->token = \Smarty\Parser\TemplateParser::TP_VERT;
814820
}
815-
public function yy_r3_53()
821+
public function yy_r3_54()
816822
{
817823

818824
$this->token = \Smarty\Parser\TemplateParser::TP_DOT;
819825
}
820-
public function yy_r3_54()
826+
public function yy_r3_55()
821827
{
822828

823829
$this->token = \Smarty\Parser\TemplateParser::TP_COMMA;
824830
}
825-
public function yy_r3_55()
831+
public function yy_r3_56()
826832
{
827833

828834
$this->token = \Smarty\Parser\TemplateParser::TP_SEMICOLON;
829835
}
830-
public function yy_r3_56()
836+
public function yy_r3_57()
831837
{
832838

833839
$this->token = \Smarty\Parser\TemplateParser::TP_DOUBLECOLON;
834840
}
835-
public function yy_r3_57()
841+
public function yy_r3_58()
836842
{
837843

838844
$this->token = \Smarty\Parser\TemplateParser::TP_COLON;
839845
}
840-
public function yy_r3_58()
846+
public function yy_r3_59()
841847
{
842848

843849
$this->token = \Smarty\Parser\TemplateParser::TP_QMARK;
844850
}
845-
public function yy_r3_59()
851+
public function yy_r3_60()
846852
{
847853

848854
$this->token = \Smarty\Parser\TemplateParser::TP_HEX;
849855
}
850-
public function yy_r3_60()
856+
public function yy_r3_61()
851857
{
852858

853859
$this->token = \Smarty\Parser\TemplateParser::TP_SPACE;
854860
}
855-
public function yy_r3_61()
861+
public function yy_r3_62()
856862
{
857863

858864
$this->token = \Smarty\Parser\TemplateParser::TP_TEXT;

src/Lexer/TemplateLexer.plex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ class TemplateLexer
160160
'LOGOP' => '"<", "==" ... logical operator',
161161
'TLOGOP' => '"lt", "eq" ... logical operator; "is div by" ... if condition',
162162
'SCOND' => '"is even" ... if condition',
163+
'MATCHES' => '"matches" regex operator',
163164
);
164165

165166
/**
@@ -325,6 +326,7 @@ class TemplateLexer
325326
tlop = ~\s+is\s+(not\s+)?(odd|even|div)\s+by\s+~
326327
scond = ~\s+is\s+(not\s+)?(odd|even)~
327328
isin = ~\s+is\s+(not\s+)?in\s+~
329+
matches = ~\s+matches\s+~
328330
as = ~\s+as\s+~
329331
to = ~\s+to\s+~
330332
step = ~\s+step\s+~
@@ -469,6 +471,9 @@ class TemplateLexer
469471
isin {
470472
$this->token = \Smarty\Parser\TemplateParser::TP_ISIN;
471473
}
474+
matches {
475+
$this->token = \Smarty\Parser\TemplateParser::TP_MATCHES;
476+
}
472477
as {
473478
$this->token = \Smarty\Parser\TemplateParser::TP_AS;
474479
}

0 commit comments

Comments
 (0)