Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/SharedDependencies/Sources/nihstro/parser_assembly/declaration.cpp
2 views
1
// Copyright 2014 Tony Wasserka
2
// All rights reserved.
3
//
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are met:
6
//
7
// * Redistributions of source code must retain the above copyright
8
// notice, this list of conditions and the following disclaimer.
9
// * Redistributions in binary form must reproduce the above copyright
10
// notice, this list of conditions and the following disclaimer in the
11
// documentation and/or other materials provided with the distribution.
12
// * Neither the name of the owner nor the names of its contributors may
13
// be used to endorse or promote products derived from this software
14
// without specific prior written permission.
15
//
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29
#include <boost/fusion/include/adapt_struct.hpp>
30
#include <boost/phoenix/core/reference.hpp>
31
#include <boost/spirit/include/qi.hpp>
32
33
#include "nihstro/parser_assembly.h"
34
#include "nihstro/parser_assembly_private.h"
35
36
#include "nihstro/shader_binary.h"
37
#include "nihstro/shader_bytecode.h"
38
39
using spirit::_1;
40
using spirit::_2;
41
using spirit::_3;
42
using spirit::_4;
43
44
using namespace nihstro;
45
46
// Adapt parser data structures for use with boost::spirit
47
48
BOOST_FUSION_ADAPT_STRUCT(
49
ConditionInput,
50
(bool, invert)
51
(Identifier, identifier)
52
(boost::optional<InputSwizzlerMask>, swizzler_mask)
53
)
54
55
BOOST_FUSION_ADAPT_STRUCT(
56
StatementDeclaration::Extra,
57
(std::vector<float>, constant_value)
58
(boost::optional<OutputRegisterInfo::Type>, output_semantic)
59
)
60
61
BOOST_FUSION_ADAPT_STRUCT(
62
StatementDeclaration,
63
(std::string, alias_name)
64
(Identifier, identifier_start)
65
(boost::optional<Identifier>, identifier_end)
66
(boost::optional<InputSwizzlerMask>, swizzle_mask)
67
(StatementDeclaration::Extra, extra)
68
)
69
70
// Manually define a swap() overload for qi::hold to work.
71
/*namespace boost {
72
namespace spirit {
73
void swap(nihstro::Condition& a, nihstro::Condition& b) {
74
boost::fusion::swap(a, b);
75
}
76
}
77
}*/
78
79
template<>
80
DeclarationParser<ParserIterator>::DeclarationParser(const ParserContext& context)
81
: DeclarationParser::base_type(declaration),
82
common(context),
83
identifier(common.identifier), swizzle_mask(common.swizzle_mask),
84
end_of_statement(common.end_of_statement),
85
diagnostics(common.diagnostics) {
86
87
// Setup symbol table
88
output_semantics.add("position", OutputRegisterInfo::POSITION);
89
output_semantics.add("quaternion", OutputRegisterInfo::QUATERNION);
90
output_semantics.add("color", OutputRegisterInfo::COLOR);
91
output_semantics.add("texcoord0", OutputRegisterInfo::TEXCOORD0);
92
output_semantics.add("texcoord1", OutputRegisterInfo::TEXCOORD1);
93
output_semantics.add("texcoord2", OutputRegisterInfo::TEXCOORD2);
94
output_semantics.add("view", OutputRegisterInfo::VIEW);
95
output_semantics_rule = qi::lexeme[output_semantics];
96
97
// Setup rules
98
99
alias_identifier = qi::omit[qi::lexeme["alias" >> ascii::blank]] > identifier;
100
101
// e.g. 5.4 or (1.1, 2, 3)
102
constant = (qi::repeat(1)[qi::float_]
103
| (qi::lit('(') > (qi::float_ % qi::lit(',')) > qi::lit(')')));
104
105
dummy_const = qi::attr(std::vector<float>());
106
dummy_semantic = qi::attr(boost::optional<OutputRegisterInfo::Type>());
107
108
// match a constant or a semantic, and fill the respective other one with a dummy
109
const_or_semantic = (dummy_const >> output_semantics_rule) | (constant >> dummy_semantic);
110
111
// TODO: Would like to use +ascii::blank instead, but somehow that fails to parse lines like ".alias name o2.xy texcoord0" correctly
112
string_as = qi::omit[qi::no_skip[*/*+*/ascii::blank >> qi::lit("as") >> +ascii::blank]];
113
114
declaration = (((qi::lit('.') > alias_identifier) >> identifier >> -(qi::lit('-') > identifier) >> -(qi::lit('.') > swizzle_mask))
115
>> (
116
(string_as > const_or_semantic)
117
| (dummy_const >> dummy_semantic)
118
))
119
> end_of_statement;
120
121
// Error handling
122
output_semantics_rule.name("output semantic after \"as\"");
123
alias_identifier.name("known preprocessor directive (i.e. alias).");
124
const_or_semantic.name("constant or semantic after \"as\"");
125
126
// BOOST_SPIRIT_DEBUG_NODE(output_semantics_rule);
127
// BOOST_SPIRIT_DEBUG_NODE(constant);
128
// BOOST_SPIRIT_DEBUG_NODE(alias_identifier);
129
// BOOST_SPIRIT_DEBUG_NODE(const_or_semantic);
130
// BOOST_SPIRIT_DEBUG_NODE(declaration);
131
132
// qi::on_error<qi::fail>(declaration, error_handler(phoenix::ref(diagnostics), _1, _2, _3, _4));
133
}
134
135