Class: CSquare::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/csquare/base.rb

Direct Known Subclasses

Function

Constant Summary collapse

EXTRA_TYPE_NAMES =
%w{bool}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, type_names) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/csquare/base.rb', line 4

def initialize code, type_names
  @parser = C::Parser.new
  type_names = (type_names + EXTRA_TYPE_NAMES).uniq
  type_names.each { |t| @parser.type_names << t }

  tree = begin
    @parser.parse(code)
  rescue C::ParseError => e
    if e.message.include?("parse error on ID (")
      STDERR.puts "CSquare::Base: A parse error was encountered: #{e.message}"
      STDERR.puts "This typically means you're using a special typename which CSquare does not know how to recognize."
      STDERR.puts "It is recommended that you avoid doing so; however, if you must, try modifying CSquare::Base::EXTRA_TYPE_NAMES."
    else
      STDERR.puts "CSquare::Base: An unrecognized parse error was encountered."
      STDERR.puts code.to_s
    end

    STDERR.puts "The currently defined types are: #{type_names.inspect}"

    raise(e)
  end

  raise(ArgumentError, "multiple entities not supported") if tree.entities.size > 1

  @entity     = tree.entities[0]
end

Instance Attribute Details

#entityObject (readonly)

Returns the value of attribute entity.



35
36
37
# File 'lib/csquare/base.rb', line 35

def entity
  @entity
end

#parserObject (readonly)

Returns the value of attribute parser.



35
36
37
# File 'lib/csquare/base.rb', line 35

def parser
  @parser
end

Instance Method Details

#codeObject



31
32
33
# File 'lib/csquare/base.rb', line 31

def code
  entity.to_s
end