本文整理汇总了C++中Lex::TokStr方法的典型用法代码示例。如果您正苦于以下问题:C++ Lex::TokStr方法的具体用法?C++ Lex::TokStr怎么用?C++ Lex::TokStr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lex
的用法示例。
在下文中一共展示了Lex::TokStr方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Expect
void Expect(int t)
{
if (lex.token != t)
lex.Error(lex.TokStr(t) + " expected, found: " + lex.TokStr());
lex.Next();
}
示例2: ParseFactor
Value ParseFactor()
{
switch (lex.token)
{
case 'INT': { int i = atoi(lex.sattr.c_str()); lex.Next(); return Value(i); }
case 'FLT': { double f = atof(lex.sattr.c_str()); lex.Next(); return Value((float)f); }
case 'STR': { string s = lex.sattr; lex.Next(); auto str = g_vm->NewString(s); allocated.push_back(str); return Value(str); }
case 'NIL': { lex.Next(); return Value(0, V_NIL); }
case '-':
{
lex.Next();
Value v = ParseFactor();
switch (v.type)
{
case V_INT: v.ival *= -1; break;
case V_FLOAT: v.fval *= -1; break;
default: lex.Error("numeric value expected");
}
return v;
}
case '[':
{
lex.Next();
Gobble('LF');
vector<Value> elems;
if (lex.token == ']') lex.Next();
else
{
for (;;)
{
elems.push_back(ParseFactor());
bool haslf = lex.token == 'LF';
if (haslf) lex.Next();
if (lex.token == ']') break;
if (!haslf) Expect(',');
}
lex.Next();
}
int type = -1;
if (lex.token == ':')
{
lex.Next();
string sname = lex.sattr;
Expect('ID');
size_t reqargs = 0;
int idx = g_vm->StructIdx(sname, reqargs);
if (idx >= 0) // if unknown type, becomes regular vector
{
while (elems.size() < reqargs) { elems.push_back(Value(0, V_NIL)); } // pad with NIL if current type has more fields
while (elems.size() > reqargs) { elems.back().DEC(); elems.pop_back(); } // drop elements if current type has less fields
type = idx;
}
}
auto vec = g_vm->NewVector(elems.size(), type);
allocated.push_back(vec);
for (auto &e : elems) vec->push(e.INC());
return Value(vec);
}
default:
lex.Error("illegal start of expression: " + lex.TokStr());
return Value();
}
}
示例3: ParseFactor
Value ParseFactor(type_elem_t typeoff)
{
auto &ti = g_vm->GetTypeInfo(typeoff);
auto vt = ti.t;
// TODO: also support boxed parsing as V_ANY.
// means boxing int/float, deducing runtime type for V_VECTOR, and finding the existing struct.
switch (lex.token)
{
case T_INT:
{
ExpectType(V_INT, vt);
int i = atoi(lex.sattr.c_str());
lex.Next();
return Value(i);
}
case T_FLOAT:
{
ExpectType(V_FLOAT, vt);
double f = atof(lex.sattr.c_str());
lex.Next();
return Value((float)f);
}
case T_STR:
{
ExpectType(V_STRING, vt);
string s = lex.sattr;
lex.Next();
auto str = g_vm->NewString(s);
str->Inc();
allocated.push_back(str);
return Value(str);
}
case T_NIL:
{
ExpectType(V_NIL, vt);
lex.Next();
return Value();
}
case T_MINUS:
{
lex.Next();
Value v = ParseFactor(typeoff);
switch (typeoff)
{
case TYPE_ELEM_INT:
v.setival(v.ival() * -1);
break;
case TYPE_ELEM_FLOAT:
v.setfval(v.fval() * -1);
break;
default:
lex.Error("unary minus: numeric value expected");
}
return v;
}
case T_LEFTBRACKET:
{
ExpectType(V_VECTOR, vt);
lex.Next();
return ParseElems(T_RIGHTBRACKET, typeoff);
}
case T_IDENT:
{
ExpectType(V_STRUCT, vt);
string sname = lex.sattr;
lex.Next();
Expect(T_LEFTCURLY);
auto name = g_vm->StructName(ti);
if (name != sname) lex.Error("struct type " + name + " required, " + sname + " given");
return ParseElems(T_RIGHTCURLY, typeoff, ti.len);
}
default:
lex.Error("illegal start of expression: " + lex.TokStr());
return Value();
}
}