/* GRAMMAR OF A CONSTANT DECLARATION constant-declaration → attributes opt declaration-specifiers opt let pattern-initializer-list pattern-initializer-list → pattern-initializer | pattern-initializer,pattern-initializer-list pattern-initializer → pattern initializer opt initializer → =expression */ DeclarationPtr Parser::parseLet(const std::vector<AttributePtr>& attrs, int specifiers) { Token token; Flags flag(this, UNDER_LET); expect(Keyword::Let, token); ValueBindingsPtr ret = nodeFactory->createValueBindings(token.state); ret->setReadOnly(true); ret->setAttributes(attrs); ret->setSpecifiers(specifiers); do { PatternPtr pattern = parsePattern(); ExpressionPtr initializer = NULL; if(match(L"=")) { initializer = parseExpression(); } ValueBindingPtr constant = nodeFactory->createValueBinding(*pattern->getSourceInfo()); constant->setAttributes(attrs); constant->setSpecifiers(specifiers); constant->setName(pattern); TypedPatternPtr typedPattern = std::dynamic_pointer_cast<TypedPattern>(pattern); if(typedPattern) { //promote typed pattern constant->setDeclaredType(typedPattern->getDeclaredType()); constant->setName(typedPattern->getPattern()); } constant->setInitializer(initializer); ret->add(constant); }while(match(L",")); return ret; }
ValueBindingPtr Parser::parseVariableDeclaration() { Token token; Attributes attrs; PatternPtr key = parsePattern(); TypeNodePtr type = NULL; ExpressionPtr val = NULL; if(match(L":")) { type = parseTypeAnnotation(); } if(match(L"=")) { Flags flags(this, SUPPRESS_TRAILING_CLOSURE); val = parseExpression(); } ValueBindingPtr ret = nodeFactory->createValueBinding(*key->getSourceInfo()); if(TypedPatternPtr p = std::dynamic_pointer_cast<TypedPattern>(key)) { ret->setName(p->getPattern()); ret->setDeclaredType(p->getDeclaredType()); } else { ret->setName(key); ret->setDeclaredType(type); } ret->setTypeAttributes(attrs); ret->setInitializer(val); return ret; }