redsnow 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +34 -0
- data/.gitmodules +3 -0
- data/.travis.yml +20 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +62 -0
- data/Rakefile +36 -0
- data/Vagrantfile +20 -0
- data/ext/snowcrash/Makefile +64 -0
- data/ext/snowcrash/Vagrantfile +20 -0
- data/ext/snowcrash/bin/snowcrash +0 -0
- data/ext/snowcrash/common.gypi +163 -0
- data/ext/snowcrash/config.gypi +10 -0
- data/ext/snowcrash/config.mk +5 -0
- data/ext/snowcrash/configure +213 -0
- data/ext/snowcrash/provisioning.sh +15 -0
- data/ext/snowcrash/snowcrash.gyp +141 -0
- data/ext/snowcrash/src/ActionParser.h +503 -0
- data/ext/snowcrash/src/AssetParser.h +215 -0
- data/ext/snowcrash/src/BlockUtility.h +186 -0
- data/ext/snowcrash/src/Blueprint.h +283 -0
- data/ext/snowcrash/src/BlueprintParser.h +347 -0
- data/ext/snowcrash/src/BlueprintParserCore.h +190 -0
- data/ext/snowcrash/src/BlueprintSection.h +140 -0
- data/ext/snowcrash/src/BlueprintUtility.h +126 -0
- data/ext/snowcrash/src/CBlueprint.cc +600 -0
- data/ext/snowcrash/src/CBlueprint.h +354 -0
- data/ext/snowcrash/src/CSourceAnnotation.cc +140 -0
- data/ext/snowcrash/src/CSourceAnnotation.h +106 -0
- data/ext/snowcrash/src/CodeBlockUtility.h +189 -0
- data/ext/snowcrash/src/DescriptionSectionUtility.h +156 -0
- data/ext/snowcrash/src/HTTP.cc +46 -0
- data/ext/snowcrash/src/HTTP.h +105 -0
- data/ext/snowcrash/src/HeaderParser.h +289 -0
- data/ext/snowcrash/src/ListBlockUtility.h +273 -0
- data/ext/snowcrash/src/ListUtility.h +95 -0
- data/ext/snowcrash/src/MarkdownBlock.cc +176 -0
- data/ext/snowcrash/src/MarkdownBlock.h +93 -0
- data/ext/snowcrash/src/MarkdownParser.cc +266 -0
- data/ext/snowcrash/src/MarkdownParser.h +88 -0
- data/ext/snowcrash/src/ParameterDefinitonParser.h +570 -0
- data/ext/snowcrash/src/ParametersParser.h +252 -0
- data/ext/snowcrash/src/Parser.cc +71 -0
- data/ext/snowcrash/src/Parser.h +29 -0
- data/ext/snowcrash/src/ParserCore.cc +120 -0
- data/ext/snowcrash/src/ParserCore.h +82 -0
- data/ext/snowcrash/src/PayloadParser.h +672 -0
- data/ext/snowcrash/src/Platform.h +54 -0
- data/ext/snowcrash/src/RegexMatch.h +32 -0
- data/ext/snowcrash/src/ResourceGroupParser.h +195 -0
- data/ext/snowcrash/src/ResourceParser.h +584 -0
- data/ext/snowcrash/src/SectionUtility.h +142 -0
- data/ext/snowcrash/src/Serialize.cc +52 -0
- data/ext/snowcrash/src/Serialize.h +69 -0
- data/ext/snowcrash/src/SerializeJSON.cc +601 -0
- data/ext/snowcrash/src/SerializeJSON.h +21 -0
- data/ext/snowcrash/src/SerializeYAML.cc +336 -0
- data/ext/snowcrash/src/SerializeYAML.h +21 -0
- data/ext/snowcrash/src/SourceAnnotation.h +177 -0
- data/ext/snowcrash/src/StringUtility.h +109 -0
- data/ext/snowcrash/src/SymbolTable.h +83 -0
- data/ext/snowcrash/src/UriTemplateParser.cc +195 -0
- data/ext/snowcrash/src/UriTemplateParser.h +243 -0
- data/ext/snowcrash/src/Version.h +39 -0
- data/ext/snowcrash/src/csnowcrash.cc +23 -0
- data/ext/snowcrash/src/csnowcrash.h +38 -0
- data/ext/snowcrash/src/posix/RegexMatch.cc +99 -0
- data/ext/snowcrash/src/snowcrash.cc +18 -0
- data/ext/snowcrash/src/snowcrash.h +41 -0
- data/ext/snowcrash/src/snowcrash/snowcrash.cc +170 -0
- data/ext/snowcrash/src/win/RegexMatch.cc +78 -0
- data/ext/snowcrash/sundown/CONTRIBUTING.md +10 -0
- data/ext/snowcrash/sundown/Makefile +83 -0
- data/ext/snowcrash/sundown/Makefile.win +33 -0
- data/ext/snowcrash/sundown/examples/smartypants.c +72 -0
- data/ext/snowcrash/sundown/examples/sundown.c +80 -0
- data/ext/snowcrash/sundown/html/houdini.h +37 -0
- data/ext/snowcrash/sundown/html/houdini_href_e.c +108 -0
- data/ext/snowcrash/sundown/html/houdini_html_e.c +84 -0
- data/ext/snowcrash/sundown/html/html.c +647 -0
- data/ext/snowcrash/sundown/html/html.h +77 -0
- data/ext/snowcrash/sundown/html/html_smartypants.c +389 -0
- data/ext/snowcrash/sundown/html_block_names.txt +25 -0
- data/ext/snowcrash/sundown/src/autolink.c +297 -0
- data/ext/snowcrash/sundown/src/autolink.h +51 -0
- data/ext/snowcrash/sundown/src/buffer.c +225 -0
- data/ext/snowcrash/sundown/src/buffer.h +96 -0
- data/ext/snowcrash/sundown/src/html_blocks.h +206 -0
- data/ext/snowcrash/sundown/src/markdown.c +2701 -0
- data/ext/snowcrash/sundown/src/markdown.h +147 -0
- data/ext/snowcrash/sundown/src/src_map.c +200 -0
- data/ext/snowcrash/sundown/src/src_map.h +58 -0
- data/ext/snowcrash/sundown/src/stack.c +81 -0
- data/ext/snowcrash/sundown/src/stack.h +29 -0
- data/ext/snowcrash/sundown/sundown.def +20 -0
- data/ext/snowcrash/tools/gyp/AUTHORS +11 -0
- data/ext/snowcrash/tools/gyp/DEPS +24 -0
- data/ext/snowcrash/tools/gyp/OWNERS +1 -0
- data/ext/snowcrash/tools/gyp/PRESUBMIT.py +120 -0
- data/ext/snowcrash/tools/gyp/buildbot/buildbot_run.py +190 -0
- data/ext/snowcrash/tools/gyp/codereview.settings +10 -0
- data/ext/snowcrash/tools/gyp/data/win/large-pdb-shim.cc +12 -0
- data/ext/snowcrash/tools/gyp/gyp +8 -0
- data/ext/snowcrash/tools/gyp/gyp.bat +5 -0
- data/ext/snowcrash/tools/gyp/gyp_main.py +18 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSNew.py +340 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSProject.py +208 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSSettings.py +1063 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSToolFile.py +58 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSUserFile.py +147 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSUtil.py +267 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSVersion.py +409 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/__init__.py +537 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/__init__.pyc +0 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/common.py +521 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/common.pyc +0 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/easy_xml.py +157 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/flock_tool.py +49 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/generator/__init__.py +0 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/generator/__init__.pyc +0 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/generator/android.py +1069 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/generator/cmake.py +1143 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/generator/dump_dependency_json.py +81 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/generator/eclipse.py +335 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/generator/gypd.py +87 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/generator/gypsh.py +56 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/generator/make.py +2181 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/generator/make.pyc +0 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/generator/msvs.py +3335 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/generator/ninja.py +2156 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/generator/xcode.py +1224 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/generator/xcode.pyc +0 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/input.py +2809 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/input.pyc +0 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/mac_tool.py +510 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/msvs_emulation.py +972 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/ninja_syntax.py +160 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/ordered_dict.py +289 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/win_tool.py +292 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/xcode_emulation.py +1440 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/xcode_emulation.pyc +0 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/xcodeproj_file.py +2889 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/xcodeproj_file.pyc +0 -0
- data/ext/snowcrash/tools/gyp/pylib/gyp/xml_fix.py +69 -0
- data/ext/snowcrash/tools/gyp/pylintrc +307 -0
- data/ext/snowcrash/tools/gyp/samples/samples +81 -0
- data/ext/snowcrash/tools/gyp/samples/samples.bat +5 -0
- data/ext/snowcrash/tools/gyp/setup.py +19 -0
- data/ext/snowcrash/tools/gyp/tools/Xcode/Specifications/gyp.pbfilespec +27 -0
- data/ext/snowcrash/tools/gyp/tools/Xcode/Specifications/gyp.xclangspec +226 -0
- data/ext/snowcrash/tools/gyp/tools/emacs/gyp.el +252 -0
- data/ext/snowcrash/tools/gyp/tools/graphviz.py +100 -0
- data/ext/snowcrash/tools/gyp/tools/pretty_gyp.py +155 -0
- data/ext/snowcrash/tools/gyp/tools/pretty_sln.py +168 -0
- data/ext/snowcrash/tools/gyp/tools/pretty_vcproj.py +329 -0
- data/ext/snowcrash/tools/homebrew/snowcrash.rb +11 -0
- data/ext/snowcrash/vcbuild.bat +184 -0
- data/lib/redsnow.rb +31 -0
- data/lib/redsnow/binding.rb +132 -0
- data/lib/redsnow/blueprint.rb +365 -0
- data/lib/redsnow/object.rb +18 -0
- data/lib/redsnow/parseresult.rb +107 -0
- data/lib/redsnow/version.rb +4 -0
- data/provisioning.sh +20 -0
- data/redsnow.gemspec +35 -0
- data/test/_helper.rb +15 -0
- data/test/fixtures/sample-api-ast.json +97 -0
- data/test/fixtures/sample-api.apib +20 -0
- data/test/redsnow_binding_test.rb +35 -0
- data/test/redsnow_parseresult_test.rb +50 -0
- data/test/redsnow_test.rb +285 -0
- 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(®ex, 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(®ex, target.c_str(), 0, NULL, 0);
|
29
|
+
if (!reti) {
|
30
|
+
::regfree(®ex);
|
31
|
+
return true;
|
32
|
+
}
|
33
|
+
else if (reti == REG_NOMATCH) {
|
34
|
+
::regfree(®ex);
|
35
|
+
return false;
|
36
|
+
}
|
37
|
+
else {
|
38
|
+
char msgbuf[1024];
|
39
|
+
regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
|
40
|
+
::regfree(®ex);
|
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(®ex, 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(®ex, target.c_str(), groupSize, pmatch, 0);
|
74
|
+
if (!reti) {
|
75
|
+
::regfree(®ex);
|
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(®ex);
|
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
|
+
}
|