84 lines
1.7 KiB
C
84 lines
1.7 KiB
C
|
|
#pragma once
|
||
|
|
|
||
|
|
#include "antlr4-runtime.h"
|
||
|
|
#include <QChar>
|
||
|
|
|
||
|
|
/// Helper stream for antlr, the lexer does not need to base case sensitive
|
||
|
|
/// this is achieved by changing the case of the chars in LA how ever
|
||
|
|
/// when the text of a recognized token is captured the getText function
|
||
|
|
/// is used which does no case conversion so the parse will receive the original
|
||
|
|
/// case.
|
||
|
|
class CaseChangingCharStream: public antlr4::CharStream
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
CaseChangingCharStream(antlr4::CharStream *stream, bool upper)
|
||
|
|
: stream(stream)
|
||
|
|
, upper(upper)
|
||
|
|
{}
|
||
|
|
|
||
|
|
virtual ~CaseChangingCharStream()
|
||
|
|
{}
|
||
|
|
|
||
|
|
virtual void consume() override
|
||
|
|
{
|
||
|
|
stream->consume();
|
||
|
|
}
|
||
|
|
|
||
|
|
virtual size_t LA(ssize_t i) override
|
||
|
|
{
|
||
|
|
int c = stream->LA(i);
|
||
|
|
if (c <= 0)
|
||
|
|
return c;
|
||
|
|
|
||
|
|
if (upper)
|
||
|
|
return QChar::toUpper(c);
|
||
|
|
|
||
|
|
return QChar::toLower(c);
|
||
|
|
}
|
||
|
|
|
||
|
|
virtual std::string getText(const antlr4::misc::Interval &interval) override
|
||
|
|
{
|
||
|
|
return stream->getText(interval);
|
||
|
|
}
|
||
|
|
|
||
|
|
virtual std::string toString() const override
|
||
|
|
{
|
||
|
|
return stream->toString();
|
||
|
|
}
|
||
|
|
|
||
|
|
virtual ssize_t mark() override
|
||
|
|
{
|
||
|
|
return stream->mark();
|
||
|
|
}
|
||
|
|
|
||
|
|
virtual void release(ssize_t marker) override
|
||
|
|
{
|
||
|
|
stream->release(marker);
|
||
|
|
}
|
||
|
|
|
||
|
|
virtual size_t index() override
|
||
|
|
{
|
||
|
|
return stream->index();
|
||
|
|
}
|
||
|
|
|
||
|
|
virtual void seek(size_t index) override
|
||
|
|
{
|
||
|
|
stream->seek(index);
|
||
|
|
}
|
||
|
|
|
||
|
|
virtual size_t size() override
|
||
|
|
{
|
||
|
|
return stream->size();
|
||
|
|
}
|
||
|
|
|
||
|
|
virtual std::string getSourceName() const override
|
||
|
|
{
|
||
|
|
return stream->getSourceName();
|
||
|
|
}
|
||
|
|
|
||
|
|
private:
|
||
|
|
antlr4::CharStream *stream;
|
||
|
|
bool upper;
|
||
|
|
|
||
|
|
};
|