parser 2.6.0.0 → 2.7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +11 -11
  4. data/CHANGELOG.md +88 -0
  5. data/README.md +7 -0
  6. data/Rakefile +1 -0
  7. data/doc/AST_FORMAT.md +410 -3
  8. data/lib/parser/all.rb +1 -0
  9. data/lib/parser/ast/processor.rb +39 -1
  10. data/lib/parser/base.rb +23 -1
  11. data/lib/parser/builders/default.rb +312 -15
  12. data/lib/parser/context.rb +12 -0
  13. data/lib/parser/current.rb +15 -6
  14. data/lib/parser/current_arg_stack.rb +43 -0
  15. data/lib/parser/lexer/dedenter.rb +48 -49
  16. data/lib/parser/lexer.rb +11934 -11278
  17. data/lib/parser/lexer.rl +207 -58
  18. data/lib/parser/macruby.rb +1509 -1507
  19. data/lib/parser/macruby.y +1 -1
  20. data/lib/parser/max_numparam_stack.rb +48 -0
  21. data/lib/parser/messages.rb +55 -44
  22. data/lib/parser/meta.rb +9 -1
  23. data/lib/parser/ruby18.rb +1255 -1248
  24. data/lib/parser/ruby18.y +1 -1
  25. data/lib/parser/ruby19.rb +1645 -1638
  26. data/lib/parser/ruby19.y +1 -1
  27. data/lib/parser/ruby20.rb +1766 -1767
  28. data/lib/parser/ruby20.y +1 -1
  29. data/lib/parser/ruby21.rb +1654 -1656
  30. data/lib/parser/ruby21.y +1 -1
  31. data/lib/parser/ruby22.rb +1772 -1767
  32. data/lib/parser/ruby22.y +1 -1
  33. data/lib/parser/ruby23.rb +1088 -1092
  34. data/lib/parser/ruby23.y +1 -1
  35. data/lib/parser/ruby24.rb +1837 -1843
  36. data/lib/parser/ruby24.y +1 -1
  37. data/lib/parser/ruby25.rb +1750 -1755
  38. data/lib/parser/ruby25.y +2 -2
  39. data/lib/parser/ruby26.rb +1748 -1752
  40. data/lib/parser/ruby26.y +2 -2
  41. data/lib/parser/ruby27.rb +7749 -0
  42. data/lib/parser/ruby27.y +2923 -0
  43. data/lib/parser/rubymotion.rb +1492 -1497
  44. data/lib/parser/rubymotion.y +1 -1
  45. data/lib/parser/runner/ruby_parse.rb +2 -2
  46. data/lib/parser/runner.rb +5 -0
  47. data/lib/parser/source/tree_rewriter.rb +2 -2
  48. data/lib/parser/static_environment.rb +10 -0
  49. data/lib/parser/tree_rewriter.rb +2 -1
  50. data/lib/parser/variables_stack.rb +32 -0
  51. data/lib/parser/version.rb +1 -1
  52. data/lib/parser.rb +3 -0
  53. data/parser.gemspec +3 -2
  54. data/test/helper.rb +8 -0
  55. data/test/parse_helper.rb +2 -1
  56. data/test/test_current.rb +2 -0
  57. data/test/test_lexer.rb +72 -7
  58. data/test/test_parser.rb +2218 -8
  59. data/test/test_runner_parse.rb +35 -0
  60. metadata +20 -8
@@ -3,72 +3,71 @@
3
3
  module Parser
4
4
 
5
5
  class Lexer::Dedenter
6
+ # Tab (\t) counts as 8 spaces
7
+ TAB_WIDTH = 8
8
+
6
9
  def initialize(dedent_level)
7
10
  @dedent_level = dedent_level
8
11
  @at_line_begin = true
9
12
  @indent_level = 0
10
13
  end
11
14
 
15
+ # For a heredoc like
16
+ # <<-HERE
17
+ # a
18
+ # b
19
+ # HERE
20
+ # this method gets called with " a\n" and " b\n"
21
+ #
22
+ # However, the following heredoc:
23
+ #
24
+ # <<-HERE
25
+ # a\
26
+ # b
27
+ # HERE
28
+ # calls this method only once with a string " a\\\n b\n"
29
+ #
30
+ # This is important because technically it's a single line,
31
+ # but it has to be concatenated __after__ dedenting.
32
+ #
33
+ # It has no effect for non-squiggly heredocs, i.e. it simply removes "\\\n"
34
+ # Of course, lexer could do it but once again: it's all because of dedenting.
35
+ #
12
36
  def dedent(string)
13
- space_begin = space_end = offset = 0
14
- last_index = string.length - 1
15
- escape = false
16
- _at_line_begin = nil
37
+ lines = string.split("\\\n")
17
38
 
18
- string.chars.each_with_index do |char, index|
19
- if char == '\\'
20
- # entering escape mode
21
- escape = true
22
- string.slice!(index - offset)
23
- offset += 1
24
- _at_line_begin = @at_line_begin
25
- @at_line_begin = false
26
- elsif escape
27
- if char == ?\n
28
- # trimming \n, starting a new line
29
- string.slice!(index - offset)
30
- offset += 1
31
- @at_line_begin = true
32
- space_begin = space_end = index - offset
33
- @indent_level = 0
34
- elsif char == ?n
35
- # replacing \\n to \n
36
- string.slice!(index - offset)
37
- string.insert(index - offset, ?\n)
38
- else
39
- # exiting escape mode as it's not an escape sequence
40
- @at_line_begin = _at_line_begin
41
- escape = false
42
- redo
43
- end
44
- escape = false
45
- elsif @at_line_begin
46
- if char == ?\n || @indent_level >= @dedent_level
47
- string.slice!(space_begin...space_end)
48
- offset += space_end - space_begin
49
- @at_line_begin = false
50
- end
39
+ if @at_line_begin
40
+ lines_to_dedent = lines
41
+ else
42
+ _first, *lines_to_dedent = lines
43
+ end
51
44
 
45
+ lines_to_dedent.each do |line|
46
+ left_to_remove = @dedent_level
47
+ remove = 0
48
+
49
+ line.each_char do |char|
50
+ break if left_to_remove <= 0
52
51
  case char
53
52
  when ?\s
54
- @indent_level += 1
55
- space_end += 1
53
+ remove += 1
54
+ left_to_remove -= 1
56
55
  when ?\t
57
- @indent_level += 8 - @indent_level % 8
58
- space_end += 1
56
+ break if TAB_WIDTH * (remove / TAB_WIDTH + 1) > @dedent_level
57
+ remove += 1
58
+ left_to_remove -= TAB_WIDTH
59
+ else
60
+ # no more spaces or tabs
61
+ break
59
62
  end
60
- elsif char == ?\n && index == last_index
61
- @at_line_begin = true
62
- @indent_level = 0
63
- space_begin = space_end = index - offset + 1
64
63
  end
65
- end
66
64
 
67
- if @at_line_begin
68
- string.slice!(space_begin..space_end)
65
+ line.slice!(0, remove)
69
66
  end
70
67
 
71
- nil
68
+ string.replace(lines.join)
69
+
70
+ @at_line_begin = string.end_with?("\n")
72
71
  end
73
72
 
74
73
  def interrupt