bproto.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @file bproto.php
  4. * @author Ambroz Bizjak <ambrop7@gmail.com>
  5. *
  6. * @section LICENSE
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the author nor the
  16. * names of its contributors may be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. require_once "lime/parse_engine.php";
  31. require_once "ProtoParser.php";
  32. require_once "bproto_functions.php";
  33. function assert_failure ($script, $line, $message)
  34. {
  35. if ($message == "") {
  36. fatal_error("Assertion failure at {$script}:{$line}");
  37. } else {
  38. fatal_error("Assertion failure at {$script}:{$line}: {$message}");
  39. }
  40. }
  41. assert_options(ASSERT_CALLBACK, "assert_failure");
  42. function print_help ($name)
  43. {
  44. echo <<<EOD
  45. Usage: {$name}
  46. --name <string> Output file prefix.
  47. --input-file <file> Message file to generate source for.
  48. --output-dir <dir> Destination directory for generated files.
  49. EOD;
  50. }
  51. $name = "";
  52. $input_file = "";
  53. $output_dir = "";
  54. for ($i = 1; $i < $argc;) {
  55. $arg = $argv[$i++];
  56. switch ($arg) {
  57. case "--name":
  58. $name = $argv[$i++];
  59. break;
  60. case "--input-file":
  61. $input_file = $argv[$i++];
  62. break;
  63. case "--output-dir":
  64. $output_dir = $argv[$i++];
  65. break;
  66. case "--help":
  67. print_help($argv[0]);
  68. exit(0);
  69. default:
  70. fatal_error("Unknown option: {$arg}");
  71. }
  72. }
  73. if ($name == "") {
  74. fatal_error("--name missing");
  75. }
  76. if ($input_file == "") {
  77. fatal_error("--input-file missing");
  78. }
  79. if ($output_dir == "") {
  80. fatal_error("--output-dir missing");
  81. }
  82. if (($data = file_get_contents($input_file)) === FALSE) {
  83. fatal_error("Failed to read input file");
  84. }
  85. if (!tokenize($data, $tokens)) {
  86. fatal_error("Failed to tokenize");
  87. }
  88. $parser = new parse_engine(new ProtoParser());
  89. try {
  90. foreach ($tokens as $token) {
  91. $parser->eat($token[0], $token[1]);
  92. }
  93. $parser->eat_eof();
  94. } catch (parse_error $e) {
  95. fatal_error("$input_file: Parse error: ".$e->getMessage());
  96. }
  97. $data = generate_header($name, $parser->semantic["directives"], $parser->semantic["messages"]);
  98. if (file_put_contents("{$output_dir}/{$name}.h", $data) === NULL) {
  99. fatal_error("{$input_file}: Failed to write .h file");
  100. }