Class: Kotoshu::Grammar::RuleEngine
- Inherits:
-
Object
- Object
- Kotoshu::Grammar::RuleEngine
- Defined in:
- lib/kotoshu/grammar/rule_engine.rb
Overview
Engine for loading and executing grammar rules from YAML configuration.
This implements configuration-driven design where all linguistic data (rules, patterns, exceptions) is stored in YAML files, not hardcoded.
Instance Attribute Summary collapse
-
#language ⇒ Object
readonly
Returns the value of attribute language.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
-
#check(tokens) ⇒ Array<Hash>
Check tokens against all loaded rules.
-
#get_rule(id) ⇒ Rule?
Get a specific rule by ID.
-
#initialize(language:, rules_path: nil, dictionaries_path: nil) ⇒ RuleEngine
constructor
Create a new rule engine for a language.
-
#rule_exists?(id) ⇒ Boolean
Check if a rule exists.
-
#rule_names ⇒ Array<String>
Get list of rule IDs.
Constructor Details
#initialize(language:, rules_path: nil, dictionaries_path: nil) ⇒ RuleEngine
Create a new rule engine for a language.
25 26 27 28 29 30 |
# File 'lib/kotoshu/grammar/rule_engine.rb', line 25 def initialize(language:, rules_path: nil, dictionaries_path: nil) @language = language @rules_path = rules_path || default_rules_path(dictionaries_path) @loader = RuleLoader.new(@rules_path) @rules = @loader.load_rules end |
Instance Attribute Details
#language ⇒ Object (readonly)
Returns the value of attribute language.
18 19 20 |
# File 'lib/kotoshu/grammar/rule_engine.rb', line 18 def language @language end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
18 19 20 |
# File 'lib/kotoshu/grammar/rule_engine.rb', line 18 def rules @rules end |
Instance Method Details
#check(tokens) ⇒ Array<Hash>
Check tokens against all loaded rules.
36 37 38 39 40 41 42 43 |
# File 'lib/kotoshu/grammar/rule_engine.rb', line 36 def check(tokens) errors = [] @rules.each do |rule| rule_errors = rule.check(tokens) errors.concat(rule_errors) end errors end |
#get_rule(id) ⇒ Rule?
Get a specific rule by ID.
56 57 58 |
# File 'lib/kotoshu/grammar/rule_engine.rb', line 56 def get_rule(id) @rules.find { |r| r.id == id } end |
#rule_exists?(id) ⇒ Boolean
Check if a rule exists.
64 65 66 |
# File 'lib/kotoshu/grammar/rule_engine.rb', line 64 def rule_exists?(id) @rules.any? { |r| r.id == id } end |
#rule_names ⇒ Array<String>
Get list of rule IDs.
48 49 50 |
# File 'lib/kotoshu/grammar/rule_engine.rb', line 48 def rule_names @rules.map(&:id) end |