factor/basis/peg/parsers/parsers.factor

109 lines
2.9 KiB
Factor
Raw Normal View History

! Copyright (C) 2007, 2008 Chris Double, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
2019-09-26 10:57:31 -04:00
USING: accessors fry kernel literals make math math.parser
math.ranges peg peg.private sequences splitting strings unicode
2019-09-25 23:26:12 -04:00
vectors ;
2019-09-26 10:57:31 -04:00
FROM: peg.search => replace ;
IN: peg.parsers
2008-03-03 17:45:18 -05:00
TUPLE: just-parser p1 ;
2014-12-12 17:19:39 -05:00
CONSTANT: just-pattern [
dup [
2014-12-12 17:19:39 -05:00
dup remaining>> empty? [ drop f ] unless
2008-03-03 17:45:18 -05:00
] when
2014-12-12 17:19:39 -05:00
]
2008-03-03 17:45:18 -05:00
2008-03-20 10:05:21 -04:00
M: just-parser (compile) ( parser -- quot )
2014-12-12 17:19:39 -05:00
p1>> compile-parser-quot just-pattern compose ;
2008-03-03 17:45:18 -05:00
2008-07-03 22:20:19 -04:00
: just ( parser -- parser )
2014-12-12 17:19:39 -05:00
just-parser boa wrap-peg ;
2008-03-03 17:45:18 -05:00
: 1token ( ch -- parser ) 1string token ;
2008-03-12 11:43:18 -04:00
: (list-of) ( items separator repeat1? -- parser )
2014-12-12 17:19:39 -05:00
[ over 2seq ] dip [ repeat1 ] [ repeat0 ] if
[ concat ] action 2seq
[ unclip 1vector swap first append ] action ;
: list-of ( items separator -- parser )
2014-12-12 17:19:39 -05:00
hide f (list-of) ;
: list-of-many ( items separator -- parser )
2014-12-12 17:19:39 -05:00
hide t (list-of) ;
2019-09-25 23:26:12 -04:00
CONSTANT: epsilon $[ V{ } token ]
2019-09-25 23:26:12 -04:00
CONSTANT: any-char $[ [ drop t ] satisfy ]
<PRIVATE
: flatten-vectors ( pair -- vector )
2014-12-12 17:19:39 -05:00
first2 append! ;
PRIVATE>
2008-07-03 22:20:19 -04:00
: exactly-n ( parser n -- parser' )
2014-12-12 17:19:39 -05:00
swap <repetition> seq ;
2008-07-03 22:20:19 -04:00
: at-most-n ( parser n -- parser' )
2014-12-12 17:19:39 -05:00
[
drop epsilon
] [
[ exactly-n ] [ 1 - at-most-n ] 2bi 2choice
] if-zero ;
2008-07-03 22:20:19 -04:00
: at-least-n ( parser n -- parser' )
2014-12-12 17:19:39 -05:00
dupd exactly-n swap repeat0 2seq
[ flatten-vectors ] action ;
2008-07-03 22:20:19 -04:00
: from-m-to-n ( parser m n -- parser' )
2014-12-12 17:19:39 -05:00
[ [ exactly-n ] 2keep ] dip swap - at-most-n 2seq
[ flatten-vectors ] action ;
2008-07-03 22:20:19 -04:00
: pack ( begin body end -- parser )
[ hide ] [ ] [ hide ] tri* 3seq [ first ] action ;
: surrounded-by ( parser begin end -- parser' )
2014-12-12 17:19:39 -05:00
[ token ] bi@ swapd pack ;
: digit-parser ( -- parser )
2014-12-12 17:19:39 -05:00
[ digit? ] satisfy [ digit> ] action ;
: integer-parser ( -- parser )
[ digit? ] satisfy repeat1 [ string>number ] action ;
: string-parser ( -- parser )
2014-12-12 17:19:39 -05:00
[
[ CHAR: \" = ] satisfy hide ,
[ CHAR: \" = not ] satisfy repeat0 ,
[ CHAR: \" = ] satisfy hide ,
2014-12-12 17:19:39 -05:00
] seq* [ first >string ] action ;
2008-03-19 20:40:22 -04:00
: (range-pattern) ( pattern -- string )
2015-09-08 19:15:10 -04:00
! Given a range pattern, produce a string containing
! all characters within that range.
2014-12-12 17:19:39 -05:00
[
any-char ,
[ CHAR: - = ] satisfy hide ,
any-char ,
] seq* [
first2 [a,b] >string
2019-09-26 00:34:28 -04:00
] action replace ;
2008-03-19 20:40:22 -04:00
: range-pattern ( pattern -- parser )
2015-09-08 19:15:10 -04:00
! 'pattern' is a set of characters describing the
! parser to be produced. Any single character in
! the pattern matches that character. If the pattern
! begins with a ^ then the set is negated (the element
! matches any character not in the set). Any pair of
! characters separated with a dash (-) represents the
! range of characters from the first to the second,
! inclusive.
2019-09-26 00:34:28 -04:00
"^" ?head [
(range-pattern) '[ _ member? not ] satisfy
2014-12-12 17:19:39 -05:00
] [
2019-09-26 00:34:28 -04:00
(range-pattern) '[ _ member? ] satisfy
2014-12-12 17:19:39 -05:00
] if ;