Commit 131df3c3 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Report the syntax errors.

parent 769a31fd
......@@ -34,6 +34,7 @@
%impl glslparser.cpp
%parser GLSLParserTable
%token_prefix T_
%expect 1
%token ADD_ASSIGN "+="
%token AMPERSAND "&"
......@@ -316,6 +317,24 @@ private:
inline int tokenKind(int index) const { return _tokens.at(index).kind; }
void reduce(int ruleno);
void warning(int line, const QString &message)
{
DiagnosticMessage m;
m.setKind(DiagnosticMessage::Warning);
m.setLine(line);
m.setMessage(message);
_engine->addDiagnosticMessage(m);
}
void error(int line, const QString &message)
{
DiagnosticMessage m;
m.setKind(DiagnosticMessage::Error);
m.setLine(line);
m.setMessage(message);
_engine->addDiagnosticMessage(m);
}
template <typename T>
T *makeAstNode()
{
......@@ -530,8 +549,17 @@ TranslationUnit *Parser::parse()
}
} while (action);
fprintf(stderr, "unexpected token `%s' at line %d\n", yytoken != -1 ? spell[yytoken] : "",
_tokens[yyloc].line + 1);
const int line = _tokens[yyloc].line + 1;
QString message = QLatin1String("Syntax error");
if (yytoken != -1) {
const QLatin1String s(yytoken != -1 ? spell[yytoken] : "");
message = QString("Unexpected token `%1'").arg(s);
}
error(line, message);
// fprintf(stderr, "unexpected token `%s' at line %d\n", yytoken != -1 ? spell[yytoken] : "",
// _tokens[yyloc].line + 1);
return 0;
}
......
......@@ -99,6 +99,11 @@ MemoryPool *Engine::pool()
return &_pool;
}
QList<DiagnosticMessage> Engine::diagnosticMessages() const
{
return _diagnosticMessages;
}
void Engine::clearDiagnosticMessages()
{
_diagnosticMessages.clear();
......
......@@ -400,3 +400,21 @@ int Lexer::findKeyword(const char *word, int length) const
}
return t & ~Variant_Mask;
}
void Lexer::warning(int line, const QString &message)
{
DiagnosticMessage m;
m.setKind(DiagnosticMessage::Warning);
m.setLine(line);
m.setMessage(message);
_engine->addDiagnosticMessage(m);
}
void Lexer::error(int line, const QString &message)
{
DiagnosticMessage m;
m.setKind(DiagnosticMessage::Error);
m.setLine(line);
m.setMessage(message);
_engine->addDiagnosticMessage(m);
}
......@@ -109,6 +109,9 @@ private:
void yyinp();
int yylex_helper(const char **position, int *line);
void warning(int line, const QString &message);
void error(int line, const QString &message);
private:
Engine *_engine;
const char *_source;
......
This diff is collapsed.
#line 212 "./glsl.g"
#line 213 "./glsl.g"
/**************************************************************************
**
......@@ -107,6 +107,24 @@ private:
inline int tokenKind(int index) const { return _tokens.at(index).kind; }
void reduce(int ruleno);
void warning(int line, const QString &message)
{
DiagnosticMessage m;
m.setKind(DiagnosticMessage::Warning);
m.setLine(line);
m.setMessage(message);
_engine->addDiagnosticMessage(m);
}
void error(int line, const QString &message)
{
DiagnosticMessage m;
m.setKind(DiagnosticMessage::Error);
m.setLine(line);
m.setMessage(message);
_engine->addDiagnosticMessage(m);
}
template <typename T>
T *makeAstNode()
{
......
......@@ -256,6 +256,33 @@ void GLSLTextEditor::updateDocumentNow()
Engine engine;
Parser parser(&engine, preprocessedCode.constData(), preprocessedCode.size(), variant);
TranslationUnit *ast = parser.parse();
QTextCharFormat errorFormat;
errorFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
errorFormat.setUnderlineColor(Qt::red);
QTextCharFormat warningFormat;
warningFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
warningFormat.setUnderlineColor(Qt::darkYellow);
QList<QTextEdit::ExtraSelection> sels;
foreach (const DiagnosticMessage &m, engine.diagnosticMessages()) {
if (! m.line())
continue;
QTextCursor cursor(document()->findBlockByNumber(m.line() - 1));
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
QTextEdit::ExtraSelection sel;
sel.cursor = cursor;
sel.format = m.isError() ? errorFormat : warningFormat;
sel.format.setToolTip(m.message());
sels.append(sel);
}
setExtraSelections(CodeWarningsSelection, sels);
// ### process the ast
(void) ast;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment