Class: LL::CompiledGrammar
- Inherits:
-
Object
- Object
- LL::CompiledGrammar
- Defined in:
- lib/ll/compiled_grammar.rb
Overview
The CompiledGrammar class contains compilation results such as the parser name, the rules of the grammar, the terminals, etc.
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#header ⇒ Object
Returns the value of attribute header.
-
#inner ⇒ Object
Returns the value of attribute inner.
-
#name ⇒ Object
Returns the value of attribute name.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Instance Method Summary collapse
- #add_error(message, source_line) ⇒ Object
- #add_rule(rule) ⇒ LL::Rule
- #add_terminal(name, source_line) ⇒ LL::Terminal
- #add_warning(message, source_line) ⇒ Object
-
#display_messages ⇒ Object
Displays all warnings and errors.
-
#has_rule?(name) ⇒ TrueClass|FalseClass
Returns true if a rule for the given name has already been assigned.
-
#has_rule_with_branches?(name) ⇒ Boolean
Returns true if a rule already exists for a given name and has at least 1 branch.
- #has_terminal?(name) ⇒ TrueClass|FalseClass
-
#initialize ⇒ CompiledGrammar
constructor
A new instance of CompiledGrammar.
-
#lookup_identifier(name) ⇒ LL::Rule|LL::Terminal|NilClass
Looks up an identifier from the list of terminals and/or rules.
- #lookup_rule(name) ⇒ LL::Rule
- #output ⇒ IO
- #rule_indices ⇒ Hash
- #rules ⇒ Array
- #terminal_indices ⇒ Hash
- #terminals ⇒ Array
- #valid? ⇒ TrueClass|FalseClass
Constructor Details
#initialize ⇒ CompiledGrammar
11 12 13 14 15 16 17 18 |
# File 'lib/ll/compiled_grammar.rb', line 11 def initialize @warnings = [] @errors = [] @terminals = {} @rules = {} @inner = nil @header = nil end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
9 10 11 |
# File 'lib/ll/compiled_grammar.rb', line 9 def errors @errors end |
#header ⇒ Object
Returns the value of attribute header.
7 8 9 |
# File 'lib/ll/compiled_grammar.rb', line 7 def header @header end |
#inner ⇒ Object
Returns the value of attribute inner.
7 8 9 |
# File 'lib/ll/compiled_grammar.rb', line 7 def inner @inner end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/ll/compiled_grammar.rb', line 7 def name @name end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
9 10 11 |
# File 'lib/ll/compiled_grammar.rb', line 9 def warnings @warnings end |
Instance Method Details
#add_error(message, source_line) ⇒ Object
24 25 26 |
# File 'lib/ll/compiled_grammar.rb', line 24 def add_error(, source_line) @errors << Message.new(:error, , source_line) end |
#add_rule(rule) ⇒ LL::Rule
77 78 79 |
# File 'lib/ll/compiled_grammar.rb', line 77 def add_rule(rule) return @rules[rule.name] = rule end |
#add_terminal(name, source_line) ⇒ LL::Terminal
49 50 51 |
# File 'lib/ll/compiled_grammar.rb', line 49 def add_terminal(name, source_line) return @terminals[name] = Terminal.new(name, source_line) end |
#add_warning(message, source_line) ⇒ Object
32 33 34 |
# File 'lib/ll/compiled_grammar.rb', line 32 def add_warning(, source_line) @warnings << Message.new(:warning, , source_line) end |
#display_messages ⇒ Object
Displays all warnings and errors.
152 153 154 155 156 157 158 |
# File 'lib/ll/compiled_grammar.rb', line 152 def [:errors, :warnings].each do |type| send(type).each do |msg| output.puts(msg.to_s) end end end |
#has_rule?(name) ⇒ TrueClass|FalseClass
Returns true if a rule for the given name has already been assigned.
59 60 61 |
# File 'lib/ll/compiled_grammar.rb', line 59 def has_rule?(name) return @rules.key?(name) end |
#has_rule_with_branches?(name) ⇒ Boolean
Returns true if a rule already exists for a given name and has at least 1 branch.
69 70 71 |
# File 'lib/ll/compiled_grammar.rb', line 69 def has_rule_with_branches?(name) return has_rule?(name) && !@rules[name].branches.empty? end |
#has_terminal?(name) ⇒ TrueClass|FalseClass
40 41 42 |
# File 'lib/ll/compiled_grammar.rb', line 40 def has_terminal?(name) return @terminals.key?(name) end |
#lookup_identifier(name) ⇒ LL::Rule|LL::Terminal|NilClass
Looks up an identifier from the list of terminals and/or rules. Rules take precedence over terminals.
If no rule/terminal could be found nil is returned instead.
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ll/compiled_grammar.rb', line 98 def lookup_identifier(name) if has_rule?(name) ident = lookup_rule(name) elsif has_terminal?(name) ident = @terminals[name] else ident = nil end return ident end |
#lookup_rule(name) ⇒ LL::Rule
85 86 87 |
# File 'lib/ll/compiled_grammar.rb', line 85 def lookup_rule(name) return @rules[name] end |
#output ⇒ IO
163 164 165 |
# File 'lib/ll/compiled_grammar.rb', line 163 def output return STDERR end |
#rule_indices ⇒ Hash
120 121 122 123 124 |
# File 'lib/ll/compiled_grammar.rb', line 120 def rule_indices return rules.each_with_index.each_with_object({}) do |(rule, idx), h| h[rule] = idx end end |
#rules ⇒ Array
113 114 115 |
# File 'lib/ll/compiled_grammar.rb', line 113 def rules return @rules.values end |
#terminal_indices ⇒ Hash
136 137 138 139 140 |
# File 'lib/ll/compiled_grammar.rb', line 136 def terminal_indices return terminals.each_with_index.each_with_object({}) do |(term, idx), h| h[term] = idx end end |
#terminals ⇒ Array
129 130 131 |
# File 'lib/ll/compiled_grammar.rb', line 129 def terminals return @terminals.values end |
#valid? ⇒ TrueClass|FalseClass
145 146 147 |
# File 'lib/ll/compiled_grammar.rb', line 145 def valid? return @errors.empty? end |