develooper Front page | perl.perl5.porters | Postings from September 2000

... statement for code examples

Thread Next
From:
Nicholas Clark
Date:
September 30, 2000 08:00
Subject:
... statement for code examples
Message ID:
20000930160051.E47854@plum.flirble.org
Someone at YAPC::Europe mentioned something about a ... statement that would
allow example code such as

if ($foo) {
  # Do something
  ...
}

be syntactically legal, but a run time error. This allows you to put your
sample code into perl to syntax check it.

Here's a prototype patch for 5.6 that implements this. It makes ... in the
context of statement a synonym for "die 'Unfinished code marked by ...';"

So you can:

$ ./perl -c
if ($foo) {
  # Do something
  ...
}
- syntax OK

You don't need a trailing semicolon:

$ ./perl -c
if ($foo) {
  # Do something
  ...
  warn "here";
}
- syntax OK

You get a misc lexical warning for each if you don't have -c

$ ./perl
if ($foo) {
  # Do something
  ...
}
Unfinished code marked by ... will die at runtime at - line 3.

and you die at runtime if you ever reach the ...

$ ./perl -w      
if (0) {
  warn "not here";
  ...
  warn "not there";
} else {
  warn "in here";
  ...
  warn "in there";
}
in here at - line 6.
Unfinished code marked by ... at - line 7.

[I removed the two Unfinished code marked by ... will die at runtime warnings.]


make test passes 100%, so it doesn't affect the ... flip-flop.

Things not done:
* there's no regression test for this yet
* there's no documentation

Things that could be better:
* I'm not sure if the message wording is that good
* I don't seem to be able to turn the warning off with 'no warnings'
* Should use strict; catch these?
* die "message"; means that
  {
     ...
     return;
  }
  is legal, but
  {
     ... if ($complex_stuff}
  }
  isn't. I can't see a way to make both possible. (Is there some cunning trick
  with toke.c state that could achieve this, or does it need perly.y tweaked,
  which is probably a bad thing?)


Still, it proves that this pig is actually airworthy. :-)
Comments welcome.

Nicholas Clark

--- patchlevel.h.orig	Wed Mar 22 20:23:11 2000
+++ patchlevel.h	Sat Sep 30 15:18:33 2000
@@ -70,6 +70,7 @@
 #if !defined(PERL_PATCHLEVEL_H_IMPLICIT) && !defined(LOCAL_PATCH_COUNT)
 static	char	*local_patches[] = {
 	NULL
+	,"...001 - statement ... for code examples" /* nick@babyhippo.com */
 	,NULL
 };
 
--- toke.c.orig	Tue Mar 21 05:30:21 2000
+++ toke.c	Sat Sep 30 15:14:50 2000
@@ -3449,6 +3449,19 @@
 		s++;
 		if (*s == tmp) {
 		    s++;
+		    if (PL_expect == XSTATE) {
+			/* statement ... is synonym for die "..."; */
+			SV *sv = newSVpv("Unfinished code marked by ...", 0);
+			OP *message = newSVOP(OP_CONST, 0, sv);
+			if (!PL_minus_c) {
+			    /* Warn them if they didn't use -c to check only */
+			    Perl_warner(aTHX_ WARN_MISC, "Unfinished code marked by ... will die at runtime");
+			}
+			force_next(';');
+			PL_nextval[PL_nexttoke].opval = message;
+			force_next(THING);
+			LOP(OP_DIE,XTERM);
+		    }
 		    yylval.ival = OPf_SPECIAL;
 		}
 		else

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About