(TRICK), they belong to Ruby language Ruby https://github.com/tric/trick2022/blob/master/01-tompng/entry.rb https://github.com/tric/trick2022/blob/master/06-mame/entry.rb
a given string of text conforms to a de fi ned grammar • In programming language, its major roles are: • Checking for syntax errors • Building the abstract syntax tree. What is Parser? Grammar + 1 2 3 * AST Input Check Build
parser from a grammar de fi nition • Lrama is the tool that ful fi lls this role What is Parser Generator? Grammar fi le Generate Parser Generator Parser
• For example, since a “primary” is also an “arg”, a “primary” can be written wherever an “arg” can Inclusive relation of rules primary arg expr stmt primary primary
it's technically “arg_value”, I'll be using “arg” in this explanation to keep things simple • All four of these arguments are grammatically considered “arg” Actual arguments arg arg arg arg
of formal parameter • All four of these formal parameters' default values are grammatically considered “arg” Default value of formal parameter arg arg arg arg
arguments and the default values for formal parameters use “arg_value” • This means that the same expressions can be used for both. Both use “arg_value”
is a “primary”, not an “arg” • Because of this, you can't write an expression containing a binary operator, like in the code below Default value of block arguments
the next token is the “|” (pipe) • At this point, the parser has two options for the “|” • as a binary operator • as the delimiter for a block's arguments • This state, where there are multiple options, is called con fl ict What is con fl ict?
except for the “|” • However, Ruby does not make that choice. Instead, it adopts a grammar that uniformly disallows binary operators in that Design choice
arguments and the default values of formal parameters in Ruby • Unlike those, the default values for block arguments cannot contain binary expressions • This can be enabled by adding constraints, but that hasn't been done • It seems to prioritize consistency over allowing certain binary expressions, avoiding an exception for one speci fi c operator Summary
of where the arguments for method `m` and the command (`cmd`) end • Two arguments for `m` • Three arguments for `m` • Four arguments for `m` How many args are passed?
written as an actual argument when there is only one argument • This is because if there's only one argument, the parser doesn't need to determine where the arguments end • I personally think this is a pretty interesting design choice Exception Only one argument Multiple arguments
block • I think this design choice likely stems from an intent to keep the content you can write in an argument consistent, regardless of whether parentheses are used or not How blocks are handled
can be omitted • Sometimes it improves the writing feel • It has the side effect of making the end of a grammatical construct unclear • This may be increasing the complexity of the grammar • There are even interesting design choice, such as allowing “command” to be written as argument if it is a single argument Summary
parentheses, it belongs to “arg” and can therefore be used as an argument • If it's a command, it belongs to “stmt” and cannot be used as an argument “arg” v.s. “stmt”
command: it becomes ambiguous where the argument list ends • One argument for `m` • Two arguments for `m` • Three arguments for `m How many args are passed? (again)
discussion that a certain kind of endless method de fi nition becomes a “stmt” • https://bugs.ruby-lang.org/ issues/17398 • It includes a note of caution that endless method de fi nitions cannot be passed as arguments to a private method in some cases Feature #17398
single argument • So, by the same logic, wouldn't it be possible to allow an endless method de fi nition whose body is a command only when it's the sole argument? Modify “call_args”
it, no con fl ict occurred • The resulting Ruby accepts the script that had previously caused a syntax error • This solves the problem noted in Feature #17398 No con fl ict on new grammar
of the method is clearly marked by the `end` keyword • Since endless method de fi nitions don't have an `end` keyword, they face the same constraint as command calls, they cannot be used as arguments • However it's possible to allow it to be written as an argument by limiting its use case Summary
a "recent" syntax in Ruby's long history, so there have been multiple tickets fi led about it • Pattern matching was introduced as an experimental feature in Ruby 2.7 (2019-12-25) • Pattern matching with `case/in` was promoted to a stable feature in Ruby 3.0 (2020-12-25) • One-line pattern matching was promoted to a stable feature in Ruby 3.1 (2021-12-25) Pattern matching
https://bugs.ruby-lang.org/issues/17925 • [Bug #18080] Syntax error on one-line pattern matching • https://bugs.ruby-lang.org/issues/18080 • [Bug #21378] variable pinning does not look for method arguments • https://bugs.ruby-lang.org/issues/21378 • [Bug #21097] `x = a rescue b in c` and `def f = a rescue b in c` parsed differently between parse.y and prism • https://bugs.ruby-lang.org/issues/21097 Tickets related to pattern matching
https://bugs.ruby-lang.org/issues/17925 • [Bug #18080] Syntax error on one-line pattern matching • https://bugs.ruby-lang.org/issues/18080 • I've written a pretty detailed explanation, so feel free to check out the ticket if you'd like • Today, we're going to look at the third ticket, [Bug #21378] Tickets related to pattern matching
script where an endless method de fi nition and pattern matching are combined, like this • The reporter expected that the codes from `y` to the end of the braces would be interpreted as a pattern matching • However the parser interprets the part from `def` to `y` as the left-hand side of the pattern matching [Bug #21378]
be “arg” or “command” • Pattern matching is “expr” • “expr” is not “arg” Organize the issue Pattern matching is “expr” Body should be “arg” or “command”
Endless method de fi nition with arg is “arg” • Then this script is interpreted as “endless method de fi nition IN pattern” Organize the issue Left side of patten matching should be “arg” This is “arg”
that doesn’t specialized for pattern matching • Change pattern matching to be “arg” • Then pattern matching can be body of endless method de fi nitions General approach “arg” can be a body of endless method de fi nition If pattern matching were an “arg”
can be broadly categorized into six groups • Con fl icts on “=>” • Con fl icts on “,” • Con fl icts on “|” • Con fl icts on “^” • (Con fl icts on “..” and “…”) • (Con fl icts on “**”) • I will take a closer look at the fi rst four
the argument syntax • Therefore, if pattern matching is made an “arg”, this fat arrow causes con fl icts Con fl icts on “=>” key value Pattern matching ???
multiple patterns • Since this token is also used for binary operators, which are included in “arg”, a con fl ict arises Con fl icts on “|” Pattern 1 Pattern 2 Pattern matching arg ???
fl ict might be a bit surprising • In pattern matching, “^” is used for variable pinning, which is a unary operator • At fi rst glance, it might seem that it wouldn't con fl ict with the “^” used for binary operators Con fl icts on “^”
variable pinning or a combination of a trailing comma and a binary operator The key is the trailing comma array pattern w/ variable pinning pattern matching w/ trailing comma arg Binary Operator ???
Enforcing brackets or braces when it’s used as arguments • Disallow trailing comma • Treat ‘|’ as binary operator then multiple patterns can’t be speci fi ed or the other way around • Allow pattern matching to be written in speci fi c locations like a command Possible solutions
of including pattern matching into “arg”, allowing pattern matching on the body of endless method de fi nition Another approach Pattern matching is “expr” Body should be “arg” or “command” Pattern matching is “expr” Body should be “arg” or “command” or “pattern matching”
symbols • It’s dif fi cult to manage grammar which allows some tokens to be omitted • However, it's possible to make them grammatically acceptable by limiting where it can be written Summary
discussion in tickets and insights I've gained from looking at grammar fi les • By investigating the resulting con fl icts, understand some of the characteristics of Ruby's grammar Summary of this session
write the same content for both actual arguments and the default values of formal parameters in Ruby • Avoid a grammar which allows limited binary operators can be written • On the other hand, there are also areas where consistency is not kept • call_args allows “command” to be written only when it is a single argument Learnings through grammar modi fi cations
“command” is method call w/o parentheses • Endless method de fi nition has no “end” token • In pattern matching, brackets and braces of patterns can be omitted • Symbols are very limited resources • “|” is used as as binary operator and separator of block arguments/patterns • “,” is used as separator in many grammar rules • When these speci fi cations are combined, it sometimes causes con fl icts Learnings through grammar modi fi cations