1 USING: hashtables assocs sequences locals math accessors multiline delegate strings
2 delegate.protocols kernel peg peg.ebnf lexer namespaces combinators parser words ;
6 CONSULT: assoc-protocol lex-hash hash>> ;
7 : <lex-hash> ( a -- lex-hash ) lex-hash boa ;
9 : pos-or-0 ( neg? -- pos/0 ) dup 0 < [ drop 0 ] when ;
11 :: prepare-pos ( v i -- c l )
12 [let | n [ i v head-slice ] |
13 v CHAR: \n n last-index -1 or 1+ -
14 n [ CHAR: \n = ] count 1+
17 : store-pos ( v a -- )
18 input swap at prepare-pos
19 lexer get [ (>>line) ] keep (>>column) ;
24 [ swap hash>> set-at ]
27 :: at-pos ( t l c -- p ) t l head-slice [ length ] map sum l 1- + c + ;
31 { input [ drop lexer get text>> "\n" join t ] }
32 { pos [ drop lexer get [ text>> ] [ line>> 1- ] [ column>> 1+ ] tri at-pos t ] }
36 : with-global-lexer ( quot -- result )
39 V{ } clone error-stack set H{ } clone \ heads set
40 H{ } clone \ packrat set
41 ] f make-assoc <lex-hash>
44 : parse* ( parser -- ast )
46 [ execute [ error-stack get first throw ] unless* ] with-global-lexer
49 : create-bnf ( name parser -- )
50 reset-tokenizer [ lexer get skip-blank parse* parsed ] curry
54 CREATE-WORD reset-tokenizer ";ON-BNF" parse-multiline-string parse-ebnf
55 main swap at create-bnf ;
57 ! Tokenizer like standard factor lexer
59 space = " " | "\n" | "\t"
60 spaces = space* => [[ drop ignore ]]
61 chunk = (!(space) .)+ => [[ >string ]]