Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax error when string union with escape #996

Closed
ksss opened this issue May 14, 2022 · 1 comment · Fixed by #1043
Closed

Syntax error when string union with escape #996

ksss opened this issue May 14, 2022 · 1 comment · Fixed by #1043
Milestone

Comments

@ksss
Copy link
Collaborator

ksss commented May 14, 2022

"\\" and 1 can create a Union type.
"a" and "\\" also can create a Union type,
whereas "\\" and "a" would result in a Syntax error.

Shouldn't it be possible to create a Union type in "\\" | "a" cases?

$ ruby -r rbs -e 'p RBS::Parser.parse_type(%q{"\\" | 1})'
#<RBS::Types::Union:0x00000001047b2118 @types=[#<RBS::Types::Literal:0x00000001047b22d0 @literal="\\", @location=#<RBS::Location:820 buffer=a.rbs, start=1:0, pos=0...3, children= source='"\"'>>, #<RBS::Types::Literal:0x00000001047b21b8 @literal=1, @location=#<RBS::Location:840 buffer=a.rbs, start=1:6, pos=6...7, children= source='1'>>], @location=#<RBS::Location:860 buffer=a.rbs, start=1:0, pos=0...7, children= source='"\" | 1'>>
$ ruby -r rbs -e 'p RBS::Parser.parse_type(%q{"a" | "\\"})'
#<RBS::Types::Union:0x0000000147162590 @types=[#<RBS::Types::Literal:0x0000000147162b58 @literal="a", @location=#<RBS::Location:820 buffer=a.rbs, start=1:0, pos=0...3, children= source='"a"'>>, #<RBS::Types::Literal:0x0000000147162720 @literal="\\", @location=#<RBS::Location:840 buffer=a.rbs, start=1:6, pos=6...9, children= source='"\"'>>], @location=#<RBS::Location:860 buffer=a.rbs, start=1:0, pos=0...9, children= source='"a" | "\"'>>
$ ruby -r rbs -e 'p RBS::Parser.parse_type(%q{"\\" | "a"})'
/Users/ksss/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/rbs-2.4.0/lib/rbs/parser_aux.rb:4:in `_parse_type': a.rbs:1:7...1:8: Syntax error: expected a token `pEOF`, token=`a` (tLIDENT) (RBS::ParsingError)
	from /Users/ksss/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/rbs-2.4.0/lib/rbs/parser_aux.rb:4:in `parse_type'
	from -e:1:in `<main>'
$ ruby -v
ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [arm64-darwin21]
$ rbs -v
rbs 2.4.0
@soutaro
Copy link
Member

soutaro commented Jun 22, 2022

%q{"\\" | "a"} is "\" | "a" in RBS. So, the syntax error is expected one.

The problem is when we give "\\" | "a" in RBS. This should be parse. But, it didn't because of the dqstring pattern in lexer.re

      dqstring = ["] ("\\"["] | [^"\x00])* ["];                     // Before
      dqstring = ["] ("\\"[abefnrstv"\\] | [^"\\\x00])* ["];        // After

With the Before pattern, the first \ in "\\" was eaten by the second alternation. And the next \" in "\\" was eaton by the first alternation. It cannot detect the end of string, and parsing fails.

The After pattern is more explicit on what can come just after \. The two \\ are eaten by the first alternation, and the closing " is processed correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants