From: Mauricio Fernandez Date: 2006-02-12T18:09:30+09:00 Subject: Re: Parse error within Regexp 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. 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 -- Mauricio Fernandez