lrama
lrama copied to clipboard
Add lexer.rbs
Added lexer.rbs.
Discussions:
TypeandTokenhave type parameters namedSValue. I expect them to work well in parser.rb.- They are invariant. I could not find a good way to make them covariant while keeping
create_token(Token::Number, str, ...)untypable.
- They are invariant. I could not find a good way to make them covariant while keeping
- I added
patch.rbs, which patches the types ofStringScanner#[]andStringScanner#getch.- Another approach would be to define the non-nil versions of the methods, e.g.,
StringScanner#fetchandStringScanner#getch!, as soutaro remarked. - Yet another approach would be to replace every
ss[n]with(ss[n] || raise)and so on, but that would be hard to read.
- Another approach would be to define the non-nil versions of the methods, e.g.,
- On the other hand, I avoided patching
Array#[]andArray#firstbecauseArrayis used widely, and such patches may affect other files. Instead, I replacedarray[0...l]witharray.take(l)andarray.firstwitharray.fetch(0).
type variance: I'm not sure the point. Can I ask you to share the code which does not work well?patch.rbs: I agree to the current approach. There are 3 options for it as you write. However rewriting the method call like(ss[n] || raise)make codes un-readable. I don't have strong preference between definingStringScanner#fetchand patching rbs definitions. But patching rbs is bit better as a first step becauseStringScanneris not used widely in this project.Array: Both replacement seems reasonable for me.
Thanks for working on this. The following commit adds a type definition to the Lexer class, so this PR is closed.
- https://github.com/ruby/lrama/commit/e44b6d380b08c064befe642ba27fc76cfd223c65