Class: Inform::Verb::Grammar
- Defined in:
- lib/story_teller/grammar_parser.rb
Overview
Grammars
Constant Summary collapse
- MultipleEnquotedPrepositionPattern =
%r{^'(.*)'/'(.*)'+$}.freeze
- SingleEnquotedPrepositionPattern =
%r{^'(.*)'$}.freeze
- CustomScopePattern =
%r{^(.*)=(.*)$}.freeze
- SpaceString =
' '.freeze
- Indention =
SpaceString * 4
- AsteriskString =
'*'.freeze
- ReverseString =
'reverse'.freeze
- ArrowString =
'->'.freeze
- GrammarTokenPadding =
40
Constants inherited from Array
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#expected_tokens ⇒ Object
readonly
Returns the value of attribute expected_tokens.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
rubocop: enable Metrics/MethodLength.
- #expected_parameters ⇒ Object
-
#initialize(grammar, source, action, reverse = nil) ⇒ Grammar
constructor
rubocop: disable Metrics/MethodLength.
- #parameters ⇒ Object
- #reverse? ⇒ Boolean (also: #reversed?)
- #reversed_parameters ⇒ Object
- #to_s ⇒ Object (also: #to_str)
Methods inherited from Array
#+, #average, #having, #identities, #longest_length, #pad, #plus_operator, #ruby_to_s, #sum, #to_sentence
Constructor Details
#initialize(grammar, source, action, reverse = nil) ⇒ Grammar
rubocop: disable Metrics/MethodLength
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/story_teller/grammar_parser.rb', line 127 def initialize(grammar, source, action, reverse = nil) super() unless grammar.empty? symbolized_grammar = symbolize_grammar(grammar) concat(symbolized_grammar) unless symbolized_grammar.empty? end push(:end) @action = action @expected_tokens = symbolized_grammar&.map do |a| CustomScopePattern.match?(a) ? a.to_s.split('=').first.to_sym : a end @reverse = reverse.nil? ? false : reverse @source = source end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
124 125 126 |
# File 'lib/story_teller/grammar_parser.rb', line 124 def action @action end |
#expected_tokens ⇒ Object (readonly)
Returns the value of attribute expected_tokens.
124 125 126 |
# File 'lib/story_teller/grammar_parser.rb', line 124 def expected_tokens @expected_tokens end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
124 125 126 |
# File 'lib/story_teller/grammar_parser.rb', line 124 def source @source end |
Instance Method Details
#empty? ⇒ Boolean
rubocop: enable Metrics/MethodLength
143 144 145 |
# File 'lib/story_teller/grammar_parser.rb', line 143 def empty? length == (include?(:end) ? 1 : 0) end |
#expected_parameters ⇒ Object
174 175 176 |
# File 'lib/story_teller/grammar_parser.rb', line 174 def expected_parameters reverse? ? reversed_parameters : parameters end |
#parameters ⇒ Object
166 167 168 |
# File 'lib/story_teller/grammar_parser.rb', line 166 def parameters @parameters ||= [] end |
#reverse? ⇒ Boolean Also known as: reversed?
178 179 180 |
# File 'lib/story_teller/grammar_parser.rb', line 178 def reverse? @reverse end |
#reversed_parameters ⇒ Object
170 171 172 |
# File 'lib/story_teller/grammar_parser.rb', line 170 def reversed_parameters @reversed_parameters ||= [parameters[1], parameters[0]] + parameters[2..] end |
#to_s ⇒ Object Also known as: to_str
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/story_teller/grammar_parser.rb', line 154 def to_s Indention + AsteriskString + SpaceString + tokens_sans_end.ljust(GrammarTokenPadding) + ArrowString + SpaceString + action + if reverse? SpaceString + ReverseString else '' end end |