brakeman 4.10.0 → 5.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (197) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +46 -0
  3. data/README.md +11 -2
  4. data/bundle/load.rb +5 -3
  5. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/CHANGELOG.md +16 -0
  6. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/FAQ.md +0 -0
  7. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/Gemfile +1 -4
  8. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/MIT-LICENSE +0 -0
  9. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/README.md +2 -3
  10. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/REFERENCE.md +29 -7
  11. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/TODO +0 -0
  12. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/haml.gemspec +2 -1
  13. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml.rb +0 -0
  14. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/attribute_builder.rb +3 -3
  15. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/attribute_compiler.rb +42 -31
  16. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/attribute_parser.rb +0 -0
  17. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/buffer.rb +0 -0
  18. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/compiler.rb +0 -0
  19. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/engine.rb +0 -0
  20. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/error.rb +0 -0
  21. data/bundle/ruby/2.7.0/gems/haml-5.2.1/lib/haml/escapable.rb +77 -0
  22. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/exec.rb +0 -0
  23. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/filters.rb +0 -0
  24. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/generator.rb +0 -0
  25. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/helpers.rb +7 -1
  26. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/helpers/action_view_extensions.rb +0 -0
  27. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/helpers/action_view_mods.rb +0 -0
  28. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/helpers/action_view_xss_mods.rb +0 -0
  29. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/helpers/safe_erubi_template.rb +0 -0
  30. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/helpers/safe_erubis_template.rb +0 -0
  31. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/helpers/xss_mods.rb +6 -3
  32. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/options.rb +0 -0
  33. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/parser.rb +32 -4
  34. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/plugin.rb +0 -0
  35. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/railtie.rb +0 -0
  36. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/sass_rails_filter.rb +0 -0
  37. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/template.rb +0 -0
  38. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/template/options.rb +0 -0
  39. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/temple_engine.rb +0 -0
  40. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/temple_line_counter.rb +0 -0
  41. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/util.rb +1 -1
  42. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/lib/haml/version.rb +1 -1
  43. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/yard/default/fulldoc/html/css/common.sass +0 -0
  44. data/bundle/ruby/2.7.0/gems/{haml-5.1.2 → haml-5.2.1}/yard/default/layout/html/footer.erb +0 -0
  45. data/bundle/ruby/2.7.0/gems/parallel-1.20.1/MIT-LICENSE.txt +20 -0
  46. data/bundle/ruby/2.7.0/gems/parallel-1.20.1/lib/parallel.rb +523 -0
  47. data/bundle/ruby/2.7.0/gems/parallel-1.20.1/lib/parallel/processor_count.rb +42 -0
  48. data/bundle/ruby/2.7.0/gems/parallel-1.20.1/lib/parallel/version.rb +3 -0
  49. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/LICENSE.txt +22 -0
  50. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/NEWS.md +178 -0
  51. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/README.md +48 -0
  52. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml.rb +3 -0
  53. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/attlistdecl.rb +63 -0
  54. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/attribute.rb +205 -0
  55. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/cdata.rb +68 -0
  56. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/child.rb +97 -0
  57. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/comment.rb +80 -0
  58. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/doctype.rb +311 -0
  59. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/document.rb +451 -0
  60. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/dtd/attlistdecl.rb +11 -0
  61. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/dtd/dtd.rb +47 -0
  62. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/dtd/elementdecl.rb +18 -0
  63. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/dtd/entitydecl.rb +57 -0
  64. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/dtd/notationdecl.rb +40 -0
  65. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/element.rb +2599 -0
  66. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/encoding.rb +51 -0
  67. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/entity.rb +171 -0
  68. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/formatters/default.rb +116 -0
  69. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/formatters/pretty.rb +142 -0
  70. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/formatters/transitive.rb +58 -0
  71. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/functions.rb +447 -0
  72. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/instruction.rb +79 -0
  73. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/light/node.rb +188 -0
  74. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/namespace.rb +59 -0
  75. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/node.rb +76 -0
  76. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/output.rb +30 -0
  77. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parent.rb +166 -0
  78. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parseexception.rb +52 -0
  79. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/baseparser.rb +694 -0
  80. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/lightparser.rb +59 -0
  81. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/pullparser.rb +197 -0
  82. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/sax2parser.rb +273 -0
  83. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/streamparser.rb +61 -0
  84. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/treeparser.rb +101 -0
  85. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/ultralightparser.rb +57 -0
  86. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/parsers/xpathparser.rb +689 -0
  87. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/quickpath.rb +266 -0
  88. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/rexml.rb +37 -0
  89. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/sax2listener.rb +98 -0
  90. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/security.rb +28 -0
  91. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/source.rb +298 -0
  92. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/streamlistener.rb +93 -0
  93. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/text.rb +424 -0
  94. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/undefinednamespaceexception.rb +9 -0
  95. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/validation/relaxng.rb +539 -0
  96. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/validation/validation.rb +144 -0
  97. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/validation/validationexception.rb +10 -0
  98. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/xmldecl.rb +130 -0
  99. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/xmltokens.rb +85 -0
  100. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/xpath.rb +81 -0
  101. data/bundle/ruby/2.7.0/gems/rexml-3.2.5/lib/rexml/xpath_parser.rb +974 -0
  102. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/History.rdoc +25 -0
  103. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/Manifest.txt +2 -0
  104. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/README.rdoc +0 -0
  105. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/compare/normalize.rb +2 -2
  106. data/bundle/ruby/2.7.0/gems/ruby_parser-3.16.0/debugging.md +190 -0
  107. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/rp_extensions.rb +0 -0
  108. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/rp_stringscanner.rb +0 -0
  109. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby20_parser.rb +2392 -2384
  110. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby20_parser.y +6 -1
  111. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby21_parser.rb +2553 -2550
  112. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby21_parser.y +6 -1
  113. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby22_parser.rb +2491 -2471
  114. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby22_parser.y +6 -1
  115. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby23_parser.rb +2422 -2403
  116. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby23_parser.y +6 -1
  117. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby24_parser.rb +2460 -2450
  118. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby24_parser.y +6 -1
  119. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby25_parser.rb +2450 -2441
  120. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby25_parser.y +6 -1
  121. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby26_parser.rb +2444 -2433
  122. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby26_parser.y +7 -1
  123. data/bundle/ruby/2.7.0/gems/ruby_parser-3.16.0/lib/ruby27_parser.rb +7310 -0
  124. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby27_parser.y +21 -1
  125. data/bundle/ruby/2.7.0/gems/ruby_parser-3.16.0/lib/ruby30_parser.rb +7310 -0
  126. data/bundle/ruby/2.7.0/gems/ruby_parser-3.16.0/lib/ruby30_parser.y +2677 -0
  127. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby_lexer.rb +19 -0
  128. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby_lexer.rex +1 -1
  129. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby_lexer.rex.rb +1 -1
  130. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby_parser.rb +2 -0
  131. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby_parser.yy +27 -1
  132. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/lib/ruby_parser_extras.rb +2 -2
  133. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/tools/munge.rb +2 -2
  134. data/bundle/ruby/2.7.0/gems/{ruby_parser-3.15.0 → ruby_parser-3.16.0}/tools/ripper.rb +0 -0
  135. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/History.rdoc +12 -0
  136. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/Manifest.txt +0 -0
  137. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/README.rdoc +0 -0
  138. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/lib/composite_sexp_processor.rb +0 -0
  139. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/lib/pt_testcase.rb +2 -2
  140. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/lib/sexp.rb +0 -0
  141. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/lib/sexp_matcher.rb +0 -0
  142. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/lib/sexp_processor.rb +1 -1
  143. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/lib/strict_sexp.rb +0 -0
  144. data/bundle/ruby/2.7.0/gems/{sexp_processor-4.15.1 → sexp_processor-4.15.3}/lib/unique.rb +0 -0
  145. data/lib/brakeman.rb +21 -4
  146. data/lib/brakeman/app_tree.rb +36 -3
  147. data/lib/brakeman/checks/base_check.rb +7 -1
  148. data/lib/brakeman/checks/check_detailed_exceptions.rb +1 -1
  149. data/lib/brakeman/checks/check_evaluation.rb +1 -1
  150. data/lib/brakeman/checks/check_execute.rb +2 -1
  151. data/lib/brakeman/checks/check_mass_assignment.rb +4 -6
  152. data/lib/brakeman/checks/check_regex_dos.rb +1 -1
  153. data/lib/brakeman/checks/check_sanitize_methods.rb +2 -1
  154. data/lib/brakeman/checks/check_sql.rb +16 -3
  155. data/lib/brakeman/checks/check_unsafe_reflection_methods.rb +68 -0
  156. data/lib/brakeman/checks/check_verb_confusion.rb +75 -0
  157. data/lib/brakeman/file_parser.rb +50 -22
  158. data/lib/brakeman/options.rb +5 -1
  159. data/lib/brakeman/parsers/template_parser.rb +26 -3
  160. data/lib/brakeman/processors/alias_processor.rb +91 -19
  161. data/lib/brakeman/processors/base_processor.rb +4 -4
  162. data/lib/brakeman/processors/controller_alias_processor.rb +6 -43
  163. data/lib/brakeman/processors/controller_processor.rb +1 -1
  164. data/lib/brakeman/processors/haml_template_processor.rb +8 -1
  165. data/lib/brakeman/processors/lib/call_conversion_helper.rb +10 -0
  166. data/lib/brakeman/processors/lib/file_type_detector.rb +64 -0
  167. data/lib/brakeman/processors/lib/rails3_config_processor.rb +16 -16
  168. data/lib/brakeman/processors/lib/rails4_config_processor.rb +2 -1
  169. data/lib/brakeman/processors/library_processor.rb +9 -0
  170. data/lib/brakeman/processors/output_processor.rb +1 -1
  171. data/lib/brakeman/processors/template_alias_processor.rb +5 -0
  172. data/lib/brakeman/report.rb +12 -1
  173. data/lib/brakeman/report/ignore/interactive.rb +1 -1
  174. data/lib/brakeman/report/report_base.rb +0 -2
  175. data/lib/brakeman/report/report_csv.rb +37 -60
  176. data/lib/brakeman/report/report_github.rb +31 -0
  177. data/lib/brakeman/report/report_junit.rb +2 -2
  178. data/lib/brakeman/report/report_sarif.rb +1 -1
  179. data/lib/brakeman/report/report_sonar.rb +38 -0
  180. data/lib/brakeman/report/report_tabs.rb +1 -1
  181. data/lib/brakeman/report/report_text.rb +1 -1
  182. data/lib/brakeman/rescanner.rb +7 -5
  183. data/lib/brakeman/scanner.rb +47 -18
  184. data/lib/brakeman/tracker.rb +39 -4
  185. data/lib/brakeman/tracker/collection.rb +27 -5
  186. data/lib/brakeman/tracker/config.rb +73 -0
  187. data/lib/brakeman/tracker/controller.rb +1 -1
  188. data/lib/brakeman/tracker/method_info.rb +29 -0
  189. data/lib/brakeman/util.rb +17 -4
  190. data/lib/brakeman/version.rb +1 -1
  191. data/lib/brakeman/warning.rb +10 -2
  192. data/lib/brakeman/warning_codes.rb +2 -0
  193. data/lib/ruby_parser/bm_sexp.rb +9 -9
  194. metadata +149 -84
  195. data/bundle/ruby/2.7.0/gems/haml-5.1.2/lib/haml/escapable.rb +0 -50
  196. data/bundle/ruby/2.7.0/gems/ruby_parser-3.15.0/debugging.md +0 -57
  197. data/bundle/ruby/2.7.0/gems/ruby_parser-3.15.0/lib/ruby27_parser.rb +0 -7224
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: false
2
+ require_relative "baseparser"
3
+
4
+ module REXML
5
+ module Parsers
6
+ class StreamParser
7
+ def initialize source, listener
8
+ @listener = listener
9
+ @parser = BaseParser.new( source )
10
+ @tag_stack = []
11
+ end
12
+
13
+ def add_listener( listener )
14
+ @parser.add_listener( listener )
15
+ end
16
+
17
+ def parse
18
+ # entity string
19
+ while true
20
+ event = @parser.pull
21
+ case event[0]
22
+ when :end_document
23
+ unless @tag_stack.empty?
24
+ tag_path = "/" + @tag_stack.join("/")
25
+ raise ParseException.new("Missing end tag for '#{tag_path}'",
26
+ @parser.source)
27
+ end
28
+ return
29
+ when :start_element
30
+ @tag_stack << event[1]
31
+ attrs = event[2].each do |n, v|
32
+ event[2][n] = @parser.unnormalize( v )
33
+ end
34
+ @listener.tag_start( event[1], attrs )
35
+ when :end_element
36
+ @listener.tag_end( event[1] )
37
+ @tag_stack.pop
38
+ when :text
39
+ normalized = @parser.unnormalize( event[1] )
40
+ @listener.text( normalized )
41
+ when :processing_instruction
42
+ @listener.instruction( *event[1,2] )
43
+ when :start_doctype
44
+ @listener.doctype( *event[1..-1] )
45
+ when :end_doctype
46
+ # FIXME: remove this condition for milestone:3.2
47
+ @listener.doctype_end if @listener.respond_to? :doctype_end
48
+ when :comment, :attlistdecl, :cdata, :xmldecl, :elementdecl
49
+ @listener.send( event[0].to_s, *event[1..-1] )
50
+ when :entitydecl, :notationdecl
51
+ @listener.send( event[0].to_s, event[1..-1] )
52
+ when :externalentity
53
+ entity_reference = event[1]
54
+ content = entity_reference.gsub(/\A%|;\z/, "")
55
+ @listener.entity(content)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: false
2
+ require_relative '../validation/validationexception'
3
+ require_relative '../undefinednamespaceexception'
4
+
5
+ module REXML
6
+ module Parsers
7
+ class TreeParser
8
+ def initialize( source, build_context = Document.new )
9
+ @build_context = build_context
10
+ @parser = Parsers::BaseParser.new( source )
11
+ end
12
+
13
+ def add_listener( listener )
14
+ @parser.add_listener( listener )
15
+ end
16
+
17
+ def parse
18
+ tag_stack = []
19
+ in_doctype = false
20
+ entities = nil
21
+ begin
22
+ while true
23
+ event = @parser.pull
24
+ #STDERR.puts "TREEPARSER GOT #{event.inspect}"
25
+ case event[0]
26
+ when :end_document
27
+ unless tag_stack.empty?
28
+ raise ParseException.new("No close tag for #{@build_context.xpath}",
29
+ @parser.source, @parser)
30
+ end
31
+ return
32
+ when :start_element
33
+ tag_stack.push(event[1])
34
+ el = @build_context = @build_context.add_element( event[1] )
35
+ event[2].each do |key, value|
36
+ el.attributes[key]=Attribute.new(key,value,self)
37
+ end
38
+ when :end_element
39
+ tag_stack.pop
40
+ @build_context = @build_context.parent
41
+ when :text
42
+ if not in_doctype
43
+ if @build_context[-1].instance_of? Text
44
+ @build_context[-1] << event[1]
45
+ else
46
+ @build_context.add(
47
+ Text.new(event[1], @build_context.whitespace, nil, true)
48
+ ) unless (
49
+ @build_context.ignore_whitespace_nodes and
50
+ event[1].strip.size==0
51
+ )
52
+ end
53
+ end
54
+ when :comment
55
+ c = Comment.new( event[1] )
56
+ @build_context.add( c )
57
+ when :cdata
58
+ c = CData.new( event[1] )
59
+ @build_context.add( c )
60
+ when :processing_instruction
61
+ @build_context.add( Instruction.new( event[1], event[2] ) )
62
+ when :end_doctype
63
+ in_doctype = false
64
+ entities.each { |k,v| entities[k] = @build_context.entities[k].value }
65
+ @build_context = @build_context.parent
66
+ when :start_doctype
67
+ doctype = DocType.new( event[1..-1], @build_context )
68
+ @build_context = doctype
69
+ entities = {}
70
+ in_doctype = true
71
+ when :attlistdecl
72
+ n = AttlistDecl.new( event[1..-1] )
73
+ @build_context.add( n )
74
+ when :externalentity
75
+ n = ExternalEntity.new( event[1] )
76
+ @build_context.add( n )
77
+ when :elementdecl
78
+ n = ElementDecl.new( event[1] )
79
+ @build_context.add(n)
80
+ when :entitydecl
81
+ entities[ event[1] ] = event[2] unless event[2] =~ /PUBLIC|SYSTEM/
82
+ @build_context.add(Entity.new(event))
83
+ when :notationdecl
84
+ n = NotationDecl.new( *event[1..-1] )
85
+ @build_context.add( n )
86
+ when :xmldecl
87
+ x = XMLDecl.new( event[1], event[2], event[3] )
88
+ @build_context.add( x )
89
+ end
90
+ end
91
+ rescue REXML::Validation::ValidationException
92
+ raise
93
+ rescue REXML::ParseException
94
+ raise
95
+ rescue
96
+ raise ParseException.new( $!.message, @parser.source, @parser, $! )
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: false
2
+ require_relative 'streamparser'
3
+ require_relative 'baseparser'
4
+
5
+ module REXML
6
+ module Parsers
7
+ class UltraLightParser
8
+ def initialize stream
9
+ @stream = stream
10
+ @parser = REXML::Parsers::BaseParser.new( stream )
11
+ end
12
+
13
+ def add_listener( listener )
14
+ @parser.add_listener( listener )
15
+ end
16
+
17
+ def rewind
18
+ @stream.rewind
19
+ @parser.stream = @stream
20
+ end
21
+
22
+ def parse
23
+ root = context = []
24
+ while true
25
+ event = @parser.pull
26
+ case event[0]
27
+ when :end_document
28
+ break
29
+ when :end_doctype
30
+ context = context[1]
31
+ when :start_element, :start_doctype
32
+ context << event
33
+ event[1,0] = [context]
34
+ context = event
35
+ when :end_element
36
+ context = context[1]
37
+ else
38
+ context << event
39
+ end
40
+ end
41
+ root
42
+ end
43
+ end
44
+
45
+ # An element is an array. The array contains:
46
+ # 0 The parent element
47
+ # 1 The tag name
48
+ # 2 A hash of attributes
49
+ # 3..-1 The child elements
50
+ # An element is an array of size > 3
51
+ # Text is a String
52
+ # PIs are [ :processing_instruction, target, data ]
53
+ # Comments are [ :comment, data ]
54
+ # DocTypes are DocType structs
55
+ # The root is an array with XMLDecls, Text, DocType, Array, Text
56
+ end
57
+ end
@@ -0,0 +1,689 @@
1
+ # frozen_string_literal: false
2
+ require_relative '../namespace'
3
+ require_relative '../xmltokens'
4
+
5
+ module REXML
6
+ module Parsers
7
+ # You don't want to use this class. Really. Use XPath, which is a wrapper
8
+ # for this class. Believe me. You don't want to poke around in here.
9
+ # There is strange, dark magic at work in this code. Beware. Go back! Go
10
+ # back while you still can!
11
+ class XPathParser
12
+ include XMLTokens
13
+ LITERAL = /^'([^']*)'|^"([^"]*)"/u
14
+
15
+ def namespaces=( namespaces )
16
+ Functions::namespace_context = namespaces
17
+ @namespaces = namespaces
18
+ end
19
+
20
+ def parse path
21
+ path = path.dup
22
+ path.gsub!(/([\(\[])\s+/, '\1') # Strip ignorable spaces
23
+ path.gsub!( /\s+([\]\)])/, '\1')
24
+ parsed = []
25
+ rest = OrExpr(path, parsed)
26
+ if rest
27
+ unless rest.strip.empty?
28
+ raise ParseException.new("Garbage component exists at the end: " +
29
+ "<#{rest}>: <#{path}>")
30
+ end
31
+ end
32
+ parsed
33
+ end
34
+
35
+ def predicate path
36
+ parsed = []
37
+ Predicate( "[#{path}]", parsed )
38
+ parsed
39
+ end
40
+
41
+ def abbreviate( path )
42
+ path = path.kind_of?(String) ? parse( path ) : path
43
+ string = ""
44
+ document = false
45
+ while path.size > 0
46
+ op = path.shift
47
+ case op
48
+ when :node
49
+ when :attribute
50
+ string << "/" if string.size > 0
51
+ string << "@"
52
+ when :child
53
+ string << "/" if string.size > 0
54
+ when :descendant_or_self
55
+ string << "/"
56
+ when :self
57
+ string << "."
58
+ when :parent
59
+ string << ".."
60
+ when :any
61
+ string << "*"
62
+ when :text
63
+ string << "text()"
64
+ when :following, :following_sibling,
65
+ :ancestor, :ancestor_or_self, :descendant,
66
+ :namespace, :preceding, :preceding_sibling
67
+ string << "/" unless string.size == 0
68
+ string << op.to_s.tr("_", "-")
69
+ string << "::"
70
+ when :qname
71
+ prefix = path.shift
72
+ name = path.shift
73
+ string << prefix+":" if prefix.size > 0
74
+ string << name
75
+ when :predicate
76
+ string << '['
77
+ string << predicate_to_string( path.shift ) {|x| abbreviate( x ) }
78
+ string << ']'
79
+ when :document
80
+ document = true
81
+ when :function
82
+ string << path.shift
83
+ string << "( "
84
+ string << predicate_to_string( path.shift[0] ) {|x| abbreviate( x )}
85
+ string << " )"
86
+ when :literal
87
+ string << %Q{ "#{path.shift}" }
88
+ else
89
+ string << "/" unless string.size == 0
90
+ string << "UNKNOWN("
91
+ string << op.inspect
92
+ string << ")"
93
+ end
94
+ end
95
+ string = "/"+string if document
96
+ return string
97
+ end
98
+
99
+ def expand( path )
100
+ path = path.kind_of?(String) ? parse( path ) : path
101
+ string = ""
102
+ document = false
103
+ while path.size > 0
104
+ op = path.shift
105
+ case op
106
+ when :node
107
+ string << "node()"
108
+ when :attribute, :child, :following, :following_sibling,
109
+ :ancestor, :ancestor_or_self, :descendant, :descendant_or_self,
110
+ :namespace, :preceding, :preceding_sibling, :self, :parent
111
+ string << "/" unless string.size == 0
112
+ string << op.to_s.tr("_", "-")
113
+ string << "::"
114
+ when :any
115
+ string << "*"
116
+ when :qname
117
+ prefix = path.shift
118
+ name = path.shift
119
+ string << prefix+":" if prefix.size > 0
120
+ string << name
121
+ when :predicate
122
+ string << '['
123
+ string << predicate_to_string( path.shift ) { |x| expand(x) }
124
+ string << ']'
125
+ when :document
126
+ document = true
127
+ else
128
+ string << "/" unless string.size == 0
129
+ string << "UNKNOWN("
130
+ string << op.inspect
131
+ string << ")"
132
+ end
133
+ end
134
+ string = "/"+string if document
135
+ return string
136
+ end
137
+
138
+ def predicate_to_string( path, &block )
139
+ string = ""
140
+ case path[0]
141
+ when :and, :or, :mult, :plus, :minus, :neq, :eq, :lt, :gt, :lteq, :gteq, :div, :mod, :union
142
+ op = path.shift
143
+ case op
144
+ when :eq
145
+ op = "="
146
+ when :lt
147
+ op = "<"
148
+ when :gt
149
+ op = ">"
150
+ when :lteq
151
+ op = "<="
152
+ when :gteq
153
+ op = ">="
154
+ when :neq
155
+ op = "!="
156
+ when :union
157
+ op = "|"
158
+ end
159
+ left = predicate_to_string( path.shift, &block )
160
+ right = predicate_to_string( path.shift, &block )
161
+ string << " "
162
+ string << left
163
+ string << " "
164
+ string << op.to_s
165
+ string << " "
166
+ string << right
167
+ string << " "
168
+ when :function
169
+ path.shift
170
+ name = path.shift
171
+ string << name
172
+ string << "( "
173
+ string << predicate_to_string( path.shift, &block )
174
+ string << " )"
175
+ when :literal
176
+ path.shift
177
+ string << " "
178
+ string << path.shift.inspect
179
+ string << " "
180
+ else
181
+ string << " "
182
+ string << yield( path )
183
+ string << " "
184
+ end
185
+ return string.squeeze(" ")
186
+ end
187
+
188
+ private
189
+ #LocationPath
190
+ # | RelativeLocationPath
191
+ # | '/' RelativeLocationPath?
192
+ # | '//' RelativeLocationPath
193
+ def LocationPath path, parsed
194
+ path = path.lstrip
195
+ if path[0] == ?/
196
+ parsed << :document
197
+ if path[1] == ?/
198
+ parsed << :descendant_or_self
199
+ parsed << :node
200
+ path = path[2..-1]
201
+ else
202
+ path = path[1..-1]
203
+ end
204
+ end
205
+ return RelativeLocationPath( path, parsed ) if path.size > 0
206
+ end
207
+
208
+ #RelativeLocationPath
209
+ # | Step
210
+ # | (AXIS_NAME '::' | '@' | '') AxisSpecifier
211
+ # NodeTest
212
+ # Predicate
213
+ # | '.' | '..' AbbreviatedStep
214
+ # | RelativeLocationPath '/' Step
215
+ # | RelativeLocationPath '//' Step
216
+ AXIS = /^(ancestor|ancestor-or-self|attribute|child|descendant|descendant-or-self|following|following-sibling|namespace|parent|preceding|preceding-sibling|self)::/
217
+ def RelativeLocationPath path, parsed
218
+ loop do
219
+ original_path = path
220
+ path = path.lstrip
221
+
222
+ return original_path if path.empty?
223
+
224
+ # (axis or @ or <child::>) nodetest predicate >
225
+ # OR > / Step
226
+ # (. or ..) >
227
+ if path[0] == ?.
228
+ if path[1] == ?.
229
+ parsed << :parent
230
+ parsed << :node
231
+ path = path[2..-1]
232
+ else
233
+ parsed << :self
234
+ parsed << :node
235
+ path = path[1..-1]
236
+ end
237
+ else
238
+ path_before_axis_specifier = path
239
+ parsed_not_abberviated = []
240
+ if path[0] == ?@
241
+ parsed_not_abberviated << :attribute
242
+ path = path[1..-1]
243
+ # Goto Nodetest
244
+ elsif path =~ AXIS
245
+ parsed_not_abberviated << $1.tr('-','_').intern
246
+ path = $'
247
+ # Goto Nodetest
248
+ else
249
+ parsed_not_abberviated << :child
250
+ end
251
+
252
+ path_before_node_test = path
253
+ path = NodeTest(path, parsed_not_abberviated)
254
+ if path == path_before_node_test
255
+ return path_before_axis_specifier
256
+ end
257
+ path = Predicate(path, parsed_not_abberviated)
258
+
259
+ parsed.concat(parsed_not_abberviated)
260
+ end
261
+
262
+ original_path = path
263
+ path = path.lstrip
264
+ return original_path if path.empty?
265
+
266
+ return original_path if path[0] != ?/
267
+
268
+ if path[1] == ?/
269
+ parsed << :descendant_or_self
270
+ parsed << :node
271
+ path = path[2..-1]
272
+ else
273
+ path = path[1..-1]
274
+ end
275
+ end
276
+ end
277
+
278
+ # Returns a 1-1 map of the nodeset
279
+ # The contents of the resulting array are either:
280
+ # true/false, if a positive match
281
+ # String, if a name match
282
+ #NodeTest
283
+ # | ('*' | NCNAME ':' '*' | QNAME) NameTest
284
+ # | '*' ':' NCNAME NameTest since XPath 2.0
285
+ # | NODE_TYPE '(' ')' NodeType
286
+ # | PI '(' LITERAL ')' PI
287
+ # | '[' expr ']' Predicate
288
+ PREFIX_WILDCARD = /^\*:(#{NCNAME_STR})/u
289
+ LOCAL_NAME_WILDCARD = /^(#{NCNAME_STR}):\*/u
290
+ QNAME = Namespace::NAMESPLIT
291
+ NODE_TYPE = /^(comment|text|node)\(\s*\)/m
292
+ PI = /^processing-instruction\(/
293
+ def NodeTest path, parsed
294
+ original_path = path
295
+ path = path.lstrip
296
+ case path
297
+ when PREFIX_WILDCARD
298
+ prefix = nil
299
+ name = $1
300
+ path = $'
301
+ parsed << :qname
302
+ parsed << prefix
303
+ parsed << name
304
+ when /^\*/
305
+ path = $'
306
+ parsed << :any
307
+ when NODE_TYPE
308
+ type = $1
309
+ path = $'
310
+ parsed << type.tr('-', '_').intern
311
+ when PI
312
+ path = $'
313
+ literal = nil
314
+ if path =~ /^\s*\)/
315
+ path = $'
316
+ else
317
+ path =~ LITERAL
318
+ literal = $1
319
+ path = $'
320
+ raise ParseException.new("Missing ')' after processing instruction") if path[0] != ?)
321
+ path = path[1..-1]
322
+ end
323
+ parsed << :processing_instruction
324
+ parsed << (literal || '')
325
+ when LOCAL_NAME_WILDCARD
326
+ prefix = $1
327
+ path = $'
328
+ parsed << :namespace
329
+ parsed << prefix
330
+ when QNAME
331
+ prefix = $1
332
+ name = $2
333
+ path = $'
334
+ prefix = "" unless prefix
335
+ parsed << :qname
336
+ parsed << prefix
337
+ parsed << name
338
+ else
339
+ path = original_path
340
+ end
341
+ return path
342
+ end
343
+
344
+ # Filters the supplied nodeset on the predicate(s)
345
+ def Predicate path, parsed
346
+ original_path = path
347
+ path = path.lstrip
348
+ return original_path unless path[0] == ?[
349
+ predicates = []
350
+ while path[0] == ?[
351
+ path, expr = get_group(path)
352
+ predicates << expr[1..-2] if expr
353
+ end
354
+ predicates.each{ |pred|
355
+ preds = []
356
+ parsed << :predicate
357
+ parsed << preds
358
+ OrExpr(pred, preds)
359
+ }
360
+ path
361
+ end
362
+
363
+ # The following return arrays of true/false, a 1-1 mapping of the
364
+ # supplied nodeset, except for axe(), which returns a filtered
365
+ # nodeset
366
+
367
+ #| OrExpr S 'or' S AndExpr
368
+ #| AndExpr
369
+ def OrExpr path, parsed
370
+ n = []
371
+ rest = AndExpr( path, n )
372
+ if rest != path
373
+ while rest =~ /^\s*( or )/
374
+ n = [ :or, n, [] ]
375
+ rest = AndExpr( $', n[-1] )
376
+ end
377
+ end
378
+ if parsed.size == 0 and n.size != 0
379
+ parsed.replace(n)
380
+ elsif n.size > 0
381
+ parsed << n
382
+ end
383
+ rest
384
+ end
385
+
386
+ #| AndExpr S 'and' S EqualityExpr
387
+ #| EqualityExpr
388
+ def AndExpr path, parsed
389
+ n = []
390
+ rest = EqualityExpr( path, n )
391
+ if rest != path
392
+ while rest =~ /^\s*( and )/
393
+ n = [ :and, n, [] ]
394
+ rest = EqualityExpr( $', n[-1] )
395
+ end
396
+ end
397
+ if parsed.size == 0 and n.size != 0
398
+ parsed.replace(n)
399
+ elsif n.size > 0
400
+ parsed << n
401
+ end
402
+ rest
403
+ end
404
+
405
+ #| EqualityExpr ('=' | '!=') RelationalExpr
406
+ #| RelationalExpr
407
+ def EqualityExpr path, parsed
408
+ n = []
409
+ rest = RelationalExpr( path, n )
410
+ if rest != path
411
+ while rest =~ /^\s*(!?=)\s*/
412
+ if $1[0] == ?!
413
+ n = [ :neq, n, [] ]
414
+ else
415
+ n = [ :eq, n, [] ]
416
+ end
417
+ rest = RelationalExpr( $', n[-1] )
418
+ end
419
+ end
420
+ if parsed.size == 0 and n.size != 0
421
+ parsed.replace(n)
422
+ elsif n.size > 0
423
+ parsed << n
424
+ end
425
+ rest
426
+ end
427
+
428
+ #| RelationalExpr ('<' | '>' | '<=' | '>=') AdditiveExpr
429
+ #| AdditiveExpr
430
+ def RelationalExpr path, parsed
431
+ n = []
432
+ rest = AdditiveExpr( path, n )
433
+ if rest != path
434
+ while rest =~ /^\s*([<>]=?)\s*/
435
+ if $1[0] == ?<
436
+ sym = "lt"
437
+ else
438
+ sym = "gt"
439
+ end
440
+ sym << "eq" if $1[-1] == ?=
441
+ n = [ sym.intern, n, [] ]
442
+ rest = AdditiveExpr( $', n[-1] )
443
+ end
444
+ end
445
+ if parsed.size == 0 and n.size != 0
446
+ parsed.replace(n)
447
+ elsif n.size > 0
448
+ parsed << n
449
+ end
450
+ rest
451
+ end
452
+
453
+ #| AdditiveExpr ('+' | '-') MultiplicativeExpr
454
+ #| MultiplicativeExpr
455
+ def AdditiveExpr path, parsed
456
+ n = []
457
+ rest = MultiplicativeExpr( path, n )
458
+ if rest != path
459
+ while rest =~ /^\s*(\+|-)\s*/
460
+ if $1[0] == ?+
461
+ n = [ :plus, n, [] ]
462
+ else
463
+ n = [ :minus, n, [] ]
464
+ end
465
+ rest = MultiplicativeExpr( $', n[-1] )
466
+ end
467
+ end
468
+ if parsed.size == 0 and n.size != 0
469
+ parsed.replace(n)
470
+ elsif n.size > 0
471
+ parsed << n
472
+ end
473
+ rest
474
+ end
475
+
476
+ #| MultiplicativeExpr ('*' | S ('div' | 'mod') S) UnaryExpr
477
+ #| UnaryExpr
478
+ def MultiplicativeExpr path, parsed
479
+ n = []
480
+ rest = UnaryExpr( path, n )
481
+ if rest != path
482
+ while rest =~ /^\s*(\*| div | mod )\s*/
483
+ if $1[0] == ?*
484
+ n = [ :mult, n, [] ]
485
+ elsif $1.include?( "div" )
486
+ n = [ :div, n, [] ]
487
+ else
488
+ n = [ :mod, n, [] ]
489
+ end
490
+ rest = UnaryExpr( $', n[-1] )
491
+ end
492
+ end
493
+ if parsed.size == 0 and n.size != 0
494
+ parsed.replace(n)
495
+ elsif n.size > 0
496
+ parsed << n
497
+ end
498
+ rest
499
+ end
500
+
501
+ #| '-' UnaryExpr
502
+ #| UnionExpr
503
+ def UnaryExpr path, parsed
504
+ path =~ /^(\-*)/
505
+ path = $'
506
+ if $1 and (($1.size % 2) != 0)
507
+ mult = -1
508
+ else
509
+ mult = 1
510
+ end
511
+ parsed << :neg if mult < 0
512
+
513
+ n = []
514
+ path = UnionExpr( path, n )
515
+ parsed.concat( n )
516
+ path
517
+ end
518
+
519
+ #| UnionExpr '|' PathExpr
520
+ #| PathExpr
521
+ def UnionExpr path, parsed
522
+ n = []
523
+ rest = PathExpr( path, n )
524
+ if rest != path
525
+ while rest =~ /^\s*(\|)\s*/
526
+ n = [ :union, n, [] ]
527
+ rest = PathExpr( $', n[-1] )
528
+ end
529
+ end
530
+ if parsed.size == 0 and n.size != 0
531
+ parsed.replace( n )
532
+ elsif n.size > 0
533
+ parsed << n
534
+ end
535
+ rest
536
+ end
537
+
538
+ #| LocationPath
539
+ #| FilterExpr ('/' | '//') RelativeLocationPath
540
+ def PathExpr path, parsed
541
+ path = path.lstrip
542
+ n = []
543
+ rest = FilterExpr( path, n )
544
+ if rest != path
545
+ if rest and rest[0] == ?/
546
+ rest = RelativeLocationPath(rest, n)
547
+ parsed.concat(n)
548
+ return rest
549
+ end
550
+ end
551
+ rest = LocationPath(rest, n) if rest =~ /\A[\/\.\@\[\w*]/
552
+ parsed.concat(n)
553
+ return rest
554
+ end
555
+
556
+ #| FilterExpr Predicate
557
+ #| PrimaryExpr
558
+ def FilterExpr path, parsed
559
+ n = []
560
+ path_before_primary_expr = path
561
+ path = PrimaryExpr(path, n)
562
+ return path_before_primary_expr if path == path_before_primary_expr
563
+ path = Predicate(path, n)
564
+ parsed.concat(n)
565
+ path
566
+ end
567
+
568
+ #| VARIABLE_REFERENCE
569
+ #| '(' expr ')'
570
+ #| LITERAL
571
+ #| NUMBER
572
+ #| FunctionCall
573
+ VARIABLE_REFERENCE = /^\$(#{NAME_STR})/u
574
+ NUMBER = /^(\d*\.?\d+)/
575
+ NT = /^comment|text|processing-instruction|node$/
576
+ def PrimaryExpr path, parsed
577
+ case path
578
+ when VARIABLE_REFERENCE
579
+ varname = $1
580
+ path = $'
581
+ parsed << :variable
582
+ parsed << varname
583
+ #arry << @variables[ varname ]
584
+ when /^(\w[-\w]*)(?:\()/
585
+ fname = $1
586
+ tmp = $'
587
+ return path if fname =~ NT
588
+ path = tmp
589
+ parsed << :function
590
+ parsed << fname
591
+ path = FunctionCall(path, parsed)
592
+ when NUMBER
593
+ varname = $1.nil? ? $2 : $1
594
+ path = $'
595
+ parsed << :literal
596
+ parsed << (varname.include?('.') ? varname.to_f : varname.to_i)
597
+ when LITERAL
598
+ varname = $1.nil? ? $2 : $1
599
+ path = $'
600
+ parsed << :literal
601
+ parsed << varname
602
+ when /^\(/ #/
603
+ path, contents = get_group(path)
604
+ contents = contents[1..-2]
605
+ n = []
606
+ OrExpr( contents, n )
607
+ parsed.concat(n)
608
+ end
609
+ path
610
+ end
611
+
612
+ #| FUNCTION_NAME '(' ( expr ( ',' expr )* )? ')'
613
+ def FunctionCall rest, parsed
614
+ path, arguments = parse_args(rest)
615
+ argset = []
616
+ for argument in arguments
617
+ args = []
618
+ OrExpr( argument, args )
619
+ argset << args
620
+ end
621
+ parsed << argset
622
+ path
623
+ end
624
+
625
+ # get_group( '[foo]bar' ) -> ['bar', '[foo]']
626
+ def get_group string
627
+ ind = 0
628
+ depth = 0
629
+ st = string[0,1]
630
+ en = (st == "(" ? ")" : "]")
631
+ begin
632
+ case string[ind,1]
633
+ when st
634
+ depth += 1
635
+ when en
636
+ depth -= 1
637
+ end
638
+ ind += 1
639
+ end while depth > 0 and ind < string.length
640
+ return nil unless depth==0
641
+ [string[ind..-1], string[0..ind-1]]
642
+ end
643
+
644
+ def parse_args( string )
645
+ arguments = []
646
+ ind = 0
647
+ inquot = false
648
+ inapos = false
649
+ depth = 1
650
+ begin
651
+ case string[ind]
652
+ when ?"
653
+ inquot = !inquot unless inapos
654
+ when ?'
655
+ inapos = !inapos unless inquot
656
+ else
657
+ unless inquot or inapos
658
+ case string[ind]
659
+ when ?(
660
+ depth += 1
661
+ if depth == 1
662
+ string = string[1..-1]
663
+ ind -= 1
664
+ end
665
+ when ?)
666
+ depth -= 1
667
+ if depth == 0
668
+ s = string[0,ind].strip
669
+ arguments << s unless s == ""
670
+ string = string[ind+1..-1]
671
+ end
672
+ when ?,
673
+ if depth == 1
674
+ s = string[0,ind].strip
675
+ arguments << s unless s == ""
676
+ string = string[ind+1..-1]
677
+ ind = -1
678
+ end
679
+ end
680
+ end
681
+ end
682
+ ind += 1
683
+ end while depth > 0 and ind < string.length
684
+ return nil unless depth==0
685
+ [string,arguments]
686
+ end
687
+ end
688
+ end
689
+ end