本文整理汇总了C++中ParserResult::isNonNull方法的典型用法代码示例。如果您正苦于以下问题:C++ ParserResult::isNonNull方法的具体用法?C++ ParserResult::isNonNull怎么用?C++ ParserResult::isNonNull使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ParserResult
的用法示例。
在下文中一共展示了ParserResult::isNonNull方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: makeParserResult
/// parseTypeComposition
///
/// type-composition:
/// 'protocol' '<' type-composition-list? '>'
///
/// type-composition-list:
/// type-identifier (',' type-identifier)*
///
ParserResult<ProtocolCompositionTypeRepr> Parser::parseTypeComposition() {
SourceLoc ProtocolLoc = consumeToken(tok::kw_protocol);
// Check for the starting '<'.
if (!startsWithLess(Tok)) {
diagnose(Tok, diag::expected_langle_protocol);
return nullptr;
}
SourceLoc LAngleLoc = consumeStartingLess();
// Check for empty protocol composition.
if (startsWithGreater(Tok)) {
SourceLoc RAngleLoc = consumeStartingGreater();
return makeParserResult(new (Context) ProtocolCompositionTypeRepr(
ArrayRef<IdentTypeRepr *>(),
ProtocolLoc,
SourceRange(LAngleLoc,
RAngleLoc)));
}
// Parse the type-composition-list.
ParserStatus Status;
SmallVector<IdentTypeRepr *, 4> Protocols;
do {
// Parse the type-identifier.
ParserResult<IdentTypeRepr> Protocol = parseTypeIdentifier();
Status |= Protocol;
if (Protocol.isNonNull())
Protocols.push_back(Protocol.get());
} while (consumeIf(tok::comma));
// Check for the terminating '>'.
SourceLoc EndLoc = PreviousLoc;
if (startsWithGreater(Tok)) {
EndLoc = consumeStartingGreater();
} else {
if (Status.isSuccess()) {
diagnose(Tok, diag::expected_rangle_protocol);
diagnose(LAngleLoc, diag::opening_angle);
Status.setIsParseError();
}
// Skip until we hit the '>'.
EndLoc = skipUntilGreaterInTypeList(/*protocolComposition=*/true);
}
return makeParserResult(Status, ProtocolCompositionTypeRepr::create(
Context, Protocols, ProtocolLoc, SourceRange(LAngleLoc, EndLoc)));
}
示例2: ParsingDecl
ParserResult<GenericParamList>
Parser::parseGenericParameters(SourceLoc LAngleLoc) {
// Parse the generic parameter list.
SmallVector<GenericTypeParamDecl *, 4> GenericParams;
bool Invalid = false;
do {
// Note that we're parsing a declaration.
StructureMarkerRAII ParsingDecl(*this, Tok.getLoc(),
StructureMarkerKind::Declaration);
// Parse attributes.
DeclAttributes attributes;
if (Tok.hasComment())
attributes.add(new (Context) RawDocCommentAttr(Tok.getCommentRange()));
bool foundCCTokenInAttr;
parseDeclAttributeList(attributes, foundCCTokenInAttr);
// Parse the name of the parameter.
Identifier Name;
SourceLoc NameLoc;
if (parseIdentifier(Name, NameLoc,
diag::expected_generics_parameter_name)) {
Invalid = true;
break;
}
// Parse the ':' followed by a type.
SmallVector<TypeLoc, 1> Inherited;
if (Tok.is(tok::colon)) {
(void)consumeToken();
ParserResult<TypeRepr> Ty;
if (Tok.isAny(tok::identifier, tok::code_complete, tok::kw_protocol, tok::kw_Any)) {
Ty = parseTypeIdentifierOrTypeComposition();
} else if (Tok.is(tok::kw_class)) {
diagnose(Tok, diag::unexpected_class_constraint);
diagnose(Tok, diag::suggest_anyobject, Name)
.fixItReplace(Tok.getLoc(), "AnyObject");
consumeToken();
Invalid = true;
} else {
diagnose(Tok, diag::expected_generics_type_restriction, Name);
Invalid = true;
}
if (Ty.hasCodeCompletion())
return makeParserCodeCompletionStatus();
if (Ty.isNonNull())
Inherited.push_back(Ty.get());
}
// We always create generic type parameters with a depth of zero.
// Semantic analysis fills in the depth when it processes the generic
// parameter list.
auto Param = new (Context) GenericTypeParamDecl(CurDeclContext, Name,
NameLoc, /*Depth=*/0,
GenericParams.size());
if (!Inherited.empty())
Param->setInherited(Context.AllocateCopy(Inherited));
GenericParams.push_back(Param);
// Attach attributes.
Param->getAttrs() = attributes;
// Add this parameter to the scope.
addToScope(Param);
// Parse the comma, if the list continues.
} while (consumeIf(tok::comma));
// Parse the optional where-clause.
SourceLoc WhereLoc;
SmallVector<RequirementRepr, 4> Requirements;
bool FirstTypeInComplete;
if (Tok.is(tok::kw_where) &&
parseGenericWhereClause(WhereLoc, Requirements,
FirstTypeInComplete).isError()) {
Invalid = true;
}
// Parse the closing '>'.
SourceLoc RAngleLoc;
if (!startsWithGreater(Tok)) {
if (!Invalid) {
diagnose(Tok, diag::expected_rangle_generics_param);
diagnose(LAngleLoc, diag::opening_angle);
Invalid = true;
}
// Skip until we hit the '>'.
skipUntilGreaterInTypeList();
if (startsWithGreater(Tok))
RAngleLoc = consumeStartingGreater();
else
RAngleLoc = Tok.getLoc();
} else {
RAngleLoc = consumeStartingGreater();
}
//.........这里部分代码省略.........
示例3: consumeIf
/// parseTypeSimple
/// type-simple:
/// type-identifier
/// type-tuple
/// type-composition
/// 'Any'
/// type-simple '.Type'
/// type-simple '.Protocol'
/// type-simple '?'
/// type-simple '!'
/// type-collection
ParserResult<TypeRepr> Parser::parseTypeSimple(Diag<> MessageID,
bool HandleCodeCompletion) {
ParserResult<TypeRepr> ty;
// If this is an "inout" marker for an identifier type, consume the inout.
SourceLoc InOutLoc;
consumeIf(tok::kw_inout, InOutLoc);
switch (Tok.getKind()) {
case tok::kw_Self:
ty = parseTypeIdentifier();
break;
case tok::identifier:
case tok::kw_protocol:
case tok::kw_Any:
ty = parseTypeIdentifierOrTypeComposition();
break;
case tok::l_paren:
ty = parseTypeTupleBody();
break;
case tok::code_complete:
if (!HandleCodeCompletion)
break;
if (CodeCompletion)
CodeCompletion->completeTypeSimpleBeginning();
// Eat the code completion token because we handled it.
consumeToken(tok::code_complete);
return makeParserCodeCompletionResult<TypeRepr>();
case tok::kw_super:
case tok::kw_self:
// These keywords don't start a decl or a statement, and thus should be
// safe to skip over.
diagnose(Tok, MessageID);
ty = makeParserErrorResult(new (Context) ErrorTypeRepr(Tok.getLoc()));
consumeToken();
// FIXME: we could try to continue to parse.
return ty;
case tok::l_square:
ty = parseTypeCollection();
break;
default:
checkForInputIncomplete();
diagnose(Tok, MessageID);
return nullptr;
}
// '.Type', '.Protocol', '?', and '!' still leave us with type-simple.
while (ty.isNonNull()) {
if ((Tok.is(tok::period) || Tok.is(tok::period_prefix))) {
if (peekToken().isContextualKeyword("Type")) {
consumeToken();
SourceLoc metatypeLoc = consumeToken(tok::identifier);
ty = makeParserResult(ty,
new (Context) MetatypeTypeRepr(ty.get(), metatypeLoc));
continue;
}
if (peekToken().isContextualKeyword("Protocol")) {
consumeToken();
SourceLoc protocolLoc = consumeToken(tok::identifier);
ty = makeParserResult(ty,
new (Context) ProtocolTypeRepr(ty.get(), protocolLoc));
continue;
}
}
if (!Tok.isAtStartOfLine()) {
if (isOptionalToken(Tok)) {
ty = parseTypeOptional(ty.get());
continue;
}
if (isImplicitlyUnwrappedOptionalToken(Tok)) {
ty = parseTypeImplicitlyUnwrappedOptional(ty.get());
continue;
}
}
break;
}
// If we parsed an inout modifier, prepend it.
if (InOutLoc.isValid())
ty = makeParserResult(new (Context) InOutTypeRepr(ty.get(),
InOutLoc));
return ty;
}
示例4: makeParserResult
/// parseType
/// type:
/// attribute-list type-function
/// attribute-list type-array
///
/// type-function:
/// type-simple '->' type
///
ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
bool HandleCodeCompletion) {
// Parse attributes.
TypeAttributes attrs;
parseTypeAttributeList(attrs);
// Parse Generic Parameters. Generic Parameters are visible in the function
// body.
GenericParamList *generics = nullptr;
if (isInSILMode()) {
generics = maybeParseGenericParams().getPtrOrNull();
}
ParserResult<TypeRepr> ty = parseTypeSimple(MessageID, HandleCodeCompletion);
if (ty.hasCodeCompletion())
return makeParserCodeCompletionResult<TypeRepr>();
if (ty.isNull())
return nullptr;
// Parse a throws specifier. 'throw' is probably a typo for 'throws',
// but in local contexts we could just be at the end of a statement,
// so we need to check for the arrow.
ParserPosition beforeThrowsPos;
SourceLoc throwsLoc;
bool rethrows = false;
if (Tok.isAny(tok::kw_throws, tok::kw_rethrows) ||
(Tok.is(tok::kw_throw) && peekToken().is(tok::arrow))) {
if (Tok.is(tok::kw_throw)) {
diagnose(Tok.getLoc(), diag::throw_in_function_type)
.fixItReplace(Tok.getLoc(), "throws");
}
beforeThrowsPos = getParserPosition();
rethrows = Tok.is(tok::kw_rethrows);
throwsLoc = consumeToken();
}
// Handle type-function if we have an arrow.
SourceLoc arrowLoc;
if (consumeIf(tok::arrow, arrowLoc)) {
ParserResult<TypeRepr> SecondHalf =
parseType(diag::expected_type_function_result);
if (SecondHalf.hasCodeCompletion())
return makeParserCodeCompletionResult<TypeRepr>();
if (SecondHalf.isNull())
return nullptr;
if (rethrows) {
// 'rethrows' is only allowed on function declarations for now.
diagnose(throwsLoc, diag::rethrowing_function_type);
}
auto fnTy = new (Context) FunctionTypeRepr(generics, ty.get(),
throwsLoc,
arrowLoc,
SecondHalf.get());
return makeParserResult(applyAttributeToType(fnTy, attrs));
} else if (throwsLoc.isValid()) {
// Don't consume 'throws', so we can emit a more useful diagnostic when
// parsing a function decl.
restoreParserPosition(beforeThrowsPos);
return ty;
}
// Only function types may be generic.
if (generics) {
auto brackets = generics->getSourceRange();
diagnose(brackets.Start, diag::generic_non_function);
}
// Parse legacy array types for migration.
while (ty.isNonNull() && !Tok.isAtStartOfLine()) {
if (Tok.is(tok::l_square)) {
ty = parseTypeArray(ty.get());
} else {
break;
}
}
if (ty.isNonNull() && !ty.hasCodeCompletion()) {
ty = makeParserResult(applyAttributeToType(ty.get(), attrs));
}
return ty;
}
示例5: create
GenericParamList *Parser::parseGenericParameters(SourceLoc LAngleLoc) {
// Parse the generic parameter list.
SmallVector<GenericTypeParamDecl *, 4> GenericParams;
bool Invalid = false;
do {
// Parse the name of the parameter.
Identifier Name;
SourceLoc NameLoc;
if (parseIdentifier(Name, NameLoc, diag::expected_generics_parameter_name)) {
Invalid = true;
break;
}
// Parse the ':' followed by a type.
SmallVector<TypeLoc, 1> Inherited;
if (Tok.is(tok::colon)) {
(void)consumeToken();
ParserResult<TypeRepr> Ty;
if (Tok.getKind() == tok::identifier) {
Ty = parseTypeIdentifier();
} else if (Tok.getKind() == tok::kw_protocol) {
Ty = parseTypeComposition();
} else {
diagnose(Tok, diag::expected_generics_type_restriction, Name);
Invalid = true;
}
// FIXME: code completion not handled.
if (Ty.isNonNull())
Inherited.push_back(Ty.get());
}
// We always create generic type parameters with a depth of zero.
// Semantic analysis fills in the depth when it processes the generic
// parameter list.
auto Param = new (Context) GenericTypeParamDecl(CurDeclContext, Name,
NameLoc, /*Depth=*/0,
GenericParams.size());
if (!Inherited.empty())
Param->setInherited(Context.AllocateCopy(Inherited));
GenericParams.push_back(Param);
// Add this parameter to the scope.
addToScope(Param);
// Parse the comma, if the list continues.
} while (consumeIf(tok::comma));
// Parse the optional where-clause.
SourceLoc WhereLoc;
SmallVector<RequirementRepr, 4> Requirements;
if (Tok.is(tok::kw_where) &&
parseGenericWhereClause(WhereLoc, Requirements)) {
Invalid = true;
}
// Parse the closing '>'.
SourceLoc RAngleLoc;
if (!startsWithGreater(Tok)) {
if (!Invalid) {
diagnose(Tok, diag::expected_rangle_generics_param);
diagnose(LAngleLoc, diag::opening_angle);
Invalid = true;
}
// Skip until we hit the '>'.
skipUntilGreaterInTypeList();
if (startsWithGreater(Tok))
RAngleLoc = consumeStartingGreater();
else
RAngleLoc = Tok.getLoc();
} else {
RAngleLoc = consumeStartingGreater();
}
if (GenericParams.empty())
return nullptr;
return GenericParamList::create(Context, LAngleLoc, GenericParams,
WhereLoc, Requirements, RAngleLoc);
}
示例6: parseDefaultArgument
static ParserStatus parseDefaultArgument(Parser &P,
Parser::DefaultArgumentInfo *defaultArgs,
unsigned argIndex,
ExprHandle *&init,
Parser::ParameterContextKind paramContext) {
SourceLoc equalLoc = P.consumeToken(tok::equal);
// Enter a fresh default-argument context with a meaningless parent.
// We'll change the parent to the function later after we've created
// that declaration.
auto initDC =
P.Context.createDefaultArgumentContext(P.CurDeclContext, argIndex);
Parser::ParseFunctionBody initScope(P, initDC);
ParserResult<Expr> initR = P.parseExpr(diag::expected_init_value);
// Give back the default-argument context if we didn't need it.
if (!initScope.hasClosures()) {
P.Context.destroyDefaultArgumentContext(initDC);
// Otherwise, record it if we're supposed to accept default
// arguments here.
} else if (defaultArgs) {
defaultArgs->ParsedContexts.push_back(initDC);
}
Diag<> diagID = { DiagID() };
switch (paramContext) {
case Parser::ParameterContextKind::Function:
case Parser::ParameterContextKind::Operator:
case Parser::ParameterContextKind::Initializer:
break;
case Parser::ParameterContextKind::Closure:
diagID = diag::no_default_arg_closure;
break;
case Parser::ParameterContextKind::Subscript:
diagID = diag::no_default_arg_subscript;
break;
case Parser::ParameterContextKind::Curried:
diagID = diag::no_default_arg_curried;
break;
}
assert(((diagID.ID != DiagID()) == !defaultArgs ||
// Sometimes curried method parameter lists get default arg info.
// Remove this when they go away.
paramContext == Parser::ParameterContextKind::Curried) &&
"Default arguments specified for an unexpected parameter list kind");
if (diagID.ID != DiagID()) {
auto inFlight = P.diagnose(equalLoc, diagID);
if (initR.isNonNull())
inFlight.fixItRemove(SourceRange(equalLoc, initR.get()->getEndLoc()));
return ParserStatus();
}
defaultArgs->HasDefaultArgument = true;
if (initR.hasCodeCompletion())
return makeParserCodeCompletionStatus();
if (initR.isNull())
return makeParserError();
init = ExprHandle::get(P.Context, initR.get());
return ParserStatus();
}
示例7: GPSContext
ParserStatus
Parser::parseGenericParametersBeforeWhere(SourceLoc LAngleLoc,
SmallVectorImpl<GenericTypeParamDecl *> &GenericParams) {
ParserStatus Result;
SyntaxParsingContext GPSContext(SyntaxContext, SyntaxKind::GenericParameterList);
bool HasNextParam;
do {
SyntaxParsingContext GParamContext(SyntaxContext, SyntaxKind::GenericParameter);
// Note that we're parsing a declaration.
StructureMarkerRAII ParsingDecl(*this, Tok.getLoc(),
StructureMarkerKind::Declaration);
if (ParsingDecl.isFailed()) {
return makeParserError();
}
// Parse attributes.
DeclAttributes attributes;
if (Tok.hasComment())
attributes.add(new (Context) RawDocCommentAttr(Tok.getCommentRange()));
bool foundCCTokenInAttr;
parseDeclAttributeList(attributes, foundCCTokenInAttr);
// Parse the name of the parameter.
Identifier Name;
SourceLoc NameLoc;
if (parseIdentifier(Name, NameLoc,
diag::expected_generics_parameter_name)) {
Result.setIsParseError();
break;
}
// Parse the ':' followed by a type.
SmallVector<TypeLoc, 1> Inherited;
if (Tok.is(tok::colon)) {
(void)consumeToken();
ParserResult<TypeRepr> Ty;
if (Tok.isAny(tok::identifier, tok::code_complete, tok::kw_protocol,
tok::kw_Any)) {
Ty = parseType();
} else if (Tok.is(tok::kw_class)) {
diagnose(Tok, diag::unexpected_class_constraint);
diagnose(Tok, diag::suggest_anyobject)
.fixItReplace(Tok.getLoc(), "AnyObject");
consumeToken();
Result.setIsParseError();
} else {
diagnose(Tok, diag::expected_generics_type_restriction, Name);
Result.setIsParseError();
}
if (Ty.hasCodeCompletion())
return makeParserCodeCompletionStatus();
if (Ty.isNonNull())
Inherited.push_back(Ty.get());
}
// We always create generic type parameters with an invalid depth.
// Semantic analysis fills in the depth when it processes the generic
// parameter list.
auto Param = new (Context) GenericTypeParamDecl(CurDeclContext, Name, NameLoc,
GenericTypeParamDecl::InvalidDepth,
GenericParams.size());
if (!Inherited.empty())
Param->setInherited(Context.AllocateCopy(Inherited));
GenericParams.push_back(Param);
// Attach attributes.
Param->getAttrs() = attributes;
// Add this parameter to the scope.
addToScope(Param);
// Parse the comma, if the list continues.
HasNextParam = consumeIf(tok::comma);
} while (HasNextParam);
return Result;
}