From 78eff45ea0b1017fbb35792a80107bc20669912d Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Thu, 15 Aug 2024 23:11:49 -0300 Subject: [PATCH] Idiomatic changes --- src/actions.cr | 8 ++++---- src/bytes_regex.cr | 2 +- src/lexer.cr | 2 +- src/rules.cr | 12 +++++------- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/actions.cr b/src/actions.cr index 5e96f7c..85b5c9c 100644 --- a/src/actions.cr +++ b/src/actions.cr @@ -9,12 +9,12 @@ require "./tartrazine" # or change the state machine. module Tartrazine enum ActionType - Token - Push - Pop - Combined Bygroups + Combined Include + Pop + Push + Token Using Usingself end diff --git a/src/bytes_regex.cr b/src/bytes_regex.cr index 701cdf8..c73f694 100644 --- a/src/bytes_regex.cr +++ b/src/bytes_regex.cr @@ -57,7 +57,7 @@ module BytesRegex end end - class Match + struct Match property value : Bytes property start : UInt64 property size : UInt64 diff --git a/src/lexer.cr b/src/lexer.cr index 632e274..800dfb9 100644 --- a/src/lexer.cr +++ b/src/lexer.cr @@ -44,7 +44,7 @@ module Tartrazine # For explanations on what actions and states do # the Pygments documentation is a good place to start. # https://pygments.org/docs/lexerdevelopment/ - class Lexer + struct Lexer property config = { name: "", aliases: [] of String, diff --git a/src/rules.cr b/src/rules.cr index dc07899..597ea65 100644 --- a/src/rules.cr +++ b/src/rules.cr @@ -19,7 +19,7 @@ module Tartrazine abstract def match(text : Bytes, pos, lexer) : Tuple(Bool, Int32, Array(Token)) abstract def initialize(node : XML::Node) - property actions : Array(Action) = [] of Action + @actions : Array(Action) = [] of Action def add_actions(node : XML::Node) node.children.each do |child| @@ -31,14 +31,13 @@ module Tartrazine struct Rule < BaseRule property pattern : Regex = Regex.new "" - property actions : Array(Action) = [] of Action def match(text : Bytes, pos, lexer) : Tuple(Bool, Int32, Array(Token)) match = pattern.match(text, pos) # No match return false, pos, [] of Token if match.size == 0 - return true, pos + match[0].size, actions.flat_map(&.emit(match, lexer)) + return true, pos + match[0].size, @actions.flat_map(&.emit(match, lexer)) end def initialize(node : XML::Node) @@ -55,11 +54,10 @@ module Tartrazine # This rule includes another state. If any of the rules of the # included state matches, this rule matches. struct IncludeStateRule < BaseRule - property state : String = "" + @state : String = "" def match(text, pos, lexer) : Tuple(Bool, Int32, Array(Token)) - 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) return true, new_pos, new_tokens if matched end @@ -80,7 +78,7 @@ module Tartrazine NO_MATCH = [] of Match def match(text, pos, lexer) : Tuple(Bool, Int32, Array(Token)) - return true, pos, actions.flat_map(&.emit(NO_MATCH, lexer)) + return true, pos, @actions.flat_map(&.emit(NO_MATCH, lexer)) end def initialize(node : XML::Node)