Пример #1
0
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();
}
Пример #2
0
//	Declarations -> Decl | Decls
Declarations *parseDeclarations( FILE *source )
{
    Token token = scanner(source);
	int i;
    Declaration decl;
    Declarations *decls;
    switch(token.type){
        case FloatDeclaration:
        case IntegerDeclaration:
            decl = parseDeclaration(source, token);
            decls = parseDeclarations(source);
            return makeDeclarationTree( decl, decls );
        case PrintOp:
        case Alphabet:
			for (i = strlen(token.tok) - 1; i >= 0; i--) {
                ungetc(token.tok[i], source);
            }
            return NULL;
        case EOFsymbol:
            return NULL;
        default:
            printf("Syntax Error: Expect declarations %s\n", token.tok);
            exit(1);
    }
}
Пример #3
0
	/***************************************************************
	* Function: Parser::parseString()
	* Purpose : Parse an XML input string
	* Initial : Maxime Chevalier-Boisvert on October 20, 2008
	****************************************************************
	Revisions and bug fixes:
	*/
	Document Parser::parseString(const std::string& xmlString)
	{
		// Declare a variable for the current character index
		size_t charIndex = 0;

		// Declare a vector for the text positions in the streamlined input
		PosVector positions;

		// streamline the input raw xml string
		std::string input = streamline(xmlString, positions);

		// If the very verbose output flag is set
		if (ConfigManager::s_veryVerboseVar) {
			// Output the raw and streamlined input
			std::cout << "Raw input: " << std::endl << xmlString;
			std::cout << "Streamlined input: \"" << input << "\"" << std::endl;
		}

		// Parse the XML declaration, if present
		Declaration* pDecl = parseDeclaration(input, charIndex, positions);

		// Parse the XML element tree
		Element* pTree = parseElement(input, charIndex, positions);


		// Return the parsed XML document
		return Document(pDecl, pTree);
	}
Пример #4
0
	void XMLDecoder::parse( XMLDocument* doc, const void* data, size_t len )
	{
		setData( data, len );
		XMLNode* node = parseDeclaration();
		doc->addNode( node );
		while( ( node = parseNode() ) != NULL )
			doc->addNode( node );
	}
Пример #5
0
Ruleset* CssParser::parseRuleset () {
  Ruleset* ruleset = NULL;
  Declaration* declaration = NULL;
  Selector* selector = new Selector();

  if (!parseSelector(selector)) {
    delete selector;
    selector = NULL;
    
    if (tokenizer->getTokenType() != Token::BRACKET_OPEN) {
      return NULL;
    } 
  } else if (tokenizer->getTokenType() != Token::BRACKET_OPEN) {
    delete selector;
    throw new ParseException(tokenizer->getToken()->str,
                             "a declaration block ('{...}')");
  }
  tokenizer->readNextToken();

  ruleset = new Ruleset(selector);
  
  skipWhitespace();
  declaration = parseDeclaration();
  if (declaration != NULL)
    ruleset->addDeclaration(declaration);
  
  while (tokenizer->getTokenType() == Token::DELIMITER) {
    tokenizer->readNextToken();
    skipWhitespace();
    declaration = parseDeclaration();
    if (declaration != NULL)
      ruleset->addDeclaration(declaration);
  }
  
  if (tokenizer->getTokenType() != Token::BRACKET_CLOSED) {
    throw new ParseException(tokenizer->getToken()->str,
                             "end of declaration block ('}')");
  } 
  tokenizer->readNextToken();
  skipWhitespace();
  
  return ruleset;
}
Пример #6
0
// CONSTRUCTOR
ParsedDeclaration::ParsedDeclaration( std::string s ) {
    typeSpec.clear();
    varName.clear();
    ndims = 0;
    for (int ii = 0; ii < MAX_DIMS; ii++){
        dims[ii] = 0;
    }
    lexer.load(s);
    parseDeclaration();
}
Пример #7
0
Ptr<AST::Statement> Parser::parseStatement ()
{
	AST::Statement *statement = nullptr;

	statement = parseDeclaration();
	if (statement == nullptr) {
		statement = parseExpression();
	}

	return statement;
}
Пример #8
0
CqPrimvarToken::CqPrimvarToken(const char* token)
	: m_class(class_invalid),
	m_type(type_invalid),
	m_count(-1),
	m_name()
{
	assert(token != 0);
	const char* beginName = 0;
	const char* endName = 0;
	Ri::TypeSpec spec = parseDeclaration(token, &beginName, &endName);
	m_name.assign(beginName, endName);
	typeSpecToEqTypes(&m_class, &m_type, spec);
	m_count = spec.arraySize;
}
Пример #9
0
//{ margin: auto; color: #cc0000; }
QVector<Declaration> CSSParser::parseDeclarations(){
    consumeChar('{');
    QVector<Declaration> declarations;
    //    while(peekChar() != '}'){
    //        declarations.append(parseDeclaration());
    //    }

    while (true) {
        consumeWhitespaceOrNewline();
        if (peekChar() == '}') {
            consumeChar();
            break;
        }
        declarations.append(parseDeclaration());
    }
    return declarations;
}
Пример #10
0
CqPrimvarToken::CqPrimvarToken(const char* typeToken, const char* name)
	: m_class(class_invalid),
	m_type(type_invalid),
	m_count(1),
	m_name()
{
	assert(typeToken != 0);
	const char* beginName = 0;
	Ri::TypeSpec spec = parseDeclaration(typeToken, &beginName);
	if(beginName)
		AQSIS_THROW_XQERROR(XqParseError, EqE_BadToken,
			"invalid token: unexpected name \"" << beginName << "\" in type string \""
			<< typeToken << "\"");
	m_name = name;
	typeSpecToEqTypes(&m_class, &m_type, spec);
	m_count = spec.arraySize;
}
Пример #11
0
Declarations *parseDeclarations( FILE *source )//changed
{
    Token token = scanner(source);
    Declaration decl;
    Declarations *decls;
    switch(token.type){
        case FloatDeclaration:
        case IntegerDeclaration:
            decl = parseDeclaration(source, token);
            decls = parseDeclarations(source);
            return makeDeclarationTree( decl, decls );
        case PrintOp:
        case Alphabet:
            //ungetc(token.tok[0], source);
			ungetstring(token.tok,source);
            return NULL;
        case EOFsymbol:
            return NULL;
        default:
            printf("Syntax Error: Expect declarations %s\n", token.tok);
            exit(1);
    }
}
Пример #12
0
static PSmmAstNode parseExpressionStmt(PSmmParser parser) {
	PSmmAstNode lval;

	struct SmmFilePos fpos = parser->curToken->filePos;
	parser->curToken->canBeNewSymbol = true;
	lval = parseExpression(parser);

	if (lval == &errorNode) {
		if (findToken(parser, ';')) getNextToken(parser);
		return &errorNode;
	}

	if (!lval->isIdent && (parser->curToken->kind == ':' || parser->curToken->kind == '=')) {
		smmPostMessage(parser->msgs, errSmmOperandMustBeLVal, fpos);
		if (findToken(parser, ';')) getNextToken(parser);
		return &errorNode;
	}

	if (parser->curToken->kind == ':') {
		lval = parseDeclaration(parser, lval);
	} else if (parser->curToken->kind == '=') {
		lval = parseAssignment(parser, lval);
	}
	if (lval) {
		bool isJustIdent = lval->isIdent && (lval->kind != nkSmmCall) && (lval->kind != nkSmmError);
		bool isAnyBinOpExceptLogical = lval->isBinOp && lval->kind != nkSmmAndOp && lval->kind != nkSmmOrOp;
		if (isJustIdent || isAnyBinOpExceptLogical) {
			smmPostMessage(parser->msgs, wrnSmmNoEffectStmt, lval->token->filePos);
			if (isJustIdent) lval = NULL;
		}
	}
	if (parser->prevToken->kind != '}' && (lval != &errorNode || parser->curToken->kind == ';')) {
		expect(parser, ';');
	}
	return lval;
}
Пример #13
0
static int parse(ej_t *ep, int state, int flags)
{
	a_assert(ep);

	switch (state) {
/*
 *	Any statement, function arguments or conditional expressions
 */
	case STATE_STMT:
		if ((state = parseStmt(ep, state, flags)) != STATE_STMT_DONE &&
			state != STATE_EOF && state != STATE_STMT_BLOCK_DONE &&
			state != STATE_RET) {
			state = STATE_ERR;
		}
		break;

	case STATE_DEC:
		if ((state = parseStmt(ep, state, flags)) != STATE_DEC_DONE &&
			state != STATE_EOF) {
			state = STATE_ERR;
		}
		break;

	case STATE_EXPR:
		if ((state = parseStmt(ep, state, flags)) != STATE_EXPR_DONE &&
			state != STATE_EOF) {
			state = STATE_ERR;
		}
		break;

/*
 *	Variable declaration list
 */
	case STATE_DEC_LIST:
		state = parseDeclaration(ep, state, flags);
		break;

/*
 *	Function argument string
 */
	case STATE_ARG_LIST:
		state = parseArgs(ep, state, flags);
		break;

/*
 *	Logical condition list (relational operations separated by &&, ||)
 */
	case STATE_COND:
		state = parseCond(ep, state, flags);
		break;

/*
 *	Expression list
 */
	case STATE_RELEXP:
		state = parseExpr(ep, state, flags);
		break;
	}

	if (state == STATE_ERR && ep->error == NULL) {
		ejError(ep, T("Syntax error"));
	}
	return state;
}