Class: Inform::Verb::Grammar

Inherits:
Array show all
Defined in:
lib/runtime/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

Array::Empty

Instance Attribute Summary collapse

Instance Method Summary collapse

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



120
121
122
123
124
125
126
127
128
# File 'lib/runtime/grammar_parser.rb', line 120

def initialize(grammar, source, action, reverse = nil)
  super()
  concat(symbolize_grammar(grammar)) unless grammar.empty?
  push(:end)
  @action = action
  @expected_tokens = grammar.map { |a| a =~ CustomScopePattern ? a.to_s.split('=').first.to_sym : a }
  @reverse = reverse.nil? ? false : reverse
  @source = source
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



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

def action
  @action
end

#expected_tokensObject (readonly)

Returns the value of attribute expected_tokens.



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

def expected_tokens
  @expected_tokens
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

#empty?Boolean



130
131
132
# File 'lib/runtime/grammar_parser.rb', line 130

def empty?
  length == (include?(:end) ? 1 : 0)
end

#expected_parametersObject



161
162
163
# File 'lib/runtime/grammar_parser.rb', line 161

def expected_parameters
  reverse? ? reversed_parameters : parameters
end

#parametersObject



153
154
155
# File 'lib/runtime/grammar_parser.rb', line 153

def parameters
  @parameters ||= []
end

#reverse?Boolean Also known as: reversed?



165
166
167
# File 'lib/runtime/grammar_parser.rb', line 165

def reverse?
  @reverse
end

#reversed_parametersObject



157
158
159
# File 'lib/runtime/grammar_parser.rb', line 157

def reversed_parameters
  @reversed_parameters ||= [parameters[1], parameters[0]] + parameters[2..]
end

#to_sObject Also known as: to_str



141
142
143
144
145
146
147
148
149
150
# File 'lib/runtime/grammar_parser.rb', line 141

def to_s
  Indention + AsteriskString + SpaceString +
    tokens_sans_end.ljust(GrammarTokenPadding) +
    ArrowString + SpaceString + action +
    if reverse?
      SpaceString + ReverseString
    else
      ''
    end
end