Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance #43

Merged
merged 2 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/kpeg/code_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def output_op(code, op, indent=2)
# Let default ruby string handling figure it out
lang = ""
end
code << indentify("_tmp = scan(/\\A#{op.regexp}/#{lang})\n", indent)
code << indentify("_tmp = scan(/\\G#{op.regexp}/#{lang})\n", indent)
when CharRange
ss = save()
if op.start.bytesize == 1 and op.fin.bytesize == 1
Expand Down
5 changes: 2 additions & 3 deletions lib/kpeg/compiled_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,8 @@ def match_string(str)
end

def scan(reg)
if m = reg.match(@string[@pos..-1])
width = m.end(0)
@pos += width
if m = reg.match(@string, @pos)
@pos = m.end(0)
return true
end

Expand Down
42 changes: 22 additions & 20 deletions lib/kpeg/format_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,8 @@ def match_string(str)
end

def scan(reg)
if m = reg.match(@string[@pos..-1])
width = m.end(0)
@pos += width
if m = reg.match(@string, @pos)
@pos = m.end(0)
return true
end

Expand Down Expand Up @@ -522,7 +521,7 @@ def _var
_tmp = match_string("-")
break if _tmp
self.pos = _save1
_tmp = scan(/\A(?i-mx:[a-z][\w-]*)/)
_tmp = scan(/\G(?i-mx:[a-z][\w-]*)/)
break if _tmp
self.pos = _save1
break
Expand Down Expand Up @@ -553,7 +552,7 @@ def _method
_save = self.pos
while true # sequence
_text_start = self.pos
_tmp = scan(/\A(?i-mx:[a-z_]\w*)/)
_tmp = scan(/\G(?i-mx:[a-z_]\w*)/)
if _tmp
text = get_text(_text_start)
end
Expand Down Expand Up @@ -817,7 +816,7 @@ def _num_escapes
_save1 = self.pos
while true # sequence
_text_start = self.pos
_tmp = scan(/\A(?-mix:[0-7]{1,3})/)
_tmp = scan(/\G(?-mix:[0-7]{1,3})/)
if _tmp
text = get_text(_text_start)
end
Expand All @@ -844,7 +843,7 @@ def _num_escapes
break
end
_text_start = self.pos
_tmp = scan(/\A(?i-mx:[a-f\d]{2})/)
_tmp = scan(/\G(?i-mx:[a-f\d]{2})/)
if _tmp
text = get_text(_text_start)
end
Expand Down Expand Up @@ -875,7 +874,7 @@ def _dbl_seq
_save = self.pos
while true # sequence
_text_start = self.pos
_tmp = scan(/\A(?-mix:[^\\"]+)/)
_tmp = scan(/\G(?-mix:[^\\"]+)/)
if _tmp
text = get_text(_text_start)
end
Expand Down Expand Up @@ -1013,7 +1012,7 @@ def _sgl_seq
_save = self.pos
while true # sequence
_text_start = self.pos
_tmp = scan(/\A(?-mix:[^'])/)
_tmp = scan(/\G(?-mix:[^'])/)
if _tmp
text = get_text(_text_start)
end
Expand Down Expand Up @@ -1138,7 +1137,7 @@ def _not_slash
_tmp = match_string("\\/")
break if _tmp
self.pos = _save2
_tmp = scan(/\A(?-mix:[^\/])/)
_tmp = scan(/\G(?-mix:[^\/])/)
break if _tmp
self.pos = _save2
break
Expand All @@ -1152,7 +1151,7 @@ def _not_slash
_tmp = match_string("\\/")
break if _tmp
self.pos = _save3
_tmp = scan(/\A(?-mix:[^\/])/)
_tmp = scan(/\G(?-mix:[^\/])/)
break if _tmp
self.pos = _save3
break
Expand Down Expand Up @@ -1265,7 +1264,7 @@ def _char
_save = self.pos
while true # sequence
_text_start = self.pos
_tmp = scan(/\A(?i-mx:[a-z\d])/)
_tmp = scan(/\G(?i-mx:[a-z\d])/)
if _tmp
text = get_text(_text_start)
end
Expand Down Expand Up @@ -1335,7 +1334,7 @@ def _range_num
_save = self.pos
while true # sequence
_text_start = self.pos
_tmp = scan(/\A(?-mix:[1-9]\d*)/)
_tmp = scan(/\G(?-mix:[1-9]\d*)/)
if _tmp
text = get_text(_text_start)
end
Expand Down Expand Up @@ -1510,7 +1509,7 @@ def _curly_block
return _tmp
end

# curly = "{" < (/[^{}"']+/ | string | curly)* > "}" { @g.action(text) }
# curly = "{" < (spaces | /[^{}"']+/ | string | curly)* > "}" { @g.action(text) }
def _curly

_save = self.pos
Expand All @@ -1525,7 +1524,10 @@ def _curly

_save2 = self.pos
while true # choice
_tmp = scan(/\A(?-mix:[^{}"']+)/)
_tmp = apply(:_spaces)
break if _tmp
self.pos = _save2
_tmp = scan(/\G(?-mix:[^{}"']+)/)
break if _tmp
self.pos = _save2
_tmp = apply(:_string)
Expand Down Expand Up @@ -1578,7 +1580,7 @@ def _nested_paren

_save2 = self.pos
while true # choice
_tmp = scan(/\A(?-mix:[^()"']+)/)
_tmp = scan(/\G(?-mix:[^()"']+)/)
break if _tmp
self.pos = _save2
_tmp = apply(:_string)
Expand Down Expand Up @@ -2615,7 +2617,7 @@ def _statement
break
end
_text_start = self.pos
_tmp = scan(/\A(?-mix:[:\w]+)/)
_tmp = scan(/\G(?-mix:[:\w]+)/)
if _tmp
text = get_text(_text_start)
end
Expand Down Expand Up @@ -2913,7 +2915,7 @@ def _ast_constant
_save = self.pos
while true # sequence
_text_start = self.pos
_tmp = scan(/\A(?-mix:[A-Z]\w*)/)
_tmp = scan(/\G(?-mix:[A-Z]\w*)/)
if _tmp
text = get_text(_text_start)
end
Expand All @@ -2939,7 +2941,7 @@ def _ast_word
_save = self.pos
while true # sequence
_text_start = self.pos
_tmp = scan(/\A(?i-mx:[a-z_]\w*)/)
_tmp = scan(/\G(?i-mx:[a-z_]\w*)/)
if _tmp
text = get_text(_text_start)
end
Expand Down Expand Up @@ -3156,7 +3158,7 @@ def _ast_root
Rules[:_range_elem] = rule_info("range_elem", "< (range_num | kleene) > { text }")
Rules[:_mult_range] = rule_info("mult_range", "(\"[\" - range_elem:l - \",\" - range_elem:r - \"]\" { [l == \"*\" ? nil : l.to_i, r == \"*\" ? nil : r.to_i] } | \"[\" - range_num:e - \"]\" { [e.to_i, e.to_i] })")
Rules[:_curly_block] = rule_info("curly_block", "curly")
Rules[:_curly] = rule_info("curly", "\"{\" < (/[^{}\"']+/ | string | curly)* > \"}\" { @g.action(text) }")
Rules[:_curly] = rule_info("curly", "\"{\" < (spaces | /[^{}\"']+/ | string | curly)* > \"}\" { @g.action(text) }")
Rules[:_nested_paren] = rule_info("nested_paren", "\"(\" (/[^()\"']+/ | string | nested_paren)* \")\"")
Rules[:_value] = rule_info("value", "(value:v \":\" var:n { @g.t(v,n) } | value:v \"?\" { @g.maybe(v) } | value:v \"+\" { @g.many(v) } | value:v \"*\" { @g.kleene(v) } | value:v mult_range:r { @g.multiple(v, *r) } | \"&\" value:v { @g.andp(v) } | \"!\" value:v { @g.notp(v) } | \"(\" - expression:o - \")\" { o } | \"@<\" - expression:o - \">\" { @g.bounds(o) } | \"<\" - expression:o - \">\" { @g.collect(o) } | curly_block | \"~\" method:m < nested_paren? > { @g.action(\"\#{m}\#{text}\") } | \".\" { @g.dot } | \"@\" var:name < nested_paren? > !(- \"=\") { @g.invoke(name, text.empty? ? nil : text) } | \"^\" var:name < nested_paren? > { @g.foreign_invoke(\"parent\", name, text) } | \"%\" var:gram \".\" var:name < nested_paren? > { @g.foreign_invoke(gram, name, text) } | var:name < nested_paren? > !(- \"=\") { @g.ref(name, nil, text.empty? ? nil : text) } | char_range | regexp | string)")
Rules[:_spaces] = rule_info("spaces", "(space | comment)+")
Expand Down
7 changes: 3 additions & 4 deletions lib/kpeg/string_escape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,8 @@ def match_string(str)
end

def scan(reg)
if m = reg.match(@string[@pos..-1])
width = m.end(0)
@pos += width
if m = reg.match(@string, @pos)
@pos = m.end(0)
return true
end

Expand Down Expand Up @@ -367,7 +366,7 @@ def _segment
_save1 = self.pos
while true # sequence
_text_start = self.pos
_tmp = scan(/\A(?-mix:[\w ]+)/)
_tmp = scan(/\G(?-mix:[\w ]+)/)
if _tmp
text = get_text(_text_start)
end
Expand Down
6 changes: 3 additions & 3 deletions test/test_kpeg_code_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Test < KPeg::CompiledParser

# root = /[0-9]/
def _root
_tmp = scan(/\\A(?-mix:[0-9])/)
_tmp = scan(/\\G(?-mix:[0-9])/)
set_failed_rule :_root unless _tmp
return _tmp
end
Expand Down Expand Up @@ -114,7 +114,7 @@ class Test < KPeg::CompiledParser

# root = /./
def _root
_tmp = scan(/\\A(?-mix:.)/)
_tmp = scan(/\\G(?-mix:.)/)
set_failed_rule :_root unless _tmp
return _tmp
end
Expand All @@ -133,7 +133,7 @@ class Test < KPeg::CompiledParser

# root = /./u
def _root
_tmp = scan(/\\A(?-mix:.)/u)
_tmp = scan(/\\G(?-mix:.)/u)
set_failed_rule :_root unless _tmp
return _tmp
end
Expand Down