void Compiler::parse( const QDomElement &element ) { NSManager namespaceManager; MessageHandler messageHandler; ParserContext context; context.setNamespaceManager( &namespaceManager ); context.setMessageHandler( &messageHandler ); context.setDocumentBaseUrl( Settings::self()->wsdlBaseUrl() ); Definitions definitions; definitions.setWantedService( Settings::self()->wantedService() ); if ( definitions.loadXML( &context, element, Settings::self()->ignoreSslErrors() ) ) { KODE::Code::setDefaultIndentation( 4 ); WSDL wsdl; wsdl.setDefinitions( definitions ); wsdl.setNamespaceManager( namespaceManager ); KWSDL::Converter converter; converter.setWSDL( wsdl ); if ( !converter.convert() ) { QCoreApplication::exit( 4 ); } else { KWSDL::Creator creator; creator.create( converter.classes() ); QCoreApplication::exit( 0 ); } } else { QCoreApplication::exit( 3 ); } }
void Model_Witcher::addState(ParserContext &ctx) { if (!ctx.state || ctx.nodes.empty()) { ctx.clear(); return; } for (std::list<ModelNode_Witcher *>::iterator n = ctx.nodes.begin(); n != ctx.nodes.end(); ++n) { ctx.state->nodeList.push_back(*n); ctx.state->nodeMap.insert(std::make_pair((*n)->getName(), *n)); if (!(*n)->getParent()) ctx.state->rootNodes.push_back(*n); } _stateList.push_back(ctx.state); _stateMap.insert(std::make_pair(ctx.state->name, ctx.state)); if (!_currentState) _currentState = ctx.state; ctx.state = 0; ctx.nodes.clear(); }
int main(int argc, char **argv) { cl::ParseCommandLineOptions(argc, argv); CompilerContext context; context.inputFilename = InputFilename; context.outputFilename = OutputFilename; context.debugSymobols = DebugSymbols; context.searchPaths = IncludePaths; //yydebug = 1; ParserContext parserContext; if(!parserContext.parse(context.inputFilename)){ return -1; } context.mCompileUnit = parserContext.compileUnit; SemPass semPass(&context); semPass.doIt(); if(context.hasErrors()) { exit(1); } LLVMCodeGenerator codeGenerator(&context); codeGenerator.generateCode(parserContext.compileUnit); std::string errorCode; raw_fd_ostream output(context.outputFilename.c_str(), errorCode, sys::fs::OpenFlags::F_None); codeGenerator.getModule()->print(output, NULL); /** * could also output to llvm bitcode using: * #include <llvm/Bitcode/ReaderWriter.h> * WriteBitcodeToFile */ return 0; }
VMParserContext* ScriptVM::loadScript(std::istream* is) { ParserContext* context = new ParserContext(this); //printf("parserCtx=0x%lx\n", (uint64_t)context); context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); context->registerBuiltInIntVariables( builtInIntVariables() ); context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); context->createScanner(is); InstrScript_parse(context); dmsg(2,("Allocating %d bytes of global int VM memory.\n", context->globalIntVarCount * sizeof(int))); dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount)); if (!context->globalIntMemory) context->globalIntMemory = new ArrayList<int>(); if (!context->globalStrMemory) context->globalStrMemory = new ArrayList<String>(); context->globalIntMemory->resize(context->globalIntVarCount); memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(int)); context->globalStrMemory->resize(context->globalStrVarCount); context->destroyScanner(); return context; }
void Model_Witcher::newState(ParserContext &ctx) { ctx.clear(); ctx.state = new State; }
void Model_KotOR::newState(ParserContext &ctx) { ctx.clear(); ctx.state = new State; }
bool IfTagOption::TryParseExpression(ParserContext& context) { Token* token, * lhsToken = NULL, * rhsToken = NULL, * unaryToken = NULL; ConditionalOperator conditionalOperator; UnaryOperator unaryOperator = UnaryAnd; // Evaluate the expressions do { // Get the LHS token if (!context.TryNext(token)) { context.Error.Description = "unexpected end of file while parsing condition"; return false; } // Ensure the type of the LHS if (!IsValidFragment(token)) return context.Error.Set(token, "invalid left hand side of expression: " + token->Value); // Save the LHS token lhsToken = token; // Get the operator token if (!context.TryNext(token)) return context.Error.Set(token, "unexpected end of file while parsing condition"); // Check the type if (token->Type & TokenOperator) { // Check the operator if (token->Value == "==") conditionalOperator = ConditionalEquals; else if ((token->Value == "!=") || (token->Value == "<>")) conditionalOperator = ConditionalNotEquals; else if (token->Value == ">") conditionalOperator = ConditionalGreaterThan; else if (token->Value == "<") conditionalOperator = ConditionalLessThan; else if (token->Value == ">=") conditionalOperator = ConditionalGreaterThanOrEquals; else if (token->Value == "<=") conditionalOperator = ConditionalLessThanOrEquals; else return context.Error.Set(token, "invalid expression operator: " + token->Value); // Get the RHS token if (!context.TryNext(token)) return context.Error.Set(token, "unexpected end of file while parsing condition"); // Check the type if (!IsValidFragment(token)) return context.Error.Set(token, "invalid right hand side of expression: " + token->Value); // Store the RHS token rhsToken = token; // Try to get the unary operator if (!context.TryNext(token)) return context.Error.Set(token, "unexpected end of file while parsing condition"); } else conditionalOperator = ConditionalImplicit; // Evaluate the unary operator if (token->Type & TokenIdentifier) { // Ensure that this is a unary operator if (token->Value == "and") unaryOperator = UnaryAnd; else if (token->Value == "or") unaryOperator = UnaryOr; else return context.Error.Set(token, "invalid unary operator: " + token->Value); // Store the unary operator token unaryToken = token; } else unaryToken = NULL; // Construct the condition Condition* condition = new Condition(lhsToken, conditionalOperator, rhsToken); if (this->_rootCondition) condition->Attach(this->_rootCondition, unaryOperator); this->_rootCondition = condition; // Reset the stored tokens lhsToken = NULL; rhsToken = NULL; } while (unaryToken != NULL); // Ensure that this is a closing tag if (!(token->Type & TokenTagClose)) return context.Error.Set(token, "expected end of tag but got " + token->Value); // Done return true; }
ManagedVector<Node> ResourceManager::parse( ParserContext &ctx, const std::string &path, const std::string &mimetype, const std::string &rel, const RttiSet &supportedTypes, ParseMode mode) { // Some references used for convenience Registry ®istry = ctx.getRegistry(); Logger &logger = ctx.getLogger(); ParserScope &scope = ctx.getScope(); Resource relativeTo = getResource(ctx.getSourceId()); // Locate the resource relative to the old resource, abort if this did not // work ResourceRequest req{path, mimetype, rel, supportedTypes, relativeTo}; Resource resource; if (!req.deduce(registry, logger) || !req.locate(registry, logger, resource)) { return ManagedVector<Node>{}; } // initialize the output vector. ManagedVector<Node> parsedNodes; // Allocate a new SourceId handle for this Resource bool newResource = false; SourceId sourceId = getSourceId(resource); if (sourceId == InvalidSourceId) { newResource = true; sourceId = allocateSourceId(resource); } // check for cycles. GuardedSetInsertion<SourceId> cycleDetection{currentlyParsing, sourceId}; if (!cycleDetection.isSuccess()) { throw LoggableException{std::string("Detected cyclic parse of ") + resource.getLocation()}; } if (!newResource && mode == ParseMode::IMPORT) { // if a already imported resource should be imported we just use the // cached node. parsedNodes.push_back(getNode(ctx.getManager(), sourceId)); } else { // We can now try to parse the given file // Set the current source id in the logger instance. Note that this // modifies the logger instance -- the GuardedLogger is just used to // make sure the default location is popped from the stack again. GuardedLogger guardedLogger(logger, SourceLocation{sourceId}); try { // Fetch the input stream and create a char reader std::unique_ptr<std::istream> is = resource.stream(); CharReader reader(*is, sourceId); // Actually parse the input stream, distinguish the IMPORT and the // INCLUDE mode switch (mode) { case ParseMode::IMPORT: { // Create a new, empty parser scope instance and a new // parser // context with this instance in place ParserScope innerScope; ParserContext childCtx = ctx.clone(innerScope, sourceId); // Run the parser req.getParser()->parse(reader, childCtx); // Make sure the scope has been unwound and perform all // deferred resolutions innerScope.checkUnwound(logger); innerScope.performDeferredResolution(logger); // Fetch the nodes that were parsed by this parser instance // and // validate them parsedNodes = innerScope.getTopLevelNodes(); for (auto parsedNode : parsedNodes) { parsedNode->validate(logger); } // Make sure the number of elements is exactly one -- we can // only store one element per top-level node. if (parsedNodes.empty()) { throw LoggableException{"Module is empty."}; } if (parsedNodes.size() > 1) { throw LoggableException{ std::string( "Expected exactly one top-level node but " "got ") + std::to_string(parsedNodes.size())}; } // Store the parsed node along with the sourceId storeNode(sourceId, parsedNodes[0]); break; } case ParseMode::INCLUDE: { // Fork the scope instance and create a new parser context // with this instance in place ParserScope forkedScope = scope.fork(); ParserContext childCtx = ctx.clone(forkedScope, sourceId); // Run the parser req.getParser()->parse(reader, childCtx); // Join the forked scope with the outer scope scope.join(forkedScope, logger); // Fetch the nodes that were parsed by this parser instance parsedNodes = forkedScope.getTopLevelNodes(); break; } } } catch (LoggableException ex) { // Log the exception and return nullptr logger.log(ex); return ManagedVector<Node>{}; } } // Make sure the parsed nodes fulfill the "supportedTypes" constraint, // remove nodes that do not the result for (auto it = parsedNodes.begin(); it != parsedNodes.end();) { const Rtti *type = (*it)->type(); if (!type->isOneOf(supportedTypes)) { logger.error(std::string("Node of internal type ") + type->name + std::string(" not supported here"), **it); it = parsedNodes.erase(it); } else { it++; } } return parsedNodes; }
bool BranchNode::TryParse(ParserContext& context) { Token* nextToken; // Parse through all nodes while (context.TryNext(nextToken)) { switch (nextToken->Type) { case TokenLiteral: { // * Construct a literal node this->_children.push_back(new LiteralNode(nextToken->Value)); break; } case TokenTagOpen: { // * Construct a specific tag node // Ensure that the tag name is an identifier if (!context.TryNext(nextToken)) return context.Error.Set(nextToken, "unexpected end of file after tag opening"); if (!(nextToken->Type & TokenIdentifier)) return context.Error.Set(nextToken, "invalid tag name: " + nextToken->Value); // Resolve the initializer for the tag TagInitializer initializer = context.Strain.ResolveTagInitializer(nextToken->Value); if (!initializer) { UnknownTagHandling handling = this->TryHandleUnknownTag(nextToken, context); switch (handling) { case UnknownUnknown: return context.Error.Set(nextToken, "unknown tag " + nextToken->Value); case UnknownError: return false; case UnknownHandled: break; case UnknownEndParsing: return true; } } else { // Invoke the initializer Node* tagNode = (*initializer)(nextToken, context); if (tagNode == NULL) return false; this->_children.push_back(tagNode); } break; } case TokenOutputOpen: { // * Construct an output node // Construct the actual node OutputNode* node = new OutputNode(); // Try to parse the output node if (!node->TryParse(context)) { delete node; return false; } // Push back the node this->_children.push_back((Node*)node); break; } default: { // * Unexpected token return context.Error.Set(nextToken, "unexpected token " + nextToken->Value); } } } return true; }