You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@corinthia.apache.org by pm...@apache.org on 2015/08/31 11:54:42 UTC

[2/2] incubator-corinthia git commit: Flat: More flexible command-line options

Flat: More flexible command-line options

Use a more generic model of command-line options which moves away from
specific use cases.  As before, the command can be run with two
filenames, GRAMMAR and INPUT, to parse a file.

The -b (or --builtin) option is now used to select the builtin grammar
instead of a user-specified one. In this case, the GRAMMAR file is not
needed.

The -g (or --grammar) option is now used to ignore the input and instead
show the grammar that would have been used to parse it. In this case,
the INPUT file is not needed.


Project: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/commit/da0d919b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/tree/da0d919b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/diff/da0d919b

Branch: refs/heads/master
Commit: da0d919b5e24c853a777c6009f7f4d2991fa9494
Parents: e899882
Author: Peter Kelly <pe...@uxproductivity.com>
Authored: Mon Aug 31 16:44:57 2015 +0700
Committer: Peter Kelly <pe...@uxproductivity.com>
Committed: Mon Aug 31 16:46:49 2015 +0700

----------------------------------------------------------------------
 experiments/flat/src/flat.c | 83 ++++++++++++++++++++++++----------------
 1 file changed, 50 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/da0d919b/experiments/flat/src/flat.c
----------------------------------------------------------------------
diff --git a/experiments/flat/src/flat.c b/experiments/flat/src/flat.c
index af69d83..e72fbe7 100644
--- a/experiments/flat/src/flat.c
+++ b/experiments/flat/src/flat.c
@@ -57,27 +57,27 @@ static Grammar *grammarFromStr(Grammar *flatGrammar, const char *filename, const
 
 void usage(void)
 {
-    printf("Usage:\n"
+    printf("Usage: flat [options] GRAMMAR INPUT\n"
            "\n"
-           "flat -g\n"
+           "Options:\n"
            "\n"
-           "    Print the built-in PEG grammar\n"
+           "  -b, --builtin   Use the built-in grammar instead of a user-supplied one\n"
+           "  -g, --grammar   Don't parse input; just show the grammar that would be used\n"
+           "  -h, --help      Print this message\n"
            "\n"
-           "flat -p FILENAME\n"
+           "Arguments:\n"
            "\n"
-           "    Parse FILENAME using the built-in PEG grammar, and print out the resulting\n"
-           "    parse tree\n"
+           "  GRAMMAR         A file written in the Flat syntax that defines the syntax of\n"
+           "                  the language to be parsed. Should be omitted if -b is used.\n"
+           "  INPUT           A file written in the syntax of the language defined by\n"
+           "                  GRAMMAR. Should be omitted if -s is used.\n"
            "\n"
-           "flat -b FILENAME\n"
+           "Examples:\n"
            "\n"
-           "    Parse FILENAME using the built-in PEG grammar, then use the resulting parse\n"
-           "    tree to build a Grammar object, and print out the constructed grammar.\n"
-           "\n"
-           "flat GRAMMAR INPUT\n"
-           "\n"
-           "    Use the grammar defined in file GRAMMAR to parse the file INPUT, and print\n"
-           "    out the resulting parse tree\n"
-           "\n");
+           "  flat -b -s      Print out the built-in grammar\n"
+           "  flat arithmetic.flat test.exp\n"
+           "                  Parse the file test.exp using the language defined in\n"
+           "                  arithmetic.flat\n");
     exit(1);
 }
 
@@ -104,26 +104,43 @@ int main(int argc, const char **argv)
     Grammar *builtGrammar = NULL;
     Term *inputTerm = NULL;
 
-    if ((argc == 2) && !strcmp(argv[1],"-g")) {
-        useBuiltinGrammar = 1;
-        showGrammar = 1;
-    }
-    else if ((argc == 3) && !strcmp(argv[1],"-p")) {
-        useBuiltinGrammar = 1;
-        inputFilename = argv[2];
-    }
-    else if ((argc == 3) && !strcmp(argv[1],"-b")) {
-        grammarFilename = argv[2];
-        showGrammar = 1;
-    }
-    else if (argc == 3) {
-        grammarFilename = argv[1];
-        inputFilename = argv[2];
-    }
-    else {
-        usage();
+    for (int i = 1; i < argc; i++) {
+        if (!strcmp(argv[i],"-b") || !strcmp(argv[i],"--builtin"))
+            useBuiltinGrammar = 1;
+        else if (!strcmp(argv[i],"-g") || !strcmp(argv[i],"--grammar"))
+            showGrammar = 1;
+        else if (!strcmp(argv[i],"-h") || !strcmp(argv[i],"--help"))
+            usage();
+        else if ((strlen(argv[i]) > 1) && argv[i][0] == '-')
+            usage();
+        else if ((grammarFilename == NULL) && !useBuiltinGrammar)
+            grammarFilename = argv[i];
+        else if ((inputFilename == NULL) && !showGrammar)
+            inputFilename = argv[i];
+        else
+            usage();
     }
 
+//    if ((argc == 2) && !strcmp(argv[1],"-g")) {
+//        useBuiltinGrammar = 1;
+//        showGrammar = 1;
+//    }
+//    else if ((argc == 3) && !strcmp(argv[1],"-p")) {
+//        useBuiltinGrammar = 1;
+//        inputFilename = argv[2];
+//    }
+//    else if ((argc == 3) && !strcmp(argv[1],"-b")) {
+//        grammarFilename = argv[2];
+//        showGrammar = 1;
+//    }
+//    else if (argc == 3) {
+//        grammarFilename = argv[1];
+//        inputFilename = argv[2];
+//    }
+//    else {
+//        usage();
+//    }
+
     Grammar *flatGrammar = GrammarNewBuiltin();
 
     inputStr = maybeReadFile(inputFilename);