Class: Parser::Base

Inherits:
Racc::Parser
  • Object
show all
Defined in:
lib/parser/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder = Parser::Builders::Default.new) ⇒ Base

Returns a new instance of Base.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/parser/base.rb', line 31

def initialize(builder=Parser::Builders::Default.new)
  @diagnostics = Diagnostic::Engine.new

  @static_env  = StaticEnvironment.new

  @comments    = []

  @lexer = Lexer.new(version)
  @lexer.diagnostics = @diagnostics
  @lexer.static_env  = @static_env

  @builder = builder
  @builder.parser = self

  if self.class::Racc_debug_parser && ENV['RACC_DEBUG']
    @yydebug = true
  end

  reset
end

Instance Attribute Details

#diagnosticsObject (readonly)

Returns the value of attribute diagnostics.



25
26
27
# File 'lib/parser/base.rb', line 25

def diagnostics
  @diagnostics
end

#source_bufferObject (readonly)

The source file currently being parsed.



29
30
31
# File 'lib/parser/base.rb', line 29

def source_buffer
  @source_buffer
end

#static_envObject (readonly)

Returns the value of attribute static_env.



26
27
28
# File 'lib/parser/base.rb', line 26

def static_env
  @static_env
end

Class Method Details

.parse(string, file = '(string)', line = 1) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/parser/base.rb', line 4

def self.parse(string, file='(string)', line=1)
  parser = new

  parser.diagnostics.all_errors_are_fatal = true
  parser.diagnostics.ignore_warnings      = true

  # Temporary, for manual testing convenience
  parser.diagnostics.consumer = lambda do |diagnostic|
    $stderr.puts(diagnostic.render)
  end

  source_buffer = Source::Buffer.new(file, line)
  source_buffer.source = string

  parser.parse(source_buffer)
end

.parse_file(filename) ⇒ Object



21
22
23
# File 'lib/parser/base.rb', line 21

def self.parse_file(filename)
  parse(File.read(filename), filename)
end

Instance Method Details

#check_kwarg_name(name_t) ⇒ Object (protected)



88
89
90
91
92
93
94
95
# File 'lib/parser/base.rb', line 88

def check_kwarg_name(name_t)
  case name_t[0]
  when /^[a-z_]/
    # OK
  when /^[A-Z]/
    diagnostic :error, :argument_const, name_t
  end
end

#diagnostic(level, kind, location_t, highlights_ts = []) ⇒ Object (protected)



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/parser/base.rb', line 97

def diagnostic(level, kind, location_t, highlights_ts=[])
  _, location = location_t

  highlights = highlights_ts.map do |token|
    _, range = token
    range
  end

  message = ERRORS[kind]
  @diagnostics.process(
      Diagnostic.new(level, message, location, highlights))

  if level == :error
    yyerror
  end
end

#in_def?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/parser/base.rb', line 73

def in_def?
  @def_level > 0
end

#next_tokenObject (protected)



79
80
81
# File 'lib/parser/base.rb', line 79

def next_token
  @lexer.advance
end

#on_error(error_token_id, error_value, value_stack) ⇒ Object (protected)



114
115
116
117
118
119
120
121
# File 'lib/parser/base.rb', line 114

def on_error(error_token_id, error_value, value_stack)
  token_name = token_to_str(error_token_id)
  _, location = error_value

  message = ERRORS[:unexpected_token] % { :token => token_name }
  @diagnostics.process(
      Diagnostic.new(:error, message, location))
end

#parse(source_buffer) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/parser/base.rb', line 62

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

  do_parse
ensure
  # Don't keep references to the source file.
  @source_buffer       = nil
  @lexer.source_buffer = nil
end

#resetObject



52
53
54
55
56
57
58
59
60
# File 'lib/parser/base.rb', line 52

def reset
  @source_buffer = nil
  @def_level   = 0 # count of nested def's.

  @lexer.reset
  @static_env.reset

  self
end

#value_expr(v) ⇒ Object (protected)



83
84
85
86
# File 'lib/parser/base.rb', line 83

def value_expr(v)
  #p 'value_expr', v
  v
end