2008-08-26 21:24:14 -04:00
|
|
|
! Copyright (C) 2008 Doug Coleman.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-02-18 13:27:07 -05:00
|
|
|
USING: accessors arrays assocs grouping kernel
|
|
|
|
locals math namespaces sequences fry quotations
|
|
|
|
math.order math.ranges vectors unicode.categories
|
2009-02-19 17:48:46 -05:00
|
|
|
regexp.transition-tables words sets hashtables
|
2009-02-18 13:27:07 -05:00
|
|
|
unicode.case.private regexp.ast regexp.classes ;
|
2009-01-08 20:07:46 -05:00
|
|
|
! This uses unicode.case.private for ch>upper and ch>lower
|
|
|
|
! but case-insensitive matching should be done by case-folding everything
|
|
|
|
! before processing starts
|
2008-09-18 15:42:16 -04:00
|
|
|
IN: regexp.nfa
|
2008-08-26 21:24:14 -04:00
|
|
|
|
2009-02-16 21:23:00 -05:00
|
|
|
SYMBOL: negated?
|
2008-08-26 21:24:14 -04:00
|
|
|
|
2009-02-18 13:27:07 -05:00
|
|
|
: negate ( -- )
|
|
|
|
negated? [ not ] change ;
|
|
|
|
|
2008-08-26 21:24:14 -04:00
|
|
|
SINGLETON: eps
|
|
|
|
|
2009-02-18 13:27:07 -05:00
|
|
|
SYMBOL: option-stack
|
2008-11-24 23:17:47 -05:00
|
|
|
|
2009-02-18 13:27:07 -05:00
|
|
|
SYMBOL: state
|
2008-11-24 23:17:47 -05:00
|
|
|
|
2009-02-18 13:27:07 -05:00
|
|
|
: next-state ( -- state )
|
|
|
|
state [ get ] [ inc ] bi ;
|
2008-11-24 13:59:29 -05:00
|
|
|
|
2009-02-18 13:27:07 -05:00
|
|
|
SYMBOL: nfa-table
|
2009-02-19 17:48:46 -05:00
|
|
|
: table ( -- table ) nfa-table get ;
|
2008-08-26 21:24:14 -04:00
|
|
|
|
2009-02-18 13:27:07 -05:00
|
|
|
: set-each ( keys value hashtable -- )
|
|
|
|
'[ _ swap _ set-at ] each ;
|
|
|
|
|
|
|
|
: options>hash ( options -- hashtable )
|
|
|
|
H{ } clone [
|
|
|
|
[ [ on>> t ] dip set-each ]
|
|
|
|
[ [ off>> f ] dip set-each ] 2bi
|
|
|
|
] keep ;
|
|
|
|
|
|
|
|
: using-options ( options quot -- )
|
|
|
|
[ options>hash option-stack [ ?push ] change ] dip
|
|
|
|
call option-stack get pop* ; inline
|
|
|
|
|
|
|
|
: option? ( obj -- ? )
|
|
|
|
option-stack get assoc-stack ;
|
|
|
|
|
2009-02-19 17:48:46 -05:00
|
|
|
GENERIC: nfa-node ( node -- start-state end-state )
|
2008-08-26 21:24:14 -04:00
|
|
|
|
2009-02-19 17:48:46 -05:00
|
|
|
:: add-simple-entry ( obj class -- start-state end-state )
|
|
|
|
next-state :> s0
|
|
|
|
next-state :> s1
|
|
|
|
negated? get [
|
|
|
|
s0 f obj class make-transition table add-transition
|
|
|
|
s0 s1 <default-transition> table add-transition
|
|
|
|
] [
|
|
|
|
s0 s1 obj class make-transition table add-transition
|
|
|
|
] if
|
|
|
|
s0 s1 ;
|
|
|
|
|
|
|
|
: epsilon-transition ( source target -- )
|
|
|
|
eps <literal-transition> table add-transition ;
|
|
|
|
|
|
|
|
M:: star nfa-node ( node -- start end )
|
|
|
|
node term>> nfa-node :> s1 :> s0
|
|
|
|
next-state :> s2
|
|
|
|
next-state :> s3
|
|
|
|
s1 s0 epsilon-transition
|
|
|
|
s2 s0 epsilon-transition
|
|
|
|
s2 s3 epsilon-transition
|
|
|
|
s1 s3 epsilon-transition
|
|
|
|
s2 s3 ;
|
|
|
|
|
|
|
|
M: epsilon nfa-node
|
|
|
|
drop eps literal-transition add-simple-entry ;
|
|
|
|
|
|
|
|
M: concatenation nfa-node ( node -- start end )
|
|
|
|
[ first>> ] [ second>> ] bi
|
|
|
|
reversed-regexp option? [ swap ] when
|
|
|
|
[ nfa-node ] bi@
|
|
|
|
[ epsilon-transition ] dip ;
|
|
|
|
|
|
|
|
:: alternate-nodes ( s0 s1 s2 s3 -- start end )
|
|
|
|
next-state :> s4
|
|
|
|
next-state :> s5
|
|
|
|
s4 s0 epsilon-transition
|
|
|
|
s4 s2 epsilon-transition
|
|
|
|
s1 s5 epsilon-transition
|
|
|
|
s3 s5 epsilon-transition
|
|
|
|
s4 s5 ;
|
|
|
|
|
|
|
|
M: alternation nfa-node ( node -- start end )
|
|
|
|
[ first>> ] [ second>> ] bi
|
|
|
|
[ nfa-node ] bi@
|
|
|
|
alternate-nodes ;
|
|
|
|
|
|
|
|
M: integer nfa-node ( node -- start end )
|
2008-11-24 23:17:47 -05:00
|
|
|
case-insensitive option? [
|
2009-02-18 13:27:07 -05:00
|
|
|
dup [ ch>lower ] [ ch>upper ] bi
|
2008-11-24 23:17:47 -05:00
|
|
|
2dup = [
|
|
|
|
2drop
|
2009-02-18 13:27:07 -05:00
|
|
|
literal-transition add-simple-entry
|
2008-11-24 23:17:47 -05:00
|
|
|
] [
|
|
|
|
[ literal-transition add-simple-entry ] bi@
|
2009-02-19 17:48:46 -05:00
|
|
|
alternate-nodes [ nip ] dip
|
2008-11-24 23:17:47 -05:00
|
|
|
] if
|
2009-02-19 19:28:54 -05:00
|
|
|
] [ literal-transition add-simple-entry ] if ;
|
2008-08-26 21:24:14 -04:00
|
|
|
|
2009-02-19 17:48:46 -05:00
|
|
|
M: primitive-class nfa-node ( node -- start end )
|
2009-02-18 13:27:07 -05:00
|
|
|
class>> dup
|
|
|
|
{ letter-class LETTER-class } member? case-insensitive option? and
|
|
|
|
[ drop Letter-class ] when
|
|
|
|
class-transition add-simple-entry ;
|
2008-11-24 23:17:47 -05:00
|
|
|
|
2009-02-19 19:28:54 -05:00
|
|
|
M: or-class nfa-node class-transition add-simple-entry ;
|
|
|
|
M: not-class nfa-node class-transition add-simple-entry ;
|
|
|
|
|
2009-02-19 17:48:46 -05:00
|
|
|
M: any-char nfa-node ( node -- start end )
|
2008-11-24 23:17:47 -05:00
|
|
|
[ dotall option? ] dip any-char-no-nl ?
|
2008-08-26 21:24:14 -04:00
|
|
|
class-transition add-simple-entry ;
|
|
|
|
|
2009-02-19 19:28:54 -05:00
|
|
|
! M: negation nfa-node ( node -- start end )
|
|
|
|
! negate term>> nfa-node negate ;
|
2008-11-24 23:17:47 -05:00
|
|
|
|
2009-02-19 17:48:46 -05:00
|
|
|
M: range nfa-node ( node -- start end )
|
2008-11-24 23:17:47 -05:00
|
|
|
case-insensitive option? [
|
2009-01-08 20:07:46 -05:00
|
|
|
! This should be implemented for Unicode by case-folding
|
|
|
|
! the input and all strings in the regexp.
|
2008-11-24 23:17:47 -05:00
|
|
|
dup [ from>> ] [ to>> ] bi
|
|
|
|
2dup [ Letter? ] bi@ and [
|
|
|
|
rot drop
|
2009-02-18 13:27:07 -05:00
|
|
|
[ [ ch>lower ] bi@ <range> ]
|
|
|
|
[ [ ch>upper ] bi@ <range> ] 2bi
|
2008-11-24 23:17:47 -05:00
|
|
|
[ class-transition add-simple-entry ] bi@
|
|
|
|
alternate-nodes
|
|
|
|
] [
|
|
|
|
2drop
|
|
|
|
class-transition add-simple-entry
|
|
|
|
] if
|
|
|
|
] [
|
|
|
|
class-transition add-simple-entry
|
|
|
|
] if ;
|
2008-08-26 21:24:14 -04:00
|
|
|
|
2009-02-19 17:48:46 -05:00
|
|
|
M: with-options nfa-node ( node -- start end )
|
2009-02-18 13:27:07 -05:00
|
|
|
dup options>> [ tree>> nfa-node ] using-options ;
|
2008-11-24 23:17:47 -05:00
|
|
|
|
2009-02-18 13:27:07 -05:00
|
|
|
: construct-nfa ( ast -- nfa-table )
|
2008-08-26 21:24:14 -04:00
|
|
|
[
|
2009-02-18 13:27:07 -05:00
|
|
|
negated? off
|
|
|
|
0 state set
|
|
|
|
<transition-table> clone nfa-table set
|
|
|
|
nfa-node
|
2009-02-19 17:48:46 -05:00
|
|
|
table
|
|
|
|
swap dup associate >>final-states
|
|
|
|
swap >>start-state
|
2008-08-26 21:24:14 -04:00
|
|
|
] with-scope ;
|