9
9
require_relative 'fact'
10
10
require_relative 'term'
11
11
12
- # Syntax.
12
+ # Syntax parser .
13
13
#
14
14
# This is an internal class, it is not supposed to be instantiated directly.
15
15
#
16
- # However, you can use it directly, if you need a parser of our syntax. You can
17
- # create your own "Term" class and let this parser make instances of it for
18
- # every term it meets in the query:
19
- #
20
- # require 'factbase/syntax'
21
- # t = Factbase::Syntax.new('(hello world)', MyTerm).to_term
22
- #
23
- # The +MyTerm+ class should have a constructor with two arguments:
24
- # the operator and the list of operands (Array).
25
- #
26
16
# Author:: Yegor Bugayenko ([email protected] )
27
17
# Copyright:: Copyright (c) 2024-2025 Yegor Bugayenko
28
18
# License:: MIT
@@ -37,12 +27,8 @@ class Broken < StandardError; end
37
27
# a child of +Factbase::Term+.
38
28
#
39
29
# @param [String] query The query, for example "(eq id 42)"
40
- # @param [Class] term The class to instantiate to make every term
41
- def initialize ( query , term : Factbase ::Term )
30
+ def initialize ( query )
42
31
@query = query
43
- raise "Term must be a Class, while #{ term . class . name } provided" unless term . is_a? ( Class )
44
- raise "The 'term' must be a child of Factbase::Term, while #{ term . name } provided" unless term <= Factbase ::Term
45
- @term = term
46
32
end
47
33
48
34
# Convert it to a term.
@@ -73,7 +59,7 @@ def build(fb)
73
59
raise "Too many terms (#{ @ast [ 1 ] } != #{ @tokens . size } )" if @ast [ 1 ] != @tokens . size
74
60
t = @ast [ 0 ]
75
61
raise 'No terms found in the AST' if t . nil?
76
- raise "#{ t . class . name } is not an instance of #{ @term } , thus not a proper term " unless t . is_a? ( @term )
62
+ raise "#{ t . class . name } is not an instance of Term " unless t . is_a? ( Factbase :: Term )
77
63
t
78
64
end
79
65
@@ -107,7 +93,7 @@ def to_ast(tokens, at, fb)
107
93
operands << operand
108
94
break if tokens [ at ] == :close
109
95
end
110
- t = @term . new ( op , operands , fb :)
96
+ t = Factbase :: Term . new ( op , operands , fb :)
111
97
[ t , at + 1 ]
112
98
end
113
99
0 commit comments