Path: blob/a-new-beginning/SharedDependencies/Sources/nihstro/parser_assembly/declaration.cpp
2 views
// Copyright 2014 Tony Wasserka1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are met:5//6// * Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// * Redistributions in binary form must reproduce the above copyright9// notice, this list of conditions and the following disclaimer in the10// documentation and/or other materials provided with the distribution.11// * Neither the name of the owner nor the names of its contributors may12// be used to endorse or promote products derived from this software13// without specific prior written permission.14//15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS16// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT17// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR18// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT19// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,20// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT21// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT24// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.262728#include <boost/fusion/include/adapt_struct.hpp>29#include <boost/phoenix/core/reference.hpp>30#include <boost/spirit/include/qi.hpp>3132#include "nihstro/parser_assembly.h"33#include "nihstro/parser_assembly_private.h"3435#include "nihstro/shader_binary.h"36#include "nihstro/shader_bytecode.h"3738using spirit::_1;39using spirit::_2;40using spirit::_3;41using spirit::_4;4243using namespace nihstro;4445// Adapt parser data structures for use with boost::spirit4647BOOST_FUSION_ADAPT_STRUCT(48ConditionInput,49(bool, invert)50(Identifier, identifier)51(boost::optional<InputSwizzlerMask>, swizzler_mask)52)5354BOOST_FUSION_ADAPT_STRUCT(55StatementDeclaration::Extra,56(std::vector<float>, constant_value)57(boost::optional<OutputRegisterInfo::Type>, output_semantic)58)5960BOOST_FUSION_ADAPT_STRUCT(61StatementDeclaration,62(std::string, alias_name)63(Identifier, identifier_start)64(boost::optional<Identifier>, identifier_end)65(boost::optional<InputSwizzlerMask>, swizzle_mask)66(StatementDeclaration::Extra, extra)67)6869// Manually define a swap() overload for qi::hold to work.70/*namespace boost {71namespace spirit {72void swap(nihstro::Condition& a, nihstro::Condition& b) {73boost::fusion::swap(a, b);74}75}76}*/7778template<>79DeclarationParser<ParserIterator>::DeclarationParser(const ParserContext& context)80: DeclarationParser::base_type(declaration),81common(context),82identifier(common.identifier), swizzle_mask(common.swizzle_mask),83end_of_statement(common.end_of_statement),84diagnostics(common.diagnostics) {8586// Setup symbol table87output_semantics.add("position", OutputRegisterInfo::POSITION);88output_semantics.add("quaternion", OutputRegisterInfo::QUATERNION);89output_semantics.add("color", OutputRegisterInfo::COLOR);90output_semantics.add("texcoord0", OutputRegisterInfo::TEXCOORD0);91output_semantics.add("texcoord1", OutputRegisterInfo::TEXCOORD1);92output_semantics.add("texcoord2", OutputRegisterInfo::TEXCOORD2);93output_semantics.add("view", OutputRegisterInfo::VIEW);94output_semantics_rule = qi::lexeme[output_semantics];9596// Setup rules9798alias_identifier = qi::omit[qi::lexeme["alias" >> ascii::blank]] > identifier;99100// e.g. 5.4 or (1.1, 2, 3)101constant = (qi::repeat(1)[qi::float_]102| (qi::lit('(') > (qi::float_ % qi::lit(',')) > qi::lit(')')));103104dummy_const = qi::attr(std::vector<float>());105dummy_semantic = qi::attr(boost::optional<OutputRegisterInfo::Type>());106107// match a constant or a semantic, and fill the respective other one with a dummy108const_or_semantic = (dummy_const >> output_semantics_rule) | (constant >> dummy_semantic);109110// TODO: Would like to use +ascii::blank instead, but somehow that fails to parse lines like ".alias name o2.xy texcoord0" correctly111string_as = qi::omit[qi::no_skip[*/*+*/ascii::blank >> qi::lit("as") >> +ascii::blank]];112113declaration = (((qi::lit('.') > alias_identifier) >> identifier >> -(qi::lit('-') > identifier) >> -(qi::lit('.') > swizzle_mask))114>> (115(string_as > const_or_semantic)116| (dummy_const >> dummy_semantic)117))118> end_of_statement;119120// Error handling121output_semantics_rule.name("output semantic after \"as\"");122alias_identifier.name("known preprocessor directive (i.e. alias).");123const_or_semantic.name("constant or semantic after \"as\"");124125// BOOST_SPIRIT_DEBUG_NODE(output_semantics_rule);126// BOOST_SPIRIT_DEBUG_NODE(constant);127// BOOST_SPIRIT_DEBUG_NODE(alias_identifier);128// BOOST_SPIRIT_DEBUG_NODE(const_or_semantic);129// BOOST_SPIRIT_DEBUG_NODE(declaration);130131// qi::on_error<qi::fail>(declaration, error_handler(phoenix::ref(diagnostics), _1, _2, _3, _4));132}133134135