Skip to content

Instantly share code, notes, and snippets.

@dockimbel
Last active October 24, 2017 08:46
Show Gist options
  • Save dockimbel/7d6e53364d8b6d5f50c363c61876165d to your computer and use it in GitHub Desktop.
Save dockimbel/7d6e53364d8b6d5f50c363c61876165d to your computer and use it in GitHub Desktop.
Function to get stats about internal parse states usage for a given parsing job.
Red [
Title: "Parse Info"
Purpose: {
Provides some stats about internal parse states usage for a given parsing job.
Could be used as a way to compare efficiency of different parsing strategies.
}
Date: 06-Oct-2017
]
context [
info: context [push: pop: fetch: match: iterate: paren: end: none]
on-parse-event: func [
event [word!] "Trace events: push, pop, fetch, match, iterate, paren, end"
match? [logic!] "Result of last matching operation"
rule [block!] "Current rule at current position"
input [series!] "Input series at next position to match"
stack [block!] "Internal parse rules stack"
return: [logic!] "TRUE: continue parsing, FALSE: stop and exit parsing"
][
info/:event: info/:event + 1
true
]
set 'parse-info func [
"Wrapper for parse/trace using a stats gathering event processor"
input [series!]
rules [block!]
/case
/part
limit [integer!]
return: [logic! block!]
][
set info 0
either case [
parse/case/trace input rules :on-parse-event
][
either part [
parse/part/trace input rules limit :on-parse-event
][
parse/trace input rules :on-parse-event
]
]
info
]
]
comment {
=== Example ===
>> probe parse-info "hello 123 world!" [some skip]
make object! [
push: 2
pop: 2
fetch: 16
match: 16
iterate: 15
paren: 0
end: 1
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment