Class: Prism::Translation::Parser

Inherits:
Parser::Base
  • Object
show all
Defined in:
lib/prism/translation/parser.rb,
lib/prism/translation/parser/lexer.rb,
lib/prism/translation/parser/rubocop.rb,
lib/prism/translation/parser/compiler.rb

Overview

This class is the entry-point for converting a prism syntax tree into the whitequark/parser gem’s syntax tree. It inherits from the base parser for the parser gem, and overrides the parse* methods to parse with prism and then translate.

Defined Under Namespace

Modules: ProcessedSource Classes: Diagnostic

Constant Summary collapse

Racc_debug_parser =

:nodoc:

false
VERSION_3_3 =

This is the special version number that should be used in rubocop configuration files to trigger using prism.

80_82_73_83_77.33

Instance Method Summary collapse

Instance Method Details

#default_encodingObject

The default encoding for Ruby files is UTF-8.



33
34
35
# File 'lib/prism/translation/parser.rb', line 33

def default_encoding
  Encoding::UTF_8
end

#parse(source_buffer) ⇒ Object

Parses a source buffer and returns the AST.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/prism/translation/parser.rb', line 41

def parse(source_buffer)
  @source_buffer = source_buffer
  source = source_buffer.source

  offset_cache = build_offset_cache(source)
  result = unwrap(Prism.parse(source, filepath: source_buffer.name), offset_cache)

  build_ast(result.value, offset_cache)
ensure
  @source_buffer = nil
end

#parse_with_comments(source_buffer) ⇒ Object

Parses a source buffer and returns the AST and the source code comments.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/prism/translation/parser.rb', line 54

def parse_with_comments(source_buffer)
  @source_buffer = source_buffer
  source = source_buffer.source

  offset_cache = build_offset_cache(source)
  result = unwrap(Prism.parse(source, filepath: source_buffer.name), offset_cache)

  [
    build_ast(result.value, offset_cache),
    build_comments(result.comments, offset_cache)
  ]
ensure
  @source_buffer = nil
end

#tokenize(source_buffer, _recover = false) ⇒ Object

Parses a source buffer and returns the AST, the source code comments, and the tokens emitted by the lexer.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/prism/translation/parser.rb', line 71

def tokenize(source_buffer, _recover = false)
  @source_buffer = source_buffer
  source = source_buffer.source

  offset_cache = build_offset_cache(source)
  result = unwrap(Prism.parse_lex(source, filepath: source_buffer.name), offset_cache)

  program, tokens = result.value

  [
    build_ast(program, offset_cache),
    build_comments(result.comments, offset_cache),
    build_tokens(tokens, offset_cache)
  ]
ensure
  @source_buffer = nil
end

#try_declare_numparam(node) ⇒ Object

Since prism resolves num params for us, we don’t need to support this kind of logic here.



91
92
93
# File 'lib/prism/translation/parser.rb', line 91

def try_declare_numparam(node)
  node.children[0].match?(/\A_[1-9]\z/)
end

#versionObject

:nodoc:



28
29
30
# File 'lib/prism/translation/parser.rb', line 28

def version # :nodoc:
  34
end

#yyerrorObject

:nodoc:



37
38
# File 'lib/prism/translation/parser.rb', line 37

def yyerror # :nodoc:
end