]> gitweb.factorcode.org Git - factor.git/blob - basis/io/unix/launcher/parser/parser.factor
e5e83ab4e9599e94fec6225f425ceb1f7174fdaa
[factor.git] / basis / io / unix / launcher / parser / parser.factor
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
5
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 ;
13
14 : 'quoted-char' ( delimiter -- parser' )
15     'escaped-char'
16     swap [ member? not ] curry satisfy
17     2choice ; inline
18
19 : 'quoted' ( delimiter -- parser )
20     dup 'quoted-char' repeat0 swap dup surrounded-by ;
21
22 : 'unquoted' ( -- parser ) " '\"" 'quoted-char' repeat1 ;
23
24 : 'argument' ( -- parser )
25     "\"" 'quoted'
26     "'" 'quoted'
27     'unquoted' 3choice
28     [ >string ] action ;
29
30 PEG: tokenize-command ( command -- ast/f )
31     'argument' " " token repeat1 list-of
32     " " token repeat0 swap over pack
33     just ;