lib/sfp/parser.rb in sfp-0.3.5 vs lib/sfp/parser.rb in sfp-0.3.6

- old
+ new

@@ -1,136 +1,50 @@ -module Sfp - # main class which processes configuration description in SFP language either - # in file or as a string - class Parser - # enable this class to process SFP into FDR (SAS+) - include Sfp::SasTranslator +# main class which processes configuration description in SFP language either +# in file or as a string +class Sfp::Parser + # enable this class to process SFP into FDR (SAS+) + include Sfp::SasTranslator - attr_accessor :root_dir, :home_dir, :conformant - attr_reader :root + attr_accessor :root_dir, :home_dir, :conformant + attr_reader :root, :constraint_next_id - def initialize(params={}) - @root_dir = (params[:root_dir].is_a?(String) ? - params[:root_dir].strip : - nil) - @home_dir = (params[:home_dir].is_a?(String) ? - params[:home_dir].strip : - nil) - @root = params[:root] - @conformant = !!params[:conformant] - end - - # @param string : a string in SFP language - def parse(string) - lexer = SfpLang::Lexer.new(string) - tokens = ANTLR3::CommonTokenStream.new(lexer) - parser = SfpLang::Parser.new(tokens) - parser.root_dir = @root_dir - parser.home_dir = @home_dir - parser.sfp - @root = parser.root - @conformant = parser.conformant - @parser_arrays = parser.arrays - end - - def to_json(params={}) - return 'null' if @root.nil? - return Sfp::Helper.to_pretty_json(@root) if params[:pretty] - return Sfp::Helper.to_json(@root) - end - - def self.parse_file(filepath) - homedir = File.expand_path(File.dirname(filepath)) - parser = Sfp::Parser.new({:home_dir => homedir}) - parser.parse(File.read(filepath)) - parser.root - end - -=begin - # Parse SFP file and return its JSON representation - def self.parse_file(file) - return file_to_sfp(file) - end - - def self.file_to_sfp(file) - parser = Parser.new - parser.parse_file(file) - return parser.to_sfp - end - - def self.to_sfp(string) - parser = Parser.new - parser.parse(string) - return parser.to_sfp - end - - # parse SFP file - def parse_file(file) - f = File.open(file, 'rb') - lexer = SFP::Lexer.new(f) - tokens = ANTLR3::CommonTokenStream.new(lexer) - parser = SFP::Parser.new(tokens) - parser.root_dir = (@root_dir == nil or @root_dir == '' ? - File.expand_path('.') : @root_dir) - parser.home_dir = File.dirname(f.path) - parser.sfp - @conformant = parser.conformant - @root = parser.root - @parser_arrays = parser.arrays - end - - # parse SFP in a string - def parse(text) - lexer = SFP::Lexer.new(text) - tokens = ANTLR3::CommonTokenStream.new(lexer) - parser = SFP::Parser.new(tokens) - parser.root_dir = (@root_dir == nil or @root_dir == '' ? - File.expand_path(File.dirname('.')) : @root_dir) - parser.home_dir = parser.root_dir - parser.sfp - @root = parser.root - @parser_arrays = parser.arrays - end - - # dump the parsed specification into standard output - def dump(root=nil) - return if root == nil - root = Nuri::Sfp.deep_clone(@root) - root.accept(ParentEliminator.new) - puts JSON.pretty_generate(root) - end - - def self.dump(root) - return if root == nil - root = Nuri::Sfp.deep_clone(root) - root.accept(ParentEliminator.new) - puts JSON.pretty_generate(root) - end - - def to_sfp - @root - end - - def to_json - root = self.to_sfp - return if root == nil - root = Nuri::Sfp.deep_clone(root) - return Nuri::Sfp.to_json(root) - end -=end + def initialize(params={}) + @root_dir = (params[:root_dir].is_a?(String) ? + params[:root_dir].strip : + nil) + @home_dir = (params[:home_dir].is_a?(String) ? + params[:home_dir].strip : + nil) + @root = params[:root] + @conformant = !!params[:conformant] + @constraint_next_id = (params[:constraint_next_id] ? params[:constraint_next_id] : 0) end -=begin - def self.to_json(sfp) - root = Sfp::Helper.deep_clone(sfp) - root.accept(Nuri::Sfp::ParentEliminator.new) - return JSON.generate(root) + # @param string : a string in SFP language + def parse(string, options={}) + lexer = SfpLang::Lexer.new(string) + tokens = ANTLR3::CommonTokenStream.new(lexer) + parser = SfpLang::Parser.new(tokens) + parser.root_dir = @root_dir + parser.home_dir = @home_dir + parser.constraint_next_id = @constraint_next_id + parser.sfp + @constraint_next_id = parser.constraint_next_id + @root = parser.root + @conformant = parser.conformant + @parser_arrays = parser.arrays end - def self.to_pretty_json(sfp) - root = Nuri::Sfp.deep_clone(sfp) - root.accept(Nuri::Sfp::ParentEliminator.new) - return JSON.pretty_generate(root) + def to_json(params={}) + return 'null' if @root.nil? + return Sfp::Helper.to_pretty_json(@root) if params[:pretty] + return Sfp::Helper.to_json(@root) end -=end + def self.parse_file(filepath, options={}) + #homedir = File.expand_path(File.dirname(filepath)) + options[:home_dir] = File.expand_path(File.dirname(filepath)) if !options[:home_dir] + parser = Sfp::Parser.new(options) #{:home_dir => homedir}) + parser.parse(File.read(filepath)) + parser.root + end end