mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-09-20 07:21:22 +00:00
cleanup
This commit is contained in:
parent
115debdec6
commit
029495590c
13
src/rules.cr
13
src/rules.cr
@ -18,35 +18,23 @@ module Tartrazine
|
|||||||
class Rule
|
class Rule
|
||||||
property pattern : Regex = Regex.new ""
|
property pattern : Regex = Regex.new ""
|
||||||
property actions : Array(Action) = [] of Action
|
property actions : Array(Action) = [] of Action
|
||||||
property xml : String = "foo"
|
|
||||||
|
|
||||||
def match(text : Bytes, pos, lexer) : Tuple(Bool, Int32, Array(Token))
|
def match(text : Bytes, pos, lexer) : Tuple(Bool, Int32, Array(Token))
|
||||||
match = pattern.match(text, pos)
|
match = pattern.match(text, pos)
|
||||||
# We don't match if the match doesn't move the cursor
|
# We don't match if the match doesn't move the cursor
|
||||||
# because that causes infinite loops
|
# because that causes infinite loops
|
||||||
return false, pos, [] of Token if match.empty? || match[0].size == 0
|
return false, pos, [] of Token if match.empty? || match[0].size == 0
|
||||||
# p! match, String.new(text[pos..pos+20])
|
|
||||||
# Log.trace { "#{match}, #{pattern.inspect}, #{text}, #{pos}" }
|
|
||||||
tokens = [] of Token
|
tokens = [] of Token
|
||||||
# Emit the tokens
|
|
||||||
actions.each do |action|
|
actions.each do |action|
|
||||||
# Emit the token
|
|
||||||
tokens += action.emit(match, lexer)
|
tokens += action.emit(match, lexer)
|
||||||
end
|
end
|
||||||
Log.trace { "#{xml}, #{pos + match[0].size}, #{tokens}" }
|
|
||||||
return true, pos + match[0].size, tokens
|
return true, pos + match[0].size, tokens
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(node : XML::Node, multiline, dotall, ignorecase)
|
def initialize(node : XML::Node, multiline, dotall, ignorecase)
|
||||||
@xml = node.to_s
|
@xml = node.to_s
|
||||||
pattern = node["pattern"]
|
pattern = node["pattern"]
|
||||||
# flags = Regex::Options::ANCHORED
|
|
||||||
# MULTILINE implies DOTALL which we don't want, so we
|
|
||||||
# use in-pattern flag (?m) instead
|
|
||||||
# flags |= Regex::Options::MULTILINE if multiline
|
|
||||||
pattern = "(?m)" + pattern if multiline
|
pattern = "(?m)" + pattern if multiline
|
||||||
# flags |= Regex::Options::DOTALL if dotall
|
|
||||||
# flags |= Regex::Options::IGNORE_CASE if ignorecase
|
|
||||||
@pattern = Regex.new(pattern, multiline, dotall, ignorecase, true)
|
@pattern = Regex.new(pattern, multiline, dotall, ignorecase, true)
|
||||||
add_actions(node)
|
add_actions(node)
|
||||||
end
|
end
|
||||||
@ -68,7 +56,6 @@ module Tartrazine
|
|||||||
Log.trace { "Including state #{state} from #{lexer.state_stack.last}" }
|
Log.trace { "Including state #{state} from #{lexer.state_stack.last}" }
|
||||||
lexer.states[state].rules.each do |rule|
|
lexer.states[state].rules.each do |rule|
|
||||||
matched, new_pos, new_tokens = rule.match(text, pos, lexer)
|
matched, new_pos, new_tokens = rule.match(text, pos, lexer)
|
||||||
Log.trace { "#{xml}, #{new_pos}, #{new_tokens}" } if matched
|
|
||||||
return true, new_pos, new_tokens if matched
|
return true, new_pos, new_tokens if matched
|
||||||
end
|
end
|
||||||
return false, pos, [] of Token
|
return false, pos, [] of Token
|
||||||
|
Loading…
Reference in New Issue
Block a user