Class: Lrama::Grammar
- Inherits:
-
Object
- Object
- Lrama::Grammar
- Extended by:
- Forwardable
- Defined in:
- lib/lrama/grammar.rb,
lib/lrama/grammar/code.rb,
lib/lrama/grammar/rule.rb,
lib/lrama/grammar/type.rb,
lib/lrama/grammar/union.rb,
lib/lrama/grammar/symbol.rb,
lib/lrama/grammar/binding.rb,
lib/lrama/grammar/counter.rb,
lib/lrama/grammar/printer.rb,
lib/lrama/grammar/auxiliary.rb,
lib/lrama/grammar/reference.rb,
lib/lrama/grammar/destructor.rb,
lib/lrama/grammar/precedence.rb,
lib/lrama/grammar/error_token.rb,
lib/lrama/grammar/percent_code.rb,
lib/lrama/grammar/rule_builder.rb,
lib/lrama/grammar/code/rule_action.rb,
lib/lrama/grammar/symbols/resolver.rb,
lib/lrama/grammar/code/printer_code.rb,
lib/lrama/grammar/code/destructor_code.rb,
lib/lrama/grammar/code/no_reference_code.rb,
lib/lrama/grammar/parameterizing_rule/rhs.rb,
lib/lrama/grammar/code/initial_action_code.rb,
lib/lrama/grammar/parameterizing_rule/rule.rb,
lib/lrama/grammar/parameterizing_rule/resolver.rb
Overview
Grammar is the result of parsing an input grammar file
Defined Under Namespace
Classes: Auxiliary, Binding, Code, Counter, Destructor, ErrorToken, ParameterizingRule, PercentCode, Precedence, Printer, Reference, Rule, RuleBuilder, Symbol, Symbols, Type, Union
Instance Attribute Summary collapse
-
#accept_symbol ⇒ Object
readonly
Returns the value of attribute accept_symbol.
-
#after_pop_stack ⇒ Object
Returns the value of attribute after_pop_stack.
-
#after_reduce ⇒ Object
Returns the value of attribute after_reduce.
-
#after_shift ⇒ Object
Returns the value of attribute after_shift.
-
#after_shift_error_token ⇒ Object
Returns the value of attribute after_shift_error_token.
-
#aux ⇒ Object
readonly
Returns the value of attribute aux.
-
#before_reduce ⇒ Object
Returns the value of attribute before_reduce.
-
#define ⇒ Object
Returns the value of attribute define.
-
#eof_symbol ⇒ Object
readonly
Returns the value of attribute eof_symbol.
-
#error_symbol ⇒ Object
readonly
Returns the value of attribute error_symbol.
-
#error_tokens ⇒ Object
Returns the value of attribute error_tokens.
-
#expect ⇒ Object
Returns the value of attribute expect.
-
#initial_action ⇒ Object
Returns the value of attribute initial_action.
-
#lex_param ⇒ Object
Returns the value of attribute lex_param.
-
#locations ⇒ Object
Returns the value of attribute locations.
-
#no_stdlib ⇒ Object
Returns the value of attribute no_stdlib.
-
#parameterizing_rule_resolver ⇒ Object
readonly
Returns the value of attribute parameterizing_rule_resolver.
-
#parse_param ⇒ Object
Returns the value of attribute parse_param.
-
#percent_codes ⇒ Object
readonly
Returns the value of attribute percent_codes.
-
#printers ⇒ Object
Returns the value of attribute printers.
-
#rule_builders ⇒ Object
Returns the value of attribute rule_builders.
-
#rules ⇒ Object
Returns the value of attribute rules.
-
#sym_to_rules ⇒ Object
Returns the value of attribute sym_to_rules.
-
#symbols_resolver ⇒ Object
Returns the value of attribute symbols_resolver.
-
#types ⇒ Object
Returns the value of attribute types.
-
#undef_symbol ⇒ Object
readonly
Returns the value of attribute undef_symbol.
-
#union ⇒ Object
Returns the value of attribute union.
Instance Method Summary collapse
- #add_destructor(ident_or_tags:, token_code:, lineno:) ⇒ Object
- #add_error_token(ident_or_tags:, token_code:, lineno:) ⇒ Object
- #add_left(sym, precedence) ⇒ Object
- #add_nonassoc(sym, precedence) ⇒ Object
- #add_parameterizing_rule(rule) ⇒ Object
- #add_percent_code(id:, code:) ⇒ Object
- #add_precedence(sym, precedence) ⇒ Object
- #add_printer(ident_or_tags:, token_code:, lineno:) ⇒ Object
- #add_right(sym, precedence) ⇒ Object
- #add_rule_builder(builder) ⇒ Object
- #add_type(id:, tag:) ⇒ Object
- #create_rule_builder(rule_counter, midrule_action_counter) ⇒ Object
- #epilogue=(epilogue) ⇒ Object
- #epilogue_first_lineno=(epilogue_first_lineno) ⇒ Object
- #find_rules_by_symbol(sym) ⇒ Object
- #find_rules_by_symbol!(sym) ⇒ Object
- #ielr_defined? ⇒ Boolean
-
#initialize(rule_counter, define = {}) ⇒ Grammar
constructor
A new instance of Grammar.
- #insert_before_parameterizing_rules(rules) ⇒ Object
- #parameterizing_rules ⇒ Object
- #prepare ⇒ Object
- #prologue=(prologue) ⇒ Object
- #prologue_first_lineno=(prologue_first_lineno) ⇒ Object
- #set_precedence(sym, precedence) ⇒ Object
- #set_union(code, lineno) ⇒ Object
-
#validate! ⇒ Object
TODO: More validation methods.
Constructor Details
#initialize(rule_counter, define = {}) ⇒ Grammar
Returns a new instance of Grammar.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/lrama/grammar.rb', line 38 def initialize(rule_counter, define = {}) @rule_counter = rule_counter # Code defined by "%code" @percent_codes = [] @printers = [] @destructors = [] @error_tokens = [] @symbols_resolver = Grammar::Symbols::Resolver.new @types = [] @rule_builders = [] @rules = [] @sym_to_rules = {} @parameterizing_rule_resolver = ParameterizingRule::Resolver.new @empty_symbol = nil @eof_symbol = nil @error_symbol = nil @undef_symbol = nil @accept_symbol = nil @aux = Auxiliary.new @no_stdlib = false @locations = false @define = define.map {|d| d.split('=') }.to_h append_special_symbols end |
Instance Attribute Details
#accept_symbol ⇒ Object (readonly)
Returns the value of attribute accept_symbol.
28 29 30 |
# File 'lib/lrama/grammar.rb', line 28 def accept_symbol @accept_symbol end |
#after_pop_stack ⇒ Object
Returns the value of attribute after_pop_stack.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def after_pop_stack @after_pop_stack end |
#after_reduce ⇒ Object
Returns the value of attribute after_reduce.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def after_reduce @after_reduce end |
#after_shift ⇒ Object
Returns the value of attribute after_shift.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def after_shift @after_shift end |
#after_shift_error_token ⇒ Object
Returns the value of attribute after_shift_error_token.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def after_shift_error_token @after_shift_error_token end |
#aux ⇒ Object (readonly)
Returns the value of attribute aux.
28 29 30 |
# File 'lib/lrama/grammar.rb', line 28 def aux @aux end |
#before_reduce ⇒ Object
Returns the value of attribute before_reduce.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def before_reduce @before_reduce end |
#define ⇒ Object
Returns the value of attribute define.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def define @define end |
#eof_symbol ⇒ Object (readonly)
Returns the value of attribute eof_symbol.
28 29 30 |
# File 'lib/lrama/grammar.rb', line 28 def eof_symbol @eof_symbol end |
#error_symbol ⇒ Object (readonly)
Returns the value of attribute error_symbol.
28 29 30 |
# File 'lib/lrama/grammar.rb', line 28 def error_symbol @error_symbol end |
#error_tokens ⇒ Object
Returns the value of attribute error_tokens.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def error_tokens @error_tokens end |
#expect ⇒ Object
Returns the value of attribute expect.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def expect @expect end |
#initial_action ⇒ Object
Returns the value of attribute initial_action.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def initial_action @initial_action end |
#lex_param ⇒ Object
Returns the value of attribute lex_param.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def lex_param @lex_param end |
#locations ⇒ Object
Returns the value of attribute locations.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def locations @locations end |
#no_stdlib ⇒ Object
Returns the value of attribute no_stdlib.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def no_stdlib @no_stdlib end |
#parameterizing_rule_resolver ⇒ Object (readonly)
Returns the value of attribute parameterizing_rule_resolver.
28 29 30 |
# File 'lib/lrama/grammar.rb', line 28 def parameterizing_rule_resolver @parameterizing_rule_resolver end |
#parse_param ⇒ Object
Returns the value of attribute parse_param.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def parse_param @parse_param end |
#percent_codes ⇒ Object (readonly)
Returns the value of attribute percent_codes.
28 29 30 |
# File 'lib/lrama/grammar.rb', line 28 def percent_codes @percent_codes end |
#printers ⇒ Object
Returns the value of attribute printers.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def printers @printers end |
#rule_builders ⇒ Object
Returns the value of attribute rule_builders.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def rule_builders @rule_builders end |
#rules ⇒ Object
Returns the value of attribute rules.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def rules @rules end |
#sym_to_rules ⇒ Object
Returns the value of attribute sym_to_rules.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def sym_to_rules @sym_to_rules end |
#symbols_resolver ⇒ Object
Returns the value of attribute symbols_resolver.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def symbols_resolver @symbols_resolver end |
#types ⇒ Object
Returns the value of attribute types.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def types @types end |
#undef_symbol ⇒ Object (readonly)
Returns the value of attribute undef_symbol.
28 29 30 |
# File 'lib/lrama/grammar.rb', line 28 def undef_symbol @undef_symbol end |
#union ⇒ Object
Returns the value of attribute union.
29 30 31 |
# File 'lib/lrama/grammar.rb', line 29 def union @union end |
Instance Method Details
#add_destructor(ident_or_tags:, token_code:, lineno:) ⇒ Object
73 74 75 |
# File 'lib/lrama/grammar.rb', line 73 def add_destructor(ident_or_tags:, token_code:, lineno:) @destructors << Destructor.new(ident_or_tags: , token_code: token_code, lineno: lineno) end |
#add_error_token(ident_or_tags:, token_code:, lineno:) ⇒ Object
81 82 83 |
# File 'lib/lrama/grammar.rb', line 81 def add_error_token(ident_or_tags:, token_code:, lineno:) @error_tokens << ErrorToken.new(ident_or_tags: , token_code: token_code, lineno: lineno) end |
#add_left(sym, precedence) ⇒ Object
93 94 95 |
# File 'lib/lrama/grammar.rb', line 93 def add_left(sym, precedence) set_precedence(sym, Precedence.new(type: :left, precedence: precedence)) end |
#add_nonassoc(sym, precedence) ⇒ Object
89 90 91 |
# File 'lib/lrama/grammar.rb', line 89 def add_nonassoc(sym, precedence) set_precedence(sym, Precedence.new(type: :nonassoc, precedence: precedence)) end |
#add_parameterizing_rule(rule) ⇒ Object
118 119 120 |
# File 'lib/lrama/grammar.rb', line 118 def add_parameterizing_rule(rule) @parameterizing_rule_resolver.add_parameterizing_rule(rule) end |
#add_percent_code(id:, code:) ⇒ Object
69 70 71 |
# File 'lib/lrama/grammar.rb', line 69 def add_percent_code(id:, code:) @percent_codes << PercentCode.new(id.s_value, code.s_value) end |
#add_precedence(sym, precedence) ⇒ Object
101 102 103 |
# File 'lib/lrama/grammar.rb', line 101 def add_precedence(sym, precedence) set_precedence(sym, Precedence.new(type: :precedence, precedence: precedence)) end |
#add_printer(ident_or_tags:, token_code:, lineno:) ⇒ Object
77 78 79 |
# File 'lib/lrama/grammar.rb', line 77 def add_printer(ident_or_tags:, token_code:, lineno:) @printers << Printer.new(ident_or_tags: , token_code: token_code, lineno: lineno) end |
#add_right(sym, precedence) ⇒ Object
97 98 99 |
# File 'lib/lrama/grammar.rb', line 97 def add_right(sym, precedence) set_precedence(sym, Precedence.new(type: :right, precedence: precedence)) end |
#add_rule_builder(builder) ⇒ Object
114 115 116 |
# File 'lib/lrama/grammar.rb', line 114 def add_rule_builder(builder) @rule_builders << builder end |
#add_type(id:, tag:) ⇒ Object
85 86 87 |
# File 'lib/lrama/grammar.rb', line 85 def add_type(id:, tag:) @types << Type.new(id: id, tag: tag) end |
#create_rule_builder(rule_counter, midrule_action_counter) ⇒ Object
65 66 67 |
# File 'lib/lrama/grammar.rb', line 65 def create_rule_builder(rule_counter, midrule_action_counter) RuleBuilder.new(rule_counter, midrule_action_counter, @parameterizing_rule_resolver) end |
#epilogue=(epilogue) ⇒ Object
142 143 144 |
# File 'lib/lrama/grammar.rb', line 142 def epilogue=(epilogue) @aux.epilogue = epilogue end |
#epilogue_first_lineno=(epilogue_first_lineno) ⇒ Object
138 139 140 |
# File 'lib/lrama/grammar.rb', line 138 def epilogue_first_lineno=(epilogue_first_lineno) @aux.epilogue_first_lineno = epilogue_first_lineno end |
#find_rules_by_symbol(sym) ⇒ Object
171 172 173 |
# File 'lib/lrama/grammar.rb', line 171 def find_rules_by_symbol(sym) @sym_to_rules[sym.number] end |
#find_rules_by_symbol!(sym) ⇒ Object
167 168 169 |
# File 'lib/lrama/grammar.rb', line 167 def find_rules_by_symbol!(sym) find_rules_by_symbol(sym) || (raise "Rules for #{sym} not found") end |
#ielr_defined? ⇒ Boolean
175 176 177 |
# File 'lib/lrama/grammar.rb', line 175 def ielr_defined? @define.key?('lr.type') && @define['lr.type'] == 'ielr' end |
#insert_before_parameterizing_rules(rules) ⇒ Object
126 127 128 |
# File 'lib/lrama/grammar.rb', line 126 def insert_before_parameterizing_rules(rules) @parameterizing_rule_resolver.rules = rules + @parameterizing_rule_resolver.rules end |
#parameterizing_rules ⇒ Object
122 123 124 |
# File 'lib/lrama/grammar.rb', line 122 def parameterizing_rules @parameterizing_rule_resolver.rules end |
#prepare ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/lrama/grammar.rb', line 146 def prepare resolve_inline_rules normalize_rules collect_symbols set_lhs_and_rhs fill_default_precedence fill_symbols fill_sym_to_rules compute_nullable compute_first_set set_locations end |
#prologue=(prologue) ⇒ Object
134 135 136 |
# File 'lib/lrama/grammar.rb', line 134 def prologue=(prologue) @aux.prologue = prologue end |
#prologue_first_lineno=(prologue_first_lineno) ⇒ Object
130 131 132 |
# File 'lib/lrama/grammar.rb', line 130 def prologue_first_lineno=(prologue_first_lineno) @aux.prologue_first_lineno = prologue_first_lineno end |
#set_precedence(sym, precedence) ⇒ Object
105 106 107 108 |
# File 'lib/lrama/grammar.rb', line 105 def set_precedence(sym, precedence) raise "" if sym.nterm? sym.precedence = precedence end |
#set_union(code, lineno) ⇒ Object
110 111 112 |
# File 'lib/lrama/grammar.rb', line 110 def set_union(code, lineno) @union = Union.new(code: code, lineno: lineno) end |
#validate! ⇒ Object
TODO: More validation methods
-
Validation for no_declared_type_reference
162 163 164 165 |
# File 'lib/lrama/grammar.rb', line 162 def validate! @symbols_resolver.validate! validate_rule_lhs_is_nterm! end |