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/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.

Direct Known Subclasses

Parser33, Parser34

Defined Under Namespace

Classes: Compiler, PrismDiagnostic

Constant Summary collapse

Racc_debug_parser =

:nodoc:

false

Instance Method Summary collapse

Instance Method Details

#default_encodingObject

The default encoding for Ruby files is UTF-8.



41
42
43
# File 'lib/prism/translation/parser.rb', line 41

def default_encoding
  Encoding::UTF_8
end

#parse(source_buffer) ⇒ Object

Parses a source buffer and returns the AST.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/prism/translation/parser.rb', line 49

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, version: convert_for_prism(version), partial_script: true, encoding: false), 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.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/prism/translation/parser.rb', line 62

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, version: convert_for_prism(version), partial_script: true, encoding: false), 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.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/prism/translation/parser.rb', line 79

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

  offset_cache = build_offset_cache(source)
  result =
    begin
      unwrap(Prism.parse_lex(source, filepath: source_buffer.name, version: convert_for_prism(version), partial_script: true, encoding: false), offset_cache)
    rescue ::Parser::SyntaxError
      raise if !recover
    end

  program, tokens = result.value
  ast = build_ast(program, offset_cache) if result.success?

  [
    ast,
    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.



105
106
107
# File 'lib/prism/translation/parser.rb', line 105

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

#versionObject

:nodoc:



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

def version # :nodoc:
  34
end

#yyerrorObject

:nodoc:



45
46
# File 'lib/prism/translation/parser.rb', line 45

def yyerror # :nodoc:
end