]> gitweb.factorcode.org Git - factor.git/blob - core/effects/parser/parser.factor
change ERROR: words from throw-foo back to foo.
[factor.git] / core / effects / parser / parser.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays combinators effects kernel lexer make namespaces
4 parser sequences splitting words ;
5 IN: effects.parser
6
7 DEFER: parse-effect
8
9 ERROR: bad-effect ;
10 ERROR: invalid-row-variable ;
11 ERROR: row-variable-can't-have-type ;
12 ERROR: stack-effect-omits-dashes ;
13
14 SYMBOL: effect-var
15
16 <PRIVATE
17 : end-token? ( end token -- token ? ) [ nip ] [ = ] 2bi ; inline
18 : effect-opener? ( token -- token ? ) dup { f "(" "((" "--" } member? ; inline
19 : effect-closer? ( token -- token ? ) dup { ")" "))" } member? ; inline
20 : row-variable? ( token -- token' ? ) ".." ?head ; inline
21
22 : parse-effect-var ( first? var name -- var )
23     nip
24     [ ":" ?tail [ row-variable-can't-have-type ] when ] curry
25     [ invalid-row-variable ] if ;
26
27 : parse-effect-value ( token -- value )
28     ":" ?tail [ scan-object 2array ] when ;
29 PRIVATE>
30
31 : parse-effect-token ( first? var end -- var more? )
32     scan-token {
33         { [ end-token? ] [ drop nip f ] }
34         { [ effect-opener? ] [ bad-effect ] }
35         { [ effect-closer? ] [ stack-effect-omits-dashes ] }
36         { [ row-variable? ] [ parse-effect-var t ] }
37         [ [ drop ] 2dip parse-effect-value , t ]
38     } cond ;
39
40 : parse-effect-tokens ( end -- var tokens )
41     [
42         [ t f ] dip [ parse-effect-token [ f ] 2dip ] curry [ ] while nip
43     ] { } make ;
44
45 : parse-effect ( end -- effect )
46     [ "--" parse-effect-tokens ] dip parse-effect-tokens
47     <variable-effect> ;
48
49 : scan-effect ( -- effect )
50     "(" expect ")" parse-effect ;
51
52 : parse-call-paren ( accum word -- accum )
53     [ ")" parse-effect ] dip 2array append! ;
54
55 SYMBOL: in-definition
56
57 ERROR: can't-nest-definitions word ;
58
59 : check-in-definition ( -- )
60     in-definition get [ last-word can't-nest-definitions ] when ;
61
62 : with-definition ( quot -- )
63     [ check-in-definition t in-definition ] dip with-variable ; inline
64
65 : (:) ( -- word def effect )
66     [
67         scan-new-word
68         scan-effect
69         parse-definition swap
70     ] with-definition ;