Skip to content

scanner: continue a line that starts with ||, and or or (Ruby 4.0) - #303

Open
clarifydoc wants to merge 1 commit into
tree-sitter:masterfrom
clarifydoc:leading-logical-operators
Open

clarifydoc wants to merge 1 commit into
tree-sitter:masterfrom
clarifydoc:leading-logical-operators

Conversation

@clarifydoc

Copy link
Copy Markdown

Fixes #302.

Ruby 4.0 lets a line that starts with ||, &&, and or or continue the previous line (Feature #20925). A leading && already parsed, because the scanner never ends a line before & (that is what keeps &. chains together). A leading || was a parse error, and a leading and/or was read as a call to a method named and/or.

scan_whitespace now treats a newline followed by ||, or by and/or as a whole word, the way it treats a newline followed by .: no line break is emitted. It uses the same word test as prism: android, order, and?, or! and and: still end the line. A single | (block parameters on the next line) is unchanged.

Before 4.0 all of these lines were syntax errors, so no older program parses differently.

Only src/scanner.c changes; the grammar and parser.c are untouched.

Tests

  • New corpus cases in test/corpus/expressions.txt: the four operators, a comment line between the two lines, and identifiers that start with and/or. The first two fail on master and pass here; all 294 corpus tests pass.
  • Parsed with master and with this branch, comparing whole trees: ruby/spec (4,434 files, the CI examples), rails, discourse and chatwoot, 23,961 files in all. No tree changed, and the ruby/spec error count is the same (11 files).
  • Compiles with -Wall -Wextra -Wpedantic with no new warnings.

Ruby 4.0 lets a line starting with a logical operator continue the
previous line, the way a leading `.` or `&.` continues a method chain:

    if user.admin?
       || user.owner?
      ...

A leading `&&` already parses, because the scanner never ends a line
before `&` (that is what keeps `&.` chains together). A leading `||`
was a parse error, and a leading `and`/`or` was silently read as a
call to a method named `and`/`or`.

Before 4.0 all of these were syntax errors, so no older program
parses differently. `android`, `order`, `and?` and `or!` at the start
of a line still end it.
Magikdidi24 pushed a commit to Magikdidi24/tree-sitter-ruby that referenced this pull request Oct 25, 2026
…previous one (tree-sitter#302)

Ruby 4.0 (Feature #20925). From upstream PR tree-sitter#303, with the char test made locale-independent
like the rest of this scanner (no <wctype.h>). Trees unchanged on 955 stdlib/gem files.

Before: c / || d  -> (identifier) (binary left: (identifier) (MISSING identifier) ...)
        e / and f -> (identifier) (call method: (identifier) arguments: (argument_list (identifier)))
After:  c / || d  -> (binary left: (identifier) right: (identifier))
        e / and f -> (binary left: (identifier) right: (identifier))

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: a line starting with ||, and or or does not continue the previous line (Ruby 4.0)

1 participant