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.
module Tartrazine
enum ActionType
Token
Push
Pop
Combined
Bygroups
Combined
Include
Pop
Push
Token
Using
Usingself
end

View File

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

View File

@ -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,

View File

@ -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)