From 0db0d9cd444eaa920088f172844b4fdbc0f690b7 Mon Sep 17 00:00:00 2001 From: Chris Double Date: Sat, 29 Mar 2008 16:24:13 +1300 Subject: [PATCH 1/4] Move towards having ebnf infer --- extra/peg/ebnf/ebnf.factor | 2 +- extra/peg/peg.factor | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/extra/peg/ebnf/ebnf.factor b/extra/peg/ebnf/ebnf.factor index 3efe2d6979..76e851efd3 100644 --- a/extra/peg/ebnf/ebnf.factor +++ b/extra/peg/ebnf/ebnf.factor @@ -282,7 +282,7 @@ M: ebnf-non-terminal (transform) ( ast -- parser ) : ebnf>quot ( string -- hashtable quot ) 'ebnf' parse check-parse-result - parse-result-ast transform dup main swap at compile [ parse ] curry ; + parse-result-ast transform dup main swap at compile [ compiled-parse ] curry ; : [EBNF "EBNF]" parse-multiline-string ebnf>quot nip parsed ; parsing diff --git a/extra/peg/peg.factor b/extra/peg/peg.factor index 8f7522bda9..be4bba25fc 100755 --- a/extra/peg/peg.factor +++ b/extra/peg/peg.factor @@ -192,7 +192,7 @@ C: peg-head f lrstack set H{ } clone heads set H{ } clone packrat set - ] H{ } make-assoc swap bind ; + ] H{ } make-assoc swap bind ; inline : compiled-parsers ( -- cache ) @@ -236,9 +236,11 @@ GENERIC: (compile) ( parser -- quot ) : compile ( parser -- word ) [ compiled-parser ] with-compilation-unit ; +: compiled-parse ( state word -- result ) + swap [ execute ] with-packrat ; inline + : parse ( state parser -- result ) - dup word? [ compile ] unless - [ execute ] curry with-packrat ; + dup word? [ compile ] unless compiled-parse ; Date: Sat, 29 Mar 2008 17:42:21 +1300 Subject: [PATCH 2/4] Don't use 'delay' parser in ebnf --- extra/peg/ebnf/ebnf.factor | 7 ++++--- extra/peg/peg.factor | 12 ++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/extra/peg/ebnf/ebnf.factor b/extra/peg/ebnf/ebnf.factor index 76e851efd3..c1e2ce8546 100644 --- a/extra/peg/ebnf/ebnf.factor +++ b/extra/peg/ebnf/ebnf.factor @@ -262,8 +262,8 @@ M: ebnf-terminal (transform) ( ast -- parser ) M: ebnf-non-terminal (transform) ( ast -- parser ) symbol>> [ - , parser get , \ at , - ] [ ] make delay sp ; + , parser get , \ at , \ sp , + ] [ ] make box ; : transform-ebnf ( string -- object ) 'ebnf' parse parse-result-ast transform ; @@ -282,7 +282,8 @@ M: ebnf-non-terminal (transform) ( ast -- parser ) : ebnf>quot ( string -- hashtable quot ) 'ebnf' parse check-parse-result - parse-result-ast transform dup main swap at compile [ compiled-parse ] curry ; + parse-result-ast transform dup dup parser [ main swap at compile ] with-variable + [ compiled-parse ] curry ; : [EBNF "EBNF]" parse-multiline-string ebnf>quot nip parsed ; parsing diff --git a/extra/peg/peg.factor b/extra/peg/peg.factor index be4bba25fc..5ec934d994 100755 --- a/extra/peg/peg.factor +++ b/extra/peg/peg.factor @@ -489,6 +489,15 @@ M: delay-parser (compile) ( parser -- quot ) { } { "word" } memoize-quot [ % \ execute , ] [ ] make ; +TUPLE: box-parser quot ; + +M: box-parser (compile) ( parser -- quot ) + #! Calls the quotation at compile time + #! to produce the parser to be compiled. + #! This differs from 'delay' which calls + #! it at run time. + quot>> call compiled-parser 1quotation ; + PRIVATE> : token ( string -- parser ) @@ -557,6 +566,9 @@ PRIVATE> : delay ( quot -- parser ) delay-parser construct-boa init-parser ; +: box ( quot -- parser ) + box-parser construct-boa init-parser ; + : PEG: (:) [ [ From 8105e66aece2f6c466523542c0c04b0553d79b67 Mon Sep 17 00:00:00 2001 From: Chris Double Date: Sat, 29 Mar 2008 17:45:21 +1300 Subject: [PATCH 3/4] Add box parser to docs --- extra/peg/peg-docs.factor | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/extra/peg/peg-docs.factor b/extra/peg/peg-docs.factor index d2ca353ba1..7b13e06d5a 100644 --- a/extra/peg/peg-docs.factor +++ b/extra/peg/peg-docs.factor @@ -159,4 +159,17 @@ HELP: delay { $description "Delays the construction of a parser until it is actually required to parse. This " "allows for calling a parser that results in a recursive call to itself. The quotation " - "should return the constructed parser." } ; + "should return the constructed parser and is called the first time the parser is run." + "The compiled result is memoized for future runs. See " { $link box } " for a word " + "that calls the quotation at compile time." } ; + +HELP: box +{ $values + { "quot" "a quotation" } + { "parser" "a parser" } +} +{ $description + "Delays the construction of a parser until the parser is compiled. The quotation " + "should return the constructed parser and is called when the parser is compiled." + "The compiled result is memoized for future runs. See " { $link delay } " for a word " + "that calls the quotation at runtime." } ; From ca4f77575611df8c4c6d6f53d1ac25372f8cac7f Mon Sep 17 00:00:00 2001 From: Chris Double Date: Sat, 29 Mar 2008 18:33:37 +1300 Subject: [PATCH 4/4] Fix PEG: --- extra/peg/peg.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/peg/peg.factor b/extra/peg/peg.factor index 5ec934d994..6f2e5bce95 100755 --- a/extra/peg/peg.factor +++ b/extra/peg/peg.factor @@ -572,7 +572,7 @@ PRIVATE> : PEG: (:) [ [ - call compile 1quotation + call compile [ compiled-parse ] curry [ dup [ parse-result-ast ] [ "Parse failed" throw ] if ] append define ] with-compilation-unit