Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ errors:
message: "unexpected an `undef` at a non-statement position"
- name: STRING_CONCATENATION
message: "expected a string for concatenation"
- name: STRING_CONCATENATION_ENCODING
message: "string literal encodings differ ({slice} / {slice})"
- name: STRING_INTERPOLATED_TERM
message: "unterminated string; expected a closing delimiter for the interpolated string"
- name: STRING_LITERAL_EOF
Expand Down Expand Up @@ -1330,6 +1332,8 @@ flags:
values:
- name: CONTAINS_PRE_EXECUTION
comment: "the statement list directly contains a `BEGIN` block"
- name: LEADING_SEMICOLON
comment: "a semicolon precedes the first statement in the list"
comment: Flags for statement lists.
- name: StringFlags
values:
Expand Down
9 changes: 9 additions & 0 deletions include/prism/internal/encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ pm_encoding_string(const pm_encoding_t *encoding) {
return name;
}

/* Join the encodings of two strings' non-ASCII bytes. NULL means all ASCII. */
static PRISM_INLINE bool
pm_encoding_concat(const pm_encoding_t **left, const pm_encoding_t *right) {
if (right == NULL) return true;
if (*left != NULL && *left != right) return false;
*left = right;
return true;
}

/*
* Return true if the encoding is a multibyte encoding. Every kind other than
* single-byte and ASCII has characters that span more than one byte.
Expand Down
19 changes: 19 additions & 0 deletions include/prism/internal/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "prism/arena.h"
#include "prism/internal/assert.h"
#include "prism/internal/encoding.h"

#include <stddef.h>
#include <stdint.h>
Expand All @@ -31,6 +32,24 @@ pm_location_join(pm_location_t left, pm_location_t right) {
return location;
}

/*
* Return the sole expression in a statements node, if there is one.
* A leading semicolon introduces an empty statement before the expression.
*/
static PRISM_INLINE pm_node_t *
pm_statements_node_single_expression(const pm_statements_node_t *node) {
if (node == NULL || node->body.size != 1 || PM_NODE_FLAG_P(node, PM_STATEMENTS_FLAGS_LEADING_SEMICOLON)) return NULL;
return node->body.nodes[0];
}

/* A string literal uses the source encoding unless an escape forced one. */
static PRISM_INLINE const pm_encoding_t *
pm_string_node_encoding(const pm_string_node_t *node, const pm_encoding_t *source_encoding) {
if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FORCED_UTF8_ENCODING)) return PM_ENCODING_UTF_8_ENTRY;
if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FORCED_BINARY_ENCODING)) return PM_ENCODING_ASCII_8BIT_ENTRY;
return source_encoding;
}

/*
* Marks an interpolated string whose value is known while parsing. That is
* true when every part is a string literal or another string with this flag,
Expand Down
96 changes: 26 additions & 70 deletions lib/prism/translation/ripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ def visit_begin_node(node)
on_kw("begin")
end

clauses = visit_begin_node_clauses(node.begin_keyword_loc, node, false)
clauses = visit_begin_node_clauses(node)

if node.end_keyword_loc
bounds(node.end_keyword_loc)
Expand All @@ -1041,35 +1041,18 @@ def visit_begin_node(node)
end

# Visit the clauses of a begin node to form an on_bodystmt call.
private def visit_begin_node_clauses(location, node, allow_newline)
statements =
if node.statements.nil?
on_stmts_add(on_stmts_new, on_void_stmt)
else
body = node.statements.body
body = [nil, *body] if void_stmt?(location, node.statements.body[0].location, allow_newline)

bounds(node.statements.location)
visit_statements_node_body(body)
end
private def visit_begin_node_clauses(node, class_name_loc = nil)
bounds(node.statements.location) if node.statements
statements = visit_statements_node_body(node.statements, class_name_loc)

rescue_clause = visit(node.rescue_clause)
else_clause =
unless (else_clause_node = node.else_clause).nil?
bounds(else_clause_node.else_keyword_loc)
on_kw("else")

else_statements =
if else_clause_node.statements.nil?
[nil]
else
body = else_clause_node.statements.body
body = [nil, *body] if void_stmt?(else_clause_node.else_keyword_loc, else_clause_node.statements.body[0].location, allow_newline)
body
end

bounds(else_clause_node.location)
visit_statements_node_body(else_statements)
visit_statements_node_body(else_clause_node.statements)
end
ensure_clause = visit(node.ensure_clause)

Expand All @@ -1080,19 +1063,19 @@ def visit_begin_node(node)
# Visit the body of a structure that can have either a set of statements
# or statements wrapped in rescue/else/ensure.
private def visit_body_node(location, node, allow_newline = false)
class_name_loc = location if allow_newline

case node
when nil
bounds(location)
on_bodystmt(visit_statements_node_body([nil]), nil, nil, nil)
on_bodystmt(visit_statements_node_body(nil), nil, nil, nil)
when StatementsNode
body = [*node.body]
body = [nil, *body] if void_stmt?(location, body[0].location, allow_newline)
stmts = visit_statements_node_body(body)
stmts = visit_statements_node_body(node, class_name_loc)

bounds(node.body.first.location)
on_bodystmt(stmts, nil, nil, nil)
when BeginNode
visit_begin_node_clauses(location, node, allow_newline)
visit_begin_node_clauses(node, class_name_loc)
else
raise
end
Expand Down Expand Up @@ -1134,9 +1117,7 @@ def visit_block_node(node)
bounds(node.location)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
when StatementsNode
stmts = node.body.body
stmts = [nil, *stmts] if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location, false)
stmts = visit_statements_node_body(stmts)
stmts = visit_statements_node_body(node.body)

bounds(node.body.location)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
Expand Down Expand Up @@ -2048,16 +2029,7 @@ def visit_else_node(node)
bounds(node.else_keyword_loc)
on_kw("else")

statements =
if node.statements.nil?
[nil]
else
body = node.statements.body
body = [nil, *body] if void_stmt?(node.else_keyword_loc, node.statements.body[0].location, false)
body
end

else_statements = visit_statements_node_body(statements)
else_statements = visit_statements_node_body(node.statements)

bounds(node.end_keyword_loc)
on_kw("end")
Expand Down Expand Up @@ -2103,16 +2075,7 @@ def visit_ensure_node(node)
bounds(node.ensure_keyword_loc)
on_kw("ensure")

statements =
if node.statements.nil?
[nil]
else
body = node.statements.body
body = [nil, *body] if void_stmt?(node.ensure_keyword_loc, body[0].location, false)
body
end

statements = visit_statements_node_body(statements)
statements = visit_statements_node_body(node.statements)

bounds(node.location)
on_ensure(statements)
Expand Down Expand Up @@ -2887,9 +2850,7 @@ def visit_lambda_node(node)
bounds(node.location)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
when StatementsNode
stmts = node.body.body
stmts = [nil, *stmts] if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location, false)
stmts = visit_statements_node_body(stmts)
stmts = visit_statements_node_body(node.body)

bounds(node.body.location)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
Expand Down Expand Up @@ -3381,9 +3342,7 @@ def visit_pre_execution_node(node)

# The top-level program node.
def visit_program_node(node)
body = node.statements.body
body = [nil] if body.empty?
statements = visit_statements_node_body(body)
statements = visit_statements_node_body(node.statements)

bounds(node.location)
on_program(statements)
Expand Down Expand Up @@ -3647,17 +3606,20 @@ def visit_splat_node(node)
# A list of statements.
def visit_statements_node(node)
bounds(node.location)
visit_statements_node_body(node.body)
visit_statements_node_body(node)
end

# Visit the list of statements of a statements node. We support nil
# statements in the list. This would normally not be allowed by the
# structure of the prism parse tree, but we manually add them here so that
# we can mirror Ripper's void stmt.
private def visit_statements_node_body(body)
body.inject(on_stmts_new) do |stmts, stmt|
on_stmts_add(stmts, stmt.nil? ? on_void_stmt : visit(stmt))
# Ripper emits one void_stmt for an empty list or a leading semicolon.
# A newline after a class or module name can also produce a void_stmt.
private def visit_statements_node_body(node, class_name_loc = nil)
stmts = on_stmts_new
return on_stmts_add(stmts, on_void_stmt) if node.nil? || node.body.empty?

if node.leading_semicolon? || (class_name_loc && class_name_loc.end_line < node.start_line)
stmts = on_stmts_add(stmts, on_void_stmt)
end

node.body.inject(stmts) { |list, stmt| on_stmts_add(list, visit(stmt)) }
end

# "foo"
Expand Down Expand Up @@ -4122,12 +4084,6 @@ def trailing_comma?(left, right)
source.byteslice(left.end_offset...right.start_offset).include?(",")
end

# Returns true if there is a semicolon between the two locations.
def void_stmt?(left, right, allow_newline)
pattern = allow_newline ? /[;\n]/ : /;/
source.byteslice(left.end_offset...right.start_offset).match?(pattern)
end

# Visit the string content of a particular node. This method is used to
# split into the various token types.
def visit_token(token, allow_keywords = true)
Expand Down
7 changes: 7 additions & 0 deletions rbi/generated/prism/node.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions rust/ruby-prism-sys/src/bindings.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions sig/generated/prism/node.rbs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions snapshots/arrays.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,7 @@
│ │ └── block: ∅
│ ├── body:
│ │ @ StatementsNode (location: (108,10)-(108,14))
│ │ ├── flags: ∅
│ │ ├── flags: leading_semicolon
│ │ └── body: (length: 1)
│ │ └── @ CallNode (location: (108,10)-(108,14))
│ │ ├── flags: newline
Expand Down Expand Up @@ -1529,7 +1529,7 @@
│ │ └── block: ∅
│ ├── body:
│ │ @ StatementsNode (location: (110,10)-(110,17))
│ │ ├── flags: ∅
│ │ ├── flags: leading_semicolon
│ │ └── body: (length: 1)
│ │ └── @ CallNode (location: (110,10)-(110,17))
│ │ ├── flags: newline
Expand Down Expand Up @@ -1592,7 +1592,7 @@
│ │ └── block: ∅
│ ├── body:
│ │ @ StatementsNode (location: (112,10)-(112,18))
│ │ ├── flags: ∅
│ │ ├── flags: leading_semicolon
│ │ └── body: (length: 1)
│ │ └── @ CallNode (location: (112,10)-(112,18))
│ │ ├── flags: newline, attribute_write
Expand Down Expand Up @@ -1655,7 +1655,7 @@
│ │ └── block: ∅
│ ├── body:
│ │ @ StatementsNode (location: (114,10)-(114,21))
│ │ ├── flags: ∅
│ │ ├── flags: leading_semicolon
│ │ └── body: (length: 1)
│ │ └── @ CallNode (location: (114,10)-(114,21))
│ │ ├── flags: newline, attribute_write
Expand Down Expand Up @@ -1721,7 +1721,7 @@
│ │ └── block: ∅
│ ├── body:
│ │ @ StatementsNode (location: (116,10)-(116,19))
│ │ ├── flags: ∅
│ │ ├── flags: leading_semicolon
│ │ └── body: (length: 1)
│ │ └── @ IndexOperatorWriteNode (location: (116,10)-(116,19))
│ │ ├── flags: newline
Expand Down Expand Up @@ -1784,7 +1784,7 @@
│ │ └── block: ∅
│ ├── body:
│ │ @ StatementsNode (location: (118,10)-(118,23))
│ │ ├── flags: ∅
│ │ ├── flags: leading_semicolon
│ │ └── body: (length: 1)
│ │ └── @ IndexAndWriteNode (location: (118,10)-(118,23))
│ │ ├── flags: newline
Expand Down
4 changes: 2 additions & 2 deletions snapshots/begin_ensure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
│ ├── begin_keyword_loc: (7,0)-(7,5) = "begin"
│ ├── statements:
│ │ @ StatementsNode (location: (7,7)-(7,8))
│ │ ├── flags: ∅
│ │ ├── flags: leading_semicolon
│ │ └── body: (length: 1)
│ │ └── @ CallNode (location: (7,7)-(7,8))
│ │ ├── flags: newline, variable_call, ignore_visibility
Expand All @@ -72,7 +72,7 @@
│ │ ├── ensure_keyword_loc: (7,10)-(7,16) = "ensure"
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (7,18)-(7,19))
│ │ │ ├── flags: ∅
│ │ │ ├── flags: leading_semicolon
│ │ │ └── body: (length: 1)
│ │ │ └── @ CallNode (location: (7,18)-(7,19))
│ │ │ ├── flags: newline, variable_call, ignore_visibility
Expand Down
Loading
Loading