factor/basis/io/unix/launcher/parser/parser.factor

34 lines
925 B
Factor
Raw Normal View History

2008-03-03 17:45:18 -05:00
! Copyright (C) 2008 Slava Pestov
! See http://factorcode.org/license.txt for BSD license.
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
: 'escaped-char' ( -- parser )
"\\" token any-char 2seq [ second ] action ;
2008-03-03 17:45:18 -05:00
: 'quoted-char' ( delimiter -- parser' )
2008-03-03 17:45:18 -05:00
'escaped-char'
swap [ member? not ] curry satisfy
2choice ; inline
: 'quoted' ( delimiter -- parser )
2008-03-03 17:45:18 -05:00
dup 'quoted-char' repeat0 swap dup surrounded-by ;
: 'unquoted' ( -- parser ) " '\"" 'quoted-char' repeat1 ;
2008-03-03 17:45:18 -05: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 ;