Commit graph

70 commits

Author SHA1 Message Date
Stan Lo
e1d7066a5f [ruby/irb] Deprecate RubyLex and warn about referencing to it
(https://github.com/ruby/irb/pull/692)

`RubyLex` has always been a private component of IRB, so we should
explicitly discourage usages of it.
Also, it should be placed under the `IRB` module like other components.

069b5625f7
2023-08-24 15:35:40 +00:00
Stan Lo
9099d62ac7 [ruby/irb] Move IO configuration to IRB::Irb
(https://github.com/ruby/irb/pull/681)

It shouldn't be `RubyLex`'s responsibility to handle IO. So this moves
the configuration to `IRB::Irb`.

daff750076
2023-08-13 15:23:04 +00:00
tomoya ishida
d42891079f [ruby/irb] Remove needless removal of trailing whitespace in
check_code_state
(https://github.com/ruby/irb/pull/678)

4a6af7d1ed
2023-08-11 18:51:25 +00:00
Stan Lo
0781e55206 [ruby/irb] Move assignment check to RubyLex
(https://github.com/ruby/irb/pull/670)

Since assignment check relies on tokenization with `Ripper`, it feels like
the responsibility of `RubyLex`. `Irb#eval_input` should simply get the result
when calling `each_top_level_statement` on `RubyLex`.

89d1adb3fd
2023-08-11 18:44:52 +00:00
Stan Lo
d6ad334d6e [ruby/irb] Reduce boilerplate code in RubyLexTest
(https://github.com/ruby/irb/pull/644)

* Avoid initialising Row at every test input

* Extract common assertion patterns into methods

* Remove unnecessary PromptRow strcut and boilerplate code

1ba586c0c6
2023-07-18 15:18:58 +00:00
tomoya ishida
be98bfc4ee [ruby/irb] Indent multiline percent literals
(https://github.com/ruby/irb/pull/643)

18bb4022a9
2023-07-15 18:12:05 +00:00
tomoya ishida
9398734010 [ruby/irb] Re-implement prev_spaces feature for pasted code
(https://github.com/ruby/irb/pull/607)

9d97a192a5
2023-06-30 14:23:24 +00:00
tomoya ishida
78ecb93f9d [ruby/irb] Reduce test pend truffleruby
(https://github.com/ruby/irb/pull/619)

b7b071774f
2023-06-28 17:33:20 +00:00
tomoya ishida
eaad44adb2 [ruby/irb] Always add \n at the end of the test input in RubyLex
test
(https://github.com/ruby/irb/pull/614)

e68c6128aa
2023-06-27 10:19:03 +00:00
tomoya ishida
00216c8aa0 [ruby/irb] Fix process_continue(rename to should_continue?) and
check_code_block(rename to check_code_syntax)
(https://github.com/ruby/irb/pull/611)

b7f4bfaaa4
2023-06-25 05:12:16 +00:00
tomoya ishida
406799cae8 [ruby/irb] Omit nesting_level, use indent_level to build prompt
string
(https://github.com/ruby/irb/pull/610)

f01ff0811b
2023-06-24 22:20:43 +00:00
tomoya ishida
e25403d0d9 [ruby/irb] Improve indentation: bugfix, heredoc, embdoc, strings
(https://github.com/ruby/irb/pull/515)

* Implement heredoc embdoc and string indentation with bugfix

* Fix test_ruby_lex's indentation value

* Add embdoc indent test

* Add workaround for lines==[nil] passed to auto_indent when exit IRB with CTRL+d
2023-06-20 15:13:43 +00:00
tomoya ishida
b8cd79c82f [ruby/irb] Add missing token that ignored by ripper
(https://github.com/ruby/irb/pull/608)

1cd3b45402
2023-06-19 10:38:24 +00:00
tomoya ishida
364a6d56d7 [ruby/irb] Rewrite RubyLex to fix some bugs and make it possible to
add new features easily
(https://github.com/ruby/irb/pull/500)

* Add nesting level parser for multiple use (indent, prompt, termination check)

* Rewrite RubyLex using NestingParser

* Add nesting parser tests, fix some existing tests

* Add description comment, rename method to NestingParser

* Add comments and tweak code to RubyLex

* Update NestingParser test

* Extract list of ltype tokens to constants
2023-06-15 15:39:58 +00:00
Stan Lo
91b106fe4b [ruby/irb] Stanardise test class names with Test postfix instead
of prefix
(https://github.com/ruby/irb/pull/603)

359cb28def
2023-06-13 11:02:26 +00:00
ima1zumi
64e156f20c [ruby/irb] Fix typo (https://github.com/ruby/irb/pull/587)
* Fix typo

* s/braking/breaking/g

Co-authored-by: Stan Lo <stan001212@gmail.com>

---------

5f8e69f5f2

Co-authored-by: Stan Lo <stan001212@gmail.com>
2023-05-21 02:57:50 +00:00
Stan Lo
9ce6c08caf [ruby/irb] Add assertion for dynamic_prompt's assertion execution
(https://github.com/ruby/irb/pull/586)

Because the assertions for `dynamic_prompt` lives inside a block given to
`RubyLex`, they could be skipped unnoticed if the setup is not correct.

This commit adds a simple assertion to check if the block was actually
executed.
2023-05-20 04:28:23 +00:00
tomoya ishida
6d9875ccbf [ruby/irb] Fix dynamic_prompt test not executed, remove unnecessary
set_input
(https://github.com/ruby/irb/pull/585)

b5f3efdcf0
2023-05-20 01:08:58 +00:00
tomoya ishida
e8c9f727e8 [ruby/irb] Simplify each_top_level_statement
(https://github.com/ruby/irb/pull/576)

* Simplify each_top_level_statement, reduce instance vars

* Update lib/irb/ruby-lex.rb

Co-authored-by: Stan Lo <stan001212@gmail.com>

* Remove unused ltype from TestRubyLex#check_state response

* Remove unnecessary const path of TerminateLineInput

* Combine duplicated code state check into method

---------

172453cec4

Co-authored-by: Stan Lo <stan001212@gmail.com>
2023-05-19 13:48:08 +00:00
Stan Lo
cfb7997353 [ruby/irb] Refactor RubyLex's input/io methods
(https://github.com/ruby/irb/pull/583)

1. Make `RubyLex#set_input` simply assign the input block. This matches
   the behavior of `RubyLex#set_prompt`.
2. Merge `RubyLex#set_input`'s IO configuration logic with `#set_auto_indent`
   into `#configure_io`.
2023-05-18 19:00:33 +00:00
tomoya ishida
f8115ec727 [ruby/irb] Fix RubyLex's heredoc_with_hembexpr test to avoid ripper
tokenizing issue
(https://github.com/ruby/irb/pull/558)

f68e891ed1
2023-04-07 11:03:28 +00:00
Stan Lo
2f8e5c80e6 [ruby/irb] Drop Ruby 2.6 support
(https://github.com/ruby/irb/pull/555)

* Remove all Ruby 2.6 support

* Drop Ruby 2.6 specific testing conditions

* Only run Ruby 2.7+ on CI

* Bump Ruby requirement to 2.7+

3f714b616c
2023-04-05 21:40:40 +00:00
Stan Lo
50e77b6a9c [ruby/irb] Improve RubyLex's tests
(https://github.com/ruby/irb/pull/484)

* Improve assert_indenting helper

Instead of putting assertions inside the `auto_indent` block, we
can just make `auto_indent` return the calculated space count, and use
it for assertion outside of the `auto_indent` block call.

This simplifies the setup code and makes the intention easier to
understand.

* Introduce assert_row_indenting helper

1. Helper users shouldn't need to write 2 assertions for the current and
   the next line's indentation.
2. With this new approach, we can generate clearer error message for
   both cases:

When the current line's space count doesn't match

```
  Incorrect spaces calculation for line:

  ```
> def each_top_level_statement
  ```

  All lines:

  ```
  def each_top_level_statement
  ```

<0> expected but was
<nil>
```

When the next line's space count doesn't match

```
  Incorrect spaces calculation for line after the current line:

  ```
  def each_top_level_statement
>
  ```

  All lines:

  ```
  def each_top_level_statement
  ```

<3> expected but was
<2>
```

* Replace assert_indenting with assert_row_indenting
2023-02-21 19:38:09 +00:00
Stan Lo
cb9b885e78 [ruby/irb] Store context in RubyLex
Some background for this refactor:

1. Through a RubyLex instance's lifetime, the context passed to its methods
   should be the same.
   Given that `Context` is only initialised in `Irb#initialize`,
   this should be true.

2. When `RubyLex` is initialised, the context object should be accessible.
   This is also true in all 3 of `RubyLex.new`'s invocations.

With the above observations, we should be able to store the context in `RubyLex`
as an instance variable. And doing so will make `RubyLex`'s instance methods
easier to use and maintain.

5c8d3df2df
2023-01-14 09:19:09 +00:00
Stan Lo
c693dfd7ef [ruby/irb] Drop unused arguments in RubyLex
(https://github.com/ruby/irb/pull/504)

* Simplify `RubyLex#set_prompt`

It's optional argument is never used by any caller.

* Remove the optional `p` argument from `RubyLex#set_input`

The argument is only used in a test case, which can be easily replaced by
a block argument.
2023-01-11 21:26:18 +00:00
tomoya ishida
34f8ca1714 [ruby/irb] Fix prompt and code mismatch
(https://github.com/ruby/irb/pull/386)

* fix prompt and code mismatch

* Add test for prompt and code mismatch bug

a5765d8177

Co-authored-by: Stan Lo <stan001212@gmail.com>
2023-01-02 19:25:11 +00:00
Stan Lo
140c93e2dc [ruby/irb] Add dynamic prompt test case for quoted heredoc
(https://github.com/ruby/irb/pull/486)

31cfbeacbb
2022-12-27 16:43:44 +00:00
Takashi Kokubun
d5985049c7 Sync IRB master: tool/sync_default_gems.rb irb
It looks like tool/sync_default_gems.rb is not capable of cherry-picking
commits from ruby/irb. I just executed `tool/sync_default_gems.rb irb`
to fix the sync status.

I'm not sure if what's the cause. It could be related to some diff that
doesn't exist in ruby/ruby, or it might be related to non-linear history
due to merge commits. For next time, I'd like to at least exclude the
second possibility, so I disabled merge commits in ruby/irb.
2022-12-26 13:10:37 -08:00
st0012
923c1aaed7
Drop Ruby 2.5 support
Because it has reached EOL for more than 1.5 years and it won't be
supported by the next reline version either.
2022-11-17 13:06:32 +09:00
Stan Lo
367c072ac6 [ruby/irb] Silent the noise created when building Context in tests
27e4274b3c
2022-11-04 18:29:54 +00:00
Stan Lo
a923203811 [ruby/irb] Provide a base test class and let tests restore encodings
conveniently
(https://github.com/ruby/irb/pull/429)

* Create a base TestIRB::TestCase class

* Save/restore encodings for tests that initializes InputMethod classes

Because `RelineInputMethod#initializes` calls `set_encoding`, which
changes stdio/out/err and Encoding's default encoding values, we need to
make sure any test that directly or indirectly (e.g. through Context)
initializes `RelineInputMethod` restores encodings.

`ReadlineInputMethod` also changes encodings but currently no tests
cover it.

* Remove unnecessary TestHelper module

Since we now have a base TestCase, without_rdoc can just live there.

c2874ec121
2022-11-03 22:13:11 +00:00
Takashi Kokubun
56884b64de [ruby/irb] Require rubygems to run the test alone on ruby/ruby
`Gem` is not undefined on test-all

08ac803d61
2022-11-03 17:42:45 +00:00
Takashi Kokubun
611b5e7f40 [ruby/irb] Fix build_context for ruby/ruby CI
Co-Authored-By: Stan Lo <stan.lo@shopify.com>

d1fe234a9a
2022-11-03 17:35:15 +00:00
Stan Lo
00f559641a [ruby/irb] Require the entire irb lib in RubyLex test
(https://github.com/ruby/irb/pull/428)

RubyLex is not designed to be used alone. It's usually used with an IRB
context, which requires workspace. So its tests should have access to
those components too.

608f261da4
2022-11-03 17:12:35 +00:00
Stan Lo
c5d6a483f5 [ruby/irb] Refactor RubyLex and its tests
(https://github.com/ruby/irb/pull/427)

* Make sure `RubyLex#set_input`'s context is always present in tests

In real-world scenarios, the context should always be non-nil:
https://github.com/ruby/irb/blob/master/lib/irb.rb#L489

So we should make sure our test setup reflects that.

* Make context a required keyword

Since in practice, `set_input`'s context should always be non-nil, its
parameters should reflect that.

And since `RubyLex#check_state` is only called by `#lex` and
`#set_input`, both of which now always require context, we can assume
its context should be non-nil too.

1aeeb86203
2022-11-03 16:32:22 +00:00
tomoya ishida
a09f764ce5 [ruby/irb] Always use local variables in current context to parse code (https://github.com/ruby/irb/pull/397)
* Use local_variables for colorize, code_block_open check, nesting_level and assignment_expression check

* Check if expression is an assignment BEFORE evaluating it. evaluate might define new localvars and change result of assignment_expression?

* Add local_variables dependent code test

* pend local variable dependent test on truffleruby

code_block_open is not working on truffleruby

* Always pass context to RubyLex#lex

* Rename local_variable_assign_code generator method name

* Add assignment expression truncate test

* Add Context#local_variables and make generate_local_variables_assign_code more simple

* Update lib/irb/input-method.rb

Co-authored-by: Stan Lo <stan001212@gmail.com>

* Add a comment why assignment expression check should be done before evaluate

c8b3877281

Co-authored-by: Stan Lo <stan001212@gmail.com>
Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com>
2022-10-18 05:44:07 +00:00
tomoya ishida
344e6c915f [ruby/irb] Fix code terminated check with heredoc and backtick (https://github.com/ruby/irb/pull/390)
* Fix backtick method def method call handled as backtick open

* Fix handling heredoc in check_string_literal

* Sort result of lexer.parse by pos in ruby<2.7. It's not sorted when the given code includes heredoc.

* Update lib/irb/ruby-lex.rb

Co-authored-by: Stan Lo <stan001212@gmail.com>

* Update lib/irb/ruby-lex.rb

Co-authored-by: Stan Lo <stan001212@gmail.com>

* Add check_string_literal test for heredoc code that does not end with newline

44bc712460

Co-authored-by: Stan Lo <stan001212@gmail.com>
2022-10-18 05:30:33 +00:00
tompng
641310ce37 [ruby/irb] Fix ripper_lex_without_warning duplicated heredoc token
45b539af39
2022-10-01 04:17:15 +09:00
Kaíque Kandy Koga
6b64e78823 [ruby/irb] Examine indentation of in keyword when trying to type include
Use in_keyword_case_scope?

Return fast

8acc7f8dc7
2021-12-03 00:56:43 +09:00
schneems
2b22c93533 Compatibility with IRB
Instead of accessing the struct as an array, access it via methods. There are other places inside of this file already using this API (for example e0a5c3d2b7/lib/irb/ruby-lex.rb (L829-L830)).

This commit moves all struct array-ish calls to use their method calls instead. It is also ~1.23 faster accessing values via a method instead of as an array according to this microbenchmark:

```ruby
Elem = Struct.new(:pos, :event, :tok, :state, :message) do
  def initialize(pos, event, tok, state, message = nil)
    super(pos, event, tok, State.new(state), message)
  end

  # ...

  def to_a
    a = super
    a.pop unless a.empty?
    a
  end
end

class ElemClass
  attr_accessor :pos, :event, :tok, :state, :message

  def initialize(pos, event, tok, state, message = nil)
    @pos = pos
    @event = event
    @tok = tok
    @state = State.new(state)
    @message = message
  end

  def to_a
    if @message
      [@pos, @event, @tok, @state, @message]
    else
      [@pos, @event, @tok, @state]
    end
  end
end

# stub state class creation for now
class State; def initialize(val); end; end
```

```ruby
Benchmark.ips do |x|
  x.report("struct") { struct[1] }
  x.report("class ") { from_class.event }
  x.compare!
end; nil
```

```
Warming up --------------------------------------
              struct     1.624M i/100ms
              class      1.958M i/100ms
Calculating -------------------------------------
              struct     17.139M (± 2.6%) i/s -     86.077M in   5.025801s
              class      21.104M (± 3.4%) i/s -    105.709M in   5.015193s

Comparison:
              class : 21103826.3 i/s
              struct: 17139201.5 i/s - 1.23x  (± 0.00) slower
```
2021-12-02 15:55:42 +09:00
Kaíque Kandy Koga
782d1d876b [ruby/irb] Use typed spaces when the line is inside the here documents
Use first method instead of square brackets to support 2.5 and 2.6 versions

Use tokens

Clear check_newline_depth_difference

6fec2a5d46
2021-09-22 23:26:41 +09:00
aycabta
f085a6fb69 [ruby/irb] Support symbol with backtick
0aa2425883
2021-09-10 04:59:17 +09:00
Hiroshi SHIBATA
598f4f4219 [ruby/irb] Use pend instead of skip
f441ce35bf
2021-08-30 12:39:23 +09:00
aycabta
0259ee6008 [ruby/irb] Change ripper_lex_without_warning to a class method
d9f8abc17e
2021-03-24 14:39:50 +09:00
aycabta
182cde8dfb [ruby/irb] Add a test for not continuing when endless range at eol
1020ac9c65
2021-03-05 22:03:11 +09:00
aycabta
743c44ee21 [ruby/irb] Indent correctly with method calling with receiver
e7c68e74a0
2021-01-24 14:35:34 +09:00
aycabta
fc54af8aa1 [ruby/irb] Indent correctly with keyword "for" and "in"
47c83ea724
2021-01-24 14:35:26 +09:00
aycabta
0123bc9d38 [ruby/irb] Use error tokens if there are no correct tokens in the same place
For example, the broken code "%www" will result in only one error token.

9fa39a7cf3
2021-01-05 18:06:43 +09:00
aycabta
5a1866caff [ruby/irb] Use Ripper::Lexer#scan to take broken tokens
ref. https://github.com/ruby/reline/pull/242

54f90cb6c9
2021-01-05 18:06:34 +09:00
aycabta
505e01fe12 [ruby/irb] Heredoc may contain multiple newlines in a single token
Use the start token as the indentation criteria so that it works properly in
heredoc.

ref. https://github.com/ruby/reline/pull/242

9704808dfd
2021-01-05 18:06:26 +09:00