Project

General

Profile

Bug #10653

do-end block in ternary operator is syntax error

Added by yancya (Shinta Koyanagi) over 4 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
ruby -v:
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
[ruby-dev:48790]

Description

  • For example
ruby -v -e 'true ? 1.tap do |n| p n end : 0'
  • Result by 2.1.5
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]
1
  • Result by 2.2.0
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
-e:1: syntax error, unexpected keyword_do_cond, expecting ':'
true ? 1.tap do |n| p n end : 0
               ^
-e:1: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '('
true ? 1.tap do |n| p n end : 0
                       ^

Related issues

Related to Ruby trunk - Feature #4276: Allow use of quotes in symbol syntactic sugar for hashesClosed01/13/2011Actions
Related to Ruby trunk - Bug #11456: Hash リテラル中で rescue をもちいると SyntaxError になるClosedActions

Associated revisions

Revision 94b1db5d
Added by nobu (Nobuyoshi Nakada) almost 4 years ago

parse.y: fix block after conditional

  • parse.y: fix syntax error at do-block after a conditional operator. separate label-allowed and after-a-label states from others as bit flags. [ruby-dev:48790] [Bug #10653]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51617 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 51617
Added by nobu (Nobuyoshi Nakada) almost 4 years ago

parse.y: fix block after conditional

  • parse.y: fix syntax error at do-block after a conditional operator. separate label-allowed and after-a-label states from others as bit flags. [ruby-dev:48790] [Bug #10653]

Revision 51617
Added by nobu (Nobuyoshi Nakada) almost 4 years ago

parse.y: fix block after conditional

  • parse.y: fix syntax error at do-block after a conditional operator. separate label-allowed and after-a-label states from others as bit flags. [ruby-dev:48790] [Bug #10653]

Revision 51617
Added by nobu (Nobuyoshi Nakada) almost 4 years ago

parse.y: fix block after conditional

  • parse.y: fix syntax error at do-block after a conditional operator. separate label-allowed and after-a-label states from others as bit flags. [ruby-dev:48790] [Bug #10653]

Revision 51617
Added by nobu (Nobuyoshi Nakada) almost 4 years ago

parse.y: fix block after conditional

  • parse.y: fix syntax error at do-block after a conditional operator. separate label-allowed and after-a-label states from others as bit flags. [ruby-dev:48790] [Bug #10653]

Revision 51617
Added by nobu (Nobuyoshi Nakada) almost 4 years ago

parse.y: fix block after conditional

  • parse.y: fix syntax error at do-block after a conditional operator. separate label-allowed and after-a-label states from others as bit flags. [ruby-dev:48790] [Bug #10653]

Revision df51beda
Added by nagachika (Tomoyuki Chikanaga) over 3 years ago

merge revision(s) 50409,51616,51617,51624: [Backport #10653] [Backport #11456]

    parse.y: %-string cannot be a label

    * parse.y (parser_yylex): %-string cannot be a label even if

terminated by single/double quotes.
* parse.y: fix syntax error at do-block after a conditional
operator. separate label-allowed and after-a-label states from
others as bit flags. [ruby-dev:48790] [Bug #10653]

    * parse.y (IS_BEG): include labeled argument state, which was
      EXPR_LABELARG.  [ruby-dev:49221] [Bug #11456]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@52046 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 52046
Added by nagachika (Tomoyuki Chikanaga) over 3 years ago

merge revision(s) 50409,51616,51617,51624: [Backport #10653] [Backport #11456]

parse.y: %-string cannot be a label

* parse.y (parser_yylex): %-string cannot be a label even if

terminated by single/double quotes.
* parse.y: fix syntax error at do-block after a conditional
operator. separate label-allowed and after-a-label states from
others as bit flags. [ruby-dev:48790] [Bug #10653]

* parse.y (IS_BEG): include labeled argument state, which was
  EXPR_LABELARG.  [ruby-dev:49221] [Bug #11456]

History

Updated by nobu (Nobuyoshi Nakada) over 4 years ago

  • Related to Feature #4276: Allow use of quotes in symbol syntactic sugar for hashes added
#2

Updated by nobu (Nobuyoshi Nakada) almost 4 years ago

  • Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN to 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: REQUIRED
#3

Updated by nobu (Nobuyoshi Nakada) almost 4 years ago

  • Status changed from Open to Closed

Applied in changeset r51617.


parse.y: fix block after conditional

  • parse.y: fix syntax error at do-block after a conditional operator. separate label-allowed and after-a-label states from others as bit flags. [ruby-dev:48790] [Bug #10653]
#4

Updated by nobu (Nobuyoshi Nakada) almost 4 years ago

  • Related to Bug #11456: Hash リテラル中で rescue をもちいると SyntaxError になる added
#5

Updated by nagachika (Tomoyuki Chikanaga) over 3 years ago

After backporting r50409 and r51617, TestSyntax#test_block_after_cond failed.

#6

Updated by nagachika (Tomoyuki Chikanaga) over 3 years ago

r51616 should be backported too?

#7

Updated by nagachika (Tomoyuki Chikanaga) over 3 years ago

  • Backport changed from 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: REQUIRED to 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: DONE

r50409, r51616, r51617 and r51624 were backported into ruby_2_2 branch at r52046.
With r50409 a minor incompatibility is introduced ({ %w"key": 0 } cause SyntaxError).
I'm not confident the incompatibility is essential part of this fixes, but the accurate surgery is required to eliminate the incompatibility. I gave up.

Updated by whitequark (whitequark *) over 3 years ago

I was implementing this in my Ruby parser and I found a much simpler solution than what you've implemented here: https://github.com/whitequark/parser/commit/113442eff1f9c6f7e7205ec0dac8cb858b3b0fc7

In short, instead of explicitly enumerating all contexts where a label is valid, I only disallow quoted label at EXPR_VALUE using an additional flag. It works just as well. It also would not exhibit the bug with %w during backport.

Might I suggest a simplification to parse.y?

Updated by nobu (Nobuyoshi Nakada) over 3 years ago

Currently it's a bit flag in parse.y too.

Also available in: Atom PDF