redsnow 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +34 -0
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +20 -0
  5. data/CHANGELOG.md +4 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE +21 -0
  8. data/README.md +62 -0
  9. data/Rakefile +36 -0
  10. data/Vagrantfile +20 -0
  11. data/ext/snowcrash/Makefile +64 -0
  12. data/ext/snowcrash/Vagrantfile +20 -0
  13. data/ext/snowcrash/bin/snowcrash +0 -0
  14. data/ext/snowcrash/common.gypi +163 -0
  15. data/ext/snowcrash/config.gypi +10 -0
  16. data/ext/snowcrash/config.mk +5 -0
  17. data/ext/snowcrash/configure +213 -0
  18. data/ext/snowcrash/provisioning.sh +15 -0
  19. data/ext/snowcrash/snowcrash.gyp +141 -0
  20. data/ext/snowcrash/src/ActionParser.h +503 -0
  21. data/ext/snowcrash/src/AssetParser.h +215 -0
  22. data/ext/snowcrash/src/BlockUtility.h +186 -0
  23. data/ext/snowcrash/src/Blueprint.h +283 -0
  24. data/ext/snowcrash/src/BlueprintParser.h +347 -0
  25. data/ext/snowcrash/src/BlueprintParserCore.h +190 -0
  26. data/ext/snowcrash/src/BlueprintSection.h +140 -0
  27. data/ext/snowcrash/src/BlueprintUtility.h +126 -0
  28. data/ext/snowcrash/src/CBlueprint.cc +600 -0
  29. data/ext/snowcrash/src/CBlueprint.h +354 -0
  30. data/ext/snowcrash/src/CSourceAnnotation.cc +140 -0
  31. data/ext/snowcrash/src/CSourceAnnotation.h +106 -0
  32. data/ext/snowcrash/src/CodeBlockUtility.h +189 -0
  33. data/ext/snowcrash/src/DescriptionSectionUtility.h +156 -0
  34. data/ext/snowcrash/src/HTTP.cc +46 -0
  35. data/ext/snowcrash/src/HTTP.h +105 -0
  36. data/ext/snowcrash/src/HeaderParser.h +289 -0
  37. data/ext/snowcrash/src/ListBlockUtility.h +273 -0
  38. data/ext/snowcrash/src/ListUtility.h +95 -0
  39. data/ext/snowcrash/src/MarkdownBlock.cc +176 -0
  40. data/ext/snowcrash/src/MarkdownBlock.h +93 -0
  41. data/ext/snowcrash/src/MarkdownParser.cc +266 -0
  42. data/ext/snowcrash/src/MarkdownParser.h +88 -0
  43. data/ext/snowcrash/src/ParameterDefinitonParser.h +570 -0
  44. data/ext/snowcrash/src/ParametersParser.h +252 -0
  45. data/ext/snowcrash/src/Parser.cc +71 -0
  46. data/ext/snowcrash/src/Parser.h +29 -0
  47. data/ext/snowcrash/src/ParserCore.cc +120 -0
  48. data/ext/snowcrash/src/ParserCore.h +82 -0
  49. data/ext/snowcrash/src/PayloadParser.h +672 -0
  50. data/ext/snowcrash/src/Platform.h +54 -0
  51. data/ext/snowcrash/src/RegexMatch.h +32 -0
  52. data/ext/snowcrash/src/ResourceGroupParser.h +195 -0
  53. data/ext/snowcrash/src/ResourceParser.h +584 -0
  54. data/ext/snowcrash/src/SectionUtility.h +142 -0
  55. data/ext/snowcrash/src/Serialize.cc +52 -0
  56. data/ext/snowcrash/src/Serialize.h +69 -0
  57. data/ext/snowcrash/src/SerializeJSON.cc +601 -0
  58. data/ext/snowcrash/src/SerializeJSON.h +21 -0
  59. data/ext/snowcrash/src/SerializeYAML.cc +336 -0
  60. data/ext/snowcrash/src/SerializeYAML.h +21 -0
  61. data/ext/snowcrash/src/SourceAnnotation.h +177 -0
  62. data/ext/snowcrash/src/StringUtility.h +109 -0
  63. data/ext/snowcrash/src/SymbolTable.h +83 -0
  64. data/ext/snowcrash/src/UriTemplateParser.cc +195 -0
  65. data/ext/snowcrash/src/UriTemplateParser.h +243 -0
  66. data/ext/snowcrash/src/Version.h +39 -0
  67. data/ext/snowcrash/src/csnowcrash.cc +23 -0
  68. data/ext/snowcrash/src/csnowcrash.h +38 -0
  69. data/ext/snowcrash/src/posix/RegexMatch.cc +99 -0
  70. data/ext/snowcrash/src/snowcrash.cc +18 -0
  71. data/ext/snowcrash/src/snowcrash.h +41 -0
  72. data/ext/snowcrash/src/snowcrash/snowcrash.cc +170 -0
  73. data/ext/snowcrash/src/win/RegexMatch.cc +78 -0
  74. data/ext/snowcrash/sundown/CONTRIBUTING.md +10 -0
  75. data/ext/snowcrash/sundown/Makefile +83 -0
  76. data/ext/snowcrash/sundown/Makefile.win +33 -0
  77. data/ext/snowcrash/sundown/examples/smartypants.c +72 -0
  78. data/ext/snowcrash/sundown/examples/sundown.c +80 -0
  79. data/ext/snowcrash/sundown/html/houdini.h +37 -0
  80. data/ext/snowcrash/sundown/html/houdini_href_e.c +108 -0
  81. data/ext/snowcrash/sundown/html/houdini_html_e.c +84 -0
  82. data/ext/snowcrash/sundown/html/html.c +647 -0
  83. data/ext/snowcrash/sundown/html/html.h +77 -0
  84. data/ext/snowcrash/sundown/html/html_smartypants.c +389 -0
  85. data/ext/snowcrash/sundown/html_block_names.txt +25 -0
  86. data/ext/snowcrash/sundown/src/autolink.c +297 -0
  87. data/ext/snowcrash/sundown/src/autolink.h +51 -0
  88. data/ext/snowcrash/sundown/src/buffer.c +225 -0
  89. data/ext/snowcrash/sundown/src/buffer.h +96 -0
  90. data/ext/snowcrash/sundown/src/html_blocks.h +206 -0
  91. data/ext/snowcrash/sundown/src/markdown.c +2701 -0
  92. data/ext/snowcrash/sundown/src/markdown.h +147 -0
  93. data/ext/snowcrash/sundown/src/src_map.c +200 -0
  94. data/ext/snowcrash/sundown/src/src_map.h +58 -0
  95. data/ext/snowcrash/sundown/src/stack.c +81 -0
  96. data/ext/snowcrash/sundown/src/stack.h +29 -0
  97. data/ext/snowcrash/sundown/sundown.def +20 -0
  98. data/ext/snowcrash/tools/gyp/AUTHORS +11 -0
  99. data/ext/snowcrash/tools/gyp/DEPS +24 -0
  100. data/ext/snowcrash/tools/gyp/OWNERS +1 -0
  101. data/ext/snowcrash/tools/gyp/PRESUBMIT.py +120 -0
  102. data/ext/snowcrash/tools/gyp/buildbot/buildbot_run.py +190 -0
  103. data/ext/snowcrash/tools/gyp/codereview.settings +10 -0
  104. data/ext/snowcrash/tools/gyp/data/win/large-pdb-shim.cc +12 -0
  105. data/ext/snowcrash/tools/gyp/gyp +8 -0
  106. data/ext/snowcrash/tools/gyp/gyp.bat +5 -0
  107. data/ext/snowcrash/tools/gyp/gyp_main.py +18 -0
  108. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSNew.py +340 -0
  109. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSProject.py +208 -0
  110. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSSettings.py +1063 -0
  111. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSToolFile.py +58 -0
  112. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSUserFile.py +147 -0
  113. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSUtil.py +267 -0
  114. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSVersion.py +409 -0
  115. data/ext/snowcrash/tools/gyp/pylib/gyp/__init__.py +537 -0
  116. data/ext/snowcrash/tools/gyp/pylib/gyp/__init__.pyc +0 -0
  117. data/ext/snowcrash/tools/gyp/pylib/gyp/common.py +521 -0
  118. data/ext/snowcrash/tools/gyp/pylib/gyp/common.pyc +0 -0
  119. data/ext/snowcrash/tools/gyp/pylib/gyp/easy_xml.py +157 -0
  120. data/ext/snowcrash/tools/gyp/pylib/gyp/flock_tool.py +49 -0
  121. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/__init__.py +0 -0
  122. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/__init__.pyc +0 -0
  123. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/android.py +1069 -0
  124. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/cmake.py +1143 -0
  125. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/dump_dependency_json.py +81 -0
  126. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/eclipse.py +335 -0
  127. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/gypd.py +87 -0
  128. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/gypsh.py +56 -0
  129. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/make.py +2181 -0
  130. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/make.pyc +0 -0
  131. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/msvs.py +3335 -0
  132. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/ninja.py +2156 -0
  133. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/xcode.py +1224 -0
  134. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/xcode.pyc +0 -0
  135. data/ext/snowcrash/tools/gyp/pylib/gyp/input.py +2809 -0
  136. data/ext/snowcrash/tools/gyp/pylib/gyp/input.pyc +0 -0
  137. data/ext/snowcrash/tools/gyp/pylib/gyp/mac_tool.py +510 -0
  138. data/ext/snowcrash/tools/gyp/pylib/gyp/msvs_emulation.py +972 -0
  139. data/ext/snowcrash/tools/gyp/pylib/gyp/ninja_syntax.py +160 -0
  140. data/ext/snowcrash/tools/gyp/pylib/gyp/ordered_dict.py +289 -0
  141. data/ext/snowcrash/tools/gyp/pylib/gyp/win_tool.py +292 -0
  142. data/ext/snowcrash/tools/gyp/pylib/gyp/xcode_emulation.py +1440 -0
  143. data/ext/snowcrash/tools/gyp/pylib/gyp/xcode_emulation.pyc +0 -0
  144. data/ext/snowcrash/tools/gyp/pylib/gyp/xcodeproj_file.py +2889 -0
  145. data/ext/snowcrash/tools/gyp/pylib/gyp/xcodeproj_file.pyc +0 -0
  146. data/ext/snowcrash/tools/gyp/pylib/gyp/xml_fix.py +69 -0
  147. data/ext/snowcrash/tools/gyp/pylintrc +307 -0
  148. data/ext/snowcrash/tools/gyp/samples/samples +81 -0
  149. data/ext/snowcrash/tools/gyp/samples/samples.bat +5 -0
  150. data/ext/snowcrash/tools/gyp/setup.py +19 -0
  151. data/ext/snowcrash/tools/gyp/tools/Xcode/Specifications/gyp.pbfilespec +27 -0
  152. data/ext/snowcrash/tools/gyp/tools/Xcode/Specifications/gyp.xclangspec +226 -0
  153. data/ext/snowcrash/tools/gyp/tools/emacs/gyp.el +252 -0
  154. data/ext/snowcrash/tools/gyp/tools/graphviz.py +100 -0
  155. data/ext/snowcrash/tools/gyp/tools/pretty_gyp.py +155 -0
  156. data/ext/snowcrash/tools/gyp/tools/pretty_sln.py +168 -0
  157. data/ext/snowcrash/tools/gyp/tools/pretty_vcproj.py +329 -0
  158. data/ext/snowcrash/tools/homebrew/snowcrash.rb +11 -0
  159. data/ext/snowcrash/vcbuild.bat +184 -0
  160. data/lib/redsnow.rb +31 -0
  161. data/lib/redsnow/binding.rb +132 -0
  162. data/lib/redsnow/blueprint.rb +365 -0
  163. data/lib/redsnow/object.rb +18 -0
  164. data/lib/redsnow/parseresult.rb +107 -0
  165. data/lib/redsnow/version.rb +4 -0
  166. data/provisioning.sh +20 -0
  167. data/redsnow.gemspec +35 -0
  168. data/test/_helper.rb +15 -0
  169. data/test/fixtures/sample-api-ast.json +97 -0
  170. data/test/fixtures/sample-api.apib +20 -0
  171. data/test/redsnow_binding_test.rb +35 -0
  172. data/test/redsnow_parseresult_test.rb +50 -0
  173. data/test/redsnow_test.rb +285 -0
  174. metadata +358 -0
@@ -0,0 +1,39 @@
1
+ //
2
+ // version.h
3
+ // snowcrash
4
+ //
5
+ // Created by Zdenek Nemec on 4/14/14.
6
+ //
7
+ // Attribution Notice:
8
+ // This work might use parts of Node.js `node_version.h`
9
+ // https://github.com/joyent/node
10
+ //
11
+ // Copyright (c) 2014 Apiary Inc. All rights reserved.
12
+ //
13
+
14
+ #ifndef SNOWCRASH_VERSION_H
15
+ #define SNOWCRASH_VERSION_H
16
+
17
+ #define SNOWCRASH_MAJOR_VERSION 0
18
+ #define SNOWCRASH_MINOR_VERSION 12
19
+ #define SNOWCRASH_PATCH_VERSION 0
20
+
21
+ #define SNOWCRASH_VERSION_IS_RELEASE 1
22
+
23
+ #ifndef SNOWCRASH_STRINGIFY
24
+ # define SNOWCRASH_STRINGIFY(n) SNOWCRASH_STRINGIFY_HELPER(n)
25
+ # define SNOWCRASH_STRINGIFY_HELPER(n) #n
26
+ #endif
27
+
28
+ #define SNOWCRASH_VERSION_STRING_HELPER "v" \
29
+ SNOWCRASH_STRINGIFY(SNOWCRASH_MAJOR_VERSION) "." \
30
+ SNOWCRASH_STRINGIFY(SNOWCRASH_MINOR_VERSION) "." \
31
+ SNOWCRASH_STRINGIFY(SNOWCRASH_PATCH_VERSION)
32
+
33
+ #if SNOWCRASH_VERSION_IS_RELEASE
34
+ # define SNOWCRASH_VERSION_STRING SNOWCRASH_VERSION_STRING_HELPER
35
+ #else
36
+ # define SNOWCRASH_VERSION_STRING SNOWCRASH_VERSION_STRING_HELPER "-pre"
37
+ #endif
38
+
39
+ #endif
@@ -0,0 +1,23 @@
1
+ //
2
+ // CSnowcrash.cc
3
+ // snowcrash
4
+ //
5
+ // Created by Ali Khoramshahi on 13/6/14.
6
+ // Copyright (c) 2014 Apiary Inc. All rights reserved.
7
+ //
8
+ #include "csnowcrash.h"
9
+ #include "snowcrash.h"
10
+
11
+
12
+ int sc_c_parse(const char* source, sc_blueprint_parser_options option, sc_result_t** result, sc_blueprint_t** blueprint)
13
+ {
14
+ snowcrash::Result* t_result = ::new snowcrash::Result;
15
+ snowcrash::Blueprint* t_blueprint = ::new snowcrash::Blueprint;
16
+
17
+ int ret = snowcrash::parse(source, option, *t_result, *t_blueprint);
18
+
19
+ *blueprint = AS_TYPE(sc_blueprint_t, t_blueprint);
20
+ *result = AS_TYPE(sc_result_t, t_result);
21
+
22
+ return ret;
23
+ }
@@ -0,0 +1,38 @@
1
+ //
2
+ // CSnowcrash.h
3
+ // snowcrash
4
+ // C Implementation of Snowcrash.h for binding purposes
5
+ //
6
+ // Created by Ali Khoramshahi on 13/6/14.
7
+ // Copyright (c) 2014 Apiary Inc. All rights reserved.
8
+ //
9
+
10
+ #ifndef SC_C_SNOWCRASH_H
11
+ #define SC_C_SNOWCRASH_H
12
+
13
+ #include "CSourceAnnotation.h"
14
+ #include "CBlueprint.h"
15
+
16
+ #ifdef __cplusplus
17
+ extern "C" {
18
+ #endif
19
+
20
+ /**
21
+ * \This is C interface for snowcrash parser.
22
+ *
23
+ * \param source A textual source data to be parsed.
24
+ * \param options Parser options. Use 0 for no addtional options.
25
+ * \param result returns the pointer to result report.
26
+ * \param blueprint returns the pointer to blueprint AST.
27
+ *
28
+ * \return Error status code. Zero represents success, non-zero a failure.
29
+ *
30
+ * \this function will allocate `result` and `bluepring`, for deallocation `sc_blueprint_free` and `sc_result_free` should be called.
31
+ */
32
+ SC_API int sc_c_parse(const char* source, sc_blueprint_parser_options option, sc_result_t** result, sc_blueprint_t** blueprint);
33
+
34
+ #ifdef __cplusplus
35
+ }
36
+ #endif
37
+
38
+ #endif
@@ -0,0 +1,99 @@
1
+ //
2
+ // RegexMatch.cc
3
+ // snowcrash
4
+ //
5
+ // Created by Zdenek Nemec on 5/2/13.
6
+ // Copyright (c) 2013 Apiary Inc. All rights reserved.
7
+ //
8
+
9
+ #include <regex.h>
10
+ #include <cstring>
11
+ #include "RegexMatch.h"
12
+
13
+ // FIXME: Migrate to C++11.
14
+ // Naive implementation of regex matching using POSIX regex
15
+ bool snowcrash::RegexMatch(const std::string& target, const std::string& expression)
16
+ {
17
+ if (target.empty() || expression.empty())
18
+ return false;
19
+
20
+ regex_t regex;
21
+ int reti = ::regcomp(&regex, expression.c_str(), REG_EXTENDED | REG_NOSUB);
22
+ if (reti) {
23
+ // Unable to compile regex
24
+ return false;
25
+ }
26
+
27
+ // Execute regular expression
28
+ reti = ::regexec(&regex, target.c_str(), 0, NULL, 0);
29
+ if (!reti) {
30
+ ::regfree(&regex);
31
+ return true;
32
+ }
33
+ else if (reti == REG_NOMATCH) {
34
+ ::regfree(&regex);
35
+ return false;
36
+ }
37
+ else {
38
+ char msgbuf[1024];
39
+ regerror(reti, &regex, msgbuf, sizeof(msgbuf));
40
+ ::regfree(&regex);
41
+ return false;
42
+ }
43
+
44
+ return false;
45
+ }
46
+
47
+ std::string snowcrash::RegexCaptureFirst(const std::string& target, const std::string& expression)
48
+ {
49
+ CaptureGroups groups;
50
+ if (!RegexCapture(target, expression, groups) ||
51
+ groups.size() < 2)
52
+ return std::string();
53
+
54
+ return groups[1];
55
+ }
56
+
57
+ bool snowcrash::RegexCapture(const std::string& target, const std::string& expression, CaptureGroups& captureGroups, size_t groupSize)
58
+ {
59
+ if (target.empty() || expression.empty())
60
+ return false;
61
+
62
+ captureGroups.clear();
63
+
64
+ try {
65
+ regex_t regex;
66
+ int reti = ::regcomp(&regex, expression.c_str(), REG_EXTENDED);
67
+ if (reti)
68
+ return false;
69
+
70
+ regmatch_t *pmatch = ::new regmatch_t[groupSize];
71
+ ::memset(pmatch, 0, sizeof(regmatch_t) * groupSize);
72
+
73
+ reti = ::regexec(&regex, target.c_str(), groupSize, pmatch, 0);
74
+ if (!reti) {
75
+ ::regfree(&regex);
76
+
77
+ for (size_t i = 0; i < groupSize; ++i) {
78
+ if (pmatch[i].rm_so == -1 || pmatch[i].rm_eo == -1)
79
+ captureGroups.push_back(std::string());
80
+ else
81
+ captureGroups.push_back(std::string(target, pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so));
82
+ }
83
+
84
+ delete [] pmatch;
85
+ return true;
86
+ }
87
+ else {
88
+ ::regfree(&regex);
89
+ delete [] pmatch;
90
+ return false;
91
+ }
92
+ }
93
+ catch (...) {
94
+ }
95
+
96
+ return false;
97
+ }
98
+
99
+
@@ -0,0 +1,18 @@
1
+ //
2
+ // snowcrash.cc
3
+ // snowcrash
4
+ //
5
+ // Created by Zdenek Nemec on 4/2/13.
6
+ // Copyright (c) 2013 Apiary Inc. All rights reserved.
7
+ //
8
+
9
+ #include "snowcrash.h"
10
+
11
+ using namespace snowcrash;
12
+
13
+ int snowcrash::parse(const SourceData& source, BlueprintParserOptions options, Result& result, Blueprint& blueprint)
14
+ {
15
+ Parser p;
16
+ p.parse(source, options, result, blueprint);
17
+ return result.error.code;
18
+ }
@@ -0,0 +1,41 @@
1
+ //
2
+ // snowcrash.h
3
+ // snowcrash
4
+ //
5
+ // Created by Zdenek Nemec on 4/2/13.
6
+ // Copyright (c) 2013 Apiary Inc. All rights reserved.
7
+ //
8
+
9
+ #ifndef SNOWCRASH_H
10
+ #define SNOWCRASH_H
11
+
12
+ #include "Parser.h"
13
+
14
+ /**
15
+ * API Blueprint Parser Interface
16
+ * ------------------------------
17
+ *
18
+ * This is the parser's entry point.
19
+ *
20
+ * For Snow Crash users, this is the only interface to use.
21
+ *
22
+ * For binding writers, this is the point to start wrapping.
23
+ * Refer to https://github.com/apiaryio/snowcrash/wiki/Writing-a-binding
24
+ * for details on how to write a Snow Crash binding.
25
+ */
26
+
27
+ namespace snowcrash {
28
+
29
+ /**
30
+ * \brief Parse the source data into a blueprint abstract source tree (AST).
31
+ *
32
+ * \param source A textual source data to be parsed.
33
+ * \param options Parser options. Use 0 for no addtional options.
34
+ * \param result Parsing result report.
35
+ * \param blueprint Parsed blueprint AST.
36
+ * \return Error status code. Zero represents success, non-zero a failure.
37
+ */
38
+ int parse(const SourceData& source, BlueprintParserOptions options, Result& result, Blueprint& blueprint);
39
+ }
40
+
41
+ #endif
@@ -0,0 +1,170 @@
1
+ //
2
+ // snowcrash.cc
3
+ // snowcrash
4
+ //
5
+ // Created by Zdenek Nemec on 4/27/13.
6
+ // Copyright (c) 2013 Apiary Inc. All rights reserved.
7
+ //
8
+
9
+ #include <iostream>
10
+ #include <sstream>
11
+ #include <fstream>
12
+ #include "snowcrash.h"
13
+ #include "SerializeJSON.h"
14
+ #include "SerializeYAML.h"
15
+ #include "cmdline.h"
16
+ #include "Version.h"
17
+
18
+ using snowcrash::SourceAnnotation;
19
+ using snowcrash::Error;
20
+
21
+ static const std::string OutputArgument = "output";
22
+ static const std::string FormatArgument = "format";
23
+ static const std::string RenderArgument = "render";
24
+ static const std::string ValidateArgument = "validate";
25
+ static const std::string VersionArgument = "version";
26
+
27
+ /// \enum Snow Crash AST output format.
28
+ enum SerializationFormat {
29
+ YAMLSerializationFormat,
30
+ JSONSerializationFormat
31
+ };
32
+
33
+ /// \brief Print Markdown source annotation.
34
+ /// \param prefix A string prefix for the annotation
35
+ /// \param annotation An annotation to print
36
+ void PrintAnnotation(const std::string& prefix, const snowcrash::SourceAnnotation& annotation)
37
+ {
38
+ std::cerr << prefix;
39
+
40
+ if (annotation.code != SourceAnnotation::OK) {
41
+ std::cerr << " (" << annotation.code << ") ";
42
+ }
43
+
44
+ if (!annotation.message.empty()) {
45
+ std::cerr << " " << annotation.message;
46
+ }
47
+
48
+ if (!annotation.location.empty()) {
49
+ for (snowcrash::SourceCharactersBlock::const_iterator it = annotation.location.begin();
50
+ it != annotation.location.end();
51
+ ++it) {
52
+ std::cerr << ((it == annotation.location.begin()) ? " :" : ";");
53
+ std::cerr << it->location << ":" << it->length;
54
+ }
55
+ }
56
+
57
+ std::cerr << std::endl;
58
+ }
59
+
60
+ /// \brief Print parser result to stderr.
61
+ /// \param result A parser result to print
62
+ void PrintResult(const snowcrash::Result& result)
63
+ {
64
+ std::cerr << std::endl;
65
+
66
+ if (result.error.code == Error::OK) {
67
+ std::cerr << "OK.\n";
68
+ }
69
+ else {
70
+ PrintAnnotation("error:", result.error);
71
+ }
72
+
73
+ for (snowcrash::Warnings::const_iterator it = result.warnings.begin(); it != result.warnings.end(); ++it) {
74
+ PrintAnnotation("warning:", *it);
75
+ }
76
+ }
77
+
78
+ int main(int argc, const char *argv[])
79
+ {
80
+ cmdline::parser argumentParser;
81
+
82
+ argumentParser.set_program_name("snowcrash");
83
+ std::stringstream ss;
84
+ ss << "<input file>\n\n";
85
+ ss << "API Blueprint Parser\n";
86
+ ss << "If called without <input file>, 'snowcrash' will listen on stdin.\n";
87
+ argumentParser.footer(ss.str());
88
+
89
+ argumentParser.add<std::string>(OutputArgument, 'o', "save output AST into file", false);
90
+ argumentParser.add<std::string>(FormatArgument, 'f', "output AST format", false, "yaml", cmdline::oneof<std::string>("yaml", "json"));
91
+ // TODO: argumentParser.add("render", 'r', "render markdown descriptions");
92
+ argumentParser.add("help", 'h', "display this help message");
93
+ argumentParser.add(VersionArgument, 'v', "print Snow Crash version");
94
+ argumentParser.add(ValidateArgument, 'l', "validate input only, do not print AST");
95
+
96
+ argumentParser.parse_check(argc, argv);
97
+
98
+ // Check arguments
99
+ if (argumentParser.rest().size() > 1) {
100
+ std::cerr << "one input file expected, got " << argumentParser.rest().size() << std::endl;
101
+ exit(EXIT_FAILURE);
102
+ }
103
+
104
+ // Version query
105
+ if (argumentParser.exist(VersionArgument)) {
106
+ std::cout << SNOWCRASH_VERSION_STRING << std::endl;
107
+ exit(EXIT_SUCCESS);
108
+ }
109
+
110
+ // Input
111
+ std::stringstream inputStream;
112
+ if (argumentParser.rest().empty()) {
113
+ // Read stdin
114
+ inputStream << std::cin.rdbuf();
115
+ }
116
+ else {
117
+ // Read from file
118
+ std::ifstream inputFileStream;
119
+ std::string inputFileName = argumentParser.rest().front();
120
+ inputFileStream.open(inputFileName.c_str());
121
+ if (!inputFileStream.is_open()) {
122
+ std::cerr << "fatal: unable to open input file '" << inputFileName << "'\n";
123
+ exit(EXIT_FAILURE);
124
+ }
125
+
126
+ inputStream << inputFileStream.rdbuf();
127
+ inputFileStream.close();
128
+ }
129
+
130
+ // Parse
131
+ snowcrash::BlueprintParserOptions options = 0; // Or snowcrash::RequireBlueprintNameOption
132
+ snowcrash::Result result;
133
+ snowcrash::Blueprint blueprint;
134
+ snowcrash::parse(inputStream.str(), options, result, blueprint);
135
+
136
+ // Output
137
+ if (!argumentParser.exist(ValidateArgument)) {
138
+
139
+ std::stringstream outputStream;
140
+
141
+ if (argumentParser.get<std::string>(FormatArgument) == "json") {
142
+ SerializeJSON(blueprint, outputStream);
143
+ }
144
+ else if (argumentParser.get<std::string>(FormatArgument) == "yaml") {
145
+ SerializeYAML(blueprint, outputStream);
146
+ }
147
+
148
+ std::string outputFileName = argumentParser.get<std::string>(OutputArgument);
149
+ if (!outputFileName.empty()) {
150
+ // Serialize to file
151
+ std::ofstream outputFileStream;
152
+ outputFileStream.open(outputFileName.c_str());
153
+ if (!outputFileStream.is_open()) {
154
+ std::cerr << "fatal: unable to write to file '" << outputFileName << "'\n";
155
+ exit(EXIT_FAILURE);
156
+ }
157
+
158
+ outputFileStream << outputStream.rdbuf();
159
+ outputFileStream.close();
160
+ }
161
+ else {
162
+ // Serialize to stdout
163
+ std::cout << outputStream.rdbuf();
164
+ }
165
+ }
166
+
167
+ // Result
168
+ PrintResult(result);
169
+ return result.error.code;
170
+ }
@@ -0,0 +1,78 @@
1
+ //
2
+ // RegexMatch.cc
3
+ // snowcrash
4
+ //
5
+ // Created by Zdenek Nemec on 7/1/13.
6
+ // Copyright (c) 2013 Apiary Inc. All rights reserved.
7
+ //
8
+
9
+ #include <regex>
10
+ #include <cstring>
11
+ #include "RegexMatch.h"
12
+
13
+ using namespace std;
14
+
15
+ #if _MSC_VER == 1500
16
+ using namespace std::tr1;
17
+ #endif
18
+ //
19
+ // A C++09 implementation
20
+ //
21
+
22
+ bool snowcrash::RegexMatch(const string& target, const string& expression)
23
+ {
24
+ if (target.empty() || expression.empty())
25
+ return false;
26
+
27
+ try {
28
+ regex pattern(expression, regex_constants::extended);
29
+ return regex_search(target, pattern);
30
+ }
31
+ catch (const regex_error&) {
32
+ }
33
+ catch (...) {
34
+ }
35
+
36
+ return false;
37
+ }
38
+
39
+ string snowcrash::RegexCaptureFirst(const string& target, const string& expression)
40
+ {
41
+ CaptureGroups groups;
42
+ if (!RegexCapture(target, expression, groups) ||
43
+ groups.size() < 2)
44
+ return string();
45
+
46
+ return groups[1];
47
+ }
48
+
49
+ bool snowcrash::RegexCapture(const string& target, const string& expression, CaptureGroups& captureGroups, size_t groupSize)
50
+ {
51
+ if (target.empty() || expression.empty())
52
+ return false;
53
+
54
+ captureGroups.clear();
55
+
56
+ try {
57
+
58
+ regex pattern(expression, regex_constants::extended);
59
+ match_results<string::const_iterator> result;
60
+ if (!regex_search(target, result, pattern))
61
+ return false;
62
+
63
+ for (match_results<string::const_iterator>::const_iterator it = result.begin();
64
+ it != result.end();
65
+ ++it) {
66
+
67
+ captureGroups.push_back(*it);
68
+ }
69
+
70
+ return true;
71
+ }
72
+ catch (const regex_error&) {
73
+ }
74
+ catch (...) {
75
+ }
76
+
77
+ return false;
78
+ }