Merge branch 'master' of git://factorcode.org/git/factor
commit
070432d140
|
@ -233,15 +233,22 @@ ERROR: invalid-range a b ;
|
||||||
SINGLETON: beginning-of-input
|
SINGLETON: beginning-of-input
|
||||||
SINGLETON: end-of-input
|
SINGLETON: end-of-input
|
||||||
|
|
||||||
! : beginning-of-input ( -- obj )
|
: newlines ( -- obj1 obj2 obj3 )
|
||||||
: handle-front-anchor ( -- ) front-anchor push-stack ;
|
|
||||||
: end-of-line ( -- obj )
|
|
||||||
end-of-input
|
|
||||||
CHAR: \r <constant>
|
CHAR: \r <constant>
|
||||||
CHAR: \n <constant>
|
CHAR: \n <constant>
|
||||||
2dup 2array <concatenation> 4array <alternation> lookahead boa ;
|
2dup 2array <concatenation> ;
|
||||||
|
|
||||||
: handle-back-anchor ( -- ) end-of-line push-stack ;
|
: beginning-of-line ( -- obj )
|
||||||
|
beginning-of-input newlines 4array <alternation> lookbehind boa ;
|
||||||
|
|
||||||
|
: end-of-line ( -- obj )
|
||||||
|
end-of-input newlines 4array <alternation> lookahead boa ;
|
||||||
|
|
||||||
|
: handle-front-anchor ( -- )
|
||||||
|
get-multiline beginning-of-line beginning-of-input ? push-stack ;
|
||||||
|
|
||||||
|
: handle-back-anchor ( -- )
|
||||||
|
get-multiline end-of-line end-of-input ? push-stack ;
|
||||||
|
|
||||||
ERROR: bad-character-class obj ;
|
ERROR: bad-character-class obj ;
|
||||||
ERROR: expected-posix-class ;
|
ERROR: expected-posix-class ;
|
||||||
|
@ -412,16 +419,11 @@ DEFER: handle-left-bracket
|
||||||
[ [ push ] keep current-regexp get (>>stack) ]
|
[ [ push ] keep current-regexp get (>>stack) ]
|
||||||
[ finish-regexp-parse push-stack ] bi* ;
|
[ finish-regexp-parse push-stack ] bi* ;
|
||||||
|
|
||||||
|
|
||||||
: parse-regexp-token ( token -- ? )
|
: parse-regexp-token ( token -- ? )
|
||||||
{
|
{
|
||||||
! todo: only match these at beginning/end of regexp
|
{ CHAR: ( [ handle-left-parenthesis t ] } ! handle (?..) at beginning?
|
||||||
{ CHAR: ^ [ handle-front-anchor t ] }
|
|
||||||
{ CHAR: $ [ handle-back-anchor t ] }
|
|
||||||
|
|
||||||
{ CHAR: . [ handle-dot t ] }
|
|
||||||
{ CHAR: ( [ handle-left-parenthesis t ] }
|
|
||||||
{ CHAR: ) [ handle-right-parenthesis f ] }
|
{ CHAR: ) [ handle-right-parenthesis f ] }
|
||||||
|
{ CHAR: . [ handle-dot t ] }
|
||||||
{ CHAR: | [ handle-pipe t ] }
|
{ CHAR: | [ handle-pipe t ] }
|
||||||
{ CHAR: ? [ handle-question t ] }
|
{ CHAR: ? [ handle-question t ] }
|
||||||
{ CHAR: * [ handle-star t ] }
|
{ CHAR: * [ handle-star t ] }
|
||||||
|
@ -429,16 +431,28 @@ DEFER: handle-left-bracket
|
||||||
{ CHAR: { [ handle-left-brace t ] }
|
{ CHAR: { [ handle-left-brace t ] }
|
||||||
{ CHAR: [ [ handle-left-bracket t ] }
|
{ CHAR: [ [ handle-left-bracket t ] }
|
||||||
{ CHAR: \ [ handle-escape t ] }
|
{ CHAR: \ [ handle-escape t ] }
|
||||||
[ <constant> push-stack t ]
|
[
|
||||||
|
dup CHAR: $ = peek1 f = and [
|
||||||
|
drop
|
||||||
|
handle-back-anchor f
|
||||||
|
] [
|
||||||
|
<constant> push-stack t
|
||||||
|
] if
|
||||||
|
]
|
||||||
} case ;
|
} case ;
|
||||||
|
|
||||||
: (parse-regexp) ( -- )
|
: (parse-regexp) ( -- )
|
||||||
read1 [ parse-regexp-token [ (parse-regexp) ] when ] when* ;
|
read1 [ parse-regexp-token [ (parse-regexp) ] when ] when* ;
|
||||||
|
|
||||||
|
: parse-regexp-beginning ( -- )
|
||||||
|
peek1 CHAR: ^ = [ drop1 handle-front-anchor ] when ;
|
||||||
|
|
||||||
: parse-regexp ( regexp -- )
|
: parse-regexp ( regexp -- )
|
||||||
dup current-regexp [
|
dup current-regexp [
|
||||||
raw>> [
|
raw>> [
|
||||||
<string-reader> [ (parse-regexp) ] with-input-stream
|
<string-reader> [
|
||||||
|
parse-regexp-beginning (parse-regexp)
|
||||||
|
] with-input-stream
|
||||||
] unless-empty
|
] unless-empty
|
||||||
current-regexp get
|
current-regexp get
|
||||||
stack finish-regexp-parse
|
stack finish-regexp-parse
|
||||||
|
|
|
@ -331,4 +331,3 @@ IN: regexp-tests
|
||||||
[ { 0 3 } ] [ "abc" "(ab|a)(bc)?" <regexp> first-match ] unit-test
|
[ { 0 3 } ] [ "abc" "(ab|a)(bc)?" <regexp> first-match ] unit-test
|
||||||
|
|
||||||
[ { 23 24 } ] [ "aaaaaaaaaaaaaaaaaaaaaaab" "((a*)*b)*b" <regexp> first-match ] unit-test
|
[ { 23 24 } ] [ "aaaaaaaaaaaaaaaaaaaaaaab" "((a*)*b)*b" <regexp> first-match ] unit-test
|
||||||
|
|
||||||
|
|
|
@ -92,7 +92,6 @@ IN: regexp
|
||||||
reversed-regexp initial-option
|
reversed-regexp initial-option
|
||||||
construct-regexp ;
|
construct-regexp ;
|
||||||
|
|
||||||
|
|
||||||
: parsing-regexp ( accum end -- accum )
|
: parsing-regexp ( accum end -- accum )
|
||||||
lexer get dup skip-blank
|
lexer get dup skip-blank
|
||||||
[ [ index-from dup 1+ swap ] 2keep swapd subseq swap ] change-lexer-column
|
[ [ index-from dup 1+ swap ] 2keep swapd subseq swap ] change-lexer-column
|
||||||
|
@ -112,7 +111,6 @@ IN: regexp
|
||||||
: R{ CHAR: } parsing-regexp ; parsing
|
: R{ CHAR: } parsing-regexp ; parsing
|
||||||
: R| CHAR: | parsing-regexp ; parsing
|
: R| CHAR: | parsing-regexp ; parsing
|
||||||
|
|
||||||
|
|
||||||
: find-regexp-syntax ( string -- prefix suffix )
|
: find-regexp-syntax ( string -- prefix suffix )
|
||||||
{
|
{
|
||||||
{ "R/ " "/" }
|
{ "R/ " "/" }
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors assocs combinators kernel math math.ranges
|
USING: accessors assocs combinators kernel math math.ranges
|
||||||
quotations sequences regexp.parser regexp.classes fry arrays
|
quotations sequences regexp.parser regexp.classes fry arrays
|
||||||
combinators.short-circuit regexp.utils prettyprint regexp.nfa ;
|
combinators.short-circuit regexp.utils prettyprint regexp.nfa
|
||||||
|
shuffle ;
|
||||||
IN: regexp.traversal
|
IN: regexp.traversal
|
||||||
|
|
||||||
TUPLE: dfa-traverser
|
TUPLE: dfa-traverser
|
||||||
|
@ -23,8 +24,7 @@ TUPLE: dfa-traverser
|
||||||
[ dfa-table>> ] [ dfa-traversal-flags>> ] bi
|
[ dfa-table>> ] [ dfa-traversal-flags>> ] bi
|
||||||
dfa-traverser new
|
dfa-traverser new
|
||||||
swap >>traversal-flags
|
swap >>traversal-flags
|
||||||
swap [ start-state>> >>current-state ] keep
|
swap [ start-state>> >>current-state ] [ >>dfa-table ] bi
|
||||||
>>dfa-table
|
|
||||||
swap >>text
|
swap >>text
|
||||||
t >>traverse-forward
|
t >>traverse-forward
|
||||||
0 >>start-index
|
0 >>start-index
|
||||||
|
@ -116,7 +116,7 @@ M: capture-group-off flag-action ( dfa-traverser flag -- )
|
||||||
V{ } clone >>matches ;
|
V{ } clone >>matches ;
|
||||||
|
|
||||||
: match-literal ( transition from-state table -- to-state/f )
|
: match-literal ( transition from-state table -- to-state/f )
|
||||||
transitions>> at* [ at ] [ 2drop f ] if ;
|
transitions>> at at ;
|
||||||
|
|
||||||
: match-class ( transition from-state table -- to-state/f )
|
: match-class ( transition from-state table -- to-state/f )
|
||||||
transitions>> at* [
|
transitions>> at* [
|
||||||
|
@ -124,8 +124,7 @@ M: capture-group-off flag-action ( dfa-traverser flag -- )
|
||||||
] [ drop ] if ;
|
] [ drop ] if ;
|
||||||
|
|
||||||
: match-default ( transition from-state table -- to-state/f )
|
: match-default ( transition from-state table -- to-state/f )
|
||||||
[ nip ] dip transitions>> at*
|
nipd transitions>> at t swap at ;
|
||||||
[ t swap at* [ ] [ drop f ] if ] [ drop f ] if ;
|
|
||||||
|
|
||||||
: match-transition ( obj from-state dfa -- to-state/f )
|
: match-transition ( obj from-state dfa -- to-state/f )
|
||||||
{ [ match-literal ] [ match-class ] [ match-default ] } 3|| ;
|
{ [ match-literal ] [ match-class ] [ match-default ] } 3|| ;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
! Copyright (c) 2007, 2008 Aaron Schaefer, Samuel Tardieu.
|
! Copyright (c) 2007, 2008 Aaron Schaefer, Samuel Tardieu.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: definitions io io.files kernel math math.parser project-euler.ave-time
|
USING: definitions io io.files kernel math math.parser project-euler.ave-time
|
||||||
sequences vocabs vocabs.loader
|
sequences vocabs vocabs.loader prettyprint
|
||||||
project-euler.001 project-euler.002 project-euler.003 project-euler.004
|
project-euler.001 project-euler.002 project-euler.003 project-euler.004
|
||||||
project-euler.005 project-euler.006 project-euler.007 project-euler.008
|
project-euler.005 project-euler.006 project-euler.007 project-euler.008
|
||||||
project-euler.009 project-euler.010 project-euler.011 project-euler.012
|
project-euler.009 project-euler.010 project-euler.011 project-euler.012
|
||||||
|
@ -33,7 +33,7 @@ IN: project-euler
|
||||||
|
|
||||||
: solution-path ( n -- str/f )
|
: solution-path ( n -- str/f )
|
||||||
number>euler "project-euler." prepend
|
number>euler "project-euler." prepend
|
||||||
vocab where dup [ first ] when ;
|
vocab where dup [ first <pathname> ] when ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
|
@ -43,8 +43,8 @@ PRIVATE>
|
||||||
: run-project-euler ( -- )
|
: run-project-euler ( -- )
|
||||||
problem-prompt dup problem-solved? [
|
problem-prompt dup problem-solved? [
|
||||||
dup number>euler "project-euler." prepend run
|
dup number>euler "project-euler." prepend run
|
||||||
"Answer: " swap dup number? [ number>string ] when append print
|
"Answer: " write dup number? [ number>string ] when print
|
||||||
"Source: " swap solution-path append print
|
"Source: " write solution-path .
|
||||||
] [
|
] [
|
||||||
drop "That problem has not been solved yet..." print
|
drop "That problem has not been solved yet..." print
|
||||||
] if ;
|
] if ;
|
||||||
|
|
Loading…
Reference in New Issue