Class: Babelfish::Phrase::ParserBase
- Inherits:
-
Object
- Object
- Babelfish::Phrase::ParserBase
- Defined in:
- lib/babelfish/phrase/parser_base.rb
Overview
Babelfish abstract parser.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#escape ⇒ Object
Returns the value of attribute escape.
-
#index ⇒ Object
Returns the value of attribute index.
-
#length ⇒ Object
Returns the value of attribute length.
-
#phrase ⇒ Object
Returns the value of attribute phrase.
-
#piece ⇒ Object
Returns the value of attribute piece.
-
#prev ⇒ Object
Returns the value of attribute prev.
Instance Method Summary collapse
-
#add_to_piece(chars) ⇒ Object
Adds given chars to current piece.
-
#backward ⇒ Object
Moves cursor backward.
-
#char ⇒ Object
Gets character on current cursor position.
- #init(phrase) ⇒ Object
-
#initialize(phrase = nil) ⇒ ParserBase
constructor
A new instance of ParserBase.
-
#next_char ⇒ Object
Gets character on next cursor position.
-
#parse(phrase = nil) ⇒ Object
Parses specified phrase.
-
#throw(message) ⇒ Object
Throws given message in phrase context.
-
#to_next_char ⇒ Object
Moves cursor to next position.
Constructor Details
#initialize(phrase = nil) ⇒ ParserBase
Returns a new instance of ParserBase.
9 10 11 |
# File 'lib/babelfish/phrase/parser_base.rb', line 9 def initialize(phrase = nil) init(phrase) if phrase end |
Instance Attribute Details
#escape ⇒ Object
Returns the value of attribute escape.
6 7 8 |
# File 'lib/babelfish/phrase/parser_base.rb', line 6 def escape @escape end |
#index ⇒ Object
Returns the value of attribute index.
6 7 8 |
# File 'lib/babelfish/phrase/parser_base.rb', line 6 def index @index end |
#length ⇒ Object
Returns the value of attribute length.
6 7 8 |
# File 'lib/babelfish/phrase/parser_base.rb', line 6 def length @length end |
#phrase ⇒ Object
Returns the value of attribute phrase.
6 7 8 |
# File 'lib/babelfish/phrase/parser_base.rb', line 6 def phrase @phrase end |
#piece ⇒ Object
Returns the value of attribute piece.
6 7 8 |
# File 'lib/babelfish/phrase/parser_base.rb', line 6 def piece @piece end |
#prev ⇒ Object
Returns the value of attribute prev.
6 7 8 |
# File 'lib/babelfish/phrase/parser_base.rb', line 6 def prev @prev end |
Instance Method Details
#add_to_piece(chars) ⇒ Object
Adds given chars to current piece.
52 53 54 |
# File 'lib/babelfish/phrase/parser_base.rb', line 52 def add_to_piece(chars) self.piece += chars end |
#backward ⇒ Object
Moves cursor backward.
57 58 59 60 61 62 63 |
# File 'lib/babelfish/phrase/parser_base.rb', line 57 def backward self.index = index - 1 if index > 0 r = phrase[ index - 1 ] self.prev = r.nil? ? '' : r end end |
#char ⇒ Object
Gets character on current cursor position. Will return empty string if no character.
24 25 26 27 |
# File 'lib/babelfish/phrase/parser_base.rb', line 24 def char r = phrase[ index ] r.nil? ? '' : r end |
#init(phrase) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/babelfish/phrase/parser_base.rb', line 13 def init(phrase) self.phrase = phrase self.index = -1 self.prev = nil self.length = phrase.length self.piece = '' self.escape = false end |
#next_char ⇒ Object
Gets character on next cursor position. Will return empty string if no character.
31 32 33 34 35 |
# File 'lib/babelfish/phrase/parser_base.rb', line 31 def next_char return '' if index >= length - 1 r = phrase[ index + 1 ] r.nil? ? '' : r end |
#parse(phrase = nil) ⇒ Object
Parses specified phrase.
66 67 68 69 70 |
# File 'lib/babelfish/phrase/parser_base.rb', line 66 def parse( phrase = nil ) init(phrase) unless phrase.nil? throw( "No phrase given" ) if phrase.nil? phrase end |
#throw(message) ⇒ Object
Throws given message in phrase context.
47 48 49 |
# File 'lib/babelfish/phrase/parser_base.rb', line 47 def throw( ) raise "Cannot parse phrase \""+ ( phrase || 'nil' )+ "\" at ". ( index || '-1' )+ " index: #{}" end |
#to_next_char ⇒ Object
Moves cursor to next position. Return new current character.
39 40 41 42 43 44 |
# File 'lib/babelfish/phrase/parser_base.rb', line 39 def to_next_char self.prev = char if self.index >= 0 self.index = self.index + 1 return '' if self.index == length char() end |