Example #1
0
var option = function(name, argumentRequired) {
    return base.cons(
        text.string(name)
      , ((argumentRequired)
          ? parse.choice(
                parse.attempt(parse.next(text.string('='), argument.name))
              , parse.attempt(parse.next(text.string(' '), argument.name)))
          : parse.of(null))).map(_.spread(function(__, value) {
                return value;
            }));
};
Example #2
0
 P.bind(P.many(PT.oneOf('Lhl')).map(join), size =>
 P.choice(
   P.bind(PT.oneOf('dxoiufcs%n'), type => {
     width = width.length > 0 ? parseInt(width) : false;
     if (type === '%') {
       return P.always({kind: 'l', chars: '%'});
     }
     return P.always({kind: 'p', noStore, width, size, type})}),
   P.next(PT.character('['),
     P.bind(P.optional(false, P.next(PT.character('^'), P.always(true))), negated =>
     P.bind(fp_charset_tail, set =>
     P.always({kind: 'p', noStore, width, type: 'C', negated, set})))))))));
Example #3
0
 .map(_.spread(function(usage, rest) {
     console.log(rest);
     try {
     return { usage: usage, tests: parse.run(
         parse.eager(parse.many1(
             parse.either(
                 parse.attempt(parse.next(
                     parse.many(text.space)
                   , base.cons(
                         base.join(base.cons(
                             text.string('$ prog')
                           , parse.eager(parse.many(text.match(/ /)))
                           , parse.optional('', parse.attempt(
                                 base.join(parse.eager(parse.many(
                                     text.noneOf('\n'))))))
                         ))
                       , parse.either(
                             parse.attempt(parse.next(
                                 parse.many(text.space)
                               , text.string('"user-error"')))
                           , parse.next(
                                 parse.many(text.space)
                               , lang.between(
                                     text.string('{')
                                   , text.string('}')
                                   , base.join(
                                         parse.eager(parse.many(
                                             text.noneOf('}')))))
                                 .map(function(x) {
                                     return JSON.parse(
                                         '{' + x + '}'
                                     );
                                 })
                             )
                         )
                     )
                 )).map(_.spread(function(input, output) {
                     return { input: input, output: output };
                 }))
               , parse.eof)
         ))
       , rest
     ) };
     } catch(e) {
         console.log('>', rest.replace(/\n/g, ' '));
         console.log('>', _.repeat(' ', 12) + _.repeat('^', 3));
         console.log(e.toString());
         throw e;
     }
 }))
Example #4
0
 parse.attempt(line.chain(function(o) {
     return parse.choice(
         parse.next(
             parse.many(text.match(/\n/))
           , parse.setPosition(new parse.Position(0))
                 .chain(_.constant(self.map(function(os) {
                     return ([ o ]).concat(os);
                 })))
         )
       , parse.of([ o ])
     );
 }))
Example #5
0
var leadingFlags = parse.getParserState.chain(function(state) {
    return parse.choice(
        parse.attempt(base.cons(
            option.short
          , parse.next(
                parse.either(
                  text.string(', ')
                , text.string(' '))
              , option.long)
        ))
        .chain(_.spread(function(short, long) {
            return (
            (short.arg && long.arg && short.arg !== long.arg)
              ? parse.fail(util.mstr('\
                    Short and long arguments differ.    \n\
                    Please either use the same argument \n\
                    for both or only specify on one.    \
                '))
              : parse.of({
                    short: _.assign(short, { arg: short.arg || long.arg })
                  , long: _.assign(long,  { arg: short.arg || long.arg })
                  , arg: short.arg || long.arg
                }));
        }))
      , parse.attempt(option.short)
            .map(function(short) {
                return { short: short };
            })
      , parse.attempt(option.long)
            .map(function(long) {
                return { long: long };
            })
    )
    .chain(function(flags) {
        return parse.modifyState(function(userState) {
            return _.assign(userState || {}, {
                flagStart: state.position.index
            });
        }).chain(_.constant(parse.of(flags)));
    });
});
Example #6
0
              : parse.rec(function(self) {
                    return parse.bind(
                        parse.choice(

                            // -----------------------------
                            // Try to parse defaults
                            // Note: Must be situated at EOL
                            //       or EOF.
                            // -----------------------------
                            parse.attempt(
                                parse.next(
                                    parse.many1(base.space)
                                  , lang.then(
                                        defaults
                                      , parse.either(
                                            parse.lookahead(
                                                text.match(/\n/)
                                            )
                                          , parse.eof
                                        )
                                    )
                                ).map(function(defs) {
                                    return [ '', [ defs ] ];
                                })
                            )

                            // ---------------------------------
                            // Try to parse plain text
                            // Note: This also parses new-lines
                            //       if the indentation matches.
                            // ---------------------------------
                          , parse.attempt(parse.choice(
                                parse.attempt(parse.sequence(
                                    text.match(/\n/)
                                  , text.string(_.repeat(
                                        ' '
                                      , state.position.index
                                    ))
                                  , text.match(/[^\n ]/)
                                ))
                                .map(function(s) { return ' ' + s; })
                              , parse.attempt(text.match(/[^\n]/))
                            ).map(function(desc) { return [ desc, [] ]; }))

                           // -------------------
                           // Parse final EOF/EOL
                           // -------------------
                          , parse.look(
                                parse.either(
                                    text.match(/\n/)
                                  , parse.eof
                                ).map(_.constant([]))
                            )
                        )
                      , _.spread(function(desc, defs) {
                            return (desc !== undefined
                                || defs  !== undefined
                            )
                              ? self.chain(_.spread(function(desc1, defs1) {
                                    return parse.of([
                                        desc + desc1
                                      , defs.concat(defs1)
                                    ]);
                                }))
                              : parse.of(['', []]);
                        })
                    );
                })
Example #7
0
 P.bind(P.many(PT.noneOf(']')), rest =>
 P.next(PT.character(']'),
 P.always(sqB + join(rest)))));