Make peg compilation infer
parent
4302e36424
commit
7ed56a3cef
|
@ -5,7 +5,7 @@ sequences quotations vectors namespaces make math assocs
|
|||
continuations peg peg.parsers unicode.categories multiline
|
||||
splitting accessors effects sequences.deep peg.search
|
||||
combinators.short-circuit lexer io.streams.string stack-checker
|
||||
io combinators parser ;
|
||||
io combinators parser call ;
|
||||
IN: peg.ebnf
|
||||
|
||||
: rule ( name word -- parser )
|
||||
|
@ -36,7 +36,7 @@ TUPLE: tokenizer any one many ;
|
|||
|
||||
: TOKENIZER:
|
||||
scan search [ "Tokenizer not found" throw ] unless*
|
||||
execute \ tokenizer set-global ; parsing
|
||||
execute( -- tokenizer ) \ tokenizer set-global ; parsing
|
||||
|
||||
TUPLE: ebnf-non-terminal symbol ;
|
||||
TUPLE: ebnf-terminal symbol ;
|
||||
|
@ -391,7 +391,7 @@ M: ebnf-choice (transform) ( ast -- parser )
|
|||
options>> [ (transform) ] map choice ;
|
||||
|
||||
M: ebnf-any-character (transform) ( ast -- parser )
|
||||
drop tokenizer any>> call ;
|
||||
drop tokenizer any>> call( -- parser ) ;
|
||||
|
||||
M: ebnf-range (transform) ( ast -- parser )
|
||||
pattern>> range-pattern ;
|
||||
|
@ -469,17 +469,17 @@ ERROR: bad-effect quot effect ;
|
|||
|
||||
M: ebnf-action (transform) ( ast -- parser )
|
||||
[ parser>> (transform) ] [ code>> insert-escapes ] [ parser>> ] tri build-locals
|
||||
string-lines parse-lines check-action-effect action ;
|
||||
[ string-lines parse-lines ] call( string -- quot ) check-action-effect action ;
|
||||
|
||||
M: ebnf-semantic (transform) ( ast -- parser )
|
||||
[ parser>> (transform) ] [ code>> insert-escapes ] [ parser>> ] tri build-locals
|
||||
string-lines parse-lines semantic ;
|
||||
[ string-lines parse-lines ] call( string -- quot ) semantic ;
|
||||
|
||||
M: ebnf-var (transform) ( ast -- parser )
|
||||
parser>> (transform) ;
|
||||
|
||||
M: ebnf-terminal (transform) ( ast -- parser )
|
||||
symbol>> tokenizer one>> call ;
|
||||
symbol>> tokenizer one>> call( symbol -- parser ) ;
|
||||
|
||||
M: ebnf-foreign (transform) ( ast -- parser )
|
||||
dup word>> search
|
||||
|
@ -487,7 +487,7 @@ M: ebnf-foreign (transform) ( ast -- parser )
|
|||
swap rule>> [ main ] unless* over rule [
|
||||
nip
|
||||
] [
|
||||
execute
|
||||
execute( -- parser )
|
||||
] if* ;
|
||||
|
||||
: parser-not-found ( name -- * )
|
||||
|
|
|
@ -5,6 +5,8 @@ USING: kernel tools.test strings namespaces make arrays sequences
|
|||
peg peg.private peg.parsers accessors words math accessors ;
|
||||
IN: peg.tests
|
||||
|
||||
\ parse must-infer
|
||||
|
||||
[ ] [ reset-pegs ] unit-test
|
||||
|
||||
[
|
||||
|
|
|
@ -4,7 +4,7 @@ USING: kernel sequences strings fry namespaces make math assocs
|
|||
io vectors arrays math.parser math.order vectors combinators
|
||||
classes sets unicode.categories compiler.units parser words
|
||||
quotations effects memoize accessors locals effects splitting
|
||||
combinators.short-circuit generalizations ;
|
||||
combinators.short-circuit generalizations call ;
|
||||
IN: peg
|
||||
|
||||
TUPLE: parse-result remaining ast ;
|
||||
|
@ -298,7 +298,7 @@ SYMBOL: delayed
|
|||
#! Work through all delayed parsers and recompile their
|
||||
#! words to have the correct bodies.
|
||||
delayed get [
|
||||
call compile-parser 1quotation (( -- result )) define-declared
|
||||
call( -- parser ) compile-parser 1quotation (( -- result )) define-declared
|
||||
] assoc-each ;
|
||||
|
||||
: compile ( parser -- word )
|
||||
|
@ -309,7 +309,7 @@ SYMBOL: delayed
|
|||
] with-compilation-unit ;
|
||||
|
||||
: compiled-parse ( state word -- result )
|
||||
swap [ execute [ error-stack get first throw ] unless* ] with-packrat ; inline
|
||||
swap [ execute( -- result ) [ error-stack get first throw ] unless* ] with-packrat ;
|
||||
|
||||
: (parse) ( input parser -- result )
|
||||
dup word? [ compile ] unless compiled-parse ;
|
||||
|
@ -527,7 +527,7 @@ M: box-parser (compile) ( peg -- quot )
|
|||
#! to produce the parser to be compiled.
|
||||
#! This differs from 'delay' which calls
|
||||
#! it at run time.
|
||||
quot>> call compile-parser 1quotation ;
|
||||
quot>> call( -- parser ) compile-parser 1quotation ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
|
|
|
@ -17,3 +17,5 @@ IN: peg.search.tests
|
|||
"abc 123 def 456" 'integer' [ 2 * number>string ] action replace
|
||||
] unit-test
|
||||
|
||||
\ search must-infer
|
||||
\ replace must-infer
|
||||
|
|
Loading…
Reference in New Issue