本文整理汇总了C++中TokenStream::getToken方法的典型用法代码示例。如果您正苦于以下问题:C++ TokenStream::getToken方法的具体用法?C++ TokenStream::getToken怎么用?C++ TokenStream::getToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TokenStream
的用法示例。
在下文中一共展示了TokenStream::getToken方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: expression
double expression(TokenStream& tokenStream)
{
double result = term(tokenStream); // set result to value of term
Token token = tokenStream.getToken(); // get next token from tokenStream
while (true)
{
if (token.kind == '+')
{
result += term(tokenStream); // evaluate term and add to result
token = tokenStream.getToken(); // get next token from tokenStream
}
if (token.kind == '-')
{
result -= term(tokenStream); // evaluate term and subtract from result
token = tokenStream.getToken(); // get next token from tokenStream
}
if (token.kind != '+' && token.kind != '-')
{
tokenStream.setToken(token); // reset token into token stream
return result; // return result
}
}
}
示例2: calculate
void calculate()
{
const char quit = 'q'; // t.kind==quit means that t is a quit Token
const char print = ';'; // t.kind==print means that t is a print Token
const std::string prompt = "> ";
const std::string equals = "= "; // used to indicate that what follows is an evaluation
TokenStream tokenStream;
std::string enterToClose = "~~";
while (std::cin)
try
{
std::cout << prompt;
Token token = tokenStream.getToken();
if (token.kind == print)
token = tokenStream.getToken(); // eat ';'
if (token.kind == quit)
break;
tokenStream.setToken(token); // reset token into tokenStream
std::cout << equals << expression(tokenStream) << '\n';
}
catch (std::exception& e)
{
std::cerr << e.what() << '\n'; // write error message
tokenStream.ignore(print);
}
}
示例3: primary
double primary(TokenStream& tokenStream)
{
const char number = '8';
Token token = tokenStream.getToken();
if (token.kind == '-') return -primary(tokenStream);
else if (token.kind == '+') return primary(tokenStream);
else if (token.kind == number) return token.value; // return the number’s value
else if (token.kind == '(') // handle ‘(‘ expression ‘)’
{
double result = expression(tokenStream); // set result to value of expression
token = tokenStream.getToken(); // get next token from tokenStream
if (token.kind != ')') error("')' expected"); // check for error
return result; // return divisor
}
else // check for error
error("primary expected");
}
示例4: term
double term(TokenStream& tokenStream)
{
double result = primary(tokenStream); // set result to value of primary
Token token = tokenStream.getToken(); // get next token from tokenStream
while (true)
{
if (token.kind == '*')
{
result *= primary(tokenStream); // evaluate primary and multiply with result
token = tokenStream.getToken(); // get next token from tokenStream
}
else if (token.kind == '/')
{
double divisor = primary(tokenStream); // set divisor to value of primary
if (divisor == 0) error("divide by zero"); // check for error
result /= divisor; // divide result by divisor
token = tokenStream.getToken(); // get next token from tokenStream
}
else if (token.kind == '%') // modulo will only work on ints for this implementation
{
int numer = result; // cast result to int
int denom = primary(tokenStream); // cast denom to int
if (denom == 0) error("%: divide by zero"); // error check
result = numer % denom; // get result
token = tokenStream.getToken(); // get next token from tokenStream
}
else
{
tokenStream.setToken(token); // put token back into the Token stream
return result; // return result
}
}
}
示例5: Parse_Type_Int
TypeRef Parse_Type_Int(TokenStream& lex, bool allow_trait_list)
{
//TRACE_FUNCTION;
auto ps = lex.start_span();
Token tok;
switch( GET_TOK(tok, lex) )
{
case TOK_INTERPOLATED_TYPE:
return mv$(tok.frag_type());
// '!' - Only ever used as part of function prototypes, but is kinda a type... not allowed here though
case TOK_EXCLAM:
return TypeRef( Span(tok.get_pos()), TypeData::make_Bang({}) );
// '_' = Wildcard (type inferrence variable)
case TOK_UNDERSCORE:
return TypeRef(Span(tok.get_pos()));
// 'unsafe' - An unsafe function type
case TOK_RWORD_UNSAFE:
// 'extern' - A function type with an ABI
case TOK_RWORD_EXTERN:
// 'fn' - Rust function
case TOK_RWORD_FN:
PUTBACK(tok, lex);
return Parse_Type_Fn(lex);
case TOK_RWORD_IMPL:
return Parse_Type_ErasedType(lex, allow_trait_list);
// '<' - An associated type cast
case TOK_LT:
case TOK_DOUBLE_LT: {
PUTBACK(tok, lex);
auto path = Parse_Path(lex, PATH_GENERIC_TYPE);
return TypeRef(TypeRef::TagPath(), lex.end_span(mv$(ps)), mv$(path));
}
//
case TOK_RWORD_FOR: {
auto hrls = Parse_HRB(lex);
switch(LOOK_AHEAD(lex))
{
case TOK_RWORD_UNSAFE:
case TOK_RWORD_EXTERN:
case TOK_RWORD_FN:
return Parse_Type_Fn(lex, hrls);
default:
return Parse_Type_Path(lex, hrls, true);
}
}
// <ident> - Either a primitive, or a path
case TOK_IDENT:
if( lex.lookahead(0) == TOK_EXCLAM )
{
lex.getToken();
// TODO: path macros
return TypeRef(TypeRef::TagMacro(), Parse_MacroInvocation(ps, mv$(tok.str()), lex));
}
// or a primitive
//if( auto ct = coretype_fromstring(tok.str()) )
//{
// return TypeRef(TypeRef::TagPrimitive(), Span(tok.get_pos()), ct);
//}
PUTBACK(tok, lex);
return Parse_Type_Path(lex, {}, allow_trait_list);
// - Fall through to path handling
// '::' - Absolute path
case TOK_DOUBLE_COLON:
// 'self' - This relative path
case TOK_RWORD_SELF:
// 'super' - Parent relative path
case TOK_RWORD_SUPER:
// ':path' fragment
case TOK_INTERPOLATED_PATH:
PUTBACK(tok, lex);
return Parse_Type_Path(lex, {}, allow_trait_list);
// HACK! Convert && into & &
case TOK_DOUBLE_AMP:
lex.putback(Token(TOK_AMP));
// '&' - Reference type
case TOK_AMP: {
AST::LifetimeRef lifetime;
// Reference
tok = lex.getToken();
if( tok.type() == TOK_LIFETIME ) {
lifetime = AST::LifetimeRef(/*lex.point_span(), */lex.get_ident(::std::move(tok)));
tok = lex.getToken();
}
bool is_mut = false;
if( tok.type() == TOK_RWORD_MUT ) {
is_mut = true;
}
else {
PUTBACK(tok, lex);
}
return TypeRef(TypeRef::TagReference(), lex.end_span(mv$(ps)), ::std::move(lifetime), is_mut, Parse_Type(lex, false));
}
// '*' - Raw pointer
case TOK_STAR:
//.........这里部分代码省略.........