huntzhan / clidoc

Prove Of Conecept Fork Of Docopt.
MIT License
7 stars 1 forks source link

Design the argv match algorihtm. #22

Closed huntzhan closed 9 years ago

huntzhan commented 9 years ago

Arguments would be tokenized. The general idea of token matching algorithm is as follow:

huntzhan commented 9 years ago

Order of argument:

Guideline 11: The order of different options relative to one another should not matter, unless the options are documented as mutually-exclusive and such an option is documented to override any incompatible options preceding it. If an option that has option-arguments is repeated, the option and option-argument combinations should be interpreted in the order specified on the command line. Guideline 12: The order of operands may matter and position-related interpretations should be determined on a utility-specific basis.

Miscellaneous requirement:

there are some exceptions in POSIX.1-2008 to ensure continued operation of historical applications: a. If the SYNOPSIS of a standard utility shows an option with a mandatory option-argument (as with [ -c option_argument] in the example), a conforming application shall use separate arguments for that option and its option-argument. However, a conforming implementation shall also permit applications to specify the option and option-argument in the same argument string without intervening characters. b. If the SYNOPSIS shows an optional option-argument (as with [ -f[ option_argument]] in the example), a conforming application shall place any option-argument for that option directly adjacent to the option in the same argument string, without intervening characters. If the utility receives an argument containing only the option, it shall behave as specified in its description for an omitted option-argument; it shall not treat the next argument (if any) as the option-argument for that option. ... Guideline 5: One or more options without option-arguments, followed by at most one option that takes an option-argument, should be accepted when grouped behind one '-' delimiter. ... Guideline 8: When multiple option-arguments are specified to follow a single option, they should be presented as a single argument, using characters within that argument or characters within that argument to separate them. ... Guideline 10: The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the '-' character. ... Guideline 13: For utilities that use operands to represent files to be opened for either reading or writing, the '-' operand should be used to mean only standard input (or standard output when it is clear from context that an output file is being specified) or a file named -.

huntzhan commented 9 years ago

Matching algorithm only consider three kinds of tokens, POSIX_OPTION, GNU_OPTION and GENERAL_ELEMENT, due to #29 . Furthermore, the algorithm don't need to concern about the miscellaneous requirements, since #29 would handle that.

huntzhan commented 9 years ago

each process unit(i.e. LogicAnd node, PosixOption node) could access following variable:

principle of process unit:

AST visitor pattern:

{
  match_state_manager_.PushRollbackPoint();
  child_node->Accept(visitor_ptr_);
  if (!child_match_flag_) {
    // rollback to latest point.
    match_state_manager_.Rollback();
    return;
  }
  // remove latest rollback point.
  match_state_manager_.PopRollbackPoint();
}