Class: Habdsl::BaseParser
- Inherits:
-
Object
- Object
- Habdsl::BaseParser
- Defined in:
- lib/habdsl/base_parser.rb
Overview
Class that parses DSL code and generates a result.
Defined Under Namespace
Classes: DSLValidationError
Class Method Summary collapse
Class Method Details
.evaluate_dsl(input_code:, table:) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/habdsl/base_parser.rb', line 12 def self.evaluate_dsl(input_code:, table:) raise DSLValidationError, "Syntax error: DSL code contains a syntax error." if Ripper.sexp(input_code).nil? if table.any? && !input_code.include?("table") raise DSLValidationError, "'table' is not used in the DSL code but table data was provided." end dsl = Habdsl::Model::Dsl.new defined_objects = [] eval_context = Object.new eval_context.define_singleton_method(:location) do |**args, &block| loc = dsl.location(**args, &block) defined_objects << loc loc end eval_context.define_singleton_method(:equipment) do |**args, &block| eq = dsl.equipment(**args, &block) defined_objects << eq eq end eval_context.define_singleton_method(:point) do |**args| dsl.point(**args) end eval_context.define_singleton_method(:table) { table } begin eval_context.instance_eval(input_code) rescue StandardError => e raise DSLValidationError, "Runtime error during DSL evaluation: #{e.class} - #{e.}" end generated_output = defined_objects.map(&:to_s).join("\n") if input_code.include?("table") && table.is_a?(Array) && !table.empty? # Check if table keys were actually used in the generated output unused_keys = table.first.keys.reject do |key| table.any? {|row| generated_output.include?(row[key].to_s) } end if unused_keys.any? raise DSLValidationError, "Validation error: The following table keys are not used in the generated DSL output: #{unused_keys.join(', ')}" end end ResultParser.new(table: table, dsl: generated_output) end |