Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
- match_dot + match_char_range instead of overloaded get_byte
- no need to force `nil` for falsey state.
  • Loading branch information
funny-falcon committed Aug 13, 2023
1 parent 729cdac commit b7cc198
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 141 deletions.
17 changes: 13 additions & 4 deletions examples/calculator/calculator.kpeg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ def set_failed_rule(name)

attr_reader :failed_rule

def match_dot()
if @pos >= @string_size
return nil
end

@pos += 1
true
end

def match_string(str)
len = str.size
if @string[pos,len] == str
Expand All @@ -217,21 +226,21 @@ def scan(reg)
end

if "".respond_to? :ord
def get_byte(char_range = nil)
def match_char_range(char_range)
if @pos >= @string_size
return nil
elsif char_range && !char_range.include?(@string[@pos].ord)
elsif !char_range.include?(@string[@pos].ord)
return nil
end

@pos += 1
true
end
else
def get_byte(char_range = nil)
def match_char_range(char_range)
if @pos >= @string_size
return nil
elsif char_range && !char_range.include?(@string[@pos])
elsif !char_range.include?(@string[@pos])
return nil
end

Expand Down
17 changes: 13 additions & 4 deletions examples/foreign_reference/literals.kpeg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ def set_failed_rule(name)

attr_reader :failed_rule

def match_dot()
if @pos >= @string_size
return nil
end

@pos += 1
true
end

def match_string(str)
len = str.size
if @string[pos,len] == str
Expand All @@ -217,21 +226,21 @@ def scan(reg)
end

if "".respond_to? :ord
def get_byte(char_range = nil)
def match_char_range(char_range)
if @pos >= @string_size
return nil
elsif char_range && !char_range.include?(@string[@pos].ord)
elsif !char_range.include?(@string[@pos].ord)
return nil
end

@pos += 1
true
end
else
def get_byte(char_range = nil)
def match_char_range(char_range)
if @pos >= @string_size
return nil
elsif char_range && !char_range.include?(@string[@pos])
elsif !char_range.include?(@string[@pos])
return nil
end

Expand Down
19 changes: 14 additions & 5 deletions examples/foreign_reference/matcher.kpeg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ def set_failed_rule(name)

attr_reader :failed_rule

def match_dot()
if @pos >= @string_size
return nil
end

@pos += 1
true
end

def match_string(str)
len = str.size
if @string[pos,len] == str
Expand All @@ -217,21 +226,21 @@ def scan(reg)
end

if "".respond_to? :ord
def get_byte(char_range = nil)
def match_char_range(char_range)
if @pos >= @string_size
return nil
elsif char_range && !char_range.include?(@string[@pos].ord)
elsif !char_range.include?(@string[@pos].ord)
return nil
end

@pos += 1
true
end
else
def get_byte(char_range = nil)
def match_char_range(char_range)
if @pos >= @string_size
return nil
elsif char_range && !char_range.include?(@string[@pos])
elsif !char_range.include?(@string[@pos])
return nil
end

Expand Down Expand Up @@ -434,7 +443,7 @@ def _root
break unless _tmp
_count += 1
end
_tmp = _count >= 1 || nil
_tmp = _count >= 1
unless _tmp
self.pos = _save1
end # end repetition
Expand Down
21 changes: 15 additions & 6 deletions examples/lua_string/lua_string.kpeg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ def set_failed_rule(name)

attr_reader :failed_rule

def match_dot()
if @pos >= @string_size
return nil
end

@pos += 1
true
end

def match_string(str)
len = str.size
if @string[pos,len] == str
Expand All @@ -217,21 +226,21 @@ def scan(reg)
end

if "".respond_to? :ord
def get_byte(char_range = nil)
def match_char_range(char_range)
if @pos >= @string_size
return nil
elsif char_range && !char_range.include?(@string[@pos].ord)
elsif !char_range.include?(@string[@pos].ord)
return nil
end

@pos += 1
true
end
else
def get_byte(char_range = nil)
def match_char_range(char_range)
if @pos >= @string_size
return nil
elsif char_range && !char_range.include?(@string[@pos])
elsif !char_range.include?(@string[@pos])
return nil
end

Expand Down Expand Up @@ -475,10 +484,10 @@ def _root
begin # sequence
_save2 = self.pos
_tmp = apply_with_args(:_equal_ending, e)
_tmp = _tmp ? nil : true
_tmp = !_tmp
self.pos = _save2
break unless _tmp
_tmp = get_byte
_tmp = match_dot
end while false
unless _tmp
self.pos = _save1
Expand Down
25 changes: 17 additions & 8 deletions examples/phone_number/phone_number.kpeg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ def set_failed_rule(name)

attr_reader :failed_rule

def match_dot()
if @pos >= @string_size
return nil
end

@pos += 1
true
end

def match_string(str)
len = str.size
if @string[pos,len] == str
Expand All @@ -217,21 +226,21 @@ def scan(reg)
end

if "".respond_to? :ord
def get_byte(char_range = nil)
def match_char_range(char_range)
if @pos >= @string_size
return nil
elsif char_range && !char_range.include?(@string[@pos].ord)
elsif !char_range.include?(@string[@pos].ord)
return nil
end

@pos += 1
true
end
else
def get_byte(char_range = nil)
def match_char_range(char_range)
if @pos >= @string_size
return nil
elsif char_range && !char_range.include?(@string[@pos])
elsif !char_range.include?(@string[@pos])
return nil
end

Expand Down Expand Up @@ -408,7 +417,7 @@ def setup_foreign_grammar; end

# digit = [0-9]
def _digit
_tmp = get_byte(48..57)
_tmp = match_char_range(48..57)
set_failed_rule :_digit unless _tmp
return _tmp
end
Expand Down Expand Up @@ -477,7 +486,7 @@ def _area_code
_count += 1
break if _count == 3
end
_tmp = _count >= 3 || nil
_tmp = _count >= 3
unless _tmp
self.pos = _save1
end # end repetition
Expand Down Expand Up @@ -510,7 +519,7 @@ def _prefix
_count += 1
break if _count == 3
end
_tmp = _count >= 3 || nil
_tmp = _count >= 3
unless _tmp
self.pos = _save1
end # end repetition
Expand Down Expand Up @@ -543,7 +552,7 @@ def _suffix
_count += 1
break if _count == 4
end
_tmp = _count >= 4 || nil
_tmp = _count >= 4
unless _tmp
self.pos = _save1
end # end repetition
Expand Down
Loading

0 comments on commit b7cc198

Please sign in to comment.