static bool nextToken(Token &x) { x = Token(); const char *p = save(); if (space(x)) goto success; restore(p); if (docStartLine(x)) goto success; restore(p); if (docStartBlock(x)) goto success; restore(p); if (lineComment(x)) goto success; restore(p); if (blockComment(x)) goto success; restore(p); if (staticIndex(x)) goto success; restore(p); if (opIdentifier(x)) goto success; restore(p); if (symbol(x)) goto success; restore(p); if (op(x)) goto success; restore(p); if (llvmToken(x)) goto success; restore(p); if (keywordIdentifier(x)) goto success; restore(p); if (charToken(x)) goto success; restore(p); if (stringToken(x)) goto success; restore(p); if (floatToken(x)) goto success; restore(p); if (intToken(x)) goto success; if (p != end) { pushLocation(locationFor(p)); error("invalid token"); } return false; success : assert(x.tokenKind != T_NONE); if (!x.location.ok()) x.location = locationFor(p); return true; }
void QMLHighlighter::highlightBlock(const QString &text) { QTextCharFormat keywordFormat; keywordFormat.setForeground(QColor("#d7ffaf")); // Identifier QTextCharFormat typeFormat; typeFormat.setForeground(QColor("#afffff")); // Type QTextCharFormat commentFormat; commentFormat.setForeground(QColor("#8a8a8a")); // Comment QTextCharFormat numericConstantFormat; numericConstantFormat.setForeground(QColor("#ffffd7")); // Constant QTextCharFormat stringConstantFormat; stringConstantFormat.setForeground(QColor("#ffffd7")); QRegExp type("\\b[A-Z][A-Za-z]+\\b"); QRegExp numericConstant("[0-9]+\\.?[0-9]*"); QRegExp stringConstant("['\"].*['\"]");//Not multiline strings, but they're rare QRegExp lineComment("//[^\n]*"); QRegExp startComment("/\\*"); QRegExp endComment("\\*/"); applyBasicHighlight(text, type, typeFormat); applyBasicHighlight(text, numericConstant, numericConstantFormat); applyBasicHighlight(text, stringConstant, stringConstantFormat); applyBasicHighlight(text, lineComment, commentFormat); setCurrentBlockState(0); int startIndex = 0; if (previousBlockState() != 1) startIndex = text.indexOf(startComment); while (startIndex >= 0) { int endIndex = text.indexOf(endComment, startIndex); int commentLength; if (endIndex == -1) { setCurrentBlockState(1); commentLength = text.length() - startIndex; } else { commentLength = endIndex - startIndex + endComment.matchedLength(); } setFormat(startIndex, commentLength, commentFormat); startIndex = text.indexOf(startComment, startIndex + commentLength); } }
NitLexer::Token NitLexer::lex() { int tk; while (_ch != CHAR_EOS) { whitespace(); switch (_ch) { case CHAR_EOS: return token(TK_EOS); case '/': next(); switch (_ch) { case '*': next(); blockComment(); continue; case '/': lineComment(); continue; case '=': next(); return token(TK_DIVEQ); default: return token('/'); } break; case '=': next(); if (_ch == '>') { next(); return token(TK_LAMBDA); } if (_ch == '=') { next(); return token(TK_EQ); } return token('='); case '<': next(); if (_ch == '<') { next(); return token(TK_SHIFTL); } if (_ch != '=') return token('<'); next(); if (_ch == '>') { next(); return token(TK_THREEWAYSCMP); } return token(TK_LE); case '>': next(); if (_ch == '=') { next(); return token(TK_GE); } if (_ch != '>') return token('>'); next(); if (_ch == '>') { next(); return token(TK_USHIFTR); } return token(TK_SHIFTR); case '!': next(); if (_ch == '=') { next(); return token(TK_NE); } return token('!'); case '@': next(); if (_ch != '"' && _ch != '\'') return token('@'); if ((tk = readString(_ch, '@')) != -1) return token(tk); return error("error parsing verbatim string"); case '"': case '\'': if (readString(_ch) != -1) return token(TK_STRING_VALUE); return error("error parsing the string"); case '{': case '}': case '(': case ')': case '[': case ']': case ';': case ',': case '?': case '^': case '~': case '$': return token(next()); case '.': next(); if (_ch != '.') return token('.'); next(); if (_ch != '.') return error("invalid token '..'"); next(); return token(TK_VARPARAMS); case '&': next(); if (_ch == '&') { next(); return token(TK_AND); } return token('&'); case '|': next(); if (_ch == '|') { next(); return token(TK_OR); } return token('|'); case ':': next(); if (_ch == '=') { next(); return token(TK_NEWSLOT); } if (_ch == '>') { next(); return token(TK_WITHREF); } if (_ch == ':') { next(); return token(TK_DOUBLE_COLON); } return token(':'); case '*': next(); if (_ch == '=') { next(); return token(TK_MULEQ); } return token('*'); case '%': next(); if (_ch == '=') { next(); return token(TK_MODEQ); } return token('%'); case '-': next(); if (_ch == '=') { next(); return token(TK_MINUSEQ); } if (_ch == '-') { next(); return token(TK_MINUSMINUS); } return token('-'); case '+': next(); if (_ch == '=') { next(); return token(TK_PLUSEQ); } if (_ch == '+') { next(); return token(TK_PLUSPLUS); } return token('+'); default: if (isdigit(_ch)) return token(readNumber()); else if (isId(_ch)) return token(readId()); else return error("unexpected character '%c'", _ch); } } return token(TK_EOS); }