diff options
author | Jelmer Vernooij <jelmer@samba.org> | 2007-01-05 20:18:33 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 14:36:58 -0500 |
commit | 74239c2e944fdbe1ff137e91d80576b87c59d478 (patch) | |
tree | 06eb2349c699893cc243abedecb12ac652068bce | |
parent | 5a162488a1018d44b28475538bffeeef56b453b6 (diff) | |
download | samba-74239c2e944fdbe1ff137e91d80576b87c59d478.tar.gz samba-74239c2e944fdbe1ff137e91d80576b87c59d478.tar.bz2 samba-74239c2e944fdbe1ff137e91d80576b87c59d478.zip |
r20563: Start using the new parser in ParseExpr(). It's now trivial to use this
to check for NULL pointers when pointers are being dereferenced (#4218).
There are exactly 500 tests for pidl now :-)
(This used to be commit d3146f3bcd4541f890d6c1b072ff34853e9239d2)
-rw-r--r-- | source4/pidl/lib/Parse/Pidl/IDL.pm | 477 | ||||
-rw-r--r-- | source4/pidl/lib/Parse/Pidl/Util.pm | 25 | ||||
-rwxr-xr-x | source4/pidl/tests/util.pl | 37 |
3 files changed, 36 insertions, 503 deletions
diff --git a/source4/pidl/lib/Parse/Pidl/IDL.pm b/source4/pidl/lib/Parse/Pidl/IDL.pm index 7c4263fd11..13a34fae64 100644 --- a/source4/pidl/lib/Parse/Pidl/IDL.pm +++ b/source4/pidl/lib/Parse/Pidl/IDL.pm @@ -12,482 +12,7 @@ use vars qw ( @ISA ); use strict; @ISA= qw ( Parse::Yapp::Driver ); -#Included Parse/Yapp/Driver.pm file---------------------------------------- -{ -# -# Module Parse::Yapp::Driver -# -# This module is part of the Parse::Yapp package available on your -# nearest CPAN -# -# Any use of this module in a standalone parser make the included -# text under the same copyright as the Parse::Yapp module itself. -# -# This notice should remain unchanged. -# -# (c) Copyright 1998-2001 Francois Desarmenien, all rights reserved. -# (see the pod text in Parse::Yapp module for use and distribution rights) -# - -package Parse::Yapp::Driver; - -require 5.004; - -use strict; - -use vars qw ( $VERSION $COMPATIBLE $FILENAME ); - -$VERSION = '1.05'; -$COMPATIBLE = '0.07'; -$FILENAME=__FILE__; - -use Carp; - -#Known parameters, all starting with YY (leading YY will be discarded) -my(%params)=(YYLEX => 'CODE', 'YYERROR' => 'CODE', YYVERSION => '', - YYRULES => 'ARRAY', YYSTATES => 'ARRAY', YYDEBUG => ''); -#Mandatory parameters -my(@params)=('LEX','RULES','STATES'); - -sub new { - my($class)=shift; - my($errst,$nberr,$token,$value,$check,$dotpos); - my($self)={ ERROR => \&_Error, - ERRST => \$errst, - NBERR => \$nberr, - TOKEN => \$token, - VALUE => \$value, - DOTPOS => \$dotpos, - STACK => [], - DEBUG => 0, - CHECK => \$check }; - - _CheckParams( [], \%params, \@_, $self ); - - exists($$self{VERSION}) - and $$self{VERSION} < $COMPATIBLE - and croak "Yapp driver version $VERSION ". - "incompatible with version $$self{VERSION}:\n". - "Please recompile parser module."; - - ref($class) - and $class=ref($class); - - bless($self,$class); -} - -sub YYParse { - my($self)=shift; - my($retval); - - _CheckParams( \@params, \%params, \@_, $self ); - - if($$self{DEBUG}) { - _DBLoad(); - $retval = eval '$self->_DBParse()';#Do not create stab entry on compile - $@ and die $@; - } - else { - $retval = $self->_Parse(); - } - $retval -} - -sub YYData { - my($self)=shift; - - exists($$self{USER}) - or $$self{USER}={}; - - $$self{USER}; - -} - -sub YYErrok { - my($self)=shift; - - ${$$self{ERRST}}=0; - undef; -} - -sub YYNberr { - my($self)=shift; - - ${$$self{NBERR}}; -} - -sub YYRecovering { - my($self)=shift; - - ${$$self{ERRST}} != 0; -} - -sub YYAbort { - my($self)=shift; - - ${$$self{CHECK}}='ABORT'; - undef; -} - -sub YYAccept { - my($self)=shift; - - ${$$self{CHECK}}='ACCEPT'; - undef; -} - -sub YYError { - my($self)=shift; - - ${$$self{CHECK}}='ERROR'; - undef; -} - -sub YYSemval { - my($self)=shift; - my($index)= $_[0] - ${$$self{DOTPOS}} - 1; - - $index < 0 - and -$index <= @{$$self{STACK}} - and return $$self{STACK}[$index][1]; - - undef; #Invalid index -} - -sub YYCurtok { - my($self)=shift; - - @_ - and ${$$self{TOKEN}}=$_[0]; - ${$$self{TOKEN}}; -} - -sub YYCurval { - my($self)=shift; - - @_ - and ${$$self{VALUE}}=$_[0]; - ${$$self{VALUE}}; -} - -sub YYExpect { - my($self)=shift; - - keys %{$self->{STATES}[$self->{STACK}[-1][0]]{ACTIONS}} -} - -sub YYLexer { - my($self)=shift; - - $$self{LEX}; -} - - -################# -# Private stuff # -################# - - -sub _CheckParams { - my($mandatory,$checklist,$inarray,$outhash)=@_; - my($prm,$value); - my($prmlst)={}; - - while(($prm,$value)=splice(@$inarray,0,2)) { - $prm=uc($prm); - exists($$checklist{$prm}) - or croak("Unknow parameter '$prm'"); - ref($value) eq $$checklist{$prm} - or croak("Invalid value for parameter '$prm'"); - $prm=unpack('@2A*',$prm); - $$outhash{$prm}=$value; - } - for (@$mandatory) { - exists($$outhash{$_}) - or croak("Missing mandatory parameter '".lc($_)."'"); - } -} - -sub _Error { - print "Parse error.\n"; -} - -sub _DBLoad { - { - no strict 'refs'; - - exists(${__PACKAGE__.'::'}{_DBParse})#Already loaded ? - and return; - } - my($fname)=__FILE__; - my(@drv); - open(DRV,"<$fname") or die "Report this as a BUG: Cannot open $fname"; - while(<DRV>) { - /^\s*sub\s+_Parse\s*{\s*$/ .. /^\s*}\s*#\s*_Parse\s*$/ - and do { - s/^#DBG>//; - push(@drv,$_); - } - } - close(DRV); - - $drv[0]=~s/_P/_DBP/; - eval join('',@drv); -} - -#Note that for loading debugging version of the driver, -#this file will be parsed from 'sub _Parse' up to '}#_Parse' inclusive. -#So, DO NOT remove comment at end of sub !!! -sub _Parse { - my($self)=shift; - - my($rules,$states,$lex,$error) - = @$self{ 'RULES', 'STATES', 'LEX', 'ERROR' }; - my($errstatus,$nberror,$token,$value,$stack,$check,$dotpos) - = @$self{ 'ERRST', 'NBERR', 'TOKEN', 'VALUE', 'STACK', 'CHECK', 'DOTPOS' }; - -#DBG> my($debug)=$$self{DEBUG}; -#DBG> my($dbgerror)=0; - -#DBG> my($ShowCurToken) = sub { -#DBG> my($tok)='>'; -#DBG> for (split('',$$token)) { -#DBG> $tok.= (ord($_) < 32 or ord($_) > 126) -#DBG> ? sprintf('<%02X>',ord($_)) -#DBG> : $_; -#DBG> } -#DBG> $tok.='<'; -#DBG> }; - - $$errstatus=0; - $$nberror=0; - ($$token,$$value)=(undef,undef); - @$stack=( [ 0, undef ] ); - $$check=''; - - while(1) { - my($actions,$act,$stateno); - - $stateno=$$stack[-1][0]; - $actions=$$states[$stateno]; - -#DBG> print STDERR ('-' x 40),"\n"; -#DBG> $debug & 0x2 -#DBG> and print STDERR "In state $stateno:\n"; -#DBG> $debug & 0x08 -#DBG> and print STDERR "Stack:[". -#DBG> join(',',map { $$_[0] } @$stack). -#DBG> "]\n"; - - - if (exists($$actions{ACTIONS})) { - - defined($$token) - or do { - ($$token,$$value)=&$lex($self); -#DBG> $debug & 0x01 -#DBG> and print STDERR "Need token. Got ".&$ShowCurToken."\n"; - }; - - $act= exists($$actions{ACTIONS}{$$token}) - ? $$actions{ACTIONS}{$$token} - : exists($$actions{DEFAULT}) - ? $$actions{DEFAULT} - : undef; - } - else { - $act=$$actions{DEFAULT}; -#DBG> $debug & 0x01 -#DBG> and print STDERR "Don't need token.\n"; - } - - defined($act) - and do { - - $act > 0 - and do { #shift - -#DBG> $debug & 0x04 -#DBG> and print STDERR "Shift and go to state $act.\n"; - - $$errstatus - and do { - --$$errstatus; - -#DBG> $debug & 0x10 -#DBG> and $dbgerror -#DBG> and $$errstatus == 0 -#DBG> and do { -#DBG> print STDERR "**End of Error recovery.\n"; -#DBG> $dbgerror=0; -#DBG> }; - }; - - - push(@$stack,[ $act, $$value ]); - - $$token ne '' #Don't eat the eof - and $$token=$$value=undef; - next; - }; - - #reduce - my($lhs,$len,$code,@sempar,$semval); - ($lhs,$len,$code)=@{$$rules[-$act]}; - -#DBG> $debug & 0x04 -#DBG> and $act -#DBG> and print STDERR "Reduce using rule ".-$act." ($lhs,$len): "; - - $act - or $self->YYAccept(); - - $$dotpos=$len; - - unpack('A1',$lhs) eq '@' #In line rule - and do { - $lhs =~ /^\@[0-9]+\-([0-9]+)$/ - or die "In line rule name '$lhs' ill formed: ". - "report it as a BUG.\n"; - $$dotpos = $1; - }; - - @sempar = $$dotpos - ? map { $$_[1] } @$stack[ -$$dotpos .. -1 ] - : (); - - $semval = $code ? &$code( $self, @sempar ) - : @sempar ? $sempar[0] : undef; - - splice(@$stack,-$len,$len); - - $$check eq 'ACCEPT' - and do { - -#DBG> $debug & 0x04 -#DBG> and print STDERR "Accept.\n"; - - return($semval); - }; - - $$check eq 'ABORT' - and do { - -#DBG> $debug & 0x04 -#DBG> and print STDERR "Abort.\n"; - - return(undef); - - }; - -#DBG> $debug & 0x04 -#DBG> and print STDERR "Back to state $$stack[-1][0], then "; - - $$check eq 'ERROR' - or do { -#DBG> $debug & 0x04 -#DBG> and print STDERR -#DBG> "go to state $$states[$$stack[-1][0]]{GOTOS}{$lhs}.\n"; - -#DBG> $debug & 0x10 -#DBG> and $dbgerror -#DBG> and $$errstatus == 0 -#DBG> and do { -#DBG> print STDERR "**End of Error recovery.\n"; -#DBG> $dbgerror=0; -#DBG> }; - - push(@$stack, - [ $$states[$$stack[-1][0]]{GOTOS}{$lhs}, $semval ]); - $$check=''; - next; - }; - -#DBG> $debug & 0x04 -#DBG> and print STDERR "Forced Error recovery.\n"; - - $$check=''; - - }; - - #Error - $$errstatus - or do { - - $$errstatus = 1; - &$error($self); - $$errstatus # if 0, then YYErrok has been called - or next; # so continue parsing - -#DBG> $debug & 0x10 -#DBG> and do { -#DBG> print STDERR "**Entering Error recovery.\n"; -#DBG> ++$dbgerror; -#DBG> }; - - ++$$nberror; - - }; - - $$errstatus == 3 #The next token is not valid: discard it - and do { - $$token eq '' # End of input: no hope - and do { -#DBG> $debug & 0x10 -#DBG> and print STDERR "**At eof: aborting.\n"; - return(undef); - }; - -#DBG> $debug & 0x10 -#DBG> and print STDERR "**Dicard invalid token ".&$ShowCurToken.".\n"; - - $$token=$$value=undef; - }; - - $$errstatus=3; - - while( @$stack - and ( not exists($$states[$$stack[-1][0]]{ACTIONS}) - or not exists($$states[$$stack[-1][0]]{ACTIONS}{error}) - or $$states[$$stack[-1][0]]{ACTIONS}{error} <= 0)) { - -#DBG> $debug & 0x10 -#DBG> and print STDERR "**Pop state $$stack[-1][0].\n"; - - pop(@$stack); - } - - @$stack - or do { - -#DBG> $debug & 0x10 -#DBG> and print STDERR "**No state left on stack: aborting.\n"; - - return(undef); - }; - - #shift the error token - -#DBG> $debug & 0x10 -#DBG> and print STDERR "**Shift \$error token and go to state ". -#DBG> $$states[$$stack[-1][0]]{ACTIONS}{error}. -#DBG> ".\n"; - - push(@$stack, [ $$states[$$stack[-1][0]]{ACTIONS}{error}, undef ]); - - } - - #never reached - croak("Error in driver logic. Please, report it as a BUG"); - -}#_Parse -#DO NOT remove comment - -1; - -} -#End of include-------------------------------------------------- - +use Parse::Yapp::Driver; diff --git a/source4/pidl/lib/Parse/Pidl/Util.pm b/source4/pidl/lib/Parse/Pidl/Util.pm index 064862b7c5..2f3547bbb5 100644 --- a/source4/pidl/lib/Parse/Pidl/Util.pm +++ b/source4/pidl/lib/Parse/Pidl/Util.pm @@ -12,6 +12,8 @@ $VERSION = '0.01'; use strict; +use Parse::Pidl::Expr; + ##################################################################### # a dumper wrapper to prevent dependence on the Data::Dumper module # unless we actually need it @@ -97,22 +99,19 @@ sub useUintEnums() sub ParseExpr($$) { - my($expr,$varlist) = @_; + my($expr, $varlist) = @_; die("Undefined value in ParseExpr") if not defined($expr); - my @tokens = split /((?:[A-Za-z_])(?:(?:(?:[A-Za-z0-9_.])|(?:->))+)?)/, $expr; - my $ret = ""; - - foreach my $t (@tokens) { - if (defined($varlist->{$t})) { - $ret .= $varlist->{$t}; - } else { - $ret .= $t; - } - } - - return $ret; + my $x = new Parse::Pidl::Expr(); + + return $x->Run($expr, sub { my $x = shift; die(MyDumper($x)); }, + # Lookup fn + sub { my $x = shift; + return($varlist->{$x}) if (defined($varlist->{$x})); + return $x; + }, + undef); } 1; diff --git a/source4/pidl/tests/util.pl b/source4/pidl/tests/util.pl index 4287d78980..4c002458ea 100755 --- a/source4/pidl/tests/util.pl +++ b/source4/pidl/tests/util.pl @@ -3,7 +3,7 @@ # Published under the GNU General Public License use strict; -use Test::More tests => 41; +use Test::More tests => 53; use FindBin qw($RealBin); use lib "$RealBin/../lib"; use Parse::Pidl::Util; @@ -48,30 +48,39 @@ ok(property_matches({PROPERTIES => {x => "data"}}, "x", "data")); ok(property_matches({PROPERTIES => {x => "data"}}, "x", "^([dat]+)\$")); # ParseExpr() -is("", ParseExpr("", {})); +#is("", ParseExpr("", {})); is("a", ParseExpr("a", {"b" => "2"})); is("2", ParseExpr("a", {"a" => "2"})); -is("2*2", ParseExpr("a*a", {"a" => "2"})); -is("r->length+r->length", +is("2 * 2", ParseExpr("a*a", {"a" => "2"})); +is("r->length + r->length", ParseExpr("length+length", {"length" => "r->length"})); -is("2/2*(r->length)", +is("2 / 2 * (r->length)", ParseExpr("constant/constant*(len)", {"constant" => "2", "len" => "r->length"})); -is("2+2-r->length", +is("2 + 2 - r->length", ParseExpr("constant+constant-len", {"constant" => "2", "len" => "r->length"})); is("*r->length", ParseExpr("*len", { "len" => "r->length"})); is("**r->length", ParseExpr("**len", { "len" => "r->length"})); -is("r->length&2", ParseExpr("len&2", { "len" => "r->length"})); +is("r->length & 2", ParseExpr("len&2", { "len" => "r->length"})); is("&r->length", ParseExpr("&len", { "len" => "r->length"})); +is("calc()", ParseExpr("calc()", { "foo" => "2"})); +is("calc(2 * 2)", ParseExpr("calc(foo * 2)", { "foo" => "2"})); is("strlen(\"data\")", ParseExpr("strlen(foo)", { "foo" => "\"data\""})); is("strlen(\"data\", 4)", ParseExpr("strlen(foo, 4)", { "foo" => "\"data\""})); is("foo / bar", ParseExpr("foo / bar", { "bla" => "\"data\""})); -is("r->length%2", ParseExpr("len%2", { "len" => "r->length"})); -is("r->length==2", ParseExpr("len==2", { "len" => "r->length"})); -is("r->length!=2", ParseExpr("len!=2", { "len" => "r->length"})); +is("r->length % 2", ParseExpr("len%2", { "len" => "r->length"})); +is("r->length == 2", ParseExpr("len==2", { "len" => "r->length"})); +is("r->length != 2", ParseExpr("len!=2", { "len" => "r->length"})); is("pr->length", ParseExpr("pr->length", { "p" => "r"})); -TODO: { - todo_skip 1, "Broken at the moment"; - is("r->length", ParseExpr("p->length", { "p" => "r"})); -} +is("r->length", ParseExpr("p->length", { "p" => "r"})); +is("_foo / bla32", ParseExpr("_foo / bla32", { "bla" => "\"data\""})); +is("foo.bar.blah", ParseExpr("foo.blah", { "foo" => "foo.bar"})); +is("\"bla\"", ParseExpr("\"bla\"", {})); +is("1 << 2", ParseExpr("1 << 2", {})); +is("1 >> 2", ParseExpr("1 >> 2", {})); +is("0x200", ParseExpr("0x200", {})); +is("2?3:0", ParseExpr("2?3:0", {})); +is("~0", ParseExpr("~0", {})); +is("b->a->a", ParseExpr("a->a->a", {"a" => "b"})); +is("b.a.a", ParseExpr("a.a.a", {"a" => "b"})); |