1 # -*- coding: utf-8 -*-
3 Standard ML Lexer for Pygments.
8 from pygments
.lexer
import RegexLexer
, bygroups
9 from pygments
.token
import *
12 __all__
= ['StandardMLLexer']
15 class StandardMLLexer(RegexLexer
):
21 filenames
= ['*.sml','*.sig','*.fun','*.ML']
22 mimetypes
= ['text/x-standardml', 'application/x-standardml']
24 flags
= re
.DOTALL | re
.MULTILINE
26 alphanumid_reserved
= [
28 'abstype', 'and', 'andalso', 'as', 'case', 'do', 'datatype', 'else',
29 'end', 'exception', 'fn', 'fun', 'handle', 'if', 'in', 'infix',
30 'infixr', 'let', 'local', 'nonfix', 'of', 'op', 'open', 'orelse',
31 'raise', 'rec', 'then', 'type', 'val', 'with', 'withtype', 'while',
33 'eqtype', 'functor', 'include', 'sharing', 'sig',
34 'signature', 'struct', 'structure', 'where'
36 symbolicid_reserved
= [
38 ':', '|', '=', '=>', '->', '#',
44 '(', ')', '[', ']', '{', '}', ',', ';', '...', '_'
48 alphanumid_re
= r
"[a-zA-Z][a-zA-Z0-9_']*"
49 symbolicid_re
= r
"[!%&$#+\-/:<=>?@\\~`^|*]+"
50 long_id_re
= r
"((%s\.)*)((%s)|(%s))" % (alphanumid_re
, alphanumid_re
, symbolicid_re
)
51 primed_alphanumid_re
= r
"'[a-zA-Z0-9_']*"
53 def long_id_callback(self
, match
):
54 strids
= match
.group(1)
56 for m
in re
.finditer(r
'(%s)(\.)' % self
.alphanumid_re
, strids
) :
58 if strid
in self
.alphanumid_reserved
:
62 yield pos
, token
, strid
65 yield pos
, Punctuation
, dot
69 if nqid
in self
.alphanumid_reserved
:
71 elif nqid
in self
.symbolicid_reserved
:
76 if nqid
in self
.alphanumid_reserved
:
78 elif nqid
in self
.symbolicid_reserved
:
82 yield pos
, token
, nqid
85 printable_re
= r
'[^\x00-\x1F"\\\x7F]'
86 escape_re
= r
'\\("|\\|a|b|t|n|v|f|r|^[@-_]|[0-9]{3}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8})'
91 (r
'\(\*', Comment
.Multiline
, 'comment'),
93 (r
'~?[0-9]+\.[0-9]+((e|E)~?[0-9]+)?', Number
.Float
),
94 (r
'~?[0-9]+(e|E)~?[0-9]+', Number
.Float
),
95 (r
'0wx[0-9a-fA-F]+', Number
.Hex
),
96 (r
'~?0x[0-9a-fA-F]+', Number
.Hex
),
97 (r
'0w[0-9]+', Number
.Integer
),
98 (r
'~?[0-9]+', Number
.Integer
),
100 (r
'"', String
, 'string'),
101 (r
'(#)(")', bygroups(Punctuation
, String
), 'string'),
103 (long_id_re
, long_id_callback
),
104 (r
'(%s)' % '|'.join([re
.escape(z
) for z
in nonid_reserved
]), Punctuation
),
105 (primed_alphanumid_re
, Name
),
107 (r
'.', Error
, 'error')
113 (r
'\(\*', Comment
.Multiline
, '#push'),
114 (r
'\*\)', Comment
.Multiline
, '#pop'),
115 (r
'.', Comment
.Multiline
),
118 (printable_re
, String
),
119 (escape_re
, String
.Escape
),
120 (r
'\\\s', String
, 'gap'),
121 (r
'"', String
, '#pop'),
126 (r
'\\', String
, '#pop'),