This commit is contained in:
Roberto Alsina 2024-08-04 20:09:15 -03:00
parent 935e72c18e
commit cb02a18b03
4 changed files with 53 additions and 13 deletions

19
.ameba.yml Normal file
View File

@ -0,0 +1,19 @@
# This configuration file was generated by `ameba --gen-config`
# on 2024-08-04 23:09:09 UTC using Ameba version 1.6.1.
# The point is for the user to remove these configuration records
# one by one as the reported problems are removed from the code base.
# Problems found: 2
# Run `ameba --only Documentation/DocumentationAdmonition` for details
Documentation/DocumentationAdmonition:
Description: Reports documentation admonitions
Timezone: UTC
Excluded:
- src/tartrazine.cr
- src/actions.cr
Admonitions:
- TODO
- FIXME
- BUG
Enabled: true
Severity: Warning

View File

@ -23,13 +23,16 @@ module Tartrazine
end end
end end
# ameba:disable Metrics/CyclomaticComplexity
def emit(match : Regex::MatchData?, lexer : Lexer, match_group = 0) : Array(Token) def emit(match : Regex::MatchData?, lexer : Lexer, match_group = 0) : Array(Token)
case type case type
when "token" when "token"
raise Exception.new "Can't have a token without a match" if match.nil? raise Exception.new "Can't have a token without a match" if match.nil?
[Token.new(type: xml["type"], value: match[match_group])] [Token.new(type: xml["type"], value: match[match_group])]
when "push" when "push"
states_to_push = xml.attributes.select { |a| a.name == "state" }.map &.content states_to_push = xml.attributes.select { |attrib|
attrib.name == "state"
}.map &.content
if states_to_push.empty? if states_to_push.empty?
# Push without a state means push the current state # Push without a state means push the current state
states_to_push = [lexer.state_stack.last] states_to_push = [lexer.state_stack.last]
@ -89,8 +92,14 @@ module Tartrazine
new_lexer.tokenize(match[match_group], usingself: true) new_lexer.tokenize(match[match_group], usingself: true)
when "combined" when "combined"
# Combine two states into one anonymous state # Combine two states into one anonymous state
states = xml.attributes.select { |a| a.name == "state" }.map &.content states = xml.attributes.select { |attrib|
new_state = states.map { |name| lexer.states[name] }.reduce { |s1, s2| s1 + s2 } attrib.name == "state"
}.map &.content
new_state = states.map { |name|
lexer.states[name]
}.reduce { |state1, state2|
state1 + state2
}
lexer.states[new_state.name] = new_state lexer.states[new_state.name] = new_state
lexer.state_stack << new_state.name lexer.state_stack << new_state.name
[] of Token [] of Token

View File

@ -33,9 +33,9 @@ module Tartrazine
end end
def add_actions(node : XML::Node) def add_actions(node : XML::Node)
node.children.each do |node| node.children.each do |child|
next unless node.element? next unless child.element?
@actions << Action.new(node.name, node) @actions << Action.new(child.name, child)
end end
end end
end end
@ -57,7 +57,9 @@ module Tartrazine
def initialize(node : XML::Node) def initialize(node : XML::Node)
@xml = node.to_s @xml = node.to_s
include_node = node.children.find { |n| n.name == "include" } include_node = node.children.find { |child|
child.name == "include"
}
@state = include_node["state"] if include_node @state = include_node["state"] if include_node
add_actions(node) add_actions(node)
end end

View File

@ -79,15 +79,18 @@ module Tartrazine
pos += 1 pos += 1
end end
end end
tokens.reject { |t| t[:value] == "" } tokens.reject { |token| token[:value] == "" }
end end
# ameba:disable Metrics/CyclomaticComplexity
def self.from_xml(xml : String) : Lexer def self.from_xml(xml : String) : Lexer
l = Lexer.new l = Lexer.new
l.xml = xml l.xml = xml
lexer = XML.parse(xml).first_element_child lexer = XML.parse(xml).first_element_child
if lexer if lexer
config = lexer.children.find { |n| n.name == "config" } config = lexer.children.find { |node|
node.name == "config"
}
if config if config
l.config = { l.config = {
name: xml_to_s(config, name) || "", name: xml_to_s(config, name) || "",
@ -96,17 +99,22 @@ module Tartrazine
mime_types: xml_to_a(config, mime_type) || [] of String, mime_types: xml_to_a(config, mime_type) || [] of String,
priority: xml_to_f(config, priority) || 0.0, priority: xml_to_f(config, priority) || 0.0,
not_multiline: xml_to_s(config, not_multiline) == "true", not_multiline: xml_to_s(config, not_multiline) == "true",
# FIXME: This has no effect yet (see ) # FIXME: Because Crystal's multiline flag forces dot_all this
# doesn't work perfectly yet.
dot_all: xml_to_s(config, dot_all) == "true", dot_all: xml_to_s(config, dot_all) == "true",
case_insensitive: xml_to_s(config, case_insensitive) == "true", case_insensitive: xml_to_s(config, case_insensitive) == "true",
ensure_nl: xml_to_s(config, ensure_nl) == "true", ensure_nl: xml_to_s(config, ensure_nl) == "true",
} }
end end
rules = lexer.children.find { |n| n.name == "rules" } rules = lexer.children.find { |node|
node.name == "rules"
}
if rules if rules
# Rules contains states 🤷 # Rules contains states 🤷
rules.children.select { |n| n.name == "state" }.each do |state_node| rules.children.select { |node|
node.name == "state"
}.each do |state_node|
state = State.new state = State.new
state.name = state_node["name"] state.name = state_node["name"]
if l.states.has_key?(state.name) if l.states.has_key?(state.name)
@ -115,7 +123,9 @@ module Tartrazine
l.states[state.name] = state l.states[state.name] = state
end end
# And states contain rules 🤷 # And states contain rules 🤷
state_node.children.select { |n| n.name == "rule" }.each do |rule_node| state_node.children.select { |node|
node.name == "rule"
}.each do |rule_node|
case rule_node["pattern"]? case rule_node["pattern"]?
when nil when nil
if rule_node.first_element_child.try &.name == "include" if rule_node.first_element_child.try &.name == "include"