-
-
Notifications
You must be signed in to change notification settings - Fork 4
Conversation
Hey @palkan thanks for checking it out! I'll review this today and get back to you |
Oh gosh I completely lost track of this PR. I'm sorry! I'll review today! |
Hey @palkan - So I've pushed fixes for the rational thing (this turned out to be a bug in the prism Ruby API) and the The other thing in this PR appears to be related to hash key/value pairs. Could you point me to a snippet that does that? I'm not sure I understand it. |
Hey! Thanks for the updates. I checked it (see the latest build, I now run CI against your
case {a: 1}
in {"a" => 1}
end Currently fails with: /Users/palkan/dev/parser-prism/lib/parser/prism/compiler.rb:88:in `visit_assoc_node': undefined method `parts' for @ StringNode (location: (2,16)-(2,19)) (NoMethodError)
├── flags: ∅
├── opening_loc: (2,16)-(2,17) = "\""
├── content_loc: (2,17)-(2,18) = "a"
├── closing_loc: (2,18)-(2,19) = "\""
└── unescaped: "a"
:Prism::StringNode
visit_all(node.key.parts)
^^^^^^
from /opt/homebrew/lib/ruby/gems/3.2.0/gems/prism-0.19.0/lib/prism/node.rb:864:in `accept'
from /opt/homebrew/lib/ruby/gems/3.2.0/gems/prism-0.19.0/lib/prism/compiler.rb:34:in `block in visit_all'
case [0, 1, 2, 3]
in [0, 1,] # <---- trailing comma is the cause
true
end with /Users/palkan/dev/parser-prism/lib/parser/prism/compiler.rb:763:in `visit_implicit_rest_node': Cannot compile implicit rest nodes (Prism::ParserCompiler::CompilationError)
from /opt/homebrew/lib/ruby/gems/3.2.0/gems/prism-0.19.0/lib/prism/node.rb:8058:in `accept'
from /opt/homebrew/lib/ruby/gems/3.2.0/gems/prism-0.19.0/lib/prism/compiler.rb:34:in `block in visit_all' |
Great, thanks for the response.
|
Yeah; I guess, that's what this change was for: https://github.com/kddnewton/parser-prism/pull/7/files#diff-4e7038ac7082a00aeeabcd6f11ffb2494661b7274c9d6bc3f3a257e91a821daaR90 |
Hey @palkan — over the weekend I merged this work into prism itself. You can see the doc here: https://github.com/ruby/prism/blob/main/docs/parser_translation.md. I've fixed all 3 of these issues over there. Could you give it a shot over there? |
Hey @kddnewton! Cool! I checked locally and see the following issue with find patterns: # parsing the following code:
#
# case x; in [*, 1, *]; true; else; false; end
/Users/palkan/dev/prism/lib/prism/translation/parser/compiler.rb:616:in `visit_find_pattern_node': undefined method `rest' for @ FindPatternNode (location: (1,11)-(1,20)) (NoMethodError)
├── constant: ∅
├── left:
│ @ SplatNode (location: (1,12)-(1,13))
│ ├── operator_loc: (1,12)-(1,13) = "*"
│ └── expression: ∅
├── requireds: (length: 1)
│ └── @ IntegerNode (location: (1,15)-(1,16))
│ └── flags: decimal
├── right:
│ @ SplatNode (location: (1,18)-(1,19))
│ ├── operator_loc: (1,18)-(1,19) = "*"
│ └── expression: ∅
├── opening_loc: (1,11)-(1,12) = "["
└── closing_loc: (1,19)-(1,20) = "]"
:Prism::FindPatternNode
elements << node.rest if !node.rest.nil? && !node.rest.is_a?(ImplicitRestNode)
^^^^^
from /Users/palkan/dev/prism/lib/prism/node.rb:6268:in `accept'
from /Users/palkan/dev/prism/lib/prism/translation/parser/compiler.rb:821:in `block in visit_in_node'
from /Users/palkan/dev/prism/lib/prism/translation/parser/compiler.rb:1806:in `within_pattern'
from /Users/palkan/dev/prism/lib/prism/translation/parser/compiler.rb:821:in `visit_in_node'
from /Users/palkan/dev/prism/lib/prism/node.rb:8311:in `accept' With Parser, I get this AST:
Also, I found that parsing PRISM=true bin/parse '-> { it }.call("a")'
(send
(block
(lambda)
(args)
(lvar :"0it")) :call
(str "a"))
bin/parse '-> { it }.call("a")'
(send
(block
(lambda)
(args)
(send nil :it)) :call
(str "a")) That's fine, I can adjust my code. However, there is another problem with using Prism: it disallows me to use /Users/palkan/dev/prism/lib/prism/translation/parser.rb:105:in `unwrap': _1 is reserved for numbered parameters (SyntaxError) I need this to support numparams in Ruby <2.7 (where using Is there a similar workaround for Prism? |
Hey @palkan — Ahh that's unfortunate. I've fixed the find pattern bug. It turns out the fixture we have for pattern matching has something the parser gem doesn't support ( For the For the numbered parameter write, it's a little more difficult. We don't really have a way to turn that off. I've added a hook at the Ruby layer in ruby/prism#2335 where you could inherit from the parser class and then define a custom |
Thanks! I think, the $ RUBY_NEXT_PROPOSED=0 PRISM=true be bin/parse '-> { _1 }.call("a")'
bundler: failed to load command: bin/parse (bin/parse)
/Users/palkan/dev/prism/lib/prism/translation/parser/compiler.rb:1011:in `visit_lambda_node': undefined method `parameters' for @ NumberedParametersNode (location: (1,0)-(1,9)) (NoMethodError)
└── maximum: 1
:Prism::NumberedParametersNode
node.body&.accept(copy_compiler(forwarding: find_forwarding(node.parameters&.parameters))),
^^^^^^^^^^^^
from /Users/palkan/dev/prism/lib/prism/node.rb:10836:in `accept'
from /Users/palkan/dev/prism/lib/prism/compiler.rb:29:in `visit'
from /Users/palkan/dev/prism/lib/prism/translation/parser/compiler.rb:283:in `visit_call_node'
from /Users/palkan/dev/prism/lib/prism/node.rb:2308:in `accept'
from /Users/palkan/dev/prism/lib/prism/compiler.rb:34:in `block in visit_all'
from /Users/palkan/dev/prism/lib/prism/compiler.rb:34:in `map'
from /Users/palkan/dev/prism/lib/prism/compiler.rb:34:in `visit_all' |
@palkan oh man — thank you for your patience. I just merged a fix for this, could you try |
Thanks! This issue is fixed now and the Found another difference in parsing pattern matching: $ be bin/parse '
case _
in "a":
end'
(case-match
(send nil :_)
(in-pattern
(hash-pattern
(match-var :a)) nil nil) nil)
$ PRISM=true be bin/parse '
case _
in "a":
end'
(case-match
(send nil :_)
(in-pattern
(hash-pattern
(pair
(sym :"\"a\"")
(const nil :"\"a\""))) nil nil) nil) Parser uses a special node for hash pattern pairs ( Is it intentional or a bug? It fails for me because I use |
That was a bug — should be fixed now. Can you try |
@palkan I just released v0.21.0 that you should be able to use. I'll close this ticket for now, because I'm going to deprecate this gem. |
Hey @kddnewton,
Thanks for your on Prism and this translator!
I've started experimenting with it in Ruby Next and encountered several minor issues (fixed in this PR, also see comments).
There are still some problems left in my build (see https://github.com/ruby-next/ruby-next/actions/workflows/ruby-prism-test.yml):
**nil
) is missing location information:Here
args.last.loc.expression
isnil
: https://github.com/ruby-next/parser/blob/0af323526e73b3e8319e856863666e70f57838e0/lib/parser/builders/default.rb#L2117