Skip to content

Commit

Permalink
TUTORIAL: separating key classes from data types classes
Browse files Browse the repository at this point in the history
  • Loading branch information
famished-tiger committed Feb 14, 2022
1 parent 8cca03e commit 33b0676
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 14 deletions.
36 changes: 30 additions & 6 deletions tutorial/TOML/iter_1/toml_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,34 @@
# An instance acts merely as a wrapper around a Ruby representation
# of the key value.
class TOMLKey
# Constructor. Initialize a TOML value from one of its built-in data type.
# @param _lexeme [String] Text representation of data type in TOML.
# @param _format [Symbol, NilClass] A symbolic name of the input format.
def initialize(_lexeme, _format = nil)
# Do nothing
end

# @return [String] the literal key
def key
to_key
end

# Method called from TOML to obtain the text representation of the boolean.
# @return [String]
def to_str
key.to_s # Default implementation...
end

end

def ==(other)
return true if equal?(other)

return to_str == other if other.kind_of?(String)

key == other.key
end

protected

def to_key
raise NotImplementedError, 'Method to implement in subclass(es)'
end
Expand All @@ -25,16 +40,25 @@ def to_key
# Class implementing the TOML unquoted key data type.
class UnquotedKey < TOMLKey
# Constructor. Initialize an unquoted ket from the lexeme
def initialize(aLexeme)
def initialize(aLexeme, format = nil)
super(aLexeme, format)
@key = validated_key(aLexeme)
end

alias value key

# Part of the 'visitee' role in Visitor design pattern.
# @param visitor [TOMLASTVisitor] the visitor
def accept(visitor)
visitor.visit_unquoted_key(self)
end

protected

def validated_key(aLexeme)
aLexeme
end

def to_key
@key
end
Expand Down
2 changes: 1 addition & 1 deletion tutorial/TOML/iter_1/toml_tokenizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TOMLTokenizer
attr_reader(:line_start)

# Mapping special character tokens to symbolic names
# @return [{Char => String}]
# @return [{Char => String}]
Lexeme2name = {
'=' => 'EQUAL'
}.freeze
Expand Down
4 changes: 2 additions & 2 deletions tutorial/TOML/iter_2/toml_datatype.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def validated_value(text, _format)
compacted.to_f
end
end # class

=begin
# Class implementing the TOML unquoted key data type.
class UnquotedKey < TOMLDatatype
# Method to obtain the text representation of the object.
Expand All @@ -113,7 +113,7 @@ def validated_value(aValue, _format)
aValue
end
end # class

=end
# Class implementing the TOML string data type.
class TOMLString < TOMLDatatype
PATT_STRING_ESCAPE = /\\(?:[^Uu]|u[0-9A-Fa-f]{0,4}|U[0-9A-Fa-f]{0,8})/.freeze
Expand Down
3 changes: 3 additions & 0 deletions tutorial/TOML/iter_2/toml_key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require_relative '../iter_1/toml_key'
3 changes: 2 additions & 1 deletion tutorial/TOML/iter_2/toml_tokenizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'strscan'
require 'rley'
require_relative 'toml_datatype'
require_relative 'toml_key'

# A tokenizer for a very limited subset of TOML.
# Responsibilities:
Expand Down Expand Up @@ -50,7 +51,7 @@ class TOMLTokenizer
attr_reader(:line_start)

# Mapping special character tokens to symbolic names
# @return [{Char => String}]
# @return [{Char => String}]
Lexeme2name = {
',' => 'COMMA',
'=' => 'EQUAL',
Expand Down
4 changes: 0 additions & 4 deletions tutorial/TOML/iter_3/toml_datatype.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
require 'date'
require 'forwardable'

# Class implementing the TOML unquoted key data type.
class QuotedKey < TOMLString
end # class

# Class implementing the TOML offset date-time data type.
class TOMLOffsetDateTime < TOMLDatatype
extend Forwardable
Expand Down
27 changes: 27 additions & 0 deletions tutorial/TOML/iter_3/toml_key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require_relative '../iter_2/toml_key'
require_relative 'toml_datatype'

# Class implementing the TOML quoted key data type.
class QuotedKey < TOMLKey
extend Forwardable
def_delegators :@key, :value

def initialize(aLexeme, aFormat)
super(aLexeme, aFormat)
@key = TOMLString.new(aLexeme, aFormat)
end

# Part of the 'visitee' role in Visitor design pattern.
# @param visitor [TOMLASTVisitor] the visitor
def accept(visitor)
visitor.visit_unquoted_key(self)
end

protected

def to_key
@key.value
end
end # class
1 change: 1 addition & 0 deletions tutorial/TOML/iter_3/toml_tokenizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'strscan'
require 'rley'
require_relative 'toml_datatype'
require_relative 'toml_key'

# A tokenizer for a very limited subset of TOML.
# Responsibilities:
Expand Down

0 comments on commit 33b0676

Please sign in to comment.