mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-09-20 07:21:22 +00:00
Implement heuristics from linguist
This commit is contained in:
parent
fd7c6fa4b3
commit
5a88a51f3e
@ -37,25 +37,37 @@ module Tartrazine
|
|||||||
class Rule
|
class Rule
|
||||||
include YAML::Serializable
|
include YAML::Serializable
|
||||||
property pattern : (String | Array(String))?
|
property pattern : (String | Array(String))?
|
||||||
|
property negative_pattern : (String | Array(String))?
|
||||||
property named_pattern : String?
|
property named_pattern : String?
|
||||||
property and : Array(Rule)?
|
property and : Array(Rule)?
|
||||||
|
|
||||||
|
# ameba:disable Metrics/CyclomaticComplexity
|
||||||
def match(content, named_patterns)
|
def match(content, named_patterns)
|
||||||
result = true
|
# This rule matches without conditions
|
||||||
|
return true if !pattern && !negative_pattern && !named_pattern && !and
|
||||||
|
|
||||||
if pattern
|
if pattern
|
||||||
p_arr = [] of String
|
p_arr = [] of String
|
||||||
p_arr << pattern.as(String) if pattern.is_a? String
|
p_arr << pattern.as(String) if pattern.is_a? String
|
||||||
p_arr = pattern.as(Array(String)) if pattern.is_a? Array(String)
|
p_arr = pattern.as(Array(String)) if pattern.is_a? Array(String)
|
||||||
result = p_arr.any? { |pat| ::Regex.new(pat).match(content) }
|
return true if p_arr.any? { |pat| ::Regex.new(pat).matches?(content) }
|
||||||
elsif named_pattern
|
end
|
||||||
|
if negative_pattern
|
||||||
|
p_arr = [] of String
|
||||||
|
p_arr << negative_pattern.as(String) if negative_pattern.is_a? String
|
||||||
|
p_arr = negative_pattern.as(Array(String)) if negative_pattern.is_a? Array(String)
|
||||||
|
return true if p_arr.none? { |pat| ::Regex.new(pat).matches?(content) }
|
||||||
|
end
|
||||||
|
if named_pattern
|
||||||
p_arr = [] of String
|
p_arr = [] of String
|
||||||
if named_patterns[named_pattern].is_a? String
|
if named_patterns[named_pattern].is_a? String
|
||||||
p_arr << named_patterns[named_pattern].as(String)
|
p_arr << named_patterns[named_pattern].as(String)
|
||||||
else
|
else
|
||||||
p_arr = named_patterns[named_pattern].as(Array(String))
|
p_arr = named_patterns[named_pattern].as(Array(String))
|
||||||
end
|
end
|
||||||
result = p_arr.any? { |pat| ::Regex.new(pat).match(content) }
|
result = p_arr.any? { |pat| ::Regex.new(pat).matches?(content) }
|
||||||
elsif and
|
end
|
||||||
|
if and
|
||||||
result = and.as(Array(Rule)).all?(&.match(content, named_patterns))
|
result = and.as(Array(Rule)).all?(&.match(content, named_patterns))
|
||||||
end
|
end
|
||||||
result
|
result
|
||||||
@ -68,5 +80,5 @@ module Tartrazine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
h = Tartrazine::Heuristic.from_yaml(File.read("heuristics/heuristics.yml"))
|
# h = Tartrazine::Heuristic.from_yaml(File.read("heuristics/heuristics.yml"))
|
||||||
p! h.run("../elkjs/src/elk.h", File.read("../elkjs/src/elk.h"))
|
# p! h.run(ARGV[0], File.read(ARGV[0]))
|
||||||
|
Loading…
Reference in New Issue
Block a user