lib/sexpr.rb in sexpr-0.6.0 vs lib/sexpr.rb in sexpr-1.0.0
- old
+ new
@@ -1,8 +1,7 @@
require 'yaml'
require_relative "sexpr/version"
-require_relative "sexpr/loader"
require_relative "sexpr/errors"
require_relative "sexpr/node"
require_relative "sexpr/grammar"
require_relative "sexpr/matcher"
require_relative "sexpr/parser"
@@ -12,10 +11,12 @@
# A helper to manipulate sexp grammars
#
module Sexpr
extend Grammar::Tagging
+ YAML_OPTIONS = { :permitted_classes => %w[Regexp], :aliases => true }
+
PathLike = lambda{|x|
x.respond_to?(:to_path) or (x.is_a?(String) and File.exists?(x))
}
def self.load(input, options = {})
@@ -28,18 +29,25 @@
end
end
def self.load_file(input, options = {})
path = input.to_path rescue input.to_s
- load_hash YAML.load_file(path), options.merge(:path => input)
+ load_hash load_yaml(File.read(path)), options.merge(:path => input)
end
def self.load_string(input, options = {})
- load_hash YAML.load(input), options
+ load_hash load_yaml(input), options
end
def self.load_hash(input, options = {})
raise ArgumentError, "Invalid grammar definition: #{input}" unless Hash===input
Grammar.new input, options
end
+ def self.load_yaml(source)
+ if YAML.name == 'Psych' && Psych::VERSION >= '3.1'
+ YAML.safe_load(source, **YAML_OPTIONS)
+ else
+ YAML.load(source)
+ end
+ end
end # module Sexpr
\ No newline at end of file