A lexer is an object governing the styling of the various elements of the source code. In other words it is the “syntax highlighting” that discover elements in the text and applies styles.

The Lexer classes simply encapsulates various style elements [1] for example:

Take the LexerPython, it has various attributes... it can recognize in the document the “commentline”, “commands”, “number”, and apply styles to them. In the attribute is defined the style.

>>> lp = LexerPython()
>>> lp.comment
< TextStyle ... >

To set a lexer:

>>> s = Scintilla()
>>> s.set_lexer("python")
>>> s.set_lexer(LexerPython()) # alternative

To customize a lexer with styles that you want you have to subclass it in this manner:

class customLexer(LexerPython):
    def __init__(self):
        self.comment = TextStyle(color = "blue", bold = True, italic = True)
        self.number = TextStyle(color = "green")

and then you have to apply the lexer:

>>> s.set_lexer(customLexer())


The current implementation has hardcoded lexer definition, in a future release will be introduced config files ( in .ini style or in xml).

To retrieve the possible elements to style you can import the lexer and print the dir() return value... Excluding the “id”,”style_ids”,”keywords” attributes.

For example:

>>> from gtkscintilla.lexer import LexerPython
>>> l = dir(LexerPython)
>>> filter(lambda x: not x[0]=='_',l) # I've filtered the attributes starting with "_"
['bracebad', 'bracelight', 'character', 'classname', 'commentblock',
'commentline', 'decorator', 'default', 'defname', 'id', 'identifier',
'keywords', 'number', 'operator', 'string', 'stringeol', 'style_ids',
'triple', 'tripledouble', 'word', 'word2']

Here the complete list of lexers with the associated language [2] :

Name Lexer class
abaqus LexerAbaqus
ada LexerAda
asm LexerAsm
asn1 LexerAsn1
au3 LexerAu3
baan LexerBaan
bash LexerBash
batch LexerBatch
bullant LexerBullant
caml LexerCaml
cmake LexerCmake
conf LexerConf
cpp LexerCpp
csound LexerCsound
css LexerCss
d LexerD
diff LexerDiff
eiffel LexerEiffel
eiffelkw LexerEiffelkw
erlang LexerErlang
errorlist LexerErrorlist
escript LexerEscript
f77 LexerF77
flagship LexerFlagship
forth LexerForth
fortran LexerFortran
gap LexerGap
inno LexerInno
kix LexerKix
latex LexerLatex
lisp LexerLisp
lot LexerLot
lout LexerLout
lua LexerLua
makefile LexerMakefile
matlab LexerMatlab
metapost LexerMetapost
mmixal LexerMmixal
nimrod LexerNimrod
nncrontab LexerNncrontab
nsis LexerNsis
octave LexerOctave
opal LexerOpal
pascal LexerPascal
perl LexerPerl
pov LexerPov
powerpro LexerPowerpro
powershell LexerPowershell
ps LexerPs
python LexerPython
r LexerR
rebol LexerRebol
ruby LexerRuby
smalltalk LexerSmalltalk
specman LexerSpecman
spice LexerSpice
sql LexerSql
tcl LexerTcl
tex LexerTex
vb LexerVb
vbscript LexerVbscript
verilog LexerVerilog
vhdl LexerVhdl
xml LexerXml
yaml LexerYaml
[1]Lexer class is a mere data container, the styles are applied with the set_lexer() method
[2]Note that this list is generated, I haven’t found a good way to retrieve the “human representation” of the language name, please use some fantasy.

Previous topic

Searching and replacing

Next topic


This Page