Class: BabelBridge::Parser
- Inherits:
-
Object
- Object
- BabelBridge::Parser
- Defined in:
- lib/parser.rb
Overview
Used to parse with .parse
Class Attribute Summary collapse
-
.delimiter_pattern ⇒ Object
Returns the value of attribute delimiter_pattern.
-
.module_name ⇒ Object
Returns the value of attribute module_name.
-
.root_rule ⇒ Object
Returns the value of attribute root_rule.
-
.rules ⇒ Object
Returns the value of attribute rules.
Instance Attribute Summary collapse
-
#expecting_list ⇒ Object
Returns the value of attribute expecting_list.
-
#failed_parse ⇒ Object
gets set if the entire input was not matched.
-
#failure_index ⇒ Object
********************************************* ********************************************* parser instance implementation this methods are used for each actual parse run they are tied to an instnace of the Parser Sub-class to you can have more than one parser active at a time.
-
#parse_cache ⇒ Object
Returns the value of attribute parse_cache.
-
#source_file ⇒ Object
Returns the value of attribute source_file.
-
#src ⇒ Object
Returns the value of attribute src.
Class Method Summary collapse
- .[](i) ⇒ Object
-
.binary_operators_rule(name, operand_rule_name, operators, options = {}, &block) ⇒ Object
options => right_operators: list of all operators that should be evaluated right to left instead of left-to-write typical example is the “**” exponentiation operator which should be evaluated right-to-left.
- .could ⇒ Object
- .custom_parser(&block) ⇒ Object
- .delimiter(*pattern) ⇒ Object
- .dont ⇒ Object
- .ignore_whitespace ⇒ Object
- .many(m, delimiter = nil) ⇒ Object
- .many!(m, delimiter = nil) ⇒ Object
- .many?(m, delimiter = nil) ⇒ Boolean
- .match(*args) ⇒ Object
- .match!(*args) ⇒ Object
- .match?(*args) ⇒ Boolean
- .node_class(name, &block) ⇒ Object
- .optionally ⇒ Object
-
.rule(name, *pattern, &block) ⇒ Object
Add a rule to the parser.
Instance Method Summary collapse
- #cache_match(rule_class, match) ⇒ Object
- #cache_no_match(rule_class, offset) ⇒ Object
- #cached(rule_class, offset) ⇒ Object
- #delimiter_pattern ⇒ Object
-
#expecting_output(options = {}) ⇒ Object
options => false.
-
#initialize(options = {}) ⇒ Parser
constructor
options: :source_file => string.
- #log_parsing_failure(index, expecting) ⇒ Object
- #matching_negative ⇒ Object
- #matching_negative? ⇒ Boolean
-
#node_list_string(node_list, common_root = [], options = {}) ⇒ Object
options => false.
- #nodes_interesting_parse_path(node) ⇒ Object
-
#parse(src, options = {}) ⇒ Object
parse a string, return the root node of the parse tree.
- #parse_and_puts_errors(src, out = $stdout) ⇒ Object
- #parse_time ⇒ Object
-
#parser_failure_info(options = {}) ⇒ Object
option: :verbose => true.
- #relative_source_file ⇒ Object
- #reset_parser_tracking ⇒ Object
- #rules ⇒ Object
- #unmatching_negative ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Parser
options:
:source_file => string
181 182 183 184 |
# File 'lib/parser.rb', line 181 def initialize(={}) @source_file = [:source_file] reset_parser_tracking end |
Class Attribute Details
.delimiter_pattern ⇒ Object
Returns the value of attribute delimiter_pattern.
13 14 15 |
# File 'lib/parser.rb', line 13 def delimiter_pattern @delimiter_pattern end |
.module_name ⇒ Object
Returns the value of attribute module_name.
13 14 15 |
# File 'lib/parser.rb', line 13 def module_name @module_name end |
.root_rule ⇒ Object
Returns the value of attribute root_rule.
13 14 15 |
# File 'lib/parser.rb', line 13 def root_rule @root_rule end |
.rules ⇒ Object
Returns the value of attribute rules.
13 14 15 |
# File 'lib/parser.rb', line 13 def rules @rules end |
Instance Attribute Details
#expecting_list ⇒ Object
Returns the value of attribute expecting_list.
170 171 172 |
# File 'lib/parser.rb', line 170 def expecting_list @expecting_list end |
#failed_parse ⇒ Object
gets set if the entire input was not matched
173 174 175 |
# File 'lib/parser.rb', line 173 def failed_parse @failed_parse end |
#failure_index ⇒ Object
********************************************* ********************************************* parser instance implementation this methods are used for each actual parse run they are tied to an instnace of the Parser Sub-class to you can have more than one parser active at a time
169 170 171 |
# File 'lib/parser.rb', line 169 def failure_index @failure_index end |
#parse_cache ⇒ Object
Returns the value of attribute parse_cache.
172 173 174 |
# File 'lib/parser.rb', line 172 def parse_cache @parse_cache end |
#source_file ⇒ Object
Returns the value of attribute source_file.
171 172 173 |
# File 'lib/parser.rb', line 171 def source_file @source_file end |
#src ⇒ Object
Returns the value of attribute src.
171 172 173 |
# File 'lib/parser.rb', line 171 def src @src end |
Class Method Details
.[](i) ⇒ Object
91 92 93 |
# File 'lib/parser.rb', line 91 def [](i) rules[i] end |
.binary_operators_rule(name, operand_rule_name, operators, options = {}, &block) ⇒ Object
options
> right_operators: list of all operators that should be evaluated right to left instead of left-to-write
typical example is the "**" exponentiation operator which should be evaluated right-to-left.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/parser.rb', line 53 def binary_operators_rule(name,operand_rule_name,operators,={},&block) right_operators = [:right_operators] rule name, many(operand_rule_name,Tools::array_to_or_regexp(operators)).delimiter_name(:operators).as(:operands) do self.class_eval &block if block class <<self attr_accessor :operators_from_rule, :right_operators def operator_processor @operator_processor||=BinaryOperatorProcessor.new(operators_from_rule,self,right_operators) end end self.right_operators = right_operators self.operators_from_rule = operators def operator @operator||=operator_node.to_s.to_sym end def operator_processor self.class.operator_processor end # Override the on_post_match method to take the results of the "many" match # and restructure it into a binary tree of nodes based on the precidence of # the "operators". # TODO - Should on_post_match be run after the whole tree matches? If not, will this screw up caching? def post_match_processing super operator_processor.generate_tree operands, operators, parent end end end |
.could ⇒ Object
158 |
# File 'lib/parser.rb', line 158 def could; PatternElementHash.new.could end |
.custom_parser(&block) ⇒ Object
159 |
# File 'lib/parser.rb', line 159 def custom_parser(&block); PatternElementHash.new.parser(lambda &block) end |
.delimiter(*pattern) ⇒ Object
106 107 108 |
# File 'lib/parser.rb', line 106 def delimiter(*pattern) @delimiter = pattern end |
.dont ⇒ Object
156 |
# File 'lib/parser.rb', line 156 def dont; PatternElementHash.new.dont end |
.ignore_whitespace ⇒ Object
102 103 104 |
# File 'lib/parser.rb', line 102 def ignore_whitespace delimiter /\s*/ end |
.many(m, delimiter = nil) ⇒ Object
148 |
# File 'lib/parser.rb', line 148 def many(m,delimiter=nil) PatternElementHash.new.match.many(m).delimiter(delimiter) end |
.many!(m, delimiter = nil) ⇒ Object
150 |
# File 'lib/parser.rb', line 150 def many!(m,delimiter=nil) PatternElementHash.new.dont.match.many(m).delimiter(delimiter) end |
.many?(m, delimiter = nil) ⇒ Boolean
149 |
# File 'lib/parser.rb', line 149 def many?(m,delimiter=nil) PatternElementHash.new.optionally.match.many(m).delimiter(delimiter) end |
.match(*args) ⇒ Object
153 |
# File 'lib/parser.rb', line 153 def match(*args) PatternElementHash.new.match(*args) end |
.match!(*args) ⇒ Object
154 |
# File 'lib/parser.rb', line 154 def match!(*args) PatternElementHash.new.dont.match(*args) end |
.match?(*args) ⇒ Boolean
152 |
# File 'lib/parser.rb', line 152 def match?(*args) PatternElementHash.new.optionally.match(*args) end |
.node_class(name, &block) ⇒ Object
85 86 87 88 89 |
# File 'lib/parser.rb', line 85 def node_class(name,&block) klass=self.rules[name].node_class return klass unless block klass.class_eval &block end |
.optionally ⇒ Object
157 |
# File 'lib/parser.rb', line 157 def optionally; PatternElementHash.new.optionally end |
.rule(name, *pattern, &block) ⇒ Object
Add a rule to the parser
rules can be specified as:
rule :name, to_match1, to_match2, etc...
Can define rules INSIDE class:
class MyParser < BabelBridge::Parser
rule :name, to_match1, to_match2, etc...
end
Or can define rules OUTSIDE class:
class MyParser < BabelBridge::Parser
end
MyParser.rule :name, to_match1, to_match2, etc...
The first rule added is the root-rule for the parser. You can override by:
class MyParser < BabelBridge::Parser
root_rule = :new_root_rool
end
The block is executed in the context of the rule-varient’s node type, a subclass of: RuleNode This allows you to add whatever functionality you want to a your nodes in the final parse tree. Also note you can override the on_post_match method. This allows you to restructure the parse tree as it is parsed.
43 44 45 46 47 48 |
# File 'lib/parser.rb', line 43 def rule(name,*pattern,&block) rule = self.rules[name] ||= Rule.new(name,self) @root_rule ||= name = pattern[-1].kind_of?(Hash) ? pattern.pop : {} rule.add_variant .merge(:pattern => pattern), &block end |
Instance Method Details
#cache_match(rule_class, match) ⇒ Object
199 200 201 |
# File 'lib/parser.rb', line 199 def cache_match(rule_class,match) (parse_cache[rule_class]||={})[match.offset]=match end |
#cache_no_match(rule_class, offset) ⇒ Object
203 204 205 |
# File 'lib/parser.rb', line 203 def cache_no_match(rule_class,offset) (parse_cache[rule_class]||={})[offset]=:no_match end |
#cached(rule_class, offset) ⇒ Object
195 196 197 |
# File 'lib/parser.rb', line 195 def cached(rule_class,offset) (parse_cache[rule_class]||={})[offset] end |
#delimiter_pattern ⇒ Object
115 116 117 |
# File 'lib/parser.rb', line 115 def delimiter_pattern self.class.delimiter_pattern end |
#expecting_output(options = {}) ⇒ Object
options => false
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/parser.rb', line 300 def expecting_output(={}) return "" if expecting_list.length==0 common_root=nil expecting_list.values.each do |e| node=e[:node] pl=nodes_interesting_parse_path(node) pl.pop # ignore the node itself if common_root common_root.each_index do |i| if pl[i]!=common_root[i] common_root=common_root[0..i-1] break end end else common_root=pl end end <<ENDTXT Parse path at failure: #{node_list_string(common_root,[],:verbose=>true)} Expecting#{expecting_list.length>1 ? ' one of' : ''}: #{Tools.uniform_tabs(Tools.indent(expecting_list.values.collect do |a| list=node_list_string(nodes_interesting_parse_path(a[:node]),common_root,) "#{a[:pattern].inspect}\t #{list}" end.sort.join("\n")," "))} ENDTXT end |
#log_parsing_failure(index, expecting) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/parser.rb', line 207 def log_parsing_failure(index,expecting) if matching_negative? # ignored elsif index>failure_index @expecting_list = {expecting[:pattern] => expecting} @failure_index = index elsif index == failure_index @expecting_list[expecting[:pattern]] = expecting else # ignored end end |
#matching_negative ⇒ Object
220 221 222 223 |
# File 'lib/parser.rb', line 220 def matching_negative @matching_negative_depth||=0 @matching_negative_depth+=1 end |
#matching_negative? ⇒ Boolean
229 230 231 |
# File 'lib/parser.rb', line 229 def matching_negative? (@matching_negative_depth||0) > 0 end |
#node_list_string(node_list, common_root = [], options = {}) ⇒ Object
options => false
278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/parser.rb', line 278 def node_list_string(node_list,common_root=[],={}) return unless node_list if [:verbose] node_list[common_root.length..-1] else [node_list[-1]] end.select do |p| p.class.to_s.index(BabelBridge.to_s)!=0 end.map do |p| "#{p.relative_class_name}" end.join(" > ") end |
#nodes_interesting_parse_path(node) ⇒ Object
291 292 293 294 295 296 |
# File 'lib/parser.rb', line 291 def nodes_interesting_parse_path(node) path = node.parent_list path << node path.pop while path[-1] && !path[-1].kind_of?(RuleNode) path end |
#parse(src, options = {}) ⇒ Object
parse a string, return the root node of the parse tree. If nil is returned, parsing failed. Call .parser_failure_info after failure for a human-readable description of the failure. src: the string to parse options:
offset: where to start in the string for parsing
rule: lets you specify the root rule for matching
partial_match: allow partial matching
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/parser.rb', line 240 def parse(src, ={}) offset = [:offset] || 0 rule = [:rule] || self.class.root_rule reset_parser_tracking @start_time = Time.now self.src = src root_node = RootNode.new(self) raise "No root rule defined." unless rule ret = rules[rule].parse(root_node) if ret if ret.next<src.length && ![:partial_match] # parse only succeeds if the whole input is matched if ret.next >= @failure_index @parsing_did_not_match_entire_input=true @failure_index = ret.next @failed_parse = ret end ret=nil else reset_parser_tracking end end @end_time=Time.now ret end |
#parse_and_puts_errors(src, out = $stdout) ⇒ Object
269 270 271 272 273 274 275 |
# File 'lib/parser.rb', line 269 def parse_and_puts_errors(src,out=$stdout) ret=parse(src) unless ret out.puts parser_failure_info end ret end |
#parse_time ⇒ Object
265 266 267 |
# File 'lib/parser.rb', line 265 def parse_time @end_time-@start_time end |
#parser_failure_info(options = {}) ⇒ Object
option: :verbose => true
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/parser.rb', line 332 def parser_failure_info(={}) return unless src verbose = [:verbose] bracketing_lines=5 line,col = Tools.line_column(src, failure_index) ret=<<-ENDTXT Parsing error at line #{line} column #{col} offset #{failure_index} Source: ... #{(failure_index==0 ? "" : src[0..(failure_index-1)]).last_lines(bracketing_lines)}<HERE>#{src[(failure_index)..-1].first_lines(bracketing_lines)} ... ENDTXT if @parsing_did_not_match_entire_input ret+="\nParser did not match entire input.\n" if verbose ret+="\nParsed:\n#{Tools::indent failed_parse.inspect||"(nothing)"}\n" end end ret+expecting_output end |
#relative_source_file ⇒ Object
175 176 177 |
# File 'lib/parser.rb', line 175 def relative_source_file source_file && @relative_source_file ||= Pathname.new(File.(source_file)).relative_path_from(Pathname.getwd) end |
#reset_parser_tracking ⇒ Object
186 187 188 189 190 191 192 193 |
# File 'lib/parser.rb', line 186 def reset_parser_tracking @matching_negative_depth = 0 @parsing_did_not_match_entire_input = false @src = nil @failure_index = 0 @expecting_list = {} @parse_cache = {} end |
#rules ⇒ Object
119 120 121 |
# File 'lib/parser.rb', line 119 def rules self.class.rules end |
#unmatching_negative ⇒ Object
225 226 227 |
# File 'lib/parser.rb', line 225 def unmatching_negative @matching_negative_depth-=1 end |