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(Factbase.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-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

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

Ctor.

The class provided as the term argument must have a three-argument constructor, similar to the class Factbase::Term. Also, it must be a child of Factbase::Term.

Parameters:

  • fb (Factbase)

    Factbase

  • query (String)

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

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

    The class to instantiate to make every term



57
58
59
60
61
62
63
# File 'lib/factbase/syntax.rb', line 57

def initialize(fb, query, term: Factbase::Term)
  @fb = fb
  @query = query
  raise "Term must be a Class, while #{term.class.name} provided" unless term.is_a?(Class)
  raise "The 'term' must be a child of Factbase::Term, while #{term.name} provided" unless term <= Factbase::Term
  @term = term
end

Instance Method Details

#to_termTerm

Convert it to a term.

Returns:

  • (Term)

    The term detected



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/factbase/syntax.rb', line 67

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