in ext/scintilla/lexers/LexPerl.cxx [582:1637]
void SCI_METHOD LexerPerl::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
LexAccessor styler(pAccess);
// keywords that forces /PATTERN/ at all times; should track vim's behaviour
WordList reWords;
reWords.Set("elsif if split while");
// charset classes
CharacterSet setSingleCharOp(CharacterSet::setNone, "rwxoRWXOezsfdlpSbctugkTBMAC");
// lexing of "%*</" operators is non-trivial; these are missing in the set below
CharacterSet setPerlOperator(CharacterSet::setNone, "^&\\()-+=|{}[]:;>,?!.~");
CharacterSet setQDelim(CharacterSet::setNone, "qrwx");
CharacterSet setModifiers(CharacterSet::setAlpha);
CharacterSet setPreferRE(CharacterSet::setNone, "*/<%");
// setArray and setHash also accepts chars for special vars like $_,
// which are then truncated when the next char does not match setVar
CharacterSet setVar(CharacterSet::setAlphaNum, "#$_'", 0x80, true);
CharacterSet setArray(CharacterSet::setAlpha, "#$_+-", 0x80, true);
CharacterSet setHash(CharacterSet::setAlpha, "#$_!^+-", 0x80, true);
CharacterSet &setPOD = setModifiers;
CharacterSet setNonHereDoc(CharacterSet::setDigits, "=$@");
CharacterSet setHereDocDelim(CharacterSet::setAlphaNum, "_");
CharacterSet setSubPrototype(CharacterSet::setNone, "\\[$@%&*+];_ \t");
CharacterSet setRepetition(CharacterSet::setDigits, ")\"'");
// for format identifiers
CharacterSet setFormatStart(CharacterSet::setAlpha, "_=");
CharacterSet &setFormat = setHereDocDelim;
// Lexer for perl often has to backtrack to start of current style to determine
// which characters are being used as quotes, how deeply nested is the
// start position and what the termination string is for HERE documents.
class HereDocCls { // Class to manage HERE doc sequence
public:
int State;
// 0: '<<' encountered
// 1: collect the delimiter
// 2: here doc text (lines after the delimiter)
int Quote; // the char after '<<'
bool Quoted; // true if Quote in ('\'','"','`')
int DelimiterLength; // strlen(Delimiter)
char Delimiter[HERE_DELIM_MAX]; // the Delimiter
HereDocCls() {
State = 0;
Quote = 0;
Quoted = false;
DelimiterLength = 0;
Delimiter[0] = '\0';
}
void Append(int ch) {
Delimiter[DelimiterLength++] = static_cast<char>(ch);
Delimiter[DelimiterLength] = '\0';
}
~HereDocCls() {
}
};
HereDocCls HereDoc; // TODO: FIFO for stacked here-docs
class QuoteCls { // Class to manage quote pairs
public:
int Rep;
int Count;
int Up, Down;
QuoteCls() {
New(1);
}
void New(int r = 1) {
Rep = r;
Count = 0;
Up = '\0';
Down = '\0';
}
void Open(int u) {
Count++;
Up = u;
Down = opposite(Up);
}
};
QuoteCls Quote;
// additional state for number lexing
int numState = PERLNUM_DECIMAL;
int dotCount = 0;
Sci_PositionU endPos = startPos + length;
// Backtrack to beginning of style if required...
// If in a long distance lexical state, backtrack to find quote characters.
// Includes strings (may be multi-line), numbers (additional state), format
// bodies, as well as POD sections.
if (initStyle == SCE_PL_HERE_Q
|| initStyle == SCE_PL_HERE_QQ
|| initStyle == SCE_PL_HERE_QX
|| initStyle == SCE_PL_FORMAT
|| initStyle == SCE_PL_HERE_QQ_VAR
|| initStyle == SCE_PL_HERE_QX_VAR
) {
// backtrack through multiple styles to reach the delimiter start
int delim = (initStyle == SCE_PL_FORMAT) ? SCE_PL_FORMAT_IDENT:SCE_PL_HERE_DELIM;
while ((startPos > 1) && (styler.StyleAt(startPos) != delim)) {
startPos--;
}
startPos = styler.LineStart(styler.GetLine(startPos));
initStyle = styler.StyleAt(startPos - 1);
}
if (initStyle == SCE_PL_STRING
|| initStyle == SCE_PL_STRING_QQ
|| initStyle == SCE_PL_BACKTICKS
|| initStyle == SCE_PL_STRING_QX
|| initStyle == SCE_PL_REGEX
|| initStyle == SCE_PL_STRING_QR
|| initStyle == SCE_PL_REGSUBST
|| initStyle == SCE_PL_STRING_VAR
|| initStyle == SCE_PL_STRING_QQ_VAR
|| initStyle == SCE_PL_BACKTICKS_VAR
|| initStyle == SCE_PL_STRING_QX_VAR
|| initStyle == SCE_PL_REGEX_VAR
|| initStyle == SCE_PL_STRING_QR_VAR
|| initStyle == SCE_PL_REGSUBST_VAR
) {
// for interpolation, must backtrack through a mix of two different styles
int otherStyle = (initStyle >= SCE_PL_STRING_VAR) ?
initStyle - INTERPOLATE_SHIFT : initStyle + INTERPOLATE_SHIFT;
while (startPos > 1) {
int st = styler.StyleAt(startPos - 1);
if ((st != initStyle) && (st != otherStyle))
break;
startPos--;
}
initStyle = SCE_PL_DEFAULT;
} else if (initStyle == SCE_PL_STRING_Q
|| initStyle == SCE_PL_STRING_QW
|| initStyle == SCE_PL_XLAT
|| initStyle == SCE_PL_CHARACTER
|| initStyle == SCE_PL_NUMBER
|| initStyle == SCE_PL_IDENTIFIER
|| initStyle == SCE_PL_ERROR
|| initStyle == SCE_PL_SUB_PROTOTYPE
) {
while ((startPos > 1) && (styler.StyleAt(startPos - 1) == initStyle)) {
startPos--;
}
initStyle = SCE_PL_DEFAULT;
} else if (initStyle == SCE_PL_POD
|| initStyle == SCE_PL_POD_VERB
) {
// POD backtracking finds preceding blank lines and goes back past them
Sci_Position ln = styler.GetLine(startPos);
if (ln > 0) {
initStyle = styler.StyleAt(styler.LineStart(--ln));
if (initStyle == SCE_PL_POD || initStyle == SCE_PL_POD_VERB) {
while (ln > 0 && styler.GetLineState(ln) == SCE_PL_DEFAULT)
ln--;
}
startPos = styler.LineStart(++ln);
initStyle = styler.StyleAt(startPos - 1);
} else {
startPos = 0;
initStyle = SCE_PL_DEFAULT;
}
}
// backFlag, backPos are additional state to aid identifier corner cases.
// Look backwards past whitespace and comments in order to detect either
// operator or keyword. Later updated as we go along.
int backFlag = BACK_NONE;
Sci_PositionU backPos = startPos;
if (backPos > 0) {
backPos--;
skipWhitespaceComment(styler, backPos);
if (styler.StyleAt(backPos) == SCE_PL_OPERATOR)
backFlag = BACK_OPERATOR;
else if (styler.StyleAt(backPos) == SCE_PL_WORD)
backFlag = BACK_KEYWORD;
backPos++;
}
StyleContext sc(startPos, endPos - startPos, initStyle, styler);
for (; sc.More(); sc.Forward()) {
// Determine if the current state should terminate.
switch (sc.state) {
case SCE_PL_OPERATOR:
sc.SetState(SCE_PL_DEFAULT);
backFlag = BACK_OPERATOR;
backPos = sc.currentPos;
break;
case SCE_PL_IDENTIFIER: // identifier, bareword, inputsymbol
if ((!setWord.Contains(sc.ch) && sc.ch != '\'')
|| sc.Match('.', '.')
|| sc.chPrev == '>') { // end of inputsymbol
sc.SetState(SCE_PL_DEFAULT);
}
break;
case SCE_PL_WORD: // keyword, plus special cases
if (!setWord.Contains(sc.ch)) {
char s[100];
sc.GetCurrent(s, sizeof(s));
if ((strcmp(s, "__DATA__") == 0) || (strcmp(s, "__END__") == 0)) {
sc.ChangeState(SCE_PL_DATASECTION);
} else {
if ((strcmp(s, "format") == 0)) {
sc.SetState(SCE_PL_FORMAT_IDENT);
HereDoc.State = 0;
} else {
sc.SetState(SCE_PL_DEFAULT);
}
backFlag = BACK_KEYWORD;
backPos = sc.currentPos;
}
}
break;
case SCE_PL_SCALAR:
case SCE_PL_ARRAY:
case SCE_PL_HASH:
case SCE_PL_SYMBOLTABLE:
if (sc.Match(':', ':')) { // skip ::
sc.Forward();
} else if (!setVar.Contains(sc.ch)) {
if (sc.LengthCurrent() == 1) {
// Special variable: $(, $_ etc.
sc.Forward();
}
sc.SetState(SCE_PL_DEFAULT);
}
break;
case SCE_PL_NUMBER:
// if no early break, number style is terminated at "(go through)"
if (sc.ch == '.') {
if (sc.chNext == '.') {
// double dot is always an operator (go through)
} else if (numState <= PERLNUM_FLOAT_EXP) {
// non-decimal number or float exponent, consume next dot
sc.SetState(SCE_PL_OPERATOR);
break;
} else { // decimal or vectors allows dots
dotCount++;
if (numState == PERLNUM_DECIMAL) {
if (dotCount <= 1) // number with one dot in it
break;
if (IsADigit(sc.chNext)) { // really a vector
numState = PERLNUM_VECTOR;
break;
}
// number then dot (go through)
} else if (numState == PERLNUM_HEX) {
if (dotCount <= 1 && IsADigit(sc.chNext, 16)) {
break; // hex with one dot is a hex float
} else {
sc.SetState(SCE_PL_OPERATOR);
break;
}
// hex then dot (go through)
} else if (IsADigit(sc.chNext)) // vectors
break;
// vector then dot (go through)
}
} else if (sc.ch == '_') {
// permissive underscoring for number and vector literals
break;
} else if (numState == PERLNUM_DECIMAL) {
if (sc.ch == 'E' || sc.ch == 'e') { // exponent, sign
numState = PERLNUM_FLOAT_EXP;
if (sc.chNext == '+' || sc.chNext == '-') {
sc.Forward();
}
break;
} else if (IsADigit(sc.ch))
break;
// number then word (go through)
} else if (numState == PERLNUM_HEX) {
if (sc.ch == 'P' || sc.ch == 'p') { // hex float exponent, sign
numState = PERLNUM_FLOAT_EXP;
if (sc.chNext == '+' || sc.chNext == '-') {
sc.Forward();
}
break;
} else if (IsADigit(sc.ch, 16))
break;
// hex or hex float then word (go through)
} else if (numState == PERLNUM_VECTOR || numState == PERLNUM_V_VECTOR) {
if (IsADigit(sc.ch)) // vector
break;
if (setWord.Contains(sc.ch) && dotCount == 0) { // change to word
sc.ChangeState(SCE_PL_IDENTIFIER);
break;
}
// vector then word (go through)
} else if (IsADigit(sc.ch)) {
if (numState == PERLNUM_FLOAT_EXP) {
break;
} else if (numState == PERLNUM_OCTAL) {
if (sc.ch <= '7') break;
} else if (numState == PERLNUM_BINARY) {
if (sc.ch <= '1') break;
}
// mark invalid octal, binary numbers (go through)
numState = PERLNUM_BAD;
break;
}
// complete current number or vector
sc.ChangeState(actualNumStyle(numState));
sc.SetState(SCE_PL_DEFAULT);
break;
case SCE_PL_COMMENTLINE:
if (sc.atLineEnd) {
sc.SetState(SCE_PL_DEFAULT);
}
break;
case SCE_PL_HERE_DELIM:
if (HereDoc.State == 0) { // '<<' encountered
int delim_ch = sc.chNext;
Sci_Position ws_skip = 0;
HereDoc.State = 1; // pre-init HERE doc class
HereDoc.Quote = sc.chNext;
HereDoc.Quoted = false;
HereDoc.DelimiterLength = 0;
HereDoc.Delimiter[HereDoc.DelimiterLength] = '\0';
if (IsASpaceOrTab(delim_ch)) {
// skip whitespace; legal only for quoted delimiters
Sci_PositionU i = sc.currentPos + 1;
while ((i < endPos) && IsASpaceOrTab(delim_ch)) {
i++;
delim_ch = static_cast<unsigned char>(styler.SafeGetCharAt(i));
}
ws_skip = i - sc.currentPos - 1;
}
if (delim_ch == '\'' || delim_ch == '"' || delim_ch == '`') {
// a quoted here-doc delimiter; skip any whitespace
sc.Forward(ws_skip + 1);
HereDoc.Quote = delim_ch;
HereDoc.Quoted = true;
} else if ((ws_skip == 0 && setNonHereDoc.Contains(sc.chNext))
|| ws_skip > 0) {
// left shift << or <<= operator cases
// restore position if operator
sc.ChangeState(SCE_PL_OPERATOR);
sc.ForwardSetState(SCE_PL_DEFAULT);
backFlag = BACK_OPERATOR;
backPos = sc.currentPos;
HereDoc.State = 0;
} else {
// specially handle initial '\' for identifier
if (ws_skip == 0 && HereDoc.Quote == '\\')
sc.Forward();
// an unquoted here-doc delimiter, no special handling
// (cannot be prefixed by spaces/tabs), or
// symbols terminates; deprecated zero-length delimiter
}
} else if (HereDoc.State == 1) { // collect the delimiter
backFlag = BACK_NONE;
if (HereDoc.Quoted) { // a quoted here-doc delimiter
if (sc.ch == HereDoc.Quote) { // closing quote => end of delimiter
sc.ForwardSetState(SCE_PL_DEFAULT);
} else if (!sc.atLineEnd) {
if (sc.Match('\\', static_cast<char>(HereDoc.Quote))) { // escaped quote
sc.Forward();
}
if (sc.ch != '\r') { // skip CR if CRLF
int i = 0; // else append char, possibly an extended char
while (i < sc.width) {
HereDoc.Append(static_cast<unsigned char>(styler.SafeGetCharAt(sc.currentPos + i)));
i++;
}
}
}
} else { // an unquoted here-doc delimiter, no extended charsets
if (setHereDocDelim.Contains(sc.ch)) {
HereDoc.Append(sc.ch);
} else {
sc.SetState(SCE_PL_DEFAULT);
}
}
if (HereDoc.DelimiterLength >= HERE_DELIM_MAX - 1) {
sc.SetState(SCE_PL_ERROR);
HereDoc.State = 0;
}
}
break;
case SCE_PL_HERE_Q:
case SCE_PL_HERE_QQ:
case SCE_PL_HERE_QX:
// also implies HereDoc.State == 2
sc.Complete();
if (HereDoc.DelimiterLength == 0 || sc.Match(HereDoc.Delimiter)) {
int c = sc.GetRelative(HereDoc.DelimiterLength);
if (c == '\r' || c == '\n') { // peek first, do not consume match
sc.ForwardBytes(HereDoc.DelimiterLength);
sc.SetState(SCE_PL_DEFAULT);
backFlag = BACK_NONE;
HereDoc.State = 0;
if (!sc.atLineEnd)
sc.Forward();
break;
}
}
if (sc.state == SCE_PL_HERE_Q) { // \EOF and 'EOF' non-interpolated
while (!sc.atLineEnd)
sc.Forward();
break;
}
while (!sc.atLineEnd) { // "EOF" and `EOF` interpolated
int c, sLen = 0, endType = 0;
while ((c = sc.GetRelativeCharacter(sLen)) != 0) {
// scan to break string into segments
if (c == '\\') {
endType = 1; break;
} else if (c == '\r' || c == '\n') {
endType = 2; break;
}
sLen++;
}
if (sLen > 0) // process non-empty segments
InterpolateSegment(sc, sLen);
if (endType == 1) {
sc.Forward();
// \ at end-of-line does not appear to have any effect, skip
if (sc.ch != '\r' && sc.ch != '\n')
sc.Forward();
} else if (endType == 2) {
if (!sc.atLineEnd)
sc.Forward();
}
}
break;
case SCE_PL_POD:
case SCE_PL_POD_VERB: {
Sci_PositionU fw = sc.currentPos;
Sci_Position ln = styler.GetLine(fw);
if (sc.atLineStart && sc.Match("=cut")) { // end of POD
sc.SetState(SCE_PL_POD);
sc.Forward(4);
sc.SetState(SCE_PL_DEFAULT);
styler.SetLineState(ln, SCE_PL_POD);
break;
}
int pod = podLineScan(styler, fw, endPos); // classify POD line
styler.SetLineState(ln, pod);
if (pod == SCE_PL_DEFAULT) {
if (sc.state == SCE_PL_POD_VERB) {
Sci_PositionU fw2 = fw;
while (fw2 < (endPos - 1) && pod == SCE_PL_DEFAULT) {
fw = fw2++; // penultimate line (last blank line)
pod = podLineScan(styler, fw2, endPos);
styler.SetLineState(styler.GetLine(fw2), pod);
}
if (pod == SCE_PL_POD) { // truncate verbatim POD early
sc.SetState(SCE_PL_POD);
} else
fw = fw2;
}
} else {
if (pod == SCE_PL_POD_VERB // still part of current paragraph
&& (styler.GetLineState(ln - 1) == SCE_PL_POD)) {
pod = SCE_PL_POD;
styler.SetLineState(ln, pod);
} else if (pod == SCE_PL_POD
&& (styler.GetLineState(ln - 1) == SCE_PL_POD_VERB)) {
pod = SCE_PL_POD_VERB;
styler.SetLineState(ln, pod);
}
sc.SetState(pod);
}
sc.ForwardBytes(fw - sc.currentPos); // commit style
}
break;
case SCE_PL_REGEX:
case SCE_PL_STRING_QR:
if (Quote.Rep <= 0) {
if (!setModifiers.Contains(sc.ch))
sc.SetState(SCE_PL_DEFAULT);
} else if (!Quote.Up && !IsASpace(sc.ch)) {
Quote.Open(sc.ch);
} else {
int c, sLen = 0, endType = 0;
while ((c = sc.GetRelativeCharacter(sLen)) != 0) {
// scan to break string into segments
if (IsASpace(c)) {
break;
} else if (c == '\\' && Quote.Up != '\\') {
endType = 1; break;
} else if (c == Quote.Down) {
Quote.Count--;
if (Quote.Count == 0) {
Quote.Rep--;
break;
}
} else if (c == Quote.Up)
Quote.Count++;
sLen++;
}
if (sLen > 0) { // process non-empty segments
if (Quote.Up != '\'') {
InterpolateSegment(sc, sLen, true);
} else // non-interpolated path
sc.Forward(sLen);
}
if (endType == 1)
sc.Forward();
}
break;
case SCE_PL_REGSUBST:
case SCE_PL_XLAT:
if (Quote.Rep <= 0) {
if (!setModifiers.Contains(sc.ch))
sc.SetState(SCE_PL_DEFAULT);
} else if (!Quote.Up && !IsASpace(sc.ch)) {
Quote.Open(sc.ch);
} else {
int c, sLen = 0, endType = 0;
bool isPattern = (Quote.Rep == 2);
while ((c = sc.GetRelativeCharacter(sLen)) != 0) {
// scan to break string into segments
if (c == '\\' && Quote.Up != '\\') {
endType = 2; break;
} else if (Quote.Count == 0 && Quote.Rep == 1) {
// We matched something like s(...) or tr{...}, Perl 5.10
// appears to allow almost any character for use as the
// next delimiters. Whitespace and comments are accepted in
// between, but we'll limit to whitespace here.
// For '#', if no whitespace in between, it's a delimiter.
if (IsASpace(c)) {
// Keep going
} else if (c == '#' && IsASpaceOrTab(sc.GetRelativeCharacter(sLen - 1))) {
endType = 3;
} else
Quote.Open(c);
break;
} else if (c == Quote.Down) {
Quote.Count--;
if (Quote.Count == 0) {
Quote.Rep--;
endType = 1;
}
if (Quote.Up == Quote.Down)
Quote.Count++;
if (endType == 1)
break;
} else if (c == Quote.Up) {
Quote.Count++;
} else if (IsASpace(c))
break;
sLen++;
}
if (sLen > 0) { // process non-empty segments
if (sc.state == SCE_PL_REGSUBST && Quote.Up != '\'') {
InterpolateSegment(sc, sLen, isPattern);
} else // non-interpolated path
sc.Forward(sLen);
}
if (endType == 2) {
sc.Forward();
} else if (endType == 3)
sc.SetState(SCE_PL_DEFAULT);
}
break;
case SCE_PL_STRING_Q:
case SCE_PL_STRING_QQ:
case SCE_PL_STRING_QX:
case SCE_PL_STRING_QW:
case SCE_PL_STRING:
case SCE_PL_CHARACTER:
case SCE_PL_BACKTICKS:
if (!Quote.Down && !IsASpace(sc.ch)) {
Quote.Open(sc.ch);
} else {
int c, sLen = 0, endType = 0;
while ((c = sc.GetRelativeCharacter(sLen)) != 0) {
// scan to break string into segments
if (IsASpace(c)) {
break;
} else if (c == '\\' && Quote.Up != '\\') {
endType = 2; break;
} else if (c == Quote.Down) {
Quote.Count--;
if (Quote.Count == 0) {
endType = 3; break;
}
} else if (c == Quote.Up)
Quote.Count++;
sLen++;
}
if (sLen > 0) { // process non-empty segments
switch (sc.state) {
case SCE_PL_STRING:
case SCE_PL_STRING_QQ:
case SCE_PL_BACKTICKS:
InterpolateSegment(sc, sLen);
break;
case SCE_PL_STRING_QX:
if (Quote.Up != '\'') {
InterpolateSegment(sc, sLen);
break;
}
// (continued for ' delim)
// Falls through.
default: // non-interpolated path
sc.Forward(sLen);
}
}
if (endType == 2) {
sc.Forward();
} else if (endType == 3)
sc.ForwardSetState(SCE_PL_DEFAULT);
}
break;
case SCE_PL_SUB_PROTOTYPE: {
int i = 0;
// forward scan; must all be valid proto characters
while (setSubPrototype.Contains(sc.GetRelative(i)))
i++;
if (sc.GetRelative(i) == ')') { // valid sub prototype
sc.ForwardBytes(i);
sc.ForwardSetState(SCE_PL_DEFAULT);
} else {
// abandon prototype, restart from '('
sc.ChangeState(SCE_PL_OPERATOR);
sc.SetState(SCE_PL_DEFAULT);
}
}
break;
case SCE_PL_FORMAT: {
sc.Complete();
if (sc.Match('.')) {
sc.Forward();
if (sc.atLineEnd || ((sc.ch == '\r' && sc.chNext == '\n')))
sc.SetState(SCE_PL_DEFAULT);
}
while (!sc.atLineEnd)
sc.Forward();
}
break;
case SCE_PL_ERROR:
break;
}
// Needed for specific continuation styles (one follows the other)
switch (sc.state) {
// continued from SCE_PL_WORD
case SCE_PL_FORMAT_IDENT:
// occupies HereDoc state 3 to avoid clashing with HERE docs
if (IsASpaceOrTab(sc.ch)) { // skip whitespace
sc.ChangeState(SCE_PL_DEFAULT);
while (IsASpaceOrTab(sc.ch) && !sc.atLineEnd)
sc.Forward();
sc.SetState(SCE_PL_FORMAT_IDENT);
}
if (setFormatStart.Contains(sc.ch)) { // identifier or '='
if (sc.ch != '=') {
do {
sc.Forward();
} while (setFormat.Contains(sc.ch));
}
while (IsASpaceOrTab(sc.ch) && !sc.atLineEnd)
sc.Forward();
if (sc.ch == '=') {
sc.ForwardSetState(SCE_PL_DEFAULT);
HereDoc.State = 3;
} else {
// invalid identifier; inexact fallback, but hey
sc.ChangeState(SCE_PL_IDENTIFIER);
sc.SetState(SCE_PL_DEFAULT);
}
} else {
sc.ChangeState(SCE_PL_DEFAULT); // invalid identifier
}
backFlag = BACK_NONE;
break;
}
// Must check end of HereDoc states here before default state is handled
if (HereDoc.State == 1 && sc.atLineEnd) {
// Begin of here-doc (the line after the here-doc delimiter):
// Lexically, the here-doc starts from the next line after the >>, but the
// first line of here-doc seem to follow the style of the last EOL sequence
int st_new = SCE_PL_HERE_QQ;
HereDoc.State = 2;
if (HereDoc.Quoted) {
if (sc.state == SCE_PL_HERE_DELIM) {
// Missing quote at end of string! We are stricter than perl.
// Colour here-doc anyway while marking this bit as an error.
sc.ChangeState(SCE_PL_ERROR);
}
switch (HereDoc.Quote) {
case '\'':
st_new = SCE_PL_HERE_Q;
break;
case '"' :
st_new = SCE_PL_HERE_QQ;
break;
case '`' :
st_new = SCE_PL_HERE_QX;
break;
}
} else {
if (HereDoc.Quote == '\\')
st_new = SCE_PL_HERE_Q;
}
sc.SetState(st_new);
}
if (HereDoc.State == 3 && sc.atLineEnd) {
// Start of format body.
HereDoc.State = 0;
sc.SetState(SCE_PL_FORMAT);
}
// Determine if a new state should be entered.
if (sc.state == SCE_PL_DEFAULT) {
if (IsADigit(sc.ch) ||
(IsADigit(sc.chNext) && (sc.ch == '.' || sc.ch == 'v'))) {
sc.SetState(SCE_PL_NUMBER);
backFlag = BACK_NONE;
numState = PERLNUM_DECIMAL;
dotCount = 0;
if (sc.ch == '0') { // hex,bin,octal
if (sc.chNext == 'x' || sc.chNext == 'X') {
numState = PERLNUM_HEX;
} else if (sc.chNext == 'b' || sc.chNext == 'B') {
numState = PERLNUM_BINARY;
} else if (IsADigit(sc.chNext)) {
numState = PERLNUM_OCTAL;
}
if (numState != PERLNUM_DECIMAL) {
sc.Forward();
}
} else if (sc.ch == 'v') { // vector
numState = PERLNUM_V_VECTOR;
}
} else if (setWord.Contains(sc.ch)) {
// if immediately prefixed by '::', always a bareword
sc.SetState(SCE_PL_WORD);
if (sc.chPrev == ':' && sc.GetRelative(-2) == ':') {
sc.ChangeState(SCE_PL_IDENTIFIER);
}
Sci_PositionU bk = sc.currentPos;
Sci_PositionU fw = sc.currentPos + 1;
// first check for possible quote-like delimiter
if (sc.ch == 's' && !setWord.Contains(sc.chNext)) {
sc.ChangeState(SCE_PL_REGSUBST);
Quote.New(2);
} else if (sc.ch == 'm' && !setWord.Contains(sc.chNext)) {
sc.ChangeState(SCE_PL_REGEX);
Quote.New();
} else if (sc.ch == 'q' && !setWord.Contains(sc.chNext)) {
sc.ChangeState(SCE_PL_STRING_Q);
Quote.New();
} else if (sc.ch == 'y' && !setWord.Contains(sc.chNext)) {
sc.ChangeState(SCE_PL_XLAT);
Quote.New(2);
} else if (sc.Match('t', 'r') && !setWord.Contains(sc.GetRelative(2))) {
sc.ChangeState(SCE_PL_XLAT);
Quote.New(2);
sc.Forward();
fw++;
} else if (sc.ch == 'q' && setQDelim.Contains(sc.chNext)
&& !setWord.Contains(sc.GetRelative(2))) {
if (sc.chNext == 'q') sc.ChangeState(SCE_PL_STRING_QQ);
else if (sc.chNext == 'x') sc.ChangeState(SCE_PL_STRING_QX);
else if (sc.chNext == 'r') sc.ChangeState(SCE_PL_STRING_QR);
else sc.ChangeState(SCE_PL_STRING_QW); // sc.chNext == 'w'
Quote.New();
sc.Forward();
fw++;
} else if (sc.ch == 'x' && (sc.chNext == '=' || // repetition
!setWord.Contains(sc.chNext) ||
(setRepetition.Contains(sc.chPrev) && IsADigit(sc.chNext)))) {
sc.ChangeState(SCE_PL_OPERATOR);
}
// if potentially a keyword, scan forward and grab word, then check
// if it's really one; if yes, disambiguation test is performed
// otherwise it is always a bareword and we skip a lot of scanning
if (sc.state == SCE_PL_WORD) {
while (setWord.Contains(static_cast<unsigned char>(styler.SafeGetCharAt(fw))))
fw++;
if (!isPerlKeyword(styler.GetStartSegment(), fw, keywords, styler)) {
sc.ChangeState(SCE_PL_IDENTIFIER);
}
}
// if already SCE_PL_IDENTIFIER, then no ambiguity, skip this
// for quote-like delimiters/keywords, attempt to disambiguate
// to select for bareword, change state -> SCE_PL_IDENTIFIER
if (sc.state != SCE_PL_IDENTIFIER && bk > 0) {
if (disambiguateBareword(styler, bk, fw, backFlag, backPos, endPos))
sc.ChangeState(SCE_PL_IDENTIFIER);
}
backFlag = BACK_NONE;
} else if (sc.ch == '#') {
sc.SetState(SCE_PL_COMMENTLINE);
} else if (sc.ch == '\"') {
sc.SetState(SCE_PL_STRING);
Quote.New();
Quote.Open(sc.ch);
backFlag = BACK_NONE;
} else if (sc.ch == '\'') {
if (sc.chPrev == '&' && setWordStart.Contains(sc.chNext)) {
// Archaic call
sc.SetState(SCE_PL_IDENTIFIER);
} else {
sc.SetState(SCE_PL_CHARACTER);
Quote.New();
Quote.Open(sc.ch);
}
backFlag = BACK_NONE;
} else if (sc.ch == '`') {
sc.SetState(SCE_PL_BACKTICKS);
Quote.New();
Quote.Open(sc.ch);
backFlag = BACK_NONE;
} else if (sc.ch == '$') {
sc.SetState(SCE_PL_SCALAR);
if (sc.chNext == '{') {
sc.ForwardSetState(SCE_PL_OPERATOR);
} else if (IsASpace(sc.chNext)) {
sc.ForwardSetState(SCE_PL_DEFAULT);
} else {
sc.Forward();
if (sc.Match('`', '`') || sc.Match(':', ':')) {
sc.Forward();
}
}
backFlag = BACK_NONE;
} else if (sc.ch == '@') {
sc.SetState(SCE_PL_ARRAY);
if (setArray.Contains(sc.chNext)) {
// no special treatment
} else if (sc.chNext == ':' && sc.GetRelative(2) == ':') {
sc.ForwardBytes(2);
} else if (sc.chNext == '{' || sc.chNext == '[') {
sc.ForwardSetState(SCE_PL_OPERATOR);
} else {
sc.ChangeState(SCE_PL_OPERATOR);
}
backFlag = BACK_NONE;
} else if (setPreferRE.Contains(sc.ch)) {
// Explicit backward peeking to set a consistent preferRE for
// any slash found, so no longer need to track preferRE state.
// Find first previous significant lexed element and interpret.
// A few symbols shares this code for disambiguation.
bool preferRE = false;
bool isHereDoc = sc.Match('<', '<');
bool hereDocSpace = false; // for: SCALAR [whitespace] '<<'
Sci_PositionU bk = (sc.currentPos > 0) ? sc.currentPos - 1: 0;
sc.Complete();
styler.Flush();
if (styler.StyleAt(bk) == SCE_PL_DEFAULT)
hereDocSpace = true;
skipWhitespaceComment(styler, bk);
if (bk == 0) {
// avoid backward scanning breakage
preferRE = true;
} else {
int bkstyle = styler.StyleAt(bk);
int bkch = static_cast<unsigned char>(styler.SafeGetCharAt(bk));
switch (bkstyle) {
case SCE_PL_OPERATOR:
preferRE = true;
if (bkch == ')' || bkch == ']') {
preferRE = false;
} else if (bkch == '}') {
// backtrack by counting balanced brace pairs
// needed to test for variables like ${}, @{} etc.
bkstyle = styleBeforeBracePair(styler, bk);
if (bkstyle == SCE_PL_SCALAR
|| bkstyle == SCE_PL_ARRAY
|| bkstyle == SCE_PL_HASH
|| bkstyle == SCE_PL_SYMBOLTABLE
|| bkstyle == SCE_PL_OPERATOR) {
preferRE = false;
}
} else if (bkch == '+' || bkch == '-') {
if (bkch == static_cast<unsigned char>(styler.SafeGetCharAt(bk - 1))
&& bkch != static_cast<unsigned char>(styler.SafeGetCharAt(bk - 2)))
// exceptions for operators: unary suffixes ++, --
preferRE = false;
}
break;
case SCE_PL_IDENTIFIER:
preferRE = true;
bkstyle = styleCheckIdentifier(styler, bk);
if ((bkstyle == 1) || (bkstyle == 2)) {
// inputsymbol or var with "->" or "::" before identifier
preferRE = false;
} else if (bkstyle == 3) {
// bare identifier, test cases follows:
if (sc.ch == '/') {
// if '/', /PATTERN/ unless digit/space immediately after '/'
// if '//', always expect defined-or operator to follow identifier
if (IsASpace(sc.chNext) || IsADigit(sc.chNext) || sc.chNext == '/')
preferRE = false;
} else if (sc.ch == '*' || sc.ch == '%') {
if (IsASpace(sc.chNext) || IsADigit(sc.chNext) || sc.Match('*', '*'))
preferRE = false;
} else if (sc.ch == '<') {
if (IsASpace(sc.chNext) || sc.chNext == '=')
preferRE = false;
}
}
break;
case SCE_PL_SCALAR: // for $var<< case:
if (isHereDoc && hereDocSpace) // if SCALAR whitespace '<<', *always* a HERE doc
preferRE = true;
break;
case SCE_PL_WORD:
preferRE = true;
// for HERE docs, always true
if (sc.ch == '/') {
// adopt heuristics similar to vim-style rules:
// keywords always forced as /PATTERN/: split, if, elsif, while
// everything else /PATTERN/ unless digit/space immediately after '/'
// for '//', defined-or favoured unless special keywords
Sci_PositionU bkend = bk + 1;
while (bk > 0 && styler.StyleAt(bk - 1) == SCE_PL_WORD) {
bk--;
}
if (isPerlKeyword(bk, bkend, reWords, styler))
break;
if (IsASpace(sc.chNext) || IsADigit(sc.chNext) || sc.chNext == '/')
preferRE = false;
} else if (sc.ch == '*' || sc.ch == '%') {
if (IsASpace(sc.chNext) || IsADigit(sc.chNext) || sc.Match('*', '*'))
preferRE = false;
} else if (sc.ch == '<') {
if (IsASpace(sc.chNext) || sc.chNext == '=')
preferRE = false;
}
break;
// other styles uses the default, preferRE=false
case SCE_PL_POD:
case SCE_PL_HERE_Q:
case SCE_PL_HERE_QQ:
case SCE_PL_HERE_QX:
preferRE = true;
break;
}
}
backFlag = BACK_NONE;
if (isHereDoc) { // handle '<<', HERE doc
if (sc.Match("<<>>")) { // double-diamond operator (5.22)
sc.SetState(SCE_PL_OPERATOR);
sc.Forward(3);
} else if (preferRE) {
sc.SetState(SCE_PL_HERE_DELIM);
HereDoc.State = 0;
} else { // << operator
sc.SetState(SCE_PL_OPERATOR);
sc.Forward();
}
} else if (sc.ch == '*') { // handle '*', typeglob
if (preferRE) {
sc.SetState(SCE_PL_SYMBOLTABLE);
if (sc.chNext == ':' && sc.GetRelative(2) == ':') {
sc.ForwardBytes(2);
} else if (sc.chNext == '{') {
sc.ForwardSetState(SCE_PL_OPERATOR);
} else {
sc.Forward();
}
} else {
sc.SetState(SCE_PL_OPERATOR);
if (sc.chNext == '*') // exponentiation
sc.Forward();
}
} else if (sc.ch == '%') { // handle '%', hash
if (preferRE) {
sc.SetState(SCE_PL_HASH);
if (setHash.Contains(sc.chNext)) {
sc.Forward();
} else if (sc.chNext == ':' && sc.GetRelative(2) == ':') {
sc.ForwardBytes(2);
} else if (sc.chNext == '{') {
sc.ForwardSetState(SCE_PL_OPERATOR);
} else {
sc.ChangeState(SCE_PL_OPERATOR);
}
} else {
sc.SetState(SCE_PL_OPERATOR);
}
} else if (sc.ch == '<') { // handle '<', inputsymbol
if (preferRE) {
// forward scan
int i = InputSymbolScan(sc);
if (i > 0) {
sc.SetState(SCE_PL_IDENTIFIER);
sc.Forward(i);
} else {
sc.SetState(SCE_PL_OPERATOR);
}
} else {
sc.SetState(SCE_PL_OPERATOR);
}
} else { // handle '/', regexp
if (preferRE) {
sc.SetState(SCE_PL_REGEX);
Quote.New();
Quote.Open(sc.ch);
} else { // / and // operators
sc.SetState(SCE_PL_OPERATOR);
if (sc.chNext == '/') {
sc.Forward();
}
}
}
} else if (sc.ch == '=' // POD
&& setPOD.Contains(sc.chNext)
&& sc.atLineStart) {
sc.SetState(SCE_PL_POD);
backFlag = BACK_NONE;
} else if (sc.ch == '-' && setWordStart.Contains(sc.chNext)) { // extended '-' cases
Sci_PositionU bk = sc.currentPos;
Sci_PositionU fw = 2;
if (setSingleCharOp.Contains(sc.chNext) && // file test operators
!setWord.Contains(sc.GetRelative(2))) {
sc.SetState(SCE_PL_WORD);
} else {
// nominally a minus and bareword; find extent of bareword
while (setWord.Contains(sc.GetRelative(fw)))
fw++;
sc.SetState(SCE_PL_OPERATOR);
}
// force to bareword for hash key => or {variable literal} cases
if (disambiguateBareword(styler, bk, bk + fw, backFlag, backPos, endPos) & 2) {
sc.ChangeState(SCE_PL_IDENTIFIER);
}
backFlag = BACK_NONE;
} else if (sc.ch == '(' && sc.currentPos > 0) { // '(' or subroutine prototype
sc.Complete();
if (styleCheckSubPrototype(styler, sc.currentPos - 1)) {
sc.SetState(SCE_PL_SUB_PROTOTYPE);
backFlag = BACK_NONE;
} else {
sc.SetState(SCE_PL_OPERATOR);
}
} else if (setPerlOperator.Contains(sc.ch)) { // operators
sc.SetState(SCE_PL_OPERATOR);
if (sc.Match('.', '.')) { // .. and ...
sc.Forward();
if (sc.chNext == '.') sc.Forward();
}
} else if (sc.ch == 4 || sc.ch == 26) { // ^D and ^Z ends valid perl source
sc.SetState(SCE_PL_DATASECTION);
} else {
// keep colouring defaults
sc.Complete();
}
}
}
sc.Complete();
if (sc.state == SCE_PL_HERE_Q
|| sc.state == SCE_PL_HERE_QQ
|| sc.state == SCE_PL_HERE_QX
|| sc.state == SCE_PL_FORMAT) {
styler.ChangeLexerState(sc.currentPos, styler.Length());
}
sc.Complete();
}