2008-03-03 17:45:18 -05:00
|
|
|
! Copyright (C) 2008 Slava Pestov
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-06-04 20:33:43 -04:00
|
|
|
USING: peg peg.parsers kernel sequences strings words ;
|
2008-03-03 17:45:18 -05:00
|
|
|
IN: io.unix.launcher.parser
|
|
|
|
|
|
|
|
! Our command line parser. Supported syntax:
|
|
|
|
! foo bar baz -- simple tokens
|
|
|
|
! foo\ bar -- escaping the space
|
|
|
|
! 'foo bar' -- quotation
|
|
|
|
! "foo bar" -- quotation
|
2008-06-04 20:33:43 -04:00
|
|
|
: 'escaped-char' ( -- parser )
|
|
|
|
"\\" token any-char 2seq [ second ] action ;
|
2008-03-03 17:45:18 -05:00
|
|
|
|
2008-06-04 20:33:43 -04:00
|
|
|
: 'quoted-char' ( delimiter -- parser' )
|
2008-03-03 17:45:18 -05:00
|
|
|
'escaped-char'
|
|
|
|
swap [ member? not ] curry satisfy
|
|
|
|
2choice ; inline
|
|
|
|
|
2008-06-04 20:33:43 -04:00
|
|
|
: 'quoted' ( delimiter -- parser )
|
2008-03-03 17:45:18 -05:00
|
|
|
dup 'quoted-char' repeat0 swap dup surrounded-by ;
|
|
|
|
|
2008-06-04 20:33:43 -04:00
|
|
|
: 'unquoted' ( -- parser ) " '\"" 'quoted-char' repeat1 ;
|
2008-03-03 17:45:18 -05:00
|
|
|
|
2008-06-04 20:33:43 -04:00
|
|
|
: 'argument' ( -- parser )
|
2008-03-03 17:45:18 -05:00
|
|
|
"\"" 'quoted'
|
|
|
|
"'" 'quoted'
|
|
|
|
'unquoted' 3choice
|
|
|
|
[ >string ] action ;
|
|
|
|
|
2008-03-03 17:57:30 -05:00
|
|
|
PEG: tokenize-command ( command -- ast/f )
|
2008-03-03 17:45:18 -05:00
|
|
|
'argument' " " token repeat1 list-of
|
2008-11-10 01:16:11 -05:00
|
|
|
" " token repeat0 tuck pack
|
2008-03-03 17:45:18 -05:00
|
|
|
just ;
|