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

Make memoization opt-in #6

Merged
merged 2 commits into from
Nov 27, 2024
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 VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.1
1.5.2
2 changes: 1 addition & 1 deletion corral.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"homepage": "https://github.com/chalcolith/kiuatan",
"documentation_url": "https://chalcolith.github.io/kiuatan/kiuatan--index/",
"license": "MIT",
"version": "1.5.1"
"version": "1.5.2"
},
"deps": []
}
6 changes: 3 additions & 3 deletions kiuatan/named_rule.pony
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ class NamedRule[S, D: Any #share = None, V: Any #share = None]
"""

let name: String
let memoize_failures: Bool
let memoize: Bool
var _body: (RuleNode[S, D, V] box | None)
var _action: (Action[S, D, V] | None)

new create(
name': String,
body': (RuleNode[S, D, V] box | None) = None,
action': (Action[S, D, V] | None) = None,
memoize_failures': Bool = true)
memoize': Bool = false)
=>
name = name'
_body = body'
_action = action'
memoize_failures = memoize_failures'
memoize = memoize'

fun body(): (this->(RuleNode[S, D, V] box) | None) =>
_body
Expand Down
41 changes: 35 additions & 6 deletions kiuatan/parser.pony
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,15 @@ actor Parser[S, D: Any #share = None, V: Any #share = None]

match rule.parse(this~_parse_named_rule(), 0, loc)
| let success: Success[S, D, V] =>
callback(success, success._values(data))
_lr_states.clear()
_lr_states.compact()
_memo.compact()
_parse_succeeded(success, data, callback)
| let failure: Failure[S, D, V] =>
callback(failure, [])
_lr_states.clear()
_lr_states.compact()
_memo.compact()
_parse_failed(failure, callback)
end
else
let loc =
Expand All @@ -152,10 +158,27 @@ actor Parser[S, D: Any #share = None, V: Any #share = None]
else
Loc[S](per.Cons[Segment[S]]([], per.Nil[Segment[S]]), 0)
end
callback(
Failure[S, D, V](rule, loc, ErrorMsg.empty_source()), Array[V])
let failure = Failure[S, D, V](rule, loc, ErrorMsg.empty_source())

_lr_states.clear()
_lr_states.compact()
_memo.compact()
_parse_failed(failure, callback)
end

be _parse_succeeded(
success: Success[S, D, V],
data: D,
callback: ParseCallback[S, D, V])
=>
callback(success, success._values(data))

be _parse_failed(
failure: Failure[S, D, V],
callback: ParseCallback[S, D, V])
=>
callback(failure, [])

fun ref _parse_named_rule(
depth: USize,
rule: NamedRule[S, D, V] val,
Expand All @@ -180,10 +203,12 @@ actor Parser[S, D: Any #share = None, V: Any #share = None]
then
match body.parse(this~_parse_named_rule(), depth + 1, loc)
| let success: Success[S, D, V] =>
_memoize(depth + 1, rule, loc, success)
if rule.memoize then
_memoize(depth + 1, rule, loc, success)
end
return success
| let failure: Failure[S, D, V] =>
if rule.memoize_failures then
if rule.memoize then
_memoize(depth + 1, rule, loc, failure)
end
return failure
Expand Down Expand Up @@ -376,6 +401,10 @@ actor Parser[S, D: Any #share = None, V: Any #share = None]
result: Result[S, D, V])
=>
for (rule, loc, res) in (consume results).values() do
if not rule.memoize then
continue
end

ifdef debug then
_Dbg.out(depth, "memoize " + rule.name + " " + res.string())
end
Expand Down
10 changes: 7 additions & 3 deletions kiuatan/test/test_examples.pony
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ class iso _TestExampleMain is UnitTest
fun apply(h: TestHelper) =>
let rule =
recover val
let ws = NamedRule[U8]("WhiteSpace", Star[U8](Single[U8](" \t"), 1))
NamedRule[U8]("OneTwoThree",
let ws = NamedRule[U8](
"WhiteSpace", Star[U8](Single[U8](" \t"), 1)
where memoize' = true)
NamedRule[U8](
"OneTwoThree",
Conj[U8](
[ Literal[U8]("one")
ws
Disj[U8]([ Literal[U8]("two"); Literal[U8]("deux") ])
ws
Literal[U8]("three")
]))
])
where memoize' = true)
end

let segment = "one two three"
Expand Down
6 changes: 4 additions & 2 deletions kiuatan/test/test_flatten.pony
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class iso _TestFlatten is UnitTest
fun apply(h: TestHelper) =>
let rule =
recover val
NamedRule[U8,None,USize]("Three",
NamedRule[U8,None,USize](
"Three",
Conj[U8,None,USize]([
NamedRule[U8,None,USize]("OneTwo",
Conj[U8,None,USize]([
Expand All @@ -17,7 +18,8 @@ class iso _TestFlatten is UnitTest
Literal[U8,None,USize]("three", {(_,r,_,b) => (USize(3),b)})
NamedRule[U8,None,USize]("Four",
Literal[U8,None,USize]("four", {(_,r,_,b) => (USize(4),b)}))
]))
])
where memoize' = true)
end

let segment = "onetwothreefour"
Expand Down
6 changes: 4 additions & 2 deletions kiuatan/test/test_look.pony
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class iso _TestLookChildren is UnitTest
NamedRule[U8, None, USize](
"sub",
Literal[U8, None, USize]("a"),
{(_, _, _, b) => (USize(0), b) })
{(_, _, _, b) => (USize(0), b) },
true)
end
let rule =
recover val
Expand All @@ -20,7 +21,8 @@ class iso _TestLookChildren is UnitTest
Conj[U8, None, USize](
[ Look[U8, None, USize](sub)
sub ]),
{(_, _, c, b) => (c.size(), b) })
{(_, _, c, b) => (c.size(), b) },
true)
end

let parser = Parser[U8, None, USize]([ "a" ])
Expand Down
Loading