Class: Kumi::Core::RubyParser::ExpressionConverter

Inherits:
Object
  • Object
show all
Includes:
ErrorReporting, Syntax
Defined in:
lib/kumi/core/ruby_parser/expression_converter.rb

Overview

Converts Ruby objects and DSL expressions into AST nodes This is the bridge between Ruby’s native types and Kumi’s syntax tree

Constant Summary collapse

LITERAL_TYPES =

Use the same literal types as Sugar module to avoid duplication

Sugar::LITERAL_TYPES

Instance Method Summary collapse

Methods included from ErrorReporting

#inferred_location, #raise_localized_error, #raise_syntax_error, #raise_type_error, #report_enhanced_error, #report_error, #report_semantic_error, #report_syntax_error, #report_type_error

Constructor Details

#initialize(context) ⇒ ExpressionConverter

Returns a new instance of ExpressionConverter.



15
16
17
# File 'lib/kumi/core/ruby_parser/expression_converter.rb', line 15

def initialize(context)
  @context = context
end

Instance Method Details

#ensure_syntax(obj) ⇒ Syntax::Node

Convert any Ruby object into a syntax node

Parameters:

  • obj (Object)

    The object to convert

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kumi/core/ruby_parser/expression_converter.rb', line 22

def ensure_syntax(obj)
  case obj
  when *LITERAL_TYPES
    create_literal(obj)
  when Array
    create_list(obj)
  when Hash
    create_hash(obj)
  when Syntax::Node
    obj
  else
    handle_custom_object(obj)
  end
end

#fn(fn_name, *args) ⇒ Syntax::CallExpression

Create a function call expression

Parameters:

  • fn_name (Symbol)

    The function name

  • args (Array)

    The function arguments

Returns:



56
57
58
59
60
# File 'lib/kumi/core/ruby_parser/expression_converter.rb', line 56

def fn(fn_name, *args)
  validate_function_name(fn_name)
  expr_args = convert_arguments(args)
  Kumi::Syntax::CallExpression.new(fn_name, expr_args, loc: current_location)
end

#inputInputProxy

Access the input proxy for field references

Returns:



64
65
66
# File 'lib/kumi/core/ruby_parser/expression_converter.rb', line 64

def input
  InputProxy.new(@context)
end

#literal(value) ⇒ Syntax::Literal

Create a literal value node

Parameters:

  • value (Object)

    The literal value

Returns:



48
49
50
# File 'lib/kumi/core/ruby_parser/expression_converter.rb', line 48

def literal(value)
  Kumi::Syntax::Literal.new(value, loc: current_location)
end

#raise_error(message, location) ⇒ Object

Raise a syntax error with location information

Parameters:

  • message (String)

    Error message

  • location (Location)

    Error location



71
72
73
# File 'lib/kumi/core/ruby_parser/expression_converter.rb', line 71

def raise_error(message, location)
  raise_syntax_error(message, location: location)
end

#ref(name) ⇒ Syntax::DeclarationReference

Create a reference to another declaration

Parameters:

  • name (Symbol)

    The name to reference

Returns:



40
41
42
43
# File 'lib/kumi/core/ruby_parser/expression_converter.rb', line 40

def ref(name)
  validate_reference_name(name)
  Kumi::Syntax::DeclarationReference.new(name, loc: current_location)
end