1 ! Copyright (C) 2007, 2008 Chris Double, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors fry kernel literals make math math.parser
4 math.ranges peg peg.private sequences splitting strings unicode
6 FROM: peg.search => replace ;
9 TUPLE: just-parser p1 ;
11 M: just-parser (compile)
12 p1>> compile-parser-quot [
14 dup remaining>> empty? [ drop f ] unless
18 : just ( parser -- parser )
19 just-parser boa wrap-peg ;
21 : 1token ( ch -- parser ) 1string token ;
23 : (list-of) ( items separator repeat1? -- parser )
24 [ over 2seq ] dip [ repeat1 ] [ repeat0 ] if
25 [ concat ] action 2seq
26 [ unclip 1vector swap first append ] action ;
28 : list-of ( items separator -- parser )
31 : list-of-many ( items separator -- parser )
34 CONSTANT: epsilon $[ V{ } token ]
36 CONSTANT: any-char $[ [ drop t ] satisfy ]
40 : flatten-vectors ( pair -- vector )
45 : exactly-n ( parser n -- parser' )
46 swap <repetition> seq ;
48 : at-most-n ( parser n -- parser' )
52 [ exactly-n ] [ 1 - at-most-n ] 2bi 2choice
55 : at-least-n ( parser n -- parser' )
56 dupd exactly-n swap repeat0 2seq
57 [ flatten-vectors ] action ;
59 : from-m-to-n ( parser m n -- parser' )
60 [ [ exactly-n ] 2keep ] dip swap - at-most-n 2seq
61 [ flatten-vectors ] action ;
63 : pack ( begin body end -- parser )
64 [ hide ] [ ] [ hide ] tri* 3seq [ first ] action ;
66 : surrounded-by ( parser begin end -- parser' )
67 [ token ] bi@ swapd pack ;
69 : digit-parser ( -- parser )
70 [ digit? ] satisfy [ digit> ] action ;
72 : integer-parser ( -- parser )
73 [ digit? ] satisfy repeat1 [ string>number ] action ;
75 : string-parser ( -- parser )
77 [ CHAR: \" = ] satisfy hide ,
78 [ CHAR: \" = not ] satisfy repeat0 ,
79 [ CHAR: \" = ] satisfy hide ,
80 ] seq* [ first >string ] action ;
82 : (range-pattern) ( pattern -- string )
83 ! Given a range pattern, produce a string containing
84 ! all characters within that range.
87 [ CHAR: - = ] satisfy hide ,
93 : range-pattern ( pattern -- parser )
94 ! 'pattern' is a set of characters describing the
95 ! parser to be produced. Any single character in
96 ! the pattern matches that character. If the pattern
97 ! begins with a ^ then the set is negated (the element
98 ! matches any character not in the set). Any pair of
99 ! characters separated with a dash (-) represents the
100 ! range of characters from the first to the second,
103 (range-pattern) dup length 1 =
104 [ first '[ _ = ] ] [ '[ _ member? ] ] if
106 [ [ not ] compose ] when satisfy