Highlight reserved GLSL keywords

This commit is contained in:
Rhys Weatherley
2010-11-23 17:05:55 +10:00
parent e151381a8a
commit 7ccc6fc7f7
9 changed files with 1093 additions and 1072 deletions

View File

@@ -207,6 +207,7 @@
%token PREPROC "preprocessor directive" %token PREPROC "preprocessor directive"
%token COMMENT "comment" %token COMMENT "comment"
%token ERROR "error" %token ERROR "error"
%token RESERVED "reserved word"
%start translation_unit %start translation_unit

View File

@@ -393,10 +393,11 @@ int Lexer::findKeyword(const char *word, int length) const
if (!(t & Variant_Mask)) if (!(t & Variant_Mask))
return t; return t;
if ((_variant & t & Variant_Mask) == 0) { if ((_variant & t & Variant_Mask) == 0) {
// TODO: issue a proper error for the unsupported keyword // Return a "reserved word" token if this keyword is not permitted
QByteArray keyword(word, length); // in the current language variant so that the syntax highlighter
fprintf(stderr, "unsupported keyword `%s' at line %d\n", // can warn the user about the word.
keyword.constData(), _lineno); if (!_scanKeywords)
return Parser::T_RESERVED;
} }
return t & ~Variant_Mask; return t & ~Variant_Mask;
} }

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
#line 213 "./glsl.g" #line 214 "./glsl.g"
/************************************************************************** /**************************************************************************
** **

File diff suppressed because it is too large Load Diff

View File

@@ -172,6 +172,7 @@ public:
T_PRECISION = 104, T_PRECISION = 104,
T_PREPROC = 169, T_PREPROC = 169,
T_QUESTION = 105, T_QUESTION = 105,
T_RESERVED = 172,
T_RETURN = 106, T_RETURN = 106,
T_RIGHT_ANGLE = 107, T_RIGHT_ANGLE = 107,
T_RIGHT_ASSIGN = 108, T_RIGHT_ASSIGN = 108,
@@ -238,12 +239,12 @@ public:
ACCEPT_STATE = 440, ACCEPT_STATE = 440,
RULE_COUNT = 314, RULE_COUNT = 314,
STATE_COUNT = 459, STATE_COUNT = 459,
TERMINAL_COUNT = 172, TERMINAL_COUNT = 173,
NON_TERMINAL_COUNT = 84, NON_TERMINAL_COUNT = 84,
GOTO_INDEX_OFFSET = 459, GOTO_INDEX_OFFSET = 459,
GOTO_INFO_OFFSET = 4728, GOTO_INFO_OFFSET = 4757,
GOTO_CHECK_OFFSET = 4728 GOTO_CHECK_OFFSET = 4757
}; };
static const char *const spell []; static const char *const spell [];

View File

@@ -176,9 +176,14 @@ void GLSLTextEditor::setFontSettings(const TextEditor::FontSettings &fs)
<< QLatin1String(TextEditor::Constants::C_STRING) << QLatin1String(TextEditor::Constants::C_STRING)
<< QLatin1String(TextEditor::Constants::C_TYPE) << QLatin1String(TextEditor::Constants::C_TYPE)
<< QLatin1String(TextEditor::Constants::C_KEYWORD) << QLatin1String(TextEditor::Constants::C_KEYWORD)
<< QLatin1String(TextEditor::Constants::C_FIELD) << QLatin1String(TextEditor::Constants::C_OPERATOR)
<< QLatin1String(TextEditor::Constants::C_PREPROCESSOR)
<< QLatin1String(TextEditor::Constants::C_LABEL)
<< QLatin1String(TextEditor::Constants::C_COMMENT) << QLatin1String(TextEditor::Constants::C_COMMENT)
<< QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE); << QLatin1String(TextEditor::Constants::C_DOXYGEN_COMMENT)
<< QLatin1String(TextEditor::Constants::C_DOXYGEN_TAG)
<< QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE)
<< QLatin1String(TextEditor::Constants::C_REMOVED_LINE);
} }
highlighter->setFormats(fs.toTextCharFormats(categories)); highlighter->setFormats(fs.toTextCharFormats(categories));
@@ -252,7 +257,6 @@ void GLSLTextEditor::updateDocumentNow()
m_updateDocumentTimer->stop(); m_updateDocumentTimer->stop();
const int variant = Lexer::Variant_GLSL_Qt | // ### hardcoded const int variant = Lexer::Variant_GLSL_Qt | // ### hardcoded
Lexer::Variant_GLSL_ES_100 |
Lexer::Variant_VertexShader | Lexer::Variant_VertexShader |
Lexer::Variant_FragmentShader; Lexer::Variant_FragmentShader;
const QString contents = toPlainText(); // get the code from the editor const QString contents = toPlainText(); // get the code from the editor

View File

@@ -57,6 +57,10 @@ void Highlighter::highlightBlock(const QString &text)
lex.setState(qMax(0, previousBlockState())); lex.setState(qMax(0, previousBlockState()));
lex.setScanKeywords(false); lex.setScanKeywords(false);
lex.setScanComments(true); lex.setScanComments(true);
const int variant = GLSL::Lexer::Variant_GLSL_Qt | // ### FIXME: hardcoded
GLSL::Lexer::Variant_VertexShader |
GLSL::Lexer::Variant_FragmentShader;
lex.setVariant(variant);
GLSL::Token tk; GLSL::Token tk;
do { do {
lex.yylex(&tk); lex.yylex(&tk);
@@ -66,7 +70,10 @@ void Highlighter::highlightBlock(const QString &text)
else if (tk.is(GLSL::Parser::T_COMMENT)) else if (tk.is(GLSL::Parser::T_COMMENT))
setFormat(tk.position, tk.length, Qt::darkGreen); // ### FIXME: m_formats[GLSLCommentFormat]); setFormat(tk.position, tk.length, Qt::darkGreen); // ### FIXME: m_formats[GLSLCommentFormat]);
else if (tk.is(GLSL::Parser::T_IDENTIFIER)) { else if (tk.is(GLSL::Parser::T_IDENTIFIER)) {
if (lex.findKeyword(data.constData() + tk.position, tk.length) != GLSL::Parser::T_IDENTIFIER) int kind = lex.findKeyword(data.constData() + tk.position, tk.length);
if (kind == GLSL::Parser::T_RESERVED)
setFormat(tk.position, tk.length, m_formats[GLSLReservedKeyword]);
else if (kind != GLSL::Parser::T_IDENTIFIER)
setFormat(tk.position, tk.length, m_formats[GLSLKeywordFormat]); setFormat(tk.position, tk.length, m_formats[GLSLKeywordFormat]);
} }
} while (tk.isNot(GLSL::Parser::EOF_SYMBOL)); } while (tk.isNot(GLSL::Parser::EOF_SYMBOL));

View File

@@ -51,6 +51,7 @@ public:
GLSLDoxygenCommentFormat, GLSLDoxygenCommentFormat,
GLSLDoxygenTagFormat, GLSLDoxygenTagFormat,
GLSLVisualWhitespace, GLSLVisualWhitespace,
GLSLReservedKeyword,
NumGLSLFormats NumGLSLFormats
}; };