Idiomatic changes

This commit is contained in:
Roberto Alsina 2024-08-15 23:11:49 -03:00
parent e817aedd60
commit 78eff45ea0
4 changed files with 11 additions and 13 deletions

View File

@ -9,12 +9,12 @@ require "./tartrazine"
# or change the state machine. # or change the state machine.
module Tartrazine module Tartrazine
enum ActionType enum ActionType
Token
Push
Pop
Combined
Bygroups Bygroups
Combined
Include Include
Pop
Push
Token
Using Using
Usingself Usingself
end end

View File

@ -57,7 +57,7 @@ module BytesRegex
end end
end end
class Match struct Match
property value : Bytes property value : Bytes
property start : UInt64 property start : UInt64
property size : UInt64 property size : UInt64

View File

@ -44,7 +44,7 @@ module Tartrazine
# For explanations on what actions and states do # For explanations on what actions and states do
# the Pygments documentation is a good place to start. # the Pygments documentation is a good place to start.
# https://pygments.org/docs/lexerdevelopment/ # https://pygments.org/docs/lexerdevelopment/
class Lexer struct Lexer
property config = { property config = {
name: "", name: "",
aliases: [] of String, aliases: [] of String,

View File

@ -19,7 +19,7 @@ module Tartrazine
abstract def match(text : Bytes, pos, lexer) : Tuple(Bool, Int32, Array(Token)) abstract def match(text : Bytes, pos, lexer) : Tuple(Bool, Int32, Array(Token))
abstract def initialize(node : XML::Node) abstract def initialize(node : XML::Node)
property actions : Array(Action) = [] of Action @actions : Array(Action) = [] of Action
def add_actions(node : XML::Node) def add_actions(node : XML::Node)
node.children.each do |child| node.children.each do |child|
@ -31,14 +31,13 @@ module Tartrazine
struct Rule < BaseRule struct Rule < BaseRule
property pattern : Regex = Regex.new "" property pattern : Regex = Regex.new ""
property actions : Array(Action) = [] of Action
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)
# No match # No match
return false, pos, [] of Token if match.size == 0 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 end
def initialize(node : XML::Node) def initialize(node : XML::Node)
@ -55,11 +54,10 @@ module Tartrazine
# This rule includes another state. If any of the rules of the # This rule includes another state. If any of the rules of the
# included state matches, this rule matches. # included state matches, this rule matches.
struct IncludeStateRule < BaseRule struct IncludeStateRule < BaseRule
property state : String = "" @state : String = ""
def match(text, pos, lexer) : Tuple(Bool, Int32, Array(Token)) 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) matched, new_pos, new_tokens = rule.match(text, pos, lexer)
return true, new_pos, new_tokens if matched return true, new_pos, new_tokens if matched
end end
@ -80,7 +78,7 @@ module Tartrazine
NO_MATCH = [] of Match NO_MATCH = [] of Match
def match(text, pos, lexer) : Tuple(Bool, Int32, Array(Token)) 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 end
def initialize(node : XML::Node) def initialize(node : XML::Node)