2009-04-24 23:35:41 +00:00
|
|
|
// Scintilla source code edit control
|
|
|
|
/** @file LexPerl.cxx
|
2009-04-25 23:38:15 +00:00
|
|
|
** Lexer for Perl.
|
2011-07-17 22:30:49 +00:00
|
|
|
** Converted to lexer object by "Udo Lechner" <dlchnr(at)gmx(dot)net>
|
2009-04-24 23:35:41 +00:00
|
|
|
**/
|
|
|
|
// Copyright 1998-2008 by Neil Hodgson <neilh@scintilla.org>
|
|
|
|
// Lexical analysis fixes by Kein-Hong Man <mkh@pl.jaring.my>
|
|
|
|
// The License.txt file describes the conditions under which this software may be distributed.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2010-08-21 23:59:56 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "ILexer.h"
|
|
|
|
#include "Scintilla.h"
|
|
|
|
#include "SciLexer.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "WordList.h"
|
|
|
|
#include "LexAccessor.h"
|
2009-04-25 23:38:15 +00:00
|
|
|
#include "StyleContext.h"
|
|
|
|
#include "CharacterSet.h"
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "LexerModule.h"
|
2011-07-17 22:30:49 +00:00
|
|
|
#include "OptionSet.h"
|
2019-05-04 18:14:48 +00:00
|
|
|
#include "DefaultLexer.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
using namespace Scintilla;
|
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
// Info for HERE document handling from perldata.pod (reformatted):
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// A line-oriented form of quoting is based on the shell ``here-doc'' syntax.
|
|
|
|
// Following a << you specify a string to terminate the quoted material, and
|
|
|
|
// all lines following the current line down to the terminating string are
|
|
|
|
// the value of the item.
|
|
|
|
// * The terminating string may be either an identifier (a word), or some
|
|
|
|
// quoted text.
|
|
|
|
// * If quoted, the type of quotes you use determines the treatment of the
|
|
|
|
// text, just as in regular quoting.
|
|
|
|
// * An unquoted identifier works like double quotes.
|
|
|
|
// * There must be no space between the << and the identifier.
|
|
|
|
// (If you put a space it will be treated as a null identifier,
|
|
|
|
// which is valid, and matches the first empty line.)
|
|
|
|
// (This is deprecated, -w warns of this syntax)
|
|
|
|
// * The terminating string must appear by itself (unquoted and
|
|
|
|
// with no surrounding whitespace) on the terminating line.
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
#define HERE_DELIM_MAX 256 // maximum length of HERE doc delimiter
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
#define PERLNUM_BINARY 1 // order is significant: 1-3 cannot have a dot
|
|
|
|
#define PERLNUM_OCTAL 2
|
|
|
|
#define PERLNUM_FLOAT_EXP 3 // exponent part only
|
|
|
|
#define PERLNUM_HEX 4 // may be a hex float
|
2009-04-25 23:38:15 +00:00
|
|
|
#define PERLNUM_DECIMAL 5 // 1-5 are numbers; 6-7 are strings
|
|
|
|
#define PERLNUM_VECTOR 6
|
|
|
|
#define PERLNUM_V_VECTOR 7
|
|
|
|
#define PERLNUM_BAD 8
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
#define BACK_NONE 0 // lookback state for bareword disambiguation:
|
|
|
|
#define BACK_OPERATOR 1 // whitespace/comments are insignificant
|
|
|
|
#define BACK_KEYWORD 2 // operators/keywords are needed for disambiguation
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
#define SUB_BEGIN 0 // states for subroutine prototype scan:
|
|
|
|
#define SUB_HAS_PROTO 1 // only 'prototype' attribute allows prototypes
|
|
|
|
#define SUB_HAS_ATTRIB 2 // other attributes can exist leftward
|
|
|
|
#define SUB_HAS_MODULE 3 // sub name can have a ::identifier part
|
|
|
|
#define SUB_HAS_SUB 4 // 'sub' keyword
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
// all interpolated styles are different from their parent styles by a constant difference
|
|
|
|
// we also assume SCE_PL_STRING_VAR is the interpolated style with the smallest value
|
|
|
|
#define INTERPOLATE_SHIFT (SCE_PL_STRING_VAR - SCE_PL_STRING)
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static bool isPerlKeyword(Sci_PositionU start, Sci_PositionU end, WordList &keywords, LexAccessor &styler) {
|
2009-04-25 23:38:15 +00:00
|
|
|
// old-style keyword matcher; needed because GetCurrent() needs
|
|
|
|
// current segment to be committed, but we may abandon early...
|
2009-04-24 23:35:41 +00:00
|
|
|
char s[100];
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU i, len = end - start;
|
2009-04-25 23:38:15 +00:00
|
|
|
if (len > 30) { len = 30; }
|
2009-04-24 23:35:41 +00:00
|
|
|
for (i = 0; i < len; i++, start++) s[i] = styler[start];
|
2009-04-25 23:38:15 +00:00
|
|
|
s[i] = '\0';
|
2009-04-24 23:35:41 +00:00
|
|
|
return keywords.InList(s);
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static int disambiguateBareword(LexAccessor &styler, Sci_PositionU bk, Sci_PositionU fw,
|
|
|
|
int backFlag, Sci_PositionU backPos, Sci_PositionU endPos) {
|
2009-04-25 23:38:15 +00:00
|
|
|
// identifiers are recognized by Perl as barewords under some
|
|
|
|
// conditions, the following attempts to do the disambiguation
|
|
|
|
// by looking backward and forward; result in 2 LSB
|
|
|
|
int result = 0;
|
|
|
|
bool moreback = false; // true if passed newline/comments
|
|
|
|
bool brace = false; // true if opening brace found
|
|
|
|
// if BACK_NONE, neither operator nor keyword, so skip test
|
|
|
|
if (backFlag == BACK_NONE)
|
|
|
|
return result;
|
|
|
|
// first look backwards past whitespace/comments to set EOL flag
|
|
|
|
// (some disambiguation patterns must be on a single line)
|
2019-05-04 18:14:48 +00:00
|
|
|
if (backPos <= static_cast<Sci_PositionU>(styler.LineStart(styler.GetLine(bk))))
|
2009-04-25 23:38:15 +00:00
|
|
|
moreback = true;
|
|
|
|
// look backwards at last significant lexed item for disambiguation
|
|
|
|
bk = backPos - 1;
|
|
|
|
int ch = static_cast<unsigned char>(styler.SafeGetCharAt(bk));
|
|
|
|
if (ch == '{' && !moreback) {
|
|
|
|
// {bareword: possible variable spec
|
|
|
|
brace = true;
|
|
|
|
} else if ((ch == '&' && styler.SafeGetCharAt(bk - 1) != '&')
|
2011-07-17 22:30:49 +00:00
|
|
|
// &bareword: subroutine call
|
|
|
|
|| styler.Match(bk - 1, "->")
|
|
|
|
// ->bareword: part of variable spec
|
2019-05-04 18:14:48 +00:00
|
|
|
|| styler.Match(bk - 1, "::")
|
|
|
|
// ::bareword: part of module spec
|
2011-07-17 22:30:49 +00:00
|
|
|
|| styler.Match(bk - 2, "sub")) {
|
2019-05-04 18:14:48 +00:00
|
|
|
// sub bareword: subroutine declaration
|
|
|
|
// (implied BACK_KEYWORD, no keywords end in 'sub'!)
|
2009-04-25 23:38:15 +00:00
|
|
|
result |= 1;
|
|
|
|
}
|
|
|
|
// next, scan forward after word past tab/spaces only;
|
|
|
|
// if ch isn't one of '[{(,' we can skip the test
|
|
|
|
if ((ch == '{' || ch == '(' || ch == '['|| ch == ',')
|
2011-07-17 22:30:49 +00:00
|
|
|
&& fw < endPos) {
|
2019-05-04 18:14:48 +00:00
|
|
|
while (IsASpaceOrTab(ch = static_cast<unsigned char>(styler.SafeGetCharAt(fw)))
|
|
|
|
&& fw < endPos) {
|
2009-04-25 23:38:15 +00:00
|
|
|
fw++;
|
|
|
|
}
|
|
|
|
if ((ch == '}' && brace)
|
2011-07-17 22:30:49 +00:00
|
|
|
// {bareword}: variable spec
|
|
|
|
|| styler.Match(fw, "=>")) {
|
2019-05-04 18:14:48 +00:00
|
|
|
// [{(, bareword=>: hash literal
|
2009-04-25 23:38:15 +00:00
|
|
|
result |= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static void skipWhitespaceComment(LexAccessor &styler, Sci_PositionU &p) {
|
2009-04-25 23:38:15 +00:00
|
|
|
// when backtracking, we need to skip whitespace and comments
|
2019-05-04 18:14:48 +00:00
|
|
|
while (p > 0) {
|
|
|
|
const int style = styler.StyleAt(p);
|
|
|
|
if (style != SCE_PL_DEFAULT && style != SCE_PL_COMMENTLINE)
|
|
|
|
break;
|
2009-04-25 23:38:15 +00:00
|
|
|
p--;
|
2019-05-04 18:14:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int findPrevLexeme(LexAccessor &styler, Sci_PositionU &bk, int &style) {
|
|
|
|
// scan backward past whitespace and comments to find a lexeme
|
|
|
|
skipWhitespaceComment(styler, bk);
|
|
|
|
if (bk == 0)
|
|
|
|
return 0;
|
|
|
|
int sz = 1;
|
|
|
|
style = styler.StyleAt(bk);
|
|
|
|
while (bk > 0) { // find extent of lexeme
|
|
|
|
if (styler.StyleAt(bk - 1) == style) {
|
|
|
|
bk--; sz++;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return sz;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static int styleBeforeBracePair(LexAccessor &styler, Sci_PositionU bk) {
|
2009-04-25 23:38:15 +00:00
|
|
|
// backtrack to find open '{' corresponding to a '}', balanced
|
|
|
|
// return significant style to be tested for '/' disambiguation
|
|
|
|
int braceCount = 1;
|
|
|
|
if (bk == 0)
|
|
|
|
return SCE_PL_DEFAULT;
|
|
|
|
while (--bk > 0) {
|
|
|
|
if (styler.StyleAt(bk) == SCE_PL_OPERATOR) {
|
|
|
|
int bkch = static_cast<unsigned char>(styler.SafeGetCharAt(bk));
|
|
|
|
if (bkch == ';') { // early out
|
|
|
|
break;
|
|
|
|
} else if (bkch == '}') {
|
|
|
|
braceCount++;
|
|
|
|
} else if (bkch == '{') {
|
|
|
|
if (--braceCount == 0) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bk > 0 && braceCount == 0) {
|
|
|
|
// balanced { found, bk > 0, skip more whitespace/comments
|
|
|
|
bk--;
|
|
|
|
skipWhitespaceComment(styler, bk);
|
|
|
|
return styler.StyleAt(bk);
|
|
|
|
}
|
|
|
|
return SCE_PL_DEFAULT;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static int styleCheckIdentifier(LexAccessor &styler, Sci_PositionU bk) {
|
2009-04-25 23:38:15 +00:00
|
|
|
// backtrack to classify sub-styles of identifier under test
|
|
|
|
// return sub-style to be tested for '/' disambiguation
|
|
|
|
if (styler.SafeGetCharAt(bk) == '>') // inputsymbol, like <foo>
|
|
|
|
return 1;
|
|
|
|
// backtrack to check for possible "->" or "::" before identifier
|
|
|
|
while (bk > 0 && styler.StyleAt(bk) == SCE_PL_IDENTIFIER) {
|
|
|
|
bk--;
|
|
|
|
}
|
|
|
|
while (bk > 0) {
|
|
|
|
int bkstyle = styler.StyleAt(bk);
|
|
|
|
if (bkstyle == SCE_PL_DEFAULT
|
2011-07-17 22:30:49 +00:00
|
|
|
|| bkstyle == SCE_PL_COMMENTLINE) {
|
2009-04-25 23:38:15 +00:00
|
|
|
// skip whitespace, comments
|
|
|
|
} else if (bkstyle == SCE_PL_OPERATOR) {
|
|
|
|
// test for "->" and "::"
|
|
|
|
if (styler.Match(bk - 1, "->") || styler.Match(bk - 1, "::"))
|
|
|
|
return 2;
|
|
|
|
} else
|
|
|
|
return 3; // bare identifier
|
|
|
|
bk--;
|
|
|
|
}
|
|
|
|
return 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static int podLineScan(LexAccessor &styler, Sci_PositionU &pos, Sci_PositionU endPos) {
|
2009-04-25 23:38:15 +00:00
|
|
|
// forward scan the current line to classify line for POD style
|
|
|
|
int state = -1;
|
2015-06-07 21:19:26 +00:00
|
|
|
while (pos < endPos) {
|
2009-04-25 23:38:15 +00:00
|
|
|
int ch = static_cast<unsigned char>(styler.SafeGetCharAt(pos));
|
2015-06-07 21:19:26 +00:00
|
|
|
if (ch == '\n' || ch == '\r') {
|
2009-04-25 23:38:15 +00:00
|
|
|
if (ch == '\r' && styler.SafeGetCharAt(pos + 1) == '\n') pos++;
|
|
|
|
break;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
if (IsASpaceOrTab(ch)) { // whitespace, take note
|
|
|
|
if (state == -1)
|
|
|
|
state = SCE_PL_DEFAULT;
|
|
|
|
} else if (state == SCE_PL_DEFAULT) { // verbatim POD line
|
|
|
|
state = SCE_PL_POD_VERB;
|
|
|
|
} else if (state != SCE_PL_POD_VERB) { // regular POD line
|
|
|
|
state = SCE_PL_POD;
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
if (state == -1)
|
|
|
|
state = SCE_PL_DEFAULT;
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static bool styleCheckSubPrototype(LexAccessor &styler, Sci_PositionU bk) {
|
2009-04-25 23:38:15 +00:00
|
|
|
// backtrack to identify if we're starting a subroutine prototype
|
2019-05-04 18:14:48 +00:00
|
|
|
// we also need to ignore whitespace/comments, format is like:
|
|
|
|
// sub abc::pqr :const :prototype(...)
|
|
|
|
// lexemes are tested in pairs, e.g. '::'+'pqr', ':'+'const', etc.
|
|
|
|
// and a state machine generates legal subroutine syntax matches
|
2009-04-25 23:38:15 +00:00
|
|
|
styler.Flush();
|
2019-05-04 18:14:48 +00:00
|
|
|
int state = SUB_BEGIN;
|
|
|
|
do {
|
|
|
|
// find two lexemes, lexeme 2 follows lexeme 1
|
|
|
|
int style2 = SCE_PL_DEFAULT;
|
|
|
|
Sci_PositionU pos2 = bk;
|
|
|
|
int len2 = findPrevLexeme(styler, pos2, style2);
|
|
|
|
int style1 = SCE_PL_DEFAULT;
|
|
|
|
Sci_PositionU pos1 = pos2;
|
|
|
|
if (pos1 > 0) pos1--;
|
|
|
|
int len1 = findPrevLexeme(styler, pos1, style1);
|
|
|
|
if (len1 == 0 || len2 == 0) // lexeme pair must exist
|
|
|
|
break;
|
|
|
|
|
|
|
|
// match parts of syntax, if invalid subroutine syntax, break off
|
|
|
|
if (style1 == SCE_PL_OPERATOR && len1 == 1 &&
|
|
|
|
styler.SafeGetCharAt(pos1) == ':') { // ':'
|
|
|
|
if (style2 == SCE_PL_IDENTIFIER || style2 == SCE_PL_WORD) {
|
|
|
|
if (len2 == 9 && styler.Match(pos2, "prototype")) { // ':' 'prototype'
|
|
|
|
if (state == SUB_BEGIN) {
|
|
|
|
state = SUB_HAS_PROTO;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
} else { // ':' <attribute>
|
|
|
|
if (state == SUB_HAS_PROTO || state == SUB_HAS_ATTRIB) {
|
|
|
|
state = SUB_HAS_ATTRIB;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
} else if (style1 == SCE_PL_OPERATOR && len1 == 2 &&
|
|
|
|
styler.Match(pos1, "::")) { // '::'
|
|
|
|
if (style2 == SCE_PL_IDENTIFIER) { // '::' <identifier>
|
|
|
|
state = SUB_HAS_MODULE;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
} else if (style1 == SCE_PL_WORD && len1 == 3 &&
|
|
|
|
styler.Match(pos1, "sub")) { // 'sub'
|
|
|
|
if (style2 == SCE_PL_IDENTIFIER) { // 'sub' <identifier>
|
|
|
|
state = SUB_HAS_SUB;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
bk = pos1; // set position for finding next lexeme pair
|
|
|
|
if (bk > 0) bk--;
|
|
|
|
} while (state != SUB_HAS_SUB);
|
|
|
|
return (state == SUB_HAS_SUB);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
static int actualNumStyle(int numberStyle) {
|
|
|
|
if (numberStyle == PERLNUM_VECTOR || numberStyle == PERLNUM_V_VECTOR) {
|
|
|
|
return SCE_PL_STRING;
|
|
|
|
} else if (numberStyle == PERLNUM_BAD) {
|
|
|
|
return SCE_PL_ERROR;
|
|
|
|
}
|
|
|
|
return SCE_PL_NUMBER;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int opposite(int ch) {
|
|
|
|
if (ch == '(') return ')';
|
|
|
|
if (ch == '[') return ']';
|
|
|
|
if (ch == '{') return '}';
|
|
|
|
if (ch == '<') return '>';
|
2009-04-24 23:35:41 +00:00
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static bool IsCommentLine(Sci_Position line, LexAccessor &styler) {
|
|
|
|
Sci_Position pos = styler.LineStart(line);
|
|
|
|
Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
|
|
|
|
for (Sci_Position i = pos; i < eol_pos; i++) {
|
2011-07-17 22:30:49 +00:00
|
|
|
char ch = styler[i];
|
|
|
|
int style = styler.StyleAt(i);
|
|
|
|
if (ch == '#' && style == SCE_PL_COMMENTLINE)
|
|
|
|
return true;
|
|
|
|
else if (!IsASpaceOrTab(ch))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static bool IsPackageLine(Sci_Position line, LexAccessor &styler) {
|
|
|
|
Sci_Position pos = styler.LineStart(line);
|
2011-07-17 22:30:49 +00:00
|
|
|
int style = styler.StyleAt(pos);
|
|
|
|
if (style == SCE_PL_WORD && styler.Match(pos, "package")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static int PodHeadingLevel(Sci_Position pos, LexAccessor &styler) {
|
2011-07-17 22:30:49 +00:00
|
|
|
int lvl = static_cast<unsigned char>(styler.SafeGetCharAt(pos + 5));
|
|
|
|
if (lvl >= '1' && lvl <= '4') {
|
|
|
|
return lvl - '0';
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// An individual named option for use in an OptionSet
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
// Options used for LexerPerl
|
|
|
|
struct OptionsPerl {
|
|
|
|
bool fold;
|
|
|
|
bool foldComment;
|
|
|
|
bool foldCompact;
|
|
|
|
// Custom folding of POD and packages
|
|
|
|
bool foldPOD; // fold.perl.pod
|
|
|
|
// Enable folding Pod blocks when using the Perl lexer.
|
|
|
|
bool foldPackage; // fold.perl.package
|
|
|
|
// Enable folding packages when using the Perl lexer.
|
|
|
|
|
|
|
|
bool foldCommentExplicit;
|
|
|
|
|
|
|
|
bool foldAtElse;
|
|
|
|
|
|
|
|
OptionsPerl() {
|
|
|
|
fold = false;
|
|
|
|
foldComment = false;
|
|
|
|
foldCompact = true;
|
|
|
|
foldPOD = true;
|
|
|
|
foldPackage = true;
|
|
|
|
foldCommentExplicit = true;
|
|
|
|
foldAtElse = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const perlWordListDesc[] = {
|
|
|
|
"Keywords",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
struct OptionSetPerl : public OptionSet<OptionsPerl> {
|
|
|
|
OptionSetPerl() {
|
|
|
|
DefineProperty("fold", &OptionsPerl::fold);
|
|
|
|
|
|
|
|
DefineProperty("fold.comment", &OptionsPerl::foldComment);
|
|
|
|
|
|
|
|
DefineProperty("fold.compact", &OptionsPerl::foldCompact);
|
|
|
|
|
|
|
|
DefineProperty("fold.perl.pod", &OptionsPerl::foldPOD,
|
|
|
|
"Set to 0 to disable folding Pod blocks when using the Perl lexer.");
|
|
|
|
|
|
|
|
DefineProperty("fold.perl.package", &OptionsPerl::foldPackage,
|
|
|
|
"Set to 0 to disable folding packages when using the Perl lexer.");
|
|
|
|
|
|
|
|
DefineProperty("fold.perl.comment.explicit", &OptionsPerl::foldCommentExplicit,
|
|
|
|
"Set to 0 to disable explicit folding.");
|
|
|
|
|
|
|
|
DefineProperty("fold.perl.at.else", &OptionsPerl::foldAtElse,
|
|
|
|
"This option enables Perl folding on a \"} else {\" line of an if statement.");
|
|
|
|
|
|
|
|
DefineWordListSets(perlWordListDesc);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
class LexerPerl : public DefaultLexer {
|
2013-08-28 00:44:27 +00:00
|
|
|
CharacterSet setWordStart;
|
|
|
|
CharacterSet setWord;
|
|
|
|
CharacterSet setSpecialVar;
|
|
|
|
CharacterSet setControlVar;
|
2011-07-17 22:30:49 +00:00
|
|
|
WordList keywords;
|
|
|
|
OptionsPerl options;
|
|
|
|
OptionSetPerl osPerl;
|
|
|
|
public:
|
2013-08-28 00:44:27 +00:00
|
|
|
LexerPerl() :
|
|
|
|
setWordStart(CharacterSet::setAlpha, "_", 0x80, true),
|
|
|
|
setWord(CharacterSet::setAlphaNum, "_", 0x80, true),
|
|
|
|
setSpecialVar(CharacterSet::setNone, "\"$;<>&`'+,./\\%:=~!?@[]"),
|
|
|
|
setControlVar(CharacterSet::setNone, "ACDEFHILMNOPRSTVWX") {
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
virtual ~LexerPerl() {
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
void SCI_METHOD Release() override {
|
2011-07-17 22:30:49 +00:00
|
|
|
delete this;
|
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
int SCI_METHOD Version() const override {
|
|
|
|
return lvRelease4;
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
const char *SCI_METHOD PropertyNames() override {
|
2011-07-17 22:30:49 +00:00
|
|
|
return osPerl.PropertyNames();
|
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
int SCI_METHOD PropertyType(const char *name) override {
|
2011-07-17 22:30:49 +00:00
|
|
|
return osPerl.PropertyType(name);
|
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
const char *SCI_METHOD DescribeProperty(const char *name) override {
|
2011-07-17 22:30:49 +00:00
|
|
|
return osPerl.DescribeProperty(name);
|
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override;
|
|
|
|
const char *SCI_METHOD DescribeWordListSets() override {
|
2011-07-17 22:30:49 +00:00
|
|
|
return osPerl.DescribeWordListSets();
|
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override;
|
|
|
|
void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
|
|
|
|
void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
|
2011-07-17 22:30:49 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
void *SCI_METHOD PrivateCall(int, void *) override {
|
2011-07-17 22:30:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static ILexer4 *LexerFactoryPerl() {
|
2011-07-17 22:30:49 +00:00
|
|
|
return new LexerPerl();
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
int InputSymbolScan(StyleContext &sc);
|
|
|
|
void InterpolateSegment(StyleContext &sc, int maxSeg, bool isPattern=false);
|
2011-07-17 22:30:49 +00:00
|
|
|
};
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position SCI_METHOD LexerPerl::PropertySet(const char *key, const char *val) {
|
2011-07-17 22:30:49 +00:00
|
|
|
if (osPerl.PropertySet(&options, key, val)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position SCI_METHOD LexerPerl::WordListSet(int n, const char *wl) {
|
2011-07-17 22:30:49 +00:00
|
|
|
WordList *wordListN = 0;
|
|
|
|
switch (n) {
|
|
|
|
case 0:
|
|
|
|
wordListN = &keywords;
|
|
|
|
break;
|
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position firstModification = -1;
|
2011-07-17 22:30:49 +00:00
|
|
|
if (wordListN) {
|
|
|
|
WordList wlNew;
|
|
|
|
wlNew.Set(wl);
|
|
|
|
if (*wordListN != wlNew) {
|
|
|
|
wordListN->Set(wl);
|
|
|
|
firstModification = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return firstModification;
|
|
|
|
}
|
|
|
|
|
2013-08-28 00:44:27 +00:00
|
|
|
int LexerPerl::InputSymbolScan(StyleContext &sc) {
|
|
|
|
// forward scan for matching > on same line; file handles
|
|
|
|
int c, sLen = 0;
|
|
|
|
while ((c = sc.GetRelativeCharacter(++sLen)) != 0) {
|
|
|
|
if (c == '\r' || c == '\n') {
|
|
|
|
return 0;
|
|
|
|
} else if (c == '>') {
|
|
|
|
if (sc.Match("<=>")) // '<=>' case
|
|
|
|
return 0;
|
|
|
|
return sLen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LexerPerl::InterpolateSegment(StyleContext &sc, int maxSeg, bool isPattern) {
|
|
|
|
// interpolate a segment (with no active backslashes or delimiters within)
|
|
|
|
// switch in or out of an interpolation style or continue current style
|
|
|
|
// commit variable patterns if found, trim segment, repeat until done
|
|
|
|
while (maxSeg > 0) {
|
|
|
|
bool isVar = false;
|
|
|
|
int sLen = 0;
|
|
|
|
if ((maxSeg > 1) && (sc.ch == '$' || sc.ch == '@')) {
|
|
|
|
// $#[$]*word [$@][$]*word (where word or {word} is always present)
|
|
|
|
bool braces = false;
|
|
|
|
sLen = 1;
|
|
|
|
if (sc.ch == '$' && sc.chNext == '#') { // starts with $#
|
|
|
|
sLen++;
|
|
|
|
}
|
|
|
|
while ((maxSeg > sLen) && (sc.GetRelativeCharacter(sLen) == '$')) // >0 $ dereference within
|
|
|
|
sLen++;
|
|
|
|
if ((maxSeg > sLen) && (sc.GetRelativeCharacter(sLen) == '{')) { // { start for {word}
|
|
|
|
sLen++;
|
|
|
|
braces = true;
|
|
|
|
}
|
|
|
|
if (maxSeg > sLen) {
|
|
|
|
int c = sc.GetRelativeCharacter(sLen);
|
|
|
|
if (setWordStart.Contains(c)) { // word (various)
|
|
|
|
sLen++;
|
|
|
|
isVar = true;
|
|
|
|
while (maxSeg > sLen) {
|
|
|
|
if (!setWord.Contains(sc.GetRelativeCharacter(sLen)))
|
|
|
|
break;
|
|
|
|
sLen++;
|
|
|
|
}
|
|
|
|
} else if (braces && IsADigit(c) && (sLen == 2)) { // digit for ${digit}
|
|
|
|
sLen++;
|
|
|
|
isVar = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (braces) {
|
|
|
|
if ((maxSeg > sLen) && (sc.GetRelativeCharacter(sLen) == '}')) { // } end for {word}
|
|
|
|
sLen++;
|
|
|
|
} else
|
|
|
|
isVar = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isVar && (maxSeg > 1)) { // $- or @-specific variable patterns
|
|
|
|
int c = sc.chNext;
|
|
|
|
if (sc.ch == '$') {
|
|
|
|
sLen = 1;
|
|
|
|
if (IsADigit(c)) { // $[0-9] and slurp trailing digits
|
|
|
|
sLen++;
|
|
|
|
isVar = true;
|
|
|
|
while ((maxSeg > sLen) && IsADigit(sc.GetRelativeCharacter(sLen)))
|
|
|
|
sLen++;
|
|
|
|
} else if (setSpecialVar.Contains(c)) { // $ special variables
|
|
|
|
sLen++;
|
|
|
|
isVar = true;
|
|
|
|
} else if (!isPattern && ((c == '(') || (c == ')') || (c == '|'))) { // $ additional
|
|
|
|
sLen++;
|
|
|
|
isVar = true;
|
|
|
|
} else if (c == '^') { // $^A control-char style
|
|
|
|
sLen++;
|
|
|
|
if ((maxSeg > sLen) && setControlVar.Contains(sc.GetRelativeCharacter(sLen))) {
|
|
|
|
sLen++;
|
|
|
|
isVar = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (sc.ch == '@') {
|
|
|
|
sLen = 1;
|
|
|
|
if (!isPattern && ((c == '+') || (c == '-'))) { // @ specials non-pattern
|
|
|
|
sLen++;
|
|
|
|
isVar = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isVar) { // commit as interpolated variable or normal character
|
|
|
|
if (sc.state < SCE_PL_STRING_VAR)
|
|
|
|
sc.SetState(sc.state + INTERPOLATE_SHIFT);
|
|
|
|
sc.Forward(sLen);
|
|
|
|
maxSeg -= sLen;
|
|
|
|
} else {
|
|
|
|
if (sc.state >= SCE_PL_STRING_VAR)
|
|
|
|
sc.SetState(sc.state - INTERPOLATE_SHIFT);
|
|
|
|
sc.Forward();
|
|
|
|
maxSeg--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sc.state >= SCE_PL_STRING_VAR)
|
|
|
|
sc.SetState(sc.state - INTERPOLATE_SHIFT);
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
void SCI_METHOD LexerPerl::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
|
2011-07-17 22:30:49 +00:00
|
|
|
LexAccessor styler(pAccess);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
// keywords that forces /PATTERN/ at all times; should track vim's behaviour
|
|
|
|
WordList reWords;
|
|
|
|
reWords.Set("elsif if split while");
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
// 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, "_");
|
2019-05-04 18:14:48 +00:00
|
|
|
CharacterSet setSubPrototype(CharacterSet::setNone, "\\[$@%&*+];_ \t");
|
|
|
|
CharacterSet setRepetition(CharacterSet::setDigits, ")\"'");
|
2009-04-25 23:38:15 +00:00
|
|
|
// 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
|
2009-04-24 23:35:41 +00:00
|
|
|
public:
|
2011-07-17 22:30:49 +00:00
|
|
|
int State;
|
|
|
|
// 0: '<<' encountered
|
|
|
|
// 1: collect the delimiter
|
|
|
|
// 2: here doc text (lines after the delimiter)
|
2009-04-25 23:38:15 +00:00
|
|
|
int Quote; // the char after '<<'
|
2009-04-24 23:35:41 +00:00
|
|
|
bool Quoted; // true if Quote in ('\'','"','`')
|
|
|
|
int DelimiterLength; // strlen(Delimiter)
|
2019-05-04 18:14:48 +00:00
|
|
|
char Delimiter[HERE_DELIM_MAX]; // the Delimiter
|
2009-04-24 23:35:41 +00:00
|
|
|
HereDocCls() {
|
|
|
|
State = 0;
|
2009-04-25 23:38:15 +00:00
|
|
|
Quote = 0;
|
|
|
|
Quoted = false;
|
2009-04-24 23:35:41 +00:00
|
|
|
DelimiterLength = 0;
|
|
|
|
Delimiter[0] = '\0';
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
void Append(int ch) {
|
|
|
|
Delimiter[DelimiterLength++] = static_cast<char>(ch);
|
|
|
|
Delimiter[DelimiterLength] = '\0';
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
~HereDocCls() {
|
|
|
|
}
|
|
|
|
};
|
2009-04-25 23:38:15 +00:00
|
|
|
HereDocCls HereDoc; // TODO: FIFO for stacked here-docs
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
class QuoteCls { // Class to manage quote pairs
|
2011-07-17 22:30:49 +00:00
|
|
|
public:
|
2009-04-25 23:38:15 +00:00
|
|
|
int Rep;
|
|
|
|
int Count;
|
|
|
|
int Up, Down;
|
2009-04-24 23:35:41 +00:00
|
|
|
QuoteCls() {
|
2013-08-28 00:44:27 +00:00
|
|
|
New(1);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
void New(int r = 1) {
|
2009-04-24 23:35:41 +00:00
|
|
|
Rep = r;
|
|
|
|
Count = 0;
|
|
|
|
Up = '\0';
|
|
|
|
Down = '\0';
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
void Open(int u) {
|
2009-04-24 23:35:41 +00:00
|
|
|
Count++;
|
|
|
|
Up = u;
|
|
|
|
Down = opposite(Up);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
QuoteCls Quote;
|
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
// additional state for number lexing
|
|
|
|
int numState = PERLNUM_DECIMAL;
|
2009-04-24 23:35:41 +00:00
|
|
|
int dotCount = 0;
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU endPos = startPos + length;
|
2009-04-25 23:38:15 +00:00
|
|
|
|
|
|
|
// 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
|
2013-08-28 00:44:27 +00:00
|
|
|
|| 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
|
2011-07-17 22:30:49 +00:00
|
|
|
) {
|
2013-08-28 00:44:27 +00:00
|
|
|
// backtrack through multiple styles to reach the delimiter start
|
2009-04-25 23:38:15 +00:00
|
|
|
int delim = (initStyle == SCE_PL_FORMAT) ? SCE_PL_FORMAT_IDENT:SCE_PL_HERE_DELIM;
|
|
|
|
while ((startPos > 1) && (styler.StyleAt(startPos) != delim)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
startPos--;
|
|
|
|
}
|
|
|
|
startPos = styler.LineStart(styler.GetLine(startPos));
|
2009-04-25 23:38:15 +00:00
|
|
|
initStyle = styler.StyleAt(startPos - 1);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
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
|
2011-07-17 22:30:49 +00:00
|
|
|
|| initStyle == SCE_PL_STRING_QW
|
2013-08-28 00:44:27 +00:00
|
|
|
|| initStyle == SCE_PL_XLAT
|
2011-07-17 22:30:49 +00:00
|
|
|
|| initStyle == SCE_PL_CHARACTER
|
|
|
|
|| initStyle == SCE_PL_NUMBER
|
|
|
|
|| initStyle == SCE_PL_IDENTIFIER
|
|
|
|
|| initStyle == SCE_PL_ERROR
|
|
|
|
|| initStyle == SCE_PL_SUB_PROTOTYPE
|
|
|
|
) {
|
2009-04-25 23:38:15 +00:00
|
|
|
while ((startPos > 1) && (styler.StyleAt(startPos - 1) == initStyle)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
startPos--;
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
initStyle = SCE_PL_DEFAULT;
|
|
|
|
} else if (initStyle == SCE_PL_POD
|
2011-07-17 22:30:49 +00:00
|
|
|
|| initStyle == SCE_PL_POD_VERB
|
|
|
|
) {
|
2015-06-07 21:19:26 +00:00
|
|
|
// POD backtracking finds preceding blank lines and goes back past them
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position ln = styler.GetLine(startPos);
|
2009-04-25 23:38:15 +00:00
|
|
|
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;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
// 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;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU backPos = startPos;
|
2009-04-25 23:38:15 +00:00
|
|
|
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++;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
StyleContext sc(startPos, endPos - startPos, initStyle, styler);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
for (; sc.More(); sc.Forward()) {
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
// Determine if the current state should terminate.
|
|
|
|
switch (sc.state) {
|
2011-07-17 22:30:49 +00:00
|
|
|
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
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_PL_DEFAULT);
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
|
|
|
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;
|
2009-04-25 23:38:15 +00:00
|
|
|
} else {
|
2011-07-17 22:30:49 +00:00
|
|
|
sc.SetState(SCE_PL_DEFAULT);
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
backFlag = BACK_KEYWORD;
|
|
|
|
backPos = sc.currentPos;
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
|
|
|
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.
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.Forward();
|
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
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);
|
2009-04-25 23:38:15 +00:00
|
|
|
break;
|
2011-07-17 22:30:49 +00:00
|
|
|
} 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;
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
// number then dot (go through)
|
2019-05-04 18:14:48 +00:00
|
|
|
} 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)
|
2011-07-17 22:30:49 +00:00
|
|
|
} else if (IsADigit(sc.chNext)) // vectors
|
2009-04-25 23:38:15 +00:00
|
|
|
break;
|
2011-07-17 22:30:49 +00:00
|
|
|
// 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();
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
break;
|
|
|
|
} else if (IsADigit(sc.ch))
|
|
|
|
break;
|
|
|
|
// number then word (go through)
|
|
|
|
} else if (numState == PERLNUM_HEX) {
|
2019-05-04 18:14:48 +00:00
|
|
|
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))
|
2011-07-17 22:30:49 +00:00
|
|
|
break;
|
2019-05-04 18:14:48 +00:00
|
|
|
// hex or hex float then word (go through)
|
2011-07-17 22:30:49 +00:00
|
|
|
} 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);
|
2009-04-25 23:38:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
// 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;
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
// mark invalid octal, binary numbers (go through)
|
|
|
|
numState = PERLNUM_BAD;
|
2009-04-25 23:38:15 +00:00
|
|
|
break;
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
|
|
|
// 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;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position ws_skip = 0;
|
2011-07-17 22:30:49 +00:00
|
|
|
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
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU i = sc.currentPos + 1;
|
2011-07-17 22:30:49 +00:00
|
|
|
while ((i < endPos) && IsASpaceOrTab(delim_ch)) {
|
|
|
|
i++;
|
|
|
|
delim_ch = static_cast<unsigned char>(styler.SafeGetCharAt(i));
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
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
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.ForwardSetState(SCE_PL_DEFAULT);
|
2011-07-17 22:30:49 +00:00
|
|
|
} else if (!sc.atLineEnd) {
|
|
|
|
if (sc.Match('\\', static_cast<char>(HereDoc.Quote))) { // escaped quote
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.Forward();
|
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
if (sc.ch != '\r') { // skip CR if CRLF
|
2015-06-07 21:19:26 +00:00
|
|
|
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++;
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
} else { // an unquoted here-doc delimiter, no extended charsets
|
2011-07-17 22:30:49 +00:00
|
|
|
if (setHereDocDelim.Contains(sc.ch)) {
|
|
|
|
HereDoc.Append(sc.ch);
|
|
|
|
} else {
|
|
|
|
sc.SetState(SCE_PL_DEFAULT);
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
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:
|
2013-08-28 00:44:27 +00:00
|
|
|
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
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.ForwardBytes(HereDoc.DelimiterLength);
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_PL_DEFAULT);
|
|
|
|
backFlag = BACK_NONE;
|
|
|
|
HereDoc.State = 0;
|
2013-08-28 00:44:27 +00:00
|
|
|
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();
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_PL_POD:
|
|
|
|
case SCE_PL_POD_VERB: {
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU fw = sc.currentPos;
|
|
|
|
Sci_Position ln = styler.GetLine(fw);
|
2009-04-25 23:38:15 +00:00
|
|
|
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) {
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU fw2 = fw;
|
2015-06-07 21:19:26 +00:00
|
|
|
while (fw2 < (endPos - 1) && pod == SCE_PL_DEFAULT) {
|
2009-04-25 23:38:15 +00:00
|
|
|
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;
|
2013-08-28 00:44:27 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
} else {
|
|
|
|
if (pod == SCE_PL_POD_VERB // still part of current paragraph
|
2011-07-17 22:30:49 +00:00
|
|
|
&& (styler.GetLineState(ln - 1) == SCE_PL_POD)) {
|
2009-04-25 23:38:15 +00:00
|
|
|
pod = SCE_PL_POD;
|
|
|
|
styler.SetLineState(ln, pod);
|
|
|
|
} else if (pod == SCE_PL_POD
|
2011-07-17 22:30:49 +00:00
|
|
|
&& (styler.GetLineState(ln - 1) == SCE_PL_POD_VERB)) {
|
2009-04-25 23:38:15 +00:00
|
|
|
pod = SCE_PL_POD_VERB;
|
|
|
|
styler.SetLineState(ln, pod);
|
|
|
|
}
|
|
|
|
sc.SetState(pod);
|
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.ForwardBytes(fw - sc.currentPos); // commit style
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
|
|
|
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);
|
2013-08-28 00:44:27 +00:00
|
|
|
} 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();
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_PL_REGSUBST:
|
2013-08-28 00:44:27 +00:00
|
|
|
case SCE_PL_XLAT:
|
2011-07-17 22:30:49 +00:00
|
|
|
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);
|
2013-08-28 00:44:27 +00:00
|
|
|
} 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
|
2015-06-07 21:19:26 +00:00
|
|
|
} else if (c == '#' && IsASpaceOrTab(sc.GetRelativeCharacter(sLen - 1))) {
|
2013-08-28 00:44:27 +00:00
|
|
|
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++;
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
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);
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
|
|
|
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);
|
2013-08-28 00:44:27 +00:00
|
|
|
} 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)
|
2019-05-04 18:14:48 +00:00
|
|
|
// Falls through.
|
2013-08-28 00:44:27 +00:00
|
|
|
default: // non-interpolated path
|
|
|
|
sc.Forward(sLen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (endType == 2) {
|
|
|
|
sc.Forward();
|
|
|
|
} else if (endType == 3)
|
2011-07-17 22:30:49 +00:00
|
|
|
sc.ForwardSetState(SCE_PL_DEFAULT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_PL_SUB_PROTOTYPE: {
|
2009-04-25 23:38:15 +00:00
|
|
|
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
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.ForwardBytes(i);
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.ForwardSetState(SCE_PL_DEFAULT);
|
|
|
|
} else {
|
|
|
|
// abandon prototype, restart from '('
|
|
|
|
sc.ChangeState(SCE_PL_OPERATOR);
|
|
|
|
sc.SetState(SCE_PL_DEFAULT);
|
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_PL_FORMAT: {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.Complete();
|
2013-08-28 00:44:27 +00:00
|
|
|
if (sc.Match('.')) {
|
|
|
|
sc.Forward();
|
|
|
|
if (sc.atLineEnd || ((sc.ch == '\r' && sc.chNext == '\n')))
|
|
|
|
sc.SetState(SCE_PL_DEFAULT);
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
while (!sc.atLineEnd)
|
|
|
|
sc.Forward();
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_PL_ERROR:
|
|
|
|
break;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
// Needed for specific continuation styles (one follows the other)
|
|
|
|
switch (sc.state) {
|
|
|
|
// continued from SCE_PL_WORD
|
2011-07-17 22:30:49 +00:00
|
|
|
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 {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.Forward();
|
2011-07-17 22:30:49 +00:00
|
|
|
} while (setFormat.Contains(sc.ch));
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
while (IsASpaceOrTab(sc.ch) && !sc.atLineEnd)
|
|
|
|
sc.Forward();
|
|
|
|
if (sc.ch == '=') {
|
|
|
|
sc.ForwardSetState(SCE_PL_DEFAULT);
|
|
|
|
HereDoc.State = 3;
|
2009-04-25 23:38:15 +00:00
|
|
|
} else {
|
2015-06-07 21:19:26 +00:00
|
|
|
// invalid identifier; inexact fallback, but hey
|
2011-07-17 22:30:49 +00:00
|
|
|
sc.ChangeState(SCE_PL_IDENTIFIER);
|
|
|
|
sc.SetState(SCE_PL_DEFAULT);
|
2009-04-25 23:38:15 +00:00
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
} else {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.ChangeState(SCE_PL_DEFAULT); // invalid identifier
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
|
|
|
backFlag = BACK_NONE;
|
|
|
|
break;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
// Must check end of HereDoc states here before default state is handled
|
|
|
|
if (HereDoc.State == 1 && sc.atLineEnd) {
|
2009-04-24 23:35:41 +00:00
|
|
|
// 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
|
2009-04-25 23:38:15 +00:00
|
|
|
int st_new = SCE_PL_HERE_QQ;
|
2009-04-24 23:35:41 +00:00
|
|
|
HereDoc.State = 2;
|
|
|
|
if (HereDoc.Quoted) {
|
2009-04-25 23:38:15 +00:00
|
|
|
if (sc.state == SCE_PL_HERE_DELIM) {
|
2009-04-24 23:35:41 +00:00
|
|
|
// Missing quote at end of string! We are stricter than perl.
|
|
|
|
// Colour here-doc anyway while marking this bit as an error.
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.ChangeState(SCE_PL_ERROR);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
switch (HereDoc.Quote) {
|
2011-07-17 22:30:49 +00:00
|
|
|
case '\'':
|
2015-06-07 21:19:26 +00:00
|
|
|
st_new = SCE_PL_HERE_Q;
|
2011-07-17 22:30:49 +00:00
|
|
|
break;
|
|
|
|
case '"' :
|
|
|
|
st_new = SCE_PL_HERE_QQ;
|
|
|
|
break;
|
|
|
|
case '`' :
|
|
|
|
st_new = SCE_PL_HERE_QX;
|
|
|
|
break;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-04-25 23:38:15 +00:00
|
|
|
if (HereDoc.Quote == '\\')
|
|
|
|
st_new = SCE_PL_HERE_Q;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(st_new);
|
|
|
|
}
|
|
|
|
if (HereDoc.State == 3 && sc.atLineEnd) {
|
|
|
|
// Start of format body.
|
|
|
|
HereDoc.State = 0;
|
|
|
|
sc.SetState(SCE_PL_FORMAT);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2009-04-25 23:38:15 +00:00
|
|
|
// Determine if a new state should be entered.
|
|
|
|
if (sc.state == SCE_PL_DEFAULT) {
|
|
|
|
if (IsADigit(sc.ch) ||
|
2011-07-17 22:30:49 +00:00
|
|
|
(IsADigit(sc.chNext) && (sc.ch == '.' || sc.ch == 'v'))) {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_PL_NUMBER);
|
|
|
|
backFlag = BACK_NONE;
|
2009-04-24 23:35:41 +00:00
|
|
|
numState = PERLNUM_DECIMAL;
|
|
|
|
dotCount = 0;
|
2009-04-25 23:38:15 +00:00
|
|
|
if (sc.ch == '0') { // hex,bin,octal
|
2013-08-28 00:44:27 +00:00
|
|
|
if (sc.chNext == 'x' || sc.chNext == 'X') {
|
2009-04-24 23:35:41 +00:00
|
|
|
numState = PERLNUM_HEX;
|
2013-08-28 00:44:27 +00:00
|
|
|
} else if (sc.chNext == 'b' || sc.chNext == 'B') {
|
2009-04-25 23:38:15 +00:00
|
|
|
numState = PERLNUM_BINARY;
|
|
|
|
} else if (IsADigit(sc.chNext)) {
|
|
|
|
numState = PERLNUM_OCTAL;
|
|
|
|
}
|
|
|
|
if (numState != PERLNUM_DECIMAL) {
|
|
|
|
sc.Forward();
|
|
|
|
}
|
|
|
|
} else if (sc.ch == 'v') { // vector
|
2009-04-24 23:35:41 +00:00
|
|
|
numState = PERLNUM_V_VECTOR;
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
} 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);
|
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU bk = sc.currentPos;
|
|
|
|
Sci_PositionU fw = sc.currentPos + 1;
|
2009-04-25 23:38:15 +00:00
|
|
|
// first check for possible quote-like delimiter
|
|
|
|
if (sc.ch == 's' && !setWord.Contains(sc.chNext)) {
|
|
|
|
sc.ChangeState(SCE_PL_REGSUBST);
|
2009-04-24 23:35:41 +00:00
|
|
|
Quote.New(2);
|
2009-04-25 23:38:15 +00:00
|
|
|
} 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)) {
|
2013-08-28 00:44:27 +00:00
|
|
|
sc.ChangeState(SCE_PL_XLAT);
|
2009-04-24 23:35:41 +00:00
|
|
|
Quote.New(2);
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (sc.Match('t', 'r') && !setWord.Contains(sc.GetRelative(2))) {
|
2013-08-28 00:44:27 +00:00
|
|
|
sc.ChangeState(SCE_PL_XLAT);
|
2009-04-24 23:35:41 +00:00
|
|
|
Quote.New(2);
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.Forward();
|
|
|
|
fw++;
|
|
|
|
} else if (sc.ch == 'q' && setQDelim.Contains(sc.chNext)
|
2011-07-17 22:30:49 +00:00
|
|
|
&& !setWord.Contains(sc.GetRelative(2))) {
|
|
|
|
if (sc.chNext == 'q') sc.ChangeState(SCE_PL_STRING_QQ);
|
2009-04-25 23:38:15 +00:00
|
|
|
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
|
2011-07-17 22:30:49 +00:00
|
|
|
!setWord.Contains(sc.chNext) ||
|
2019-05-04 18:14:48 +00:00
|
|
|
(setRepetition.Contains(sc.chPrev) && IsADigit(sc.chNext)))) {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.ChangeState(SCE_PL_OPERATOR);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
// 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);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
// 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);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
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);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_PL_CHARACTER);
|
|
|
|
Quote.New();
|
|
|
|
Quote.Open(sc.ch);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
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);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.Forward();
|
|
|
|
if (sc.Match('`', '`') || sc.Match(':', ':')) {
|
|
|
|
sc.Forward();
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
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) == ':') {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.ForwardBytes(2);
|
2009-04-25 23:38:15 +00:00
|
|
|
} 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)) {
|
2009-04-24 23:35:41 +00:00
|
|
|
// 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.
|
2009-04-25 23:38:15 +00:00
|
|
|
// A few symbols shares this code for disambiguation.
|
2009-04-24 23:35:41 +00:00
|
|
|
bool preferRE = false;
|
2009-04-25 23:38:15 +00:00
|
|
|
bool isHereDoc = sc.Match('<', '<');
|
|
|
|
bool hereDocSpace = false; // for: SCALAR [whitespace] '<<'
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU bk = (sc.currentPos > 0) ? sc.currentPos - 1: 0;
|
2009-08-23 02:24:48 +00:00
|
|
|
sc.Complete();
|
2009-04-24 23:35:41 +00:00
|
|
|
styler.Flush();
|
2009-04-25 23:38:15 +00:00
|
|
|
if (styler.StyleAt(bk) == SCE_PL_DEFAULT)
|
|
|
|
hereDocSpace = true;
|
|
|
|
skipWhitespaceComment(styler, bk);
|
2009-04-24 23:35:41 +00:00
|
|
|
if (bk == 0) {
|
2009-04-25 23:38:15 +00:00
|
|
|
// avoid backward scanning breakage
|
2009-04-24 23:35:41 +00:00
|
|
|
preferRE = true;
|
|
|
|
} else {
|
|
|
|
int bkstyle = styler.StyleAt(bk);
|
2009-04-25 23:38:15 +00:00
|
|
|
int bkch = static_cast<unsigned char>(styler.SafeGetCharAt(bk));
|
2011-07-17 22:30:49 +00:00
|
|
|
switch (bkstyle) {
|
2009-04-24 23:35:41 +00:00
|
|
|
case SCE_PL_OPERATOR:
|
|
|
|
preferRE = true;
|
|
|
|
if (bkch == ')' || bkch == ']') {
|
|
|
|
preferRE = false;
|
|
|
|
} else if (bkch == '}') {
|
2009-04-25 23:38:15 +00:00
|
|
|
// backtrack by counting balanced brace pairs
|
|
|
|
// needed to test for variables like ${}, @{} etc.
|
|
|
|
bkstyle = styleBeforeBracePair(styler, bk);
|
|
|
|
if (bkstyle == SCE_PL_SCALAR
|
2011-07-17 22:30:49 +00:00
|
|
|
|| bkstyle == SCE_PL_ARRAY
|
|
|
|
|| bkstyle == SCE_PL_HASH
|
|
|
|
|| bkstyle == SCE_PL_SYMBOLTABLE
|
|
|
|
|| bkstyle == SCE_PL_OPERATOR) {
|
2009-04-25 23:38:15 +00:00
|
|
|
preferRE = false;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (bkch == '+' || bkch == '-') {
|
|
|
|
if (bkch == static_cast<unsigned char>(styler.SafeGetCharAt(bk - 1))
|
2011-07-17 22:30:49 +00:00
|
|
|
&& bkch != static_cast<unsigned char>(styler.SafeGetCharAt(bk - 2)))
|
|
|
|
// exceptions for operators: unary suffixes ++, --
|
|
|
|
preferRE = false;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_PL_IDENTIFIER:
|
|
|
|
preferRE = true;
|
2009-04-25 23:38:15 +00:00
|
|
|
bkstyle = styleCheckIdentifier(styler, bk);
|
|
|
|
if ((bkstyle == 1) || (bkstyle == 2)) {
|
|
|
|
// inputsymbol or var with "->" or "::" before identifier
|
2009-04-24 23:35:41 +00:00
|
|
|
preferRE = false;
|
2009-04-25 23:38:15 +00:00
|
|
|
} 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;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2009-04-25 23:38:15 +00:00
|
|
|
case SCE_PL_SCALAR: // for $var<< case:
|
|
|
|
if (isHereDoc && hereDocSpace) // if SCALAR whitespace '<<', *always* a HERE doc
|
|
|
|
preferRE = true;
|
|
|
|
break;
|
2009-04-24 23:35:41 +00:00
|
|
|
case SCE_PL_WORD:
|
2009-04-25 23:38:15 +00:00
|
|
|
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
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU bkend = bk + 1;
|
2009-04-25 23:38:15 +00:00
|
|
|
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;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
break;
|
2011-07-17 22:30:49 +00:00
|
|
|
|
|
|
|
// other styles uses the default, preferRE=false
|
2009-04-24 23:35:41 +00:00
|
|
|
case SCE_PL_POD:
|
|
|
|
case SCE_PL_HERE_Q:
|
|
|
|
case SCE_PL_HERE_QQ:
|
|
|
|
case SCE_PL_HERE_QX:
|
|
|
|
preferRE = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
backFlag = BACK_NONE;
|
|
|
|
if (isHereDoc) { // handle '<<', HERE doc
|
2019-05-04 18:14:48 +00:00
|
|
|
if (sc.Match("<<>>")) { // double-diamond operator (5.22)
|
|
|
|
sc.SetState(SCE_PL_OPERATOR);
|
|
|
|
sc.Forward(3);
|
|
|
|
} else if (preferRE) {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_PL_HERE_DELIM);
|
|
|
|
HereDoc.State = 0;
|
|
|
|
} else { // << operator
|
|
|
|
sc.SetState(SCE_PL_OPERATOR);
|
|
|
|
sc.Forward();
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (sc.ch == '*') { // handle '*', typeglob
|
|
|
|
if (preferRE) {
|
|
|
|
sc.SetState(SCE_PL_SYMBOLTABLE);
|
|
|
|
if (sc.chNext == ':' && sc.GetRelative(2) == ':') {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.ForwardBytes(2);
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (sc.chNext == '{') {
|
|
|
|
sc.ForwardSetState(SCE_PL_OPERATOR);
|
|
|
|
} else {
|
|
|
|
sc.Forward();
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_PL_OPERATOR);
|
|
|
|
if (sc.chNext == '*') // exponentiation
|
|
|
|
sc.Forward();
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
} 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) == ':') {
|
2015-06-07 21:19:26 +00:00
|
|
|
sc.ForwardBytes(2);
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (sc.chNext == '{') {
|
|
|
|
sc.ForwardSetState(SCE_PL_OPERATOR);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.ChangeState(SCE_PL_OPERATOR);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
} else {
|
|
|
|
sc.SetState(SCE_PL_OPERATOR);
|
|
|
|
}
|
|
|
|
} else if (sc.ch == '<') { // handle '<', inputsymbol
|
|
|
|
if (preferRE) {
|
|
|
|
// forward scan
|
2013-08-28 00:44:27 +00:00
|
|
|
int i = InputSymbolScan(sc);
|
2009-04-25 23:38:15 +00:00
|
|
|
if (i > 0) {
|
|
|
|
sc.SetState(SCE_PL_IDENTIFIER);
|
|
|
|
sc.Forward(i);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_PL_OPERATOR);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_PL_OPERATOR);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
} 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();
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (sc.ch == '=' // POD
|
2011-07-17 22:30:49 +00:00
|
|
|
&& setPOD.Contains(sc.chNext)
|
|
|
|
&& sc.atLineStart) {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_PL_POD);
|
|
|
|
backFlag = BACK_NONE;
|
|
|
|
} else if (sc.ch == '-' && setWordStart.Contains(sc.chNext)) { // extended '-' cases
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU bk = sc.currentPos;
|
|
|
|
Sci_PositionU fw = 2;
|
2009-04-25 23:38:15 +00:00
|
|
|
if (setSingleCharOp.Contains(sc.chNext) && // file test operators
|
2011-07-17 22:30:49 +00:00
|
|
|
!setWord.Contains(sc.GetRelative(2))) {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_PL_WORD);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
2009-04-25 23:38:15 +00:00
|
|
|
// nominally a minus and bareword; find extent of bareword
|
|
|
|
while (setWord.Contains(sc.GetRelative(fw)))
|
|
|
|
fw++;
|
|
|
|
sc.SetState(SCE_PL_OPERATOR);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
// 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
|
2009-08-23 02:24:48 +00:00
|
|
|
sc.Complete();
|
2009-04-25 23:38:15 +00:00
|
|
|
if (styleCheckSubPrototype(styler, sc.currentPos - 1)) {
|
|
|
|
sc.SetState(SCE_PL_SUB_PROTOTYPE);
|
|
|
|
backFlag = BACK_NONE;
|
2009-04-24 23:35:41 +00:00
|
|
|
} else {
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.SetState(SCE_PL_OPERATOR);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
} else if (setPerlOperator.Contains(sc.ch)) { // operators
|
|
|
|
sc.SetState(SCE_PL_OPERATOR);
|
|
|
|
if (sc.Match('.', '.')) { // .. and ...
|
|
|
|
sc.Forward();
|
|
|
|
if (sc.chNext == '.') sc.Forward();
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
} 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();
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
sc.Complete();
|
2011-03-22 00:16:49 +00:00
|
|
|
if (sc.state == SCE_PL_HERE_Q
|
2011-07-17 22:30:49 +00:00
|
|
|
|| sc.state == SCE_PL_HERE_QQ
|
|
|
|
|| sc.state == SCE_PL_HERE_QX
|
|
|
|
|| sc.state == SCE_PL_FORMAT) {
|
2011-03-22 00:16:49 +00:00
|
|
|
styler.ChangeLexerState(sc.currentPos, styler.Length());
|
|
|
|
}
|
2011-07-17 22:30:49 +00:00
|
|
|
sc.Complete();
|
2010-08-21 23:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PERL_HEADFOLD_SHIFT 4
|
|
|
|
#define PERL_HEADFOLD_MASK 0xF0
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
void SCI_METHOD LexerPerl::Fold(Sci_PositionU startPos, Sci_Position length, int /* initStyle */, IDocument *pAccess) {
|
2009-06-24 19:09:31 +00:00
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
if (!options.fold)
|
|
|
|
return;
|
2009-06-24 19:09:31 +00:00
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
LexAccessor styler(pAccess);
|
2009-06-24 19:09:31 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU endPos = startPos + length;
|
2009-04-24 23:35:41 +00:00
|
|
|
int visibleChars = 0;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position lineCurrent = styler.GetLine(startPos);
|
2011-07-17 22:30:49 +00:00
|
|
|
|
|
|
|
// Backtrack to previous line in case need to fix its fold status
|
|
|
|
if (startPos > 0) {
|
|
|
|
if (lineCurrent > 0) {
|
|
|
|
lineCurrent--;
|
|
|
|
startPos = styler.LineStart(lineCurrent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
int levelPrev = SC_FOLDLEVELBASE;
|
|
|
|
if (lineCurrent > 0)
|
|
|
|
levelPrev = styler.LevelAt(lineCurrent - 1) >> 16;
|
|
|
|
int levelCurrent = levelPrev;
|
|
|
|
char chNext = styler[startPos];
|
|
|
|
char chPrev = styler.SafeGetCharAt(startPos - 1);
|
|
|
|
int styleNext = styler.StyleAt(startPos);
|
|
|
|
// Used at end of line to determine if the line was a package definition
|
|
|
|
bool isPackageLine = false;
|
2010-08-21 23:59:56 +00:00
|
|
|
int podHeading = 0;
|
2019-05-04 18:14:48 +00:00
|
|
|
for (Sci_PositionU i = startPos; i < endPos; i++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
char ch = chNext;
|
|
|
|
chNext = styler.SafeGetCharAt(i + 1);
|
|
|
|
int style = styleNext;
|
|
|
|
styleNext = styler.StyleAt(i + 1);
|
2011-07-17 22:30:49 +00:00
|
|
|
int stylePrevCh = (i) ? styler.StyleAt(i - 1):SCE_PL_DEFAULT;
|
2009-04-24 23:35:41 +00:00
|
|
|
bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
|
2009-04-25 23:38:15 +00:00
|
|
|
bool atLineStart = ((chPrev == '\r') || (chPrev == '\n')) || i == 0;
|
|
|
|
// Comment folding
|
2011-07-17 22:30:49 +00:00
|
|
|
if (options.foldComment && atEOL && IsCommentLine(lineCurrent, styler)) {
|
2009-04-25 23:38:15 +00:00
|
|
|
if (!IsCommentLine(lineCurrent - 1, styler)
|
2011-07-17 22:30:49 +00:00
|
|
|
&& IsCommentLine(lineCurrent + 1, styler))
|
2009-04-25 23:38:15 +00:00
|
|
|
levelCurrent++;
|
|
|
|
else if (IsCommentLine(lineCurrent - 1, styler)
|
2011-07-17 22:30:49 +00:00
|
|
|
&& !IsCommentLine(lineCurrent + 1, styler))
|
2009-04-25 23:38:15 +00:00
|
|
|
levelCurrent--;
|
|
|
|
}
|
2010-08-21 23:59:56 +00:00
|
|
|
// {} [] block folding
|
2009-04-25 23:38:15 +00:00
|
|
|
if (style == SCE_PL_OPERATOR) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (ch == '{') {
|
2011-07-17 22:30:49 +00:00
|
|
|
if (options.foldAtElse && levelCurrent < levelPrev)
|
|
|
|
--levelPrev;
|
2009-04-24 23:35:41 +00:00
|
|
|
levelCurrent++;
|
|
|
|
} else if (ch == '}') {
|
|
|
|
levelCurrent--;
|
|
|
|
}
|
2010-08-21 23:59:56 +00:00
|
|
|
if (ch == '[') {
|
2011-07-17 22:30:49 +00:00
|
|
|
if (options.foldAtElse && levelCurrent < levelPrev)
|
|
|
|
--levelPrev;
|
2010-08-21 23:59:56 +00:00
|
|
|
levelCurrent++;
|
|
|
|
} else if (ch == ']') {
|
|
|
|
levelCurrent--;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2010-08-21 23:59:56 +00:00
|
|
|
// POD folding
|
2011-07-17 22:30:49 +00:00
|
|
|
if (options.foldPOD && atLineStart) {
|
2009-04-24 23:35:41 +00:00
|
|
|
if (style == SCE_PL_POD) {
|
|
|
|
if (stylePrevCh != SCE_PL_POD && stylePrevCh != SCE_PL_POD_VERB)
|
|
|
|
levelCurrent++;
|
|
|
|
else if (styler.Match(i, "=cut"))
|
2010-08-21 23:59:56 +00:00
|
|
|
levelCurrent = (levelCurrent & ~PERL_HEADFOLD_MASK) - 1;
|
2009-04-24 23:35:41 +00:00
|
|
|
else if (styler.Match(i, "=head"))
|
2010-08-21 23:59:56 +00:00
|
|
|
podHeading = PodHeadingLevel(i, styler);
|
2009-04-24 23:35:41 +00:00
|
|
|
} else if (style == SCE_PL_DATASECTION) {
|
2015-06-07 21:19:26 +00:00
|
|
|
if (ch == '=' && IsASCII(chNext) && isalpha(chNext) && levelCurrent == SC_FOLDLEVELBASE)
|
2009-04-25 23:38:15 +00:00
|
|
|
levelCurrent++;
|
|
|
|
else if (styler.Match(i, "=cut") && levelCurrent > SC_FOLDLEVELBASE)
|
2010-08-21 23:59:56 +00:00
|
|
|
levelCurrent = (levelCurrent & ~PERL_HEADFOLD_MASK) - 1;
|
2009-04-25 23:38:15 +00:00
|
|
|
else if (styler.Match(i, "=head"))
|
2010-08-21 23:59:56 +00:00
|
|
|
podHeading = PodHeadingLevel(i, styler);
|
2009-04-25 23:38:15 +00:00
|
|
|
// if package used or unclosed brace, level > SC_FOLDLEVELBASE!
|
|
|
|
// reset needed as level test is vs. SC_FOLDLEVELBASE
|
2010-08-21 23:59:56 +00:00
|
|
|
else if (stylePrevCh != SCE_PL_DATASECTION)
|
2009-04-25 23:38:15 +00:00
|
|
|
levelCurrent = SC_FOLDLEVELBASE;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2010-08-21 23:59:56 +00:00
|
|
|
// package folding
|
2011-07-17 22:30:49 +00:00
|
|
|
if (options.foldPackage && atLineStart) {
|
2010-08-21 23:59:56 +00:00
|
|
|
if (IsPackageLine(lineCurrent, styler)
|
2011-07-17 22:30:49 +00:00
|
|
|
&& !IsPackageLine(lineCurrent + 1, styler))
|
2009-04-24 23:35:41 +00:00
|
|
|
isPackageLine = true;
|
|
|
|
}
|
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
//heredoc folding
|
|
|
|
switch (style) {
|
|
|
|
case SCE_PL_HERE_QQ :
|
|
|
|
case SCE_PL_HERE_Q :
|
|
|
|
case SCE_PL_HERE_QX :
|
|
|
|
switch (stylePrevCh) {
|
|
|
|
case SCE_PL_HERE_QQ :
|
|
|
|
case SCE_PL_HERE_Q :
|
|
|
|
case SCE_PL_HERE_QX :
|
|
|
|
//do nothing;
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
levelCurrent++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
switch (stylePrevCh) {
|
|
|
|
case SCE_PL_HERE_QQ :
|
|
|
|
case SCE_PL_HERE_Q :
|
|
|
|
case SCE_PL_HERE_QX :
|
|
|
|
levelCurrent--;
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
//do nothing;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//explicit folding
|
|
|
|
if (options.foldCommentExplicit && style == SCE_PL_COMMENTLINE && ch == '#') {
|
|
|
|
if (chNext == '{') {
|
|
|
|
levelCurrent++;
|
|
|
|
} else if (levelCurrent > SC_FOLDLEVELBASE && chNext == '}') {
|
|
|
|
levelCurrent--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
if (atEOL) {
|
|
|
|
int lev = levelPrev;
|
2010-08-21 23:59:56 +00:00
|
|
|
// POD headings occupy bits 7-4, leaving some breathing room for
|
|
|
|
// non-standard practice -- POD sections stuck in blocks, etc.
|
|
|
|
if (podHeading > 0) {
|
|
|
|
levelCurrent = (lev & ~PERL_HEADFOLD_MASK) | (podHeading << PERL_HEADFOLD_SHIFT);
|
|
|
|
lev = levelCurrent - 1;
|
2009-04-25 23:38:15 +00:00
|
|
|
lev |= SC_FOLDLEVELHEADERFLAG;
|
2010-08-21 23:59:56 +00:00
|
|
|
podHeading = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
// Check if line was a package declaration
|
|
|
|
// because packages need "special" treatment
|
|
|
|
if (isPackageLine) {
|
|
|
|
lev = SC_FOLDLEVELBASE | SC_FOLDLEVELHEADERFLAG;
|
|
|
|
levelCurrent = SC_FOLDLEVELBASE + 1;
|
|
|
|
isPackageLine = false;
|
|
|
|
}
|
2009-04-25 23:38:15 +00:00
|
|
|
lev |= levelCurrent << 16;
|
2011-07-17 22:30:49 +00:00
|
|
|
if (visibleChars == 0 && options.foldCompact)
|
2009-04-24 23:35:41 +00:00
|
|
|
lev |= SC_FOLDLEVELWHITEFLAG;
|
|
|
|
if ((levelCurrent > levelPrev) && (visibleChars > 0))
|
|
|
|
lev |= SC_FOLDLEVELHEADERFLAG;
|
|
|
|
if (lev != styler.LevelAt(lineCurrent)) {
|
|
|
|
styler.SetLevel(lineCurrent, lev);
|
|
|
|
}
|
|
|
|
lineCurrent++;
|
|
|
|
levelPrev = levelCurrent;
|
|
|
|
visibleChars = 0;
|
|
|
|
}
|
|
|
|
if (!isspacechar(ch))
|
|
|
|
visibleChars++;
|
|
|
|
chPrev = ch;
|
|
|
|
}
|
|
|
|
// Fill in the real level of the next line, keeping the current flags as they will be filled in later
|
|
|
|
int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
|
|
|
|
styler.SetLevel(lineCurrent, levelPrev | flagsNext);
|
|
|
|
}
|
|
|
|
|
2015-06-07 21:19:26 +00:00
|
|
|
LexerModule lmPerl(SCLEX_PERL, LexerPerl::LexerFactoryPerl, "perl", perlWordListDesc);
|