1 ! Copyright (C) 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: peg peg.parsers kernel sequences strings words ;
4 IN: io.unix.launcher.parser
6 ! Our command line parser. Supported syntax:
7 ! foo bar baz -- simple tokens
8 ! foo\ bar -- escaping the space
9 ! 'foo bar' -- quotation
10 ! "foo bar" -- quotation
11 : 'escaped-char' ( -- parser )
12 "\\" token any-char 2seq [ second ] action ;
14 : 'quoted-char' ( delimiter -- parser' )
16 swap [ member? not ] curry satisfy
19 : 'quoted' ( delimiter -- parser )
20 dup 'quoted-char' repeat0 swap dup surrounded-by ;
22 : 'unquoted' ( -- parser ) " '\"" 'quoted-char' repeat1 ;
24 : 'argument' ( -- parser )
30 PEG: tokenize-command ( command -- ast/f )
31 'argument' " " token repeat1 list-of
32 " " token repeat0 swap over pack