mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-07-04 07:19:22 +00:00
Rebase to main
This commit is contained in:
31
src/rules.cr
31
src/rules.cr
@ -15,28 +15,11 @@ module Tartrazine
|
||||
alias Match = BytesRegex::Match
|
||||
alias MatchData = Array(Match)
|
||||
|
||||
class Rule
|
||||
property pattern : Regex = Regex.new ""
|
||||
property actions : Array(Action) = [] of Action
|
||||
abstract struct BaseRule
|
||||
abstract def match(text : Bytes, pos : Int32, tokenizer : Tokenizer) : Tuple(Bool, Int32, Array(Token))
|
||||
abstract def initialize(node : XML::Node)
|
||||
|
||||
def match(text : Bytes, pos, lexer) : Tuple(Bool, Int32, Array(Token))
|
||||
match = pattern.match(text, pos)
|
||||
# We don't match if the match doesn't move the cursor
|
||||
# because that causes infinite loops
|
||||
return false, pos, [] of Token if match.empty? || match[0].size == 0
|
||||
tokens = [] of Token
|
||||
actions.each do |action|
|
||||
tokens += action.emit(match, lexer)
|
||||
end
|
||||
return true, pos + match[0].size, tokens
|
||||
end
|
||||
|
||||
def initialize(node : XML::Node, multiline, dotall, ignorecase)
|
||||
pattern = node["pattern"]
|
||||
pattern = "(?m)" + pattern if multiline
|
||||
@pattern = Regex.new(pattern, multiline, dotall, ignorecase, true)
|
||||
add_actions(node)
|
||||
end
|
||||
@actions : Array(Action) = [] of Action
|
||||
|
||||
def add_actions(node : XML::Node)
|
||||
node.children.each do |child|
|
||||
@ -73,9 +56,9 @@ module Tartrazine
|
||||
struct IncludeStateRule < BaseRule
|
||||
@state : String = ""
|
||||
|
||||
def match(text, pos, lexer) : Tuple(Bool, Int32, Array(Token))
|
||||
lexer.states[state].rules.each do |rule|
|
||||
matched, new_pos, new_tokens = rule.match(text, pos, lexer)
|
||||
def match(text : Bytes, pos : Int32, tokenizer : Tokenizer) : Tuple(Bool, Int32, Array(Token))
|
||||
tokenizer.@lexer.states[@state].rules.each do |rule|
|
||||
matched, new_pos, new_tokens = rule.match(text, pos, tokenizer)
|
||||
return true, new_pos, new_tokens if matched
|
||||
end
|
||||
return false, pos, [] of Token
|
||||
|
Reference in New Issue
Block a user