Class: Parser
- Inherits:
-
Object
- Object
- Parser
- Defined in:
- lib/rdparse.rb
Defined Under Namespace
Classes: LexToken, ParseError
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#pos ⇒ Object
Returns the value of attribute pos.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Instance Method Summary collapse
-
#expect(tok) ⇒ Object
Return the next token in the queue.
-
#initialize(language_name, &block) ⇒ Parser
constructor
A new instance of Parser.
- #next_token ⇒ Object
- #parse(string) ⇒ Object
- #to_s ⇒ Object
-
#tokenize(string) ⇒ Object
Tokenize the string into small pieces.
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
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
125 126 127 |
# File 'lib/rdparse.rb', line 125 def logger @logger end |
#pos ⇒ Object
Returns the value of attribute pos.
124 125 126 |
# File 'lib/rdparse.rb', line 124 def pos @pos end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
125 126 127 |
# File 'lib/rdparse.rb', line 125 def rules @rules end |
#string ⇒ Object (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_token ⇒ Object
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_s ⇒ Object
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 |