From: dblack@... Date: 2006-02-12T21:47:24+09:00 Subject: Re: Parse error within Regexp Hi -- On Sun, 12 Feb 2006, Mauricio Fernandez wrote: > On Sun, Feb 12, 2006 at 11:42:42AM +0900, dblack@wobblini.net wrote: >>> Would something like this make sense? >>> >>> $ ./ruby19 -e '"".scan / ,/' >>> -e:1: syntax error, unexpected ',' >>> "".scan / ,/ >>> ^ >>> -e:1: unterminated regexp meets end of file >>> ======== >> >> I'm not sure the "unterminated string" makes sense as the error >> message, but I don't think it's an unterminated regex either. It >> seems that Ruby sees the first / as division, so it doesn't consider >> itself to be in mid-regex. If it thought it was in mid-regex, it >> would presumably keep going and it would find that the regex *is* >> terminated. > > I think the second '/' marks the start of the regexp, as seen by > the parser in that case (erroneously of course, but read below): > > "".scan / ,/ <---------------------------- > ^ \ > this is the unexpected , and this the unterminated regexp > > Compare that to > > $ ruby -e '"".scan / , " ' > -e:1: syntax error, unexpected ',' > "".scan / , " > ^ > -e:1: unterminated string meets end of file > -e:1: warning: useless use of a literal in void context > > The parser is doing its best to keep parsing so it can report further > errors. Perhaps, but I don't think "unterminated regexp" describes the situation very well. Of course the real situation is: I'm getting a barrage of strange error messages so something must be *really* wrong :-) > Besides, consider this case: > > $ ruby19 -e 'puts ARGF.read.scan /[-+]?(\d+(?:\.\d+)?)(?=[^\d])' > -e:1: warning: ambiguous first argument; put parentheses or even spaces > -e:1: unterminated string meets end of file > -e:1: syntax error, unexpected tSTRING_END, expecting tSTRING_CONTENT or tREGEXP_END or tSTRING_DBEG or tSTRING_DVAR > > I think the following makes more sense and is less noisy too: > > ~/src/ruby/ruby.head$ ./ruby19 -e 'puts ARGF.read.scan /[-+]?(\d+(?:\.\d+)?)(?=[^\d])' > -e:1: warning: ambiguous first argument; put parentheses or even spaces > -e:1: unterminated regexp meets end of file > -e:1: warning: parenthesize argument(s) for future version That's a different case, though: that's truly an unterminated regexp. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black