factor/core/effects/parser/parser.factor

71 lines
2.0 KiB
Factor
Raw Normal View History

! Copyright (C) 2008, 2010 Slava Pestov.
2008-06-25 04:25:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: arrays combinators effects kernel lexer make namespaces
parser sequences splitting words ;
2008-06-25 04:25:08 -04:00
IN: effects.parser
2008-07-18 20:22:59 -04:00
DEFER: parse-effect
ERROR: bad-effect ;
2010-03-11 04:25:13 -05:00
ERROR: invalid-row-variable ;
ERROR: row-variable-can't-have-type ;
ERROR: stack-effect-omits-dashes ;
SYMBOL: effect-var
2010-03-11 04:03:40 -05:00
<PRIVATE
: end-token? ( end token -- token ? ) [ nip ] [ = ] 2bi ; inline
: effect-opener? ( token -- token ? ) dup { f "(" "((" "--" } member? ; inline
: effect-closer? ( token -- token ? ) dup { ")" "))" } member? ; inline
2010-03-11 04:25:13 -05:00
: row-variable? ( token -- token' ? ) ".." ?head ; inline
2010-03-11 04:03:40 -05:00
: parse-effect-var ( first? var name -- var )
nip
2010-03-11 04:25:13 -05:00
[ ":" ?tail [ row-variable-can't-have-type ] when ] curry
[ invalid-row-variable ] if ;
2010-03-11 04:03:40 -05:00
: parse-effect-value ( token -- value )
":" ?tail [ scan-object 2array ] when ;
2010-03-11 04:03:40 -05:00
PRIVATE>
: parse-effect-token ( first? var end -- var more? )
scan-token {
2010-03-11 04:03:40 -05:00
{ [ end-token? ] [ drop nip f ] }
{ [ effect-opener? ] [ bad-effect ] }
{ [ effect-closer? ] [ stack-effect-omits-dashes ] }
2010-03-11 04:25:13 -05:00
{ [ row-variable? ] [ parse-effect-var t ] }
2010-03-11 04:03:40 -05:00
[ [ drop ] 2dip parse-effect-value , t ]
} cond ;
2008-07-18 20:22:59 -04:00
: parse-effect-tokens ( end -- var tokens )
[
[ t f ] dip [ parse-effect-token [ f ] 2dip ] curry [ ] while nip
] { } make ;
2008-07-18 20:22:59 -04:00
: parse-effect ( end -- effect )
[ "--" parse-effect-tokens ] dip parse-effect-tokens
<variable-effect> ;
2009-03-16 21:11:36 -04:00
2011-10-17 01:50:30 -04:00
: scan-effect ( -- effect )
2009-03-22 18:59:40 -04:00
"(" expect ")" parse-effect ;
2009-03-16 21:11:36 -04:00
: parse-call( ( accum word -- accum )
[ ")" parse-effect ] dip 2array append! ;
SYMBOL: in-definition
ERROR: can't-nest-definitions word ;
: check-in-definition ( -- )
in-definition get [ word can't-nest-definitions ] when ;
: with-definition ( quot -- )
[ check-in-definition t in-definition ] dip with-variable ; inline
: (:) ( -- word def effect )
[
scan-new-word
scan-effect
parse-definition swap
] with-definition ;