From fbc47dc1e489d62e9c0a1b9efa84a4cf424442c4 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Tue, 22 Dec 2015 12:26:06 -0500 Subject: [PATCH] gr: Added utility to check grammar --- CMakeLists.txt | 2 +- src/.gitignore | 1 + src/CMakeLists.txt | 3 ++ src/gr.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/gr.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6029cad2..c7e002f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -119,5 +119,5 @@ set (CPACK_SOURCE_IGNORE_FILES "CMakeCache" "CMakeFiles" "CPackConfig" "CPackSo "_CPack_Packages" "cmake_install" "install_manifest" "Makefile$" "test" "package-config" "misc/*" "src/timew$" "src/common/libcommon.a" "performance" "src/libtimew.a" "/\\\\.gitignore" "/\\\\.git/" "swp$" - "src/lex$") + "src/gr$") include (CPack) diff --git a/src/.gitignore b/src/.gitignore index d64ceca8..4df4b855 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -1,4 +1,5 @@ timew +gr libtimew.a CMakeFiles Makefile diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 30a7d0af..927f36c5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,10 +10,13 @@ set (timew_SRCS Grammar.cpp Grammar.h add_library (timew STATIC ${timew_SRCS}) add_executable (timew_executable timew.cpp) +add_executable (gr_executable gr.cpp) target_link_libraries (timew_executable common timew ${TIMEW_LIBRARIES}) +target_link_libraries (gr_executable common timew ${TIMEW_LIBRARIES}) set_property (TARGET timew_executable PROPERTY OUTPUT_NAME "timew") +set_property (TARGET gr_executable PROPERTY OUTPUT_NAME "gr") install (TARGETS timew_executable DESTINATION ${TIMEW_BINDIR}) diff --git a/src/gr.cpp b/src/gr.cpp new file mode 100644 index 00000000..c100ff9f --- /dev/null +++ b/src/gr.cpp @@ -0,0 +1,106 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Copyright 2015, Paul Beckingham, Federico Hernandez. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// +// http://www.opensource.org/licenses/mit-license.php +// +//////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include +#include + +//////////////////////////////////////////////////////////////////////////////// +void usage () +{ + std::cout << "\n" + << "Usage: gr [options] []\n" + << "\n" + << "Options are:\n" + << " -h/--help Command usage\n" + << "\n"; + exit (-1); +} + +//////////////////////////////////////////////////////////////////////////////// +int main (int argc, char** argv) +{ + // Process command line arguments + std::string grammarFile = ""; + std::string commandLine = ""; +/* + std::vector args; +*/ + + for (int i = 1; i < argc; ++i) + { + if (argv[i][0] == '-') + { + if (!strcmp (argv[i], "-h")) usage (); + else if (!strcmp (argv[i], "--help")) usage (); + else + { + std::cout << "Unrecognized option '" << argv[i] << "'" << std::endl; + usage (); + } + } + else if (grammarFile == "") + { + grammarFile = argv[i]; + } + else + { + if (commandLine != "") + commandLine += " "; + + commandLine += "'" + std::string (argv[i]) + "'"; + } + } + + // Display usage for incorrect command line. + if (grammarFile == "") + usage (); + + try + { + File file (grammarFile); + Grammar grammar; + grammar.loadFromFile (file); + std::cout << grammar.dump (); + + // TODO Test commandLine against grammar. + if (commandLine != "") + { + } + } + + catch (const std::string& error) + { + std::cout << error << "\n"; + } + + return 0; +} + +////////////////////////////////////////////////////////////////////////////////