Skip to content

Instantly share code, notes, and snippets.

View gosukiwi's full-sized avatar

Federico Ramirez gosukiwi

View GitHub Profile
class TextScanner < SimpleScanner
def self.from_string(plain_markdown)
text = plain_markdown
.each_char
.take_while { |char| SimpleScanner.from_string(char).null? }
.join('')
Token.new(type: 'TEXT', value: text)
rescue InvalidTokenError
Token.null
end
class SimpleScanner
TOKEN_TYPES = {
'_' => 'UNDERSCORE',
'*' => 'STAR',
"\n" => 'NEWLINE'
}.freeze
def self.from_string(plain_markdown)
char = plain_markdown[0]
Token.new(type: TOKEN_TYPES[char], value: char)
A paragraph __with__ some *text*
@gosukiwi
gosukiwi / demo.rb
Last active January 13, 2019 15:43
class Token
attr_reader :type, :value
def initialize(type:, value:)
@type = type
@value = value
raise InvalidTokenError if value.nil? || type.nil?
end
def self.null
NullToken.new
foo = 1
foo = bar # expects a number on the right hand side of the equation
foo # no equals, no number
foo = # nothing here!
= foo # nothing on the left hand side
RuleName := SomeOtherRule A_TERMINAL