Class: Factbase::Syntax

Inherits:
Object
  • Object
show all
Defined in:
lib/factbase/syntax.rb

Overview

Syntax.

This is an internal class, it is not supposed to be instantiated directly.

However, you can use it directly, if you need a parser of our syntax. You can create your own “Term” class and let this parser make instances of it for every term it meets in the query:

require 'factbase/syntax'
t = Factbase::Syntax.new('(hello world)', MyTerm).to_term

The MyTerm class should have a constructor with two arguments: the operator and the list of operands (Array).

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(query, term: Factbase::Term) ⇒ Syntax

Ctor.

Parameters:

  • query (String)

    The query, for example “(eq id 42)”

  • term (Class) (defaults to: Factbase::Term)

    The class to instantiate to make every term



49
50
51
52
# File 'lib/factbase/syntax.rb', line 49

def initialize(query, term: Factbase::Term)
  @query = query
  @term = term
end

Instance Method Details

#to_termTerm

Convert it to a term.

Returns:

  • (Term)

    The term detected



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/factbase/syntax.rb', line 56

def to_term
  @to_term ||=
    begin
      t = build
      t = t.simplify if t.respond_to?(:simplify)
      t
    end
rescue StandardError => e
  err = "#{e.message} in \"#{@query}\""
  err = "#{err}, tokens: #{@tokens}" unless @tokens.nil?
  raise err
end