Class: Depager::GrammarPartParser

Inherits:
Object
  • Object
show all
Includes:
Utils::CommonMethods
Defined in:
lib/depager.rb

Defined Under Namespace

Classes: Token

Constant Summary collapse

HOOK_KEYS =
%i[
  pre_rule_list post_rule_list
  pre_rule post_rule
  post_lhs
  post_rhs_list pre_rhs_list
  pre_rhs post_rhs
  post_symbol
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::CommonMethods

#error_exit, #error_message, #expanded_code_delimiter, #file, #full_target_name, #input_path, #inspect, #warning

Constructor Details

#initialize(d_parser) ⇒ GrammarPartParser

Returns a new instance of GrammarPartParser.



91
92
93
# File 'lib/depager.rb', line 91

def initialize(d_parser)
  @d_parser = d_parser
end

Instance Attribute Details

#d_parserObject (readonly)

Returns the value of attribute d_parser.



89
90
91
# File 'lib/depager.rb', line 89

def d_parser
  @d_parser
end

#hooksObject (readonly)

Returns the value of attribute hooks.



89
90
91
# File 'lib/depager.rb', line 89

def hooks
  @hooks
end

#inner_codeObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def inner_code
  @inner_code
end

#lhsObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def lhs
  @lhs
end

#lineObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def line
  @line
end

#mixinsObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def mixins
  @mixins
end

#nontermsObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def nonterms
  @nonterms
end

#nrhsObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def nrhs
  @nrhs
end

#nrulesObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def nrules
  @nrules
end

#old_lineObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def old_line
  @old_line
end

#original_lineObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def original_line
  @original_line
end

#outer_codeObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def outer_code
  @outer_code
end

#precsObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def precs
  @precs
end

#rhsObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def rhs
  @rhs
end

#rhs_namesObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def rhs_names
  @rhs_names
end

#rulesObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def rules
  @rules
end

#tableObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def table
  @table
end

#target_nameObject (readonly)

Returns the value of attribute target_name.



89
90
91
# File 'lib/depager.rb', line 89

def target_name
  @target_name
end

#target_namespaceObject (readonly)

Returns the value of attribute target_namespace.



89
90
91
# File 'lib/depager.rb', line 89

def target_namespace
  @target_namespace
end

#termsObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def terms
  @terms
end

#tokenObject

:nodoc:



87
88
89
# File 'lib/depager.rb', line 87

def token
  @token
end

Instance Method Details

#add_nonterm(sym) ⇒ Object



135
136
137
138
139
140
# File 'lib/depager.rb', line 135

def add_nonterm(sym)
  sym = sym.to_s.intern
  isym = @nonterms[sym] = @nonterms.size
  @int_to_sym[isym] = sym
  isym
end

#add_rule(lhs, rhs, prec = nil, rhs_names = nil) ⇒ Object



142
143
144
145
146
147
# File 'lib/depager.rb', line 142

def add_rule(lhs, rhs, prec = nil, rhs_names = nil)
  @lhs_syms[lhs] = true
  rule = d_parser.parsing_method::Rule[lhs, rhs, prec, rhs_names]
  @rules.push rule
  rule
end

#hook_name?(s) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/depager.rb', line 118

def hook_name?(s)
  HOOK_KEYS.include? s.to_sym
end

#insert_sym_to_rhs(pos, token_value, token_name) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/depager.rb', line 153

def insert_sym_to_rhs(pos, token_value, token_name)
  if pos < 0
    @rhs.push token_value
    @rhs_names[@rhs.size - 1] = token_name
  else
    @rhs.insert(pos, token_value)
    @rhs_names.insert(pos, token_name)
  end
  @rhs_syms[token_value] = true
end

#int_to_sym(n) ⇒ Object



127
128
129
# File 'lib/depager.rb', line 127

def int_to_sym(n)
  @int_to_sym[n]
end

#lhs_nameObject



131
132
133
# File 'lib/depager.rb', line 131

def lhs_name
  int_to_sym(lhs)
end

#name_to_rhs_index(name) ⇒ Object



149
150
151
# File 'lib/depager.rb', line 149

def name_to_rhs_index(name)
  @rhs_names.index(name)
end

#parse(full_target_name, precs = [], extensions = [], options = {}) ⇒ Object

parse and make LALR(1) table



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/depager.rb', line 96

def parse(full_target_name, precs = [], extensions = [], options = {})
  @original_line = @old_line = @line = ""
  @inner_code    = ""
  @outer_code    = ""

  full_target_name.match(/::([^:]+)$/) or raise
  @target_namespace = $`
  @target_name = $1

  extend_parser(extensions, options)
  init_grammar
  parse_grammar
  check_grammar
  make_grammar precs

  grammar = d_parser.parsing_method::Grammar.new(@rules, @terms, @nonterms, @precs)
  @table  = d_parser.parsing_method::Table.new(grammar)
  @table.check_table d_parser
  @extensions.each { |o| o.send :term_extension }
  @table
end

#update_context(line) ⇒ Object



122
123
124
125
# File 'lib/depager.rb', line 122

def update_context(line)
  @line = line
  scan_token
end