ast::Node *parseTopLevel(TokenIt &it, const TokenIt &end) { switch((*it)->type) { case Token::FUNCTION: return parseFunction(it, end, false); case Token::SUB: return parseFunction(it, end, true); default: return parseStatement(it, end); } }
/** Parses the given descriptor list via customized function. * \param descriptor Pointer to the start of the whole descriptors list. * \param totalLength Total size of descriptors in bytes. * \param parseFunction Function to parse each descriptor scanned. * Return 0 to continue parsing. * \param parseArg Argument passed to parse function. * \return Pointer to USBGenericDescriptor instance for next descriptor. */ USBGenericDescriptor *USBGenericDescriptor_Parse( const USBGenericDescriptor *descriptor, uint32_t totalLength, USBDescriptorParseFunction parseFunction, void *parseArg) { int32_t size = totalLength; if (size == 0) return 0; /* Start parsing descriptors */ while (1) { uint32_t parseRC = 0; /* Parse current descriptor */ if (parseFunction) parseRC = parseFunction((void *)descriptor, parseArg); /* Get next descriptor */ size -= USBGenericDescriptor_GetLength(descriptor); descriptor = USBGenericDescriptor_GetNextDescriptor(descriptor); if (size) { if (parseRC != 0) return (USBGenericDescriptor *)descriptor; } else break; } /* No descriptors remaining */ return 0; }
static boolean parseVimLine (const unsigned char *line) { boolean readNextLine = TRUE; if ( (!strncmp ((const char*) line, "comp", (size_t) 4) == 0) && (!strncmp ((const char*) line, "comc", (size_t) 4) == 0) && (strncmp ((const char*) line, "com", (size_t) 3) == 0) ) { readNextLine = parseCommand(line); /* TODO - Handle parseCommand returning FALSE */ } if (isMap(line)) { parseMap(line); } if (strncmp ((const char*) line, "fu", (size_t) 2) == 0) { parseFunction(line); } if (strncmp ((const char*) line, "aug", (size_t) 3) == 0) { parseAutogroup(line); } if ( strncmp ((const char*) line, "let", (size_t) 3) == 0 ) { parseLet(line); } return readNextLine; }
/*** Parse from zero-level ***/ void Parser::parse() { if(hasTokens() && peekToken().getType() == T_EOL) parseSeparation(); while(hasTokens()) { // if(peekToken().getType() == T_PROC) // parseProcedureDefinition(); // else if(peekToken().getType() == T_FUN) // parseFunctionDefinition(); // else if(peekToken().getType() == T_PROC) { parseProcedure(); parseSeparation(); } else if(peekToken().getType() == T_FUN) { parseFunction(); parseSeparation(); } else { list->pushNode(parseStatement()); parseSeparation(); } } // VariableManager manager; }
void Moc::parseSlots(ClassDef *def, FunctionDef::Access access) { next(COLON); while (inClass(def) && hasNext()) { switch (next()) { case PUBLIC: case PROTECTED: case PRIVATE: case Q_SIGNALS_TOKEN: case Q_SLOTS_TOKEN: prev(); return; case SEMIC: continue; case FRIEND: until(SEMIC); continue; case USING: error("'using' directive not supported in 'slots' section"); default: prev(); } FunctionDef funcDef; funcDef.access = access; if (!parseFunction(&funcDef)) continue; def->slotList += funcDef; while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) { funcDef.wasCloned = true; funcDef.arguments.removeLast(); def->slotList += funcDef; } } }
AstNode* Parser::parseStatement() { if (isKeyword(currentTokenValue())) { if (currentTokenValue() == "function") { return parseFunction(); } else if (currentTokenValue() == "for") { return parseFor(); } else if (currentTokenValue() == "if") { return parseIf(); } else if (currentTokenValue() == "while") { return parseWhile(); } else if (currentTokenValue() == "print") { return parsePrint(); } else if (currentTokenValue() == "int") { return parseDeclaration(VT_INT); } else if (currentTokenValue() == "double") { return parseDeclaration(VT_DOUBLE); } else if (currentTokenValue() == "string") { return parseDeclaration(VT_STRING); } else if (currentTokenValue() == "return") { return parseReturn(); } else { cout << "unhandled keyword " << currentTokenValue() << endl; assert(false); return 0; } } if (currentToken() == tLBRACE) { return parseBlock(true); } if ((currentToken() == tIDENT) && isAssignment(lookaheadToken(1))) { return parseAssignment(); } return parseExpression(); }
double Calculator::parseSymbol(std::string_view &ref) { double value = 0; skipSpaces(ref); if (!ref.empty() && ref[0] == '(') { ref.remove_prefix(1); value = parseExprSum(ref); skipSpaces(ref); if (!ref.empty() && ref[0] == ')') { ref.remove_prefix(1); return value; } else { return std::numeric_limits<double>::quiet_NaN(); } } else if (!ref.empty() && !std::isdigit(ref[0]) && ref[0] != '-') { return parseFunction(ref); } else { return parseDouble(ref); } }
static boolean parseVimLine (const unsigned char *line, int infunction) { boolean readNextLine = TRUE; if (wordMatchLen (line, "command", 3)) { readNextLine = parseCommand(line); /* TODO - Handle parseCommand returning FALSE */ } else if (isMap(line)) { parseMap(skipWord(line)); } else if (wordMatchLen (line, "function", 2)) { parseFunction(skipWord(line)); } else if (wordMatchLen (line, "augroup", 3)) { parseAutogroup(skipWord(line)); } else if (wordMatchLen (line, "let", 3)) { parseLet(skipWord(line), infunction); } return readNextLine; }
void handleFunction() { if (!parseFunction()) { // skip token for error recovery nextToken(); } }
void testCalculate() { char str[64]; int len=0, i, k=0; DParam dp; char strbuff[256] = "f(x)=-x^2"; Function *f = NULL; double val[1]; dp.values = val; f = (Function*)malloc(sizeof(Function)); f->prefix = NULL; f->domain = NULL; f->criterias = NULL; f->str = NULL; f->len = 0; f->variableNode = NULL; f->numVarNode = 0; f->valLen = 0; //printf("Input function: "); //scanf("%s", &strbuff); len = strlen(strbuff); parseFunction(strbuff, len, f); if(getErrorCode() != NMATH_NO_ERROR) { printError(getErrorColumn(), getErrorCode()); releaseFunct(f); free(f); return; } if( f->valLen==0 ) { printf("This expression is not a function due to variables not determined.\n"); } dp.error = NMATH_NO_ERROR; dp.t = f->prefix->list[0]; dp.variables[0] = 'x'; dp.values[0] = 2; //reduce_t(&dp); calc_t(&dp); if(dp.error == NMATH_NO_ERROR) { k = 0; printNMAST(dp.t, 0); printf("\n"); toString(dp.t, str, &k, 64); f->prefix->list[0] = dp.t; printf("Calculating result: %lf\n", dp.retv); } //release token list releaseFunct(f); clearPool(); free(f); }
static BGFunction* parseLine(const char* line) { int lineLength = (int)strlen(line); if (!isFunction(line, lineLength)) return 0; return parseFunction(line, lineLength); }
void BTree_ParseTree( BTree *tree, BTree_parseFunction *parseFunction, void *args ) { int ii; for ( ii = 0; ii < tree->numItems; ++ii ) { if ( tree->items[ii] != NULL ) { parseFunction( tree->items[ii]->data, args ); } } }
void Moc::parseSignals(ClassDef *def) { int defaultRevision = -1; if (test(Q_REVISION_TOKEN)) { next(LPAREN); QByteArray revision = lexemUntil(RPAREN); revision.remove(0, 1); revision.chop(1); bool ok = false; defaultRevision = revision.toInt(&ok); if (!ok || defaultRevision < 0) error("Invalid revision"); } next(COLON); while (inClass(def) && hasNext()) { switch (next()) { case PUBLIC: case PROTECTED: case PRIVATE: case Q_SIGNALS_TOKEN: case Q_SLOTS_TOKEN: prev(); return; case SEMIC: continue; case FRIEND: until(SEMIC); continue; case USING: error("'using' directive not supported in 'signals' section"); default: prev(); } FunctionDef funcDef; funcDef.access = FunctionDef::Protected; parseFunction(&funcDef); if (funcDef.isVirtual) warning("Signals cannot be declared virtual"); if (funcDef.inlineCode) error("Not a signal declaration"); if (funcDef.revision > 0) { ++def->revisionedMethods; } else if (defaultRevision != -1) { funcDef.revision = defaultRevision; ++def->revisionedMethods; } def->signalList += funcDef; while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) { funcDef.wasCloned = true; funcDef.arguments.removeLast(); def->signalList += funcDef; } } }
void ScanForFunctions(CLanguageProxy& proxy) { const char *text = proxy.Text(), *max = text + proxy.Size(); if (*max != 0) return; while (text < max) { text = comment(text); switch (*text++) { case 'c': case 'C': if (!strncasecmp(text, "onstructor", 10)) text = parseFunction(text + 10, proxy); break; case 'd': case 'D': if (!strncasecmp(text, "estructor", 9)) text = parseFunction(text + 9, proxy); break; case 'f': case 'F': if (!strncasecmp(text, "unction", 7)) text = parseFunction(text + 7, proxy); break; case 'p': case 'P': if (!strncasecmp(text, "rocedure", 8)) text = parseFunction(text + 8, proxy); break; case 'u': case 'U': if (!strncasecmp(text, "ses", 3)) text = parseUses(text + 3, proxy); break; } } }
TEST(Wikipedia, AddAndMultiply) { input = "1+0+1*1+1+1+1+1+1*1"; nextToken(); pushState(0); while((top() != STATE_ERROR) && (top() != STATE_ACCEPT)) pushState(parseFunction(token)); EXPECT_EQ(STATE_ACCEPT, top()) << "An error has occured while parsing expression"; EXPECT_EQ(7, valueStack[nbValues - 1].value); }
void LinkedList_ParseListNode( LinkedListNode *root, LinkedList_parseFunction *parseFunction, void *args ) { LinkedListNode *curr; assert(root); assert(parseFunction); curr = root; while(curr != NULL){ parseFunction(curr->data, args); curr = curr->next; } }
void LinkedList_ParseList( LinkedList *list, LinkedList_parseFunction *parseFunction, void *args ) { LinkedListNode *curr; assert(list); assert(parseFunction); curr = list->head; while(curr != NULL){ parseFunction(curr->data, args); curr = curr->next; } }
int testDerivative() { DParam d; int error; double vars[] = {4, 1}; double ret; char dstr[64]; int l = 0; Function *f; f = (Function*)malloc(sizeof(Function)); f->prefix = NULL; f->domain = NULL; f->criterias = NULL; f->str = NULL; f->len = 0; f->variableNode = NULL; f->numVarNode = 0; f->valLen = 0; printf("Input function: "); scanf("%s", &dstr); l = strlen(dstr); parseFunction(dstr, l, f); if(getErrorCode() != NMATH_NO_ERROR) { printError(getErrorColumn(), getErrorCode()); releaseFunct(f); free(f); return getErrorCode(); } if( f->valLen==0 ) { printf("This expression is not a function due to variables not determined.\n"); } d.t = f->prefix->list[0]; d.error = 0; d.returnValue = NULL; d.variables[0] = 'x'; derivative(&d); toString(d.returnValue, dstr, &l, 64); printf("f' = %s\n", dstr); clearTree(&(d.returnValue)); releaseFunct(f); clearPool(); free(f); return 0; }
/* parses declarations of the form * $var = VALUE * $var; */ static boolean parseVariable (tokenInfo *const token) { tokenInfo *name; boolean readNext = TRUE; accessType access = CurrentStatement.access; name = newToken (); copyToken (name, token, TRUE); readToken (token); if (token->type == TOKEN_EQUAL_SIGN) { phpKind kind = K_VARIABLE; if (token->parentKind == K_FUNCTION) kind = K_LOCAL_VARIABLE; readToken (token); if (token->type == TOKEN_KEYWORD && token->keyword == KEYWORD_function && PhpKinds[kind].enabled) { if (parseFunction (token, name)) readToken (token); readNext = (boolean) (token->type == TOKEN_SEMICOLON); } else { makeSimplePhpTag (name, kind, access); readNext = FALSE; } } else if (token->type == TOKEN_SEMICOLON) { /* generate tags for variable declarations in classes * class Foo { * protected $foo; * } * but don't get fooled by stuff like $foo = $bar; */ if (token->parentKind == K_CLASS || token->parentKind == K_INTERFACE || token->parentKind == K_TRAIT) makeSimplePhpTag (name, K_VARIABLE, access); } else readNext = FALSE; deleteToken (name); return readNext; }
bool CParser::parse(std::istream &stream) { m_currentLineIndex = 0; m_currentLineNumber = 1; do { // Get lexeme m_currentLineIndex += m_lexer.lex(stream); // Process lexeme switch (m_lexer.getToken()) { case ELexerToken::NewLine: ++m_currentLineNumber; m_currentLineIndex = 0; break; case ELexerToken::Keyword: if (m_lexer.getLexeme() == "func") { // Script function if (!parseFunction(stream)) { return false; } } else if (m_lexer.getLexeme() == "extern") { // Extern function if (!parseExternFunction(stream)) { return false; } } else { std::cout << "Invalid lexeme encountered: " << m_lexer.getLexeme() << std::endl; return false; } break; case ELexerToken::Invalid: return false; default: break; } } while (m_lexer.getToken() != ELexerToken::End && m_lexer.getToken() != ELexerToken::Invalid); return true; }
static Function Function_create(PG_FUNCTION_ARGS) { ParseResultData info; Function self = (Function)PgObjectClass_allocInstance(s_FunctionClass, TopMemoryContext); HeapTuple procTup = PgObject_getValidTuple(PROCOID, fcinfo->flinfo->fn_oid, "function"); memset(&info, 0, sizeof(ParseResultData)); parseFunction(&info, procTup); Function_init(self, &info, (Form_pg_proc)GETSTRUCT(procTup), fcinfo); pfree(info.buffer); ReleaseSysCache(procTup); return self; }
//lead into the program void Parser::parseBlock() { ++it; switch (it->tag) { case CONSTANT: parseConstant(); parseBlock(); break; case VAR: parseVariables(); parseBlock(); break; case TYPE: parseType(); parseBlock(); break; case FUNCTION: parseFunction(); parseBlock(); break; case PROCEDURE: parseProcedure(); parseBlock(); break; case BEGIN: { shared_ptr<Statement> block = parseStmtList(currNode == root ? FINISH : END); //root的block与函数的block if (currNode != root) { (static_pointer_cast<FunctionStmt>(currNode))->body=block; } else { block->value.value = "__Main__"; currNode->addNode(block); } bool tmp=it->tag == FINISH || it->tag == END; if (it->tag == END) { it++; match(SEMI); } } break; } }
static void enterScope (tokenInfo *const parentToken, const vString *const extraScope, const int parentKind) { tokenInfo *token = newToken (); int origParentKind = parentToken->parentKind; copyToken (token, parentToken, true); if (extraScope) { addToScope (token, extraScope); token->parentKind = parentKind; } readToken (token); while (token->type != TOKEN_EOF && token->type != TOKEN_CLOSE_CURLY) { bool readNext = true; switch (token->type) { case TOKEN_OPEN_CURLY: enterScope (token, NULL, -1); break; case TOKEN_KEYWORD: readNext = parseFunction (token); break; case TOKEN_VARIABLE: readNext = parseVariable (token); break; default: break; } if (readNext) readToken (token); } copyToken (parentToken, token, false); parentToken->parentKind = origParentKind; deleteToken (token); }
void testReuseFunction(Function *f) { double val; int error; double var[1] = {10}; releaseFunct(f); parseFunction("f(x)=x + 1/2", 12, f); if(getErrorCode() != NMATH_NO_ERROR) { printError(getErrorColumn(), getErrorCode()); releaseFunct(f); free(f); } val = calc(f, var, 1, &error); printf("Ret = %lf \n", val ); }
void Moc::parseSlotInPrivate(ClassDef *def, FunctionDef::Access access) { next(LPAREN); FunctionDef funcDef; next(IDENTIFIER); funcDef.inPrivateClass = lexem(); // also allow void functions if (test(LPAREN)) { next(RPAREN); funcDef.inPrivateClass += "()"; } next(COMMA); funcDef.access = access; parseFunction(&funcDef, true); def->slotList += funcDef; while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) { funcDef.wasCloned = true; funcDef.arguments.removeLast(); def->slotList += funcDef; } }
static void skipArgumentList (tokenInfo *const token, boolean include_newlines, vString *const repr) { int nest_level = 0; if (isType (token, TOKEN_OPEN_PAREN)) /* arguments? */ { nest_level++; if (repr) vStringPut (repr, '('); while (nest_level > 0 && ! isType (token, TOKEN_EOF)) { readTokenFull (token, FALSE, repr); if (isType (token, TOKEN_OPEN_PAREN)) nest_level++; else if (isType (token, TOKEN_CLOSE_PAREN)) nest_level--; else if (isKeyword (token, KEYWORD_function)) parseFunction (token); } readTokenFull (token, include_newlines, NULL); } }
void Moc::parseSignals(ClassDef *def) { next(COLON); while (inClass(def) && hasNext()) { switch (next()) { case PUBLIC: case PROTECTED: case PRIVATE: case Q_SIGNALS_TOKEN: case Q_SLOTS_TOKEN: prev(); return; case SEMIC: continue; case FRIEND: until(SEMIC); continue; case USING: error("'using' directive not supported in 'signals' section"); default: prev(); } FunctionDef funcDef; funcDef.access = FunctionDef::Protected; parseFunction(&funcDef); if (funcDef.isVirtual) warning("Signals cannot be declared virtual"); if (funcDef.inlineCode) error("Not a signal declaration"); def->signalList += funcDef; while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) { funcDef.wasCloned = true; funcDef.arguments.removeLast(); def->signalList += funcDef; } } }
void parseFunctions() /* Parse list of functions - skipping other declarations. */ { struct token *lastName = NULL; while (token != NULL) { char c = token->string[0]; if (isalpha(c) || c == '_') { lastName = token; } else if (c == '(') { if (lastName != NULL) parseFunction(lastName); lastName = NULL; } else { lastName = NULL; } token = token->next; } }
void QuickClassParser::parse( const QString &code ) { formCode = code; QString expr; globalClass.name = gname; globalClass.type = QuickClass::Global; currClass = &globalClass; static int functionLength = strlen( "function" ); static int constructorLength = strlen( "constructor" ); for ( pos = 0; pos < (int)formCode.length(); ++pos ) { if ( legalChars.find( formCode[ pos ] ) == -1 ) expr = ""; else expr += formCode[ pos ]; if ( expr == QString::fromLatin1("var") || expr == QString::fromLatin1("const") || expr == QString::fromLatin1("static") ) { int i = pos + 1; while ( formCode[ i ] == ' ' || formCode[ i ] == '\t' ) ++i; if ( formCode.mid( i, 8 ) != QString::fromLatin1("function") ) { ++pos; parseVariableDecl( expr ); } expr = ""; continue; } else if ( expr == QString::fromLatin1("private") || expr == QString::fromLatin1("protected") || expr == QString::fromLatin1("public") ) { lastAccess = expr; expr = QString::fromLatin1(""); continue; } else if ( expr == QString::fromLatin1("function") || expr == QString::fromLatin1("constructor") ) { ++pos; parseFunction( expr == QString::fromLatin1("function") ? functionLength : constructorLength ); expr = ""; continue; } else if ( expr == QString::fromLatin1("connect") ) { ++pos; parseConnections(); expr = ""; continue; } else if ( expr == QString::fromLatin1("//") ) { pos--; parseCppComment(); expr = ""; continue; } else if ( expr == QString::fromLatin1("/*") ) { pos--; parseCComment(); expr = ""; continue; } else if ( expr == QString::fromLatin1("class") ) { if ( currClass != &globalClass ) clsses.append( *currClass ); currClass = new QuickClass; currClass->type = QuickClass::Class; ++pos; parseClassStart(); expr = ""; } else if ( formCode[ pos ] == '}' ) { if ( currClass != &globalClass ) clsses.append( *currClass ); currClass = &globalClass; } } if ( currClass != &globalClass ) clsses.append( *currClass ); clsses.append( globalClass ); }
static boolean parseBlock (tokenInfo *const token, tokenInfo *const parent) { boolean is_class = FALSE; boolean read_next_token = TRUE; vString * saveScope = vStringNew (); token->nestLevel++; /* * Make this routine a bit more forgiving. * If called on an open_curly advance it */ if ( isType (token, TOKEN_OPEN_CURLY) && isKeyword(token, KEYWORD_NONE) ) readToken(token); if (! isType (token, TOKEN_CLOSE_CURLY)) { /* * Read until we find the closing brace, * any nested braces will be handled within */ do { read_next_token = TRUE; if (isKeyword (token, KEYWORD_this)) { /* * Means we are inside a class and have found * a class, not a function */ is_class = TRUE; vStringCopy(saveScope, token->scope); addToScope (token, parent->string); /* * Ignore the remainder of the line * findCmdTerm(token); */ parseLine (token, is_class); vStringCopy(token->scope, saveScope); } else if (isKeyword (token, KEYWORD_var)) { /* * Potentially we have found an inner function. * Set something to indicate the scope */ vStringCopy(saveScope, token->scope); addToScope (token, parent->string); parseLine (token, is_class); vStringCopy(token->scope, saveScope); } else if (isKeyword (token, KEYWORD_function)) { vStringCopy(saveScope, token->scope); addToScope (token, parent->string); parseFunction (token); vStringCopy(token->scope, saveScope); } else if (isType (token, TOKEN_OPEN_CURLY)) { /* Handle nested blocks */ parseBlock (token, parent); } else { /* * It is possible for a line to have no terminator * if the following line is a closing brace. * parseLine will detect this case and indicate * whether we should read an additional token. */ read_next_token = parseLine (token, is_class); } /* * Always read a new token unless we find a statement without * a ending terminator */ if( read_next_token ) readToken(token); /* * If we find a statement without a terminator consider the * block finished, otherwise the stack will be off by one. */ } while (! isType (token, TOKEN_CLOSE_CURLY) && read_next_token ); } vStringDelete(saveScope); token->nestLevel--; return is_class; }