Class: BabelBridge::Parser
- Inherits:
-
Object
- Object
- BabelBridge::Parser
- Defined in:
- lib/babel_bridge/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
- .any(*args) ⇒ Object
- .any!(*args) ⇒ Object
- .any?(*args) ⇒ Boolean
-
.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) ⇒ 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_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
184 185 186 187 |
# File 'lib/babel_bridge/parser.rb', line 184 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/babel_bridge/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/babel_bridge/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/babel_bridge/parser.rb', line 13 def root_rule @root_rule end |
.rules ⇒ Object
Returns the value of attribute rules.
13 14 15 |
# File 'lib/babel_bridge/parser.rb', line 13 def rules @rules end |
Instance Attribute Details
#expecting_list ⇒ Object
Returns the value of attribute expecting_list.
173 174 175 |
# File 'lib/babel_bridge/parser.rb', line 173 def expecting_list @expecting_list end |
#failed_parse ⇒ Object
gets set if the entire input was not matched
176 177 178 |
# File 'lib/babel_bridge/parser.rb', line 176 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
172 173 174 |
# File 'lib/babel_bridge/parser.rb', line 172 def failure_index @failure_index end |
#parse_cache ⇒ Object
Returns the value of attribute parse_cache.
175 176 177 |
# File 'lib/babel_bridge/parser.rb', line 175 def parse_cache @parse_cache end |
#source_file ⇒ Object
Returns the value of attribute source_file.
174 175 176 |
# File 'lib/babel_bridge/parser.rb', line 174 def source_file @source_file end |
#src ⇒ Object
Returns the value of attribute src.
174 175 176 |
# File 'lib/babel_bridge/parser.rb', line 174 def src @src end |
Class Method Details
.[](i) ⇒ Object
91 92 93 |
# File 'lib/babel_bridge/parser.rb', line 91 def [](i) rules[i] end |
.any(*args) ⇒ Object
155 |
# File 'lib/babel_bridge/parser.rb', line 155 def any(*args) PatternElementHash.new.any(args) end |
.any!(*args) ⇒ Object
157 |
# File 'lib/babel_bridge/parser.rb', line 157 def any!(*args) PatternElementHash.new.dont.any(args) end |
.any?(*args) ⇒ Boolean
156 |
# File 'lib/babel_bridge/parser.rb', line 156 def any?(*args) PatternElementHash.new.optionally.any(args) 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/babel_bridge/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
161 |
# File 'lib/babel_bridge/parser.rb', line 161 def could; PatternElementHash.new.could end |
.custom_parser(&block) ⇒ Object
162 |
# File 'lib/babel_bridge/parser.rb', line 162 def custom_parser(&block); PatternElementHash.new.parser(lambda &block) end |
.delimiter(*pattern) ⇒ Object
106 107 108 |
# File 'lib/babel_bridge/parser.rb', line 106 def delimiter(*pattern) @delimiter = pattern end |
.dont ⇒ Object
159 |
# File 'lib/babel_bridge/parser.rb', line 159 def dont; PatternElementHash.new.dont end |
.ignore_whitespace ⇒ Object
102 103 104 |
# File 'lib/babel_bridge/parser.rb', line 102 def ignore_whitespace delimiter /\s*/ end |
.many(m, delimiter = nil) ⇒ Object
148 |
# File 'lib/babel_bridge/parser.rb', line 148 def many(m,delimiter=nil) PatternElementHash.new.match.many(m).delimiter(delimiter) end |
.many?(m, delimiter = nil) ⇒ Boolean
149 |
# File 'lib/babel_bridge/parser.rb', line 149 def many?(m,delimiter=nil) PatternElementHash.new.optionally.match.many(m).delimiter(delimiter) end |
.match(*args) ⇒ Object
151 |
# File 'lib/babel_bridge/parser.rb', line 151 def match(*args) PatternElementHash.new.match(*args) end |
.match!(*args) ⇒ Object
153 |
# File 'lib/babel_bridge/parser.rb', line 153 def match!(*args) PatternElementHash.new.dont.match(*args) end |
.match?(*args) ⇒ Boolean
152 |
# File 'lib/babel_bridge/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/babel_bridge/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
160 |
# File 'lib/babel_bridge/parser.rb', line 160 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/babel_bridge/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
202 203 204 |
# File 'lib/babel_bridge/parser.rb', line 202 def cache_match(rule_class,match) (parse_cache[rule_class]||={})[match.offset]=match end |
#cache_no_match(rule_class, offset) ⇒ Object
206 207 208 |
# File 'lib/babel_bridge/parser.rb', line 206 def cache_no_match(rule_class,offset) (parse_cache[rule_class]||={})[offset]=:no_match end |
#cached(rule_class, offset) ⇒ Object
198 199 200 |
# File 'lib/babel_bridge/parser.rb', line 198 def cached(rule_class,offset) (parse_cache[rule_class]||={})[offset] end |
#delimiter_pattern ⇒ Object
115 116 117 |
# File 'lib/babel_bridge/parser.rb', line 115 def delimiter_pattern self.class.delimiter_pattern end |
#expecting_output(options = {}) ⇒ Object
options => false
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/babel_bridge/parser.rb', line 293 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]}\t #{list}" end.sort.join("\n")," "))} ENDTXT end |
#log_parsing_failure(index, expecting) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/babel_bridge/parser.rb', line 210 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
223 224 225 226 |
# File 'lib/babel_bridge/parser.rb', line 223 def matching_negative @matching_negative_depth||=0 @matching_negative_depth+=1 end |
#matching_negative? ⇒ Boolean
232 233 234 |
# File 'lib/babel_bridge/parser.rb', line 232 def matching_negative? (@matching_negative_depth||0) > 0 end |
#node_list_string(node_list, common_root = [], options = {}) ⇒ Object
options => false
271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/babel_bridge/parser.rb', line 271 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
284 285 286 287 288 289 |
# File 'lib/babel_bridge/parser.rb', line 284 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:
rule: lets you specify the root rule for matching
partial_match: allow partial matching
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/babel_bridge/parser.rb', line 242 def parse(src, ={}) 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_time ⇒ Object
266 267 268 |
# File 'lib/babel_bridge/parser.rb', line 266 def parse_time @end_time-@start_time end |
#parser_failure_info(options = {}) ⇒ Object
option: :verbose => true
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/babel_bridge/parser.rb', line 325 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
178 179 180 |
# File 'lib/babel_bridge/parser.rb', line 178 def relative_source_file source_file && @relative_source_file ||= Pathname.new(File.(source_file)).relative_path_from(Pathname.getwd) end |
#reset_parser_tracking ⇒ Object
189 190 191 192 193 194 195 196 |
# File 'lib/babel_bridge/parser.rb', line 189 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/babel_bridge/parser.rb', line 119 def rules self.class.rules end |
#unmatching_negative ⇒ Object
228 229 230 |
# File 'lib/babel_bridge/parser.rb', line 228 def unmatching_negative @matching_negative_depth-=1 end |