21#ifndef JKQTPCSSTOOLS_H_INCLUDED
22#define JKQTPCSSTOOLS_H_INCLUDED
23#include "jkqtcommon/jkqtcommon_imexport.h"
28#include "jkqtcommon/jkqtpmathtools.h"
29#include "jkqtcommon/jkqtpexpected.h"
60 inline NumberWithUnit(
double val=0.0,
const QString& unit_=QString()): unit(unit_), number(val) {};
77 const QString nu=normUnit();
78 if (nu==
"%")
return number/100.0;
80 if (nu==
"turn")
return number*360.0;
81 if (nu==
"grad")
return number/400.0*360.0;
89 const QString nu=normUnit();
98 const QString nu=normUnit();
101 if (nu==
"turn")
return fmod(number,1.0);
102 if (nu==
"grad")
return fmod(number/400.0,1.0);
103 return fmod(number/360.0, 1.0);
106 inline QString
normUnit()
const {
return unit.simplified().trimmed().toLower(); }
109 return number==other.
number && unit==other.
unit;
136 Token(
double num,
const QString& unit_=QString());
147 return (type==othertype);
151 return (type==TokenType::NAME) && (getNormString()==name.toLower().simplified().trimmed());
155 if (type==TokenType::NAME) {
156 const QString ns=getNormString();
157 for (
const auto&name: names) {
158 if (ns==name.toLower().simplified().trimmed())
return true;
165 return StringValue.toLower().simplified().trimmed();
190 return pos<0 || pos>=text.size()-1;
194 if (pos<0 || pos>=text.size())
return false;
201 if (pos<0 || pos>=text.size())
return false;
219 inline GeneralError(
const QString& err=QString(
"unspecified error"),
int p=-1): error(QString(
"%1 at pos.%2").arg(err).arg(p)), pos(p) {}
221 inline GeneralError(
const QString& context,
const QString& err,
int p=-1): error(QString(
"%1 in context %3 at pos.%2").arg(err).arg(p).arg(context)), pos(p) {}
228 GeneralError(RawErrorTag, QString(
"unexpected token at pos.%1 (found: %2, expected: %3)").arg(pos).arg(
Token::toString(foundToken)).arg(
Token::toString(expectedToken)), pos)
231 GeneralError(RawErrorTag, QString(
"unexpected token at pos.%1 (found: %2, expected: %3)").arg(pos).arg(foundToken.toString()).arg(
Token::toString(expectedToken)), pos)
234 GeneralError(RawErrorTag, QString(
"unexpected token at pos.%1 in context %4 (found: %2, expected: %3)").arg(pos).arg(foundToken.toString()).arg(
Token::toString(expectedToken)).arg(context), pos)
240 GeneralError(RawErrorTag, QString(
"unexpected term at pos.%1 (found: %2, expected: %3)").arg(pos).arg(foundToken.toString()).arg(expectedToken), pos)
243 GeneralError(RawErrorTag, QString(
"unexpected term at pos.%1 in context %4 (found: %2, expected: %3)").arg(pos).arg(foundToken.toString()).arg(expectedToken).arg(context), pos)
249 GeneralError(RawErrorTag, QString(
"wrong number of function arguments for %2() found at pos.%1 (found: %3, expected: %4...%5)").arg(pos).arg(func).arg(numArgs).arg(minArgs).arg(maxArgs), pos)
255 GeneralError(RawErrorTag, QString(
"Could not convert '%2' to %3 at pos.%1").arg(pos).arg(str).arg(target), pos)
A simple parser for certain CSS subsets.
Definition jkqtpcsstools.h:55
bool getCh(QChar &ch)
Definition jkqtpcsstools.h:193
JKQTPExpected< QColor, JKQTPCSSParser::GeneralError > parseColor(bool get)
parses a color definition <color>
QString text
this stream is used to read in the program. An object is created and assigned (and destroyed) by the ...
Definition jkqtpcsstools.h:184
JKQTPExpected< JKQTPCSSParser::NumberWithUnit, JKQTPCSSParser::GeneralError > parseNumberWithUnit(bool get)
parses a number with unit
static QGradient readGradient(const QString &prog)
parses the given expression, which should represent a QGradient
static NumberWithUnit readNumberWithUnit(const QString &prog)
parses the given expression
static QColor readColor(const QString &prog)
parses the given expression, which should represent a color (incl. color-functions like rgb() )
JKQTPExpected< QGradient, JKQTPCSSParser::GeneralError > parseGradient(bool get)
parses a color definition <gradient>
int pos
current reading position in text
Definition jkqtpcsstools.h:186
bool textAtEnd() const
indicates whether pos points to the end of text
Definition jkqtpcsstools.h:189
bool peekCh(QChar &ch)
Definition jkqtpcsstools.h:200
static RawErrorTag_t RawErrorTag
Definition jkqtpcsstools.h:217
Token CurrentToken
the current token while parsing a string
Definition jkqtpcsstools.h:179
Token getToken()
Tokenizer: extract the next token from the input.
JKQTPCSSParser(const QString &text)
class constructor
void putBackCh()
Definition jkqtpcsstools.h:206
Token peekNextToken(int *endPos=nullptr)
Tokenizer: look at the next token from the input, but don't set it as CurrentToken and don't move the...
Definition jkqtpcsstools.h:216
Definition jkqtpcsstools.h:218
GeneralError(const QString &context, const QString &err, int p=-1)
Definition jkqtpcsstools.h:221
QString error
Definition jkqtpcsstools.h:222
GeneralError(RawErrorTag_t, const QString &err, int p=-1)
Definition jkqtpcsstools.h:220
GeneralError(const QString &err=QString("unspecified error"), int p=-1)
Definition jkqtpcsstools.h:219
int pos
Definition jkqtpcsstools.h:223
encodes a number with its unit, e.g. 100% or 45deg or ...
Definition jkqtpcsstools.h:59
double number
the number value ittself
Definition jkqtpcsstools.h:64
double normNumber() const
partly evaluates the unit and transforms the number accordingly.
Definition jkqtpcsstools.h:76
QString normUnit() const
returns a normalized version of the unit, i.e. all lower-case, timmed and simplified
Definition jkqtpcsstools.h:106
NumberWithUnit(double val=0.0, const QString &unit_=QString())
Definition jkqtpcsstools.h:60
QString unit
Definition jkqtpcsstools.h:62
double normRGBNumber() const
returns number as a normalized number for RGB, output is in the range 0..1
Definition jkqtpcsstools.h:88
double normHueNumber() const
returns number as a normalized number for Hue, output is in the range 0..1
Definition jkqtpcsstools.h:97
bool operator==(const NumberWithUnit &other) const
Definition jkqtpcsstools.h:108
Definition jkqtpcsstools.h:119
QString getNormString() const
normlizes the StringValue (i.e. lower-case, trimmer, simmplified)
Definition jkqtpcsstools.h:164
QString StringValue
the string value of the current token (when applicable) during the parsing step
Definition jkqtpcsstools.h:140
TokenType type
Definition jkqtpcsstools.h:138
QString toString() const
converts the TOken information to a string
bool isNormStringAnyOf(const QStringList &names) const
checks whether the current token is of type TokenType::NAME and the String value equals any of the en...
Definition jkqtpcsstools.h:154
Token(double num, const QString &unit_=QString())
bool isNormString(const QString &name) const
checks whether the current token is of type TokenType::NAME and the String value equals name (case-in...
Definition jkqtpcsstools.h:150
Token(const QString &str, TokenType type=NAME)
static QString toString(TokenType type)
TokenType
the possible Token that can be recognized by the tokenizer in JKQTPCSSParser::getToken()
Definition jkqtpcsstools.h:121
@ LBRACKET
left brackets '('
Definition jkqtpcsstools.h:126
@ HEXSTRING
a string in HEX notation (i.e. 0-9a-fA-F)
Definition jkqtpcsstools.h:125
@ SLASH
a slash '/'
Definition jkqtpcsstools.h:129
@ NAME
a name (consisting of characters) of a variable or function
Definition jkqtpcsstools.h:123
@ NUMBER
a number, possibly with a unit(string)
Definition jkqtpcsstools.h:124
@ COMMA
a comma ','
Definition jkqtpcsstools.h:128
@ RBRACKET
right brackets ')'
Definition jkqtpcsstools.h:127
@ END
end token
Definition jkqtpcsstools.h:122
double NumberValue
the string value of the current token (when applicable) during the parsing step
Definition jkqtpcsstools.h:143
bool is(TokenType othertype) const
checks whether the current token is of the given type (does not compare any other field)
Definition jkqtpcsstools.h:146
Exception when a string cannot be converted properly.
Definition jkqtpcsstools.h:253
UnconvertobleError(const QString &str, const QString &target, int pos)
Definition jkqtpcsstools.h:254
Exception for unexpected Term.
Definition jkqtpcsstools.h:238
UnexpectedTermError(const QString &expectedToken, const Token &foundToken, int pos)
Definition jkqtpcsstools.h:239
UnexpectedTermError(const QString &context, const QString &expectedToken, const Token &foundToken, int pos)
Definition jkqtpcsstools.h:242
Exception for unexpected Token.
Definition jkqtpcsstools.h:226
UnexpectedTokenError(Token::TokenType expectedToken, const Token &foundToken, int pos)
Definition jkqtpcsstools.h:230
UnexpectedTokenError(const QString &context, Token::TokenType expectedToken, const Token &foundToken, int pos)
Definition jkqtpcsstools.h:233
UnexpectedTokenError(Token::TokenType expectedToken, Token::TokenType foundToken, int pos)
Definition jkqtpcsstools.h:227
Exception for wrong number of function arguments.
Definition jkqtpcsstools.h:247
WrongNumberOfArgumentError(const QString &func, int numArgs, int minArgs, int maxArgs, int pos)
Definition jkqtpcsstools.h:248
an "expected" datatype, which can either represent a function result of type T or an error of type E
Definition jkqtpexpected.h:61