Class: Babelfish::Phrase::ParserBase

Inherits:
Object
  • Object
show all
Defined in:
lib/babelfish/phrase/parser_base.rb

Overview

Babelfish abstract parser.

Direct Known Subclasses

Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#escapeObject

Returns the value of attribute escape.



6
7
8
# File 'lib/babelfish/phrase/parser_base.rb', line 6

def escape
  @escape
end

#indexObject

Returns the value of attribute index.



6
7
8
# File 'lib/babelfish/phrase/parser_base.rb', line 6

def index
  @index
end

#lengthObject

Returns the value of attribute length.



6
7
8
# File 'lib/babelfish/phrase/parser_base.rb', line 6

def length
  @length
end

#phraseObject

Returns the value of attribute phrase.



6
7
8
# File 'lib/babelfish/phrase/parser_base.rb', line 6

def phrase
  @phrase
end

#pieceObject

Returns the value of attribute piece.



6
7
8
# File 'lib/babelfish/phrase/parser_base.rb', line 6

def piece
  @piece
end

#prevObject

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

#backwardObject

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

#charObject

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_charObject

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( message )
    raise "Cannot parse phrase \""+ ( phrase || 'nil' )+ "\" at ". ( index || '-1' )+ " index: #{message}"
end

#to_next_charObject

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