Class: Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rdparse.rb

Defined Under Namespace

Classes: LexToken, ParseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language_name, &block) ⇒ Parser

Returns a new instance of Parser.



130
131
132
133
134
135
136
137
# File 'lib/rdparse.rb', line 130

def initialize(language_name, &block)
  @logger = Logger.new(STDOUT)
  @lex_tokens = []
  @rules = {}
  @start = nil
  @language_name = language_name
  instance_eval(&block)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



125
126
127
# File 'lib/rdparse.rb', line 125

def logger
  @logger
end

#posObject

Returns the value of attribute pos.



124
125
126
# File 'lib/rdparse.rb', line 124

def pos
  @pos
end

#rulesObject (readonly)

Returns the value of attribute rules.



125
126
127
# File 'lib/rdparse.rb', line 125

def rules
  @rules
end

#stringObject (readonly)

Returns the value of attribute string.



125
126
127
# File 'lib/rdparse.rb', line 125

def string
  @string
end

Instance Method Details

#expect(tok) ⇒ Object

Return the next token in the queue



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rdparse.rb', line 190

def expect(tok)
  return tok if tok == :empty
  t = next_token
  if @pos - 1 > @max_pos
    @max_pos = @pos - 1
    @expected = []
  end
  return t if tok === t
  @expected << tok if @max_pos == @pos - 1 && !@expected.include?(tok)
  return nil
end

#next_tokenObject



184
185
186
187
# File 'lib/rdparse.rb', line 184

def next_token
  @pos += 1
  return @tokens[@pos - 1]
end

#parse(string) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rdparse.rb', line 165

def parse(string)
  # First, split the string according to the "token" instructions given.
  # Afterwards @tokens contains all tokens that are to be parsed. 
  tokenize(string)

  # These variables are used to match if the total number of tokens
  # are consumed by the parser
  @pos = 0
  @max_pos = 0
  @expected = []
  # Parse (and evaluate) the tokens received
  result = @start.parse
  # If there are unparsed extra tokens, signal error
  if @pos != @tokens.size
    raise ParseError, "Parse error. expected: '#{@expected.join(', ')}', found '#{@tokens[@max_pos]}'"
  end
  return result
end

#to_sObject



202
203
204
# File 'lib/rdparse.rb', line 202

def to_s
  "Parser for #{@language_name}"
end

#tokenize(string) ⇒ Object

Tokenize the string into small pieces



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rdparse.rb', line 140

def tokenize(string)
  @tokens = []
  @string = string.clone
  until string.empty?
    # Unless any of the valid tokens of our language are the prefix of
    # 'string', we fail with an exception
    raise ParseError, "unable to lex '#{string}" unless @lex_tokens.any? do |tok|
      match = tok.pattern.match(string)
      # The regular expression of a token has matched the beginning of 'string'
      if match
        @logger.debug("Token #{match[0]} consumed")
        # Also, evaluate this expression by using the block
        # associated with the token
        @tokens << tok.block.call(match.to_s) if tok.block
        # consume the match and proceed with the rest of the string
        string = match.post_match
        true
      else
        # this token pattern did not match, try the next
        false
      end # if
    end # raise
  end # until
end