Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 116 additions & 116 deletions CindyScriptPygments.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
u = str
uchr = chr
else:
u = unicode
uchr = unichr
u = str
uchr = chr

def decompressUnicodeRanges(d, s, h):
utf16 = len(u"\U00012345") > 1
utf16 = len("\U00012345") > 1
j = 0
res = u"(?:[" if utf16 else u"["
res = "(?:[" if utf16 else "["
n = len(s)
i = 0
while i < n:
c = ord(s[i])
if (c >= 0xe800):
if utf16:
res += u"]|" + uchr(c - 0x1000) + u"["
res += "]|" + uchr(c - 0x1000) + "["
j = 0xdc00
else:
j = ((c - 0xe800) << 10) + 0x10000
Expand All @@ -37,26 +37,26 @@ def decompressUnicodeRanges(d, s, h):
j += d[ord(s[i]) - 32]
if j != fst:
if j != fst + 1:
res += u"-"
res += "-"
res += uchr(j)
i += 1
if utf16:
h = u''.join(c if c == '-' else uchr(ord(c) - 0x1000) for c in h)
res += u"]|[" + h + eval(r'u"][\udc00-\udfff])"')
h = ''.join(c if c == '-' else uchr(ord(c) - 0x1000) for c in h)
res += "]|[" + h + eval(r'u"][\udc00-\udfff])"')
else:
n = len(h)
i = 0
while i < n:
c = ord(h[i])
j = (c - 0xe800) << 10
res += uchr(j + 0x10000)
if i + 1 < n and h[i + 1] == u"-":
if i + 1 < n and h[i + 1] == "-":
i += 2
c = ord(h[i])
j = (c - 0xe800) << 10
res += u"-" + uchr(j + 0x103ff)
res += "-" + uchr(j + 0x103ff)
i += 1
res += u"]"
res += "]"
return res

# Regular expression matching letters (Unicode 8.0.0 category L)
Expand All @@ -71,27 +71,27 @@ def decompressUnicodeRanges(d, s, h):
107, 108, 116, 122, 130, 132, 134, 138, 160, 165, 185, 195, 196, 255, 268,
277, 310, 332, 339, 362, 365, 390, 449, 457, 470, 512, 513, 541, 568, 582,
619, 673, 726, 768, 820, 921, 991, 1164, 2684, 6581, 8453, 11171, 20949
], (u"T4(4I!)!'!&/ 0 \x96')2$+! !\x83$ %## !(! ! ; u \x86.\x88 =#!+YoA& 87C"
u"% } !5%+%) #!*! <0a,!4B1%'!&-'!1!$!33`HsG$!;!+.52'(#%#- & !$#$!*!9% 2%"
u"H''%#- & % % %B# !H *+ - & % $$!;!5%3!,(#%#- & % $$!X% 5!:! '$ #$% "
u"! %$%$ $)W!G( / 2$!> &%C( / . $$!L! %5%;( D#!*!* 4'&*$W + !#&]F %"
u"6&]% !#% !#!(# & ! !#% # %1!#$ !/#L!S( @?$\x817-!*''#$!$%+ ',6!:= !&!"
u"#7 \x90 ##& ! ##D ##B ##& ! ##9 K ##^Y2*V#'$\x9d#5 4&p((+, #2*2*2, 5OC"
u"!'!Uy.D !&_)0J<#$,E'4Q/1Pv!|8:&K<9%)E>@7 )@\x80# #$%1gT\x8e#'#=#'#( ! !"
u" ! 0#P & !$ &$##'',& &d!9!*,b!'!#. !$$(! ! ! # 1##&$'!G%\xa58 8 \x84("
u"#$%6= !&!#Q+!*/1& & & & & & & &t!\x97%E$&%'V( z #&D${:Aj2\x99\xa6q\xa9"
u"E\xa4UZ#\x8d$2)%-8*0#_J+#b#M#(S1 # /0O2[R'$! !6>)/A?+8<!*$ .)$ D3 (-"
u"/$!$[ !$%#$#! !4 #1+ 6'#'#'1& & 7 .)c0\xa86/'I\xa7\x93#\\N&6$&! . , $ !"
u" % % \x7fM\x92;R#Gh)d$ \x85=4(4,a$'#'#'# \ue800!) 4 : % 9#6@\x82\x94?$I"
u"IX*5 ((=)<#@'(\ue801!f~N.Of\x8f1-)(\ue802!'#! E %$!#/)/10^: %)-)4nQ(%T!"
u"5# AZ?$?C( >?G)-):9*\ue803!`KJ9J\ue804#PriL3>@7M$!6F2#/! !C* 3V& ! # "
u"9 .+8N(#%#- & % $$!;!6$\ue805eF-% !\x8987#=F-!k7x4\ue806\x87RB!\x95K"
u"\ue808!\xa2\ue809e\x8a\ue80d!8\ue811!\x9c\ue81a!\x9b+0c<;F*#BH&:\ue81b"
u"\xa0U,!l,\ue82c!%\ue82f!\"&,$++.\ue835!w m %#!#%## ) ! & S ##( & > # $ "
u"!$& \x91#3 3 0 3 0 3 0 3 0 3 (\ue83a!\x8b\ue83b\x98# A % !#! . # ! !(!'"
u"! ! ! % !#! ! ! ! ! % !## & # # ! . 5& $ 5\ue869!\x9f7\x8c\ue86d!"
u"\xa1,g\ue86e!<#\xa3\ue873!\x9e\ue87e!\x9a"
), u"\ue80c\ue840-\ue868\ue86a-\ue86c\ue86f-\ue872")
], ("T4(4I!)!'!&/ 0 \x96')2$+! !\x83$ %## !(! ! ; u \x86.\x88 =#!+YoA& 87C"
"% } !5%+%) #!*! <0a,!4B1%'!&-'!1!$!33`HsG$!;!+.52'(#%#- & !$#$!*!9% 2%"
"H''%#- & % % %B# !H *+ - & % $$!;!5%3!,(#%#- & % $$!X% 5!:! '$ #$% "
"! %$%$ $)W!G( / 2$!> &%C( / . $$!L! %5%;( D#!*!* 4'&*$W + !#&]F %"
"6&]% !#% !#!(# & ! !#% # %1!#$ !/#L!S( @?$\x817-!*''#$!$%+ ',6!:= !&!"
"#7 \x90 ##& ! ##D ##B ##& ! ##9 K ##^Y2*V#'$\x9d#5 4&p((+, #2*2*2, 5OC"
"!'!Uy.D !&_)0J<#$,E'4Q/1Pv!|8:&K<9%)E>@7 )@\x80# #$%1gT\x8e#'#=#'#( ! !"
" ! 0#P & !$ &$##'',& &d!9!*,b!'!#. !$$(! ! ! # 1##&$'!G%\xa58 8 \x84("
"#$%6= !&!#Q+!*/1& & & & & & & &t!\x97%E$&%'V( z #&D${:Aj2\x99\xa6q\xa9"
"E\xa4UZ#\x8d$2)%-8*0#_J+#b#M#(S1 # /0O2[R'$! !6>)/A?+8<!*$ .)$ D3 (-"
"/$!$[ !$%#$#! !4 #1+ 6'#'#'1& & 7 .)c0\xa86/'I\xa7\x93#\\N&6$&! . , $ !"
" % % \x7fM\x92;R#Gh)d$ \x85=4(4,a$'#'#'# \ue800!) 4 : % 9#6@\x82\x94?$I"
"IX*5 ((=)<#@'(\ue801!f~N.Of\x8f1-)(\ue802!'#! E %$!#/)/10^: %)-)4nQ(%T!"
"5# AZ?$?C( >?G)-):9*\ue803!`KJ9J\ue804#PriL3>@7M$!6F2#/! !C* 3V& ! # "
"9 .+8N(#%#- & % $$!;!6$\ue805eF-% !\x8987#=F-!k7x4\ue806\x87RB!\x95K"
"\ue808!\xa2\ue809e\x8a\ue80d!8\ue811!\x9c\ue81a!\x9b+0c<;F*#BH&:\ue81b"
"\xa0U,!l,\ue82c!%\ue82f!\"&,$++.\ue835!w m %#!#%## ) ! & S ##( & > # $ "
"!$& \x91#3 3 0 3 0 3 0 3 0 3 (\ue83a!\x8b\ue83b\x98# A % !#! . # ! !(!'"
"! ! ! % !#! ! ! ! ! % !## & # # ! . 5& $ 5\ue869!\x9f7\x8c\ue86d!"
"\xa1,g\ue86e!<#\xa3\ue873!\x9e\ue87e!\x9a"
), "\ue80c\ue840-\ue868\ue86a-\ue86c\ue86f-\ue872")

reName = (u(r"(?:#(?:[ \t]*[1-9])?|(?:'|L)(?:[ \t]*(?:[0-9']|L))*)")
.replace(u("L"), unicodeLetters))
Expand All @@ -100,77 +100,77 @@ def decompressUnicodeRanges(d, s, h):

# These must be sorted by decreasing string length, for longest match first
operators = [
u"~!=",
u"~<=",
u"~>=",
u":=_",
u"::=",
u"!=",
u"++",
u"--",
u"->",
u"..",
u":=",
u":>",
u"<:",
u"<=",
u"<>",
u"==",
u">=",
u"~<",
u"~=",
u"~>",
u"~~",
u"!",
u"%",
u"&",
u"*",
u"+",
u"-",
u".",
u"/",
u":",
u";",
u"<",
u"=",
u">",
u"^",
u"_",
u"|",
u"\u00ac", # ¬
u"\u00b0", # °
u"\u00b7", # ·
u"\u00d7", # ×
u"\u00f7", # ÷
u"\u2062", # invisible times
u"\u2192", # →
u"\u2208", # ∈
u"\u2209", # ∉
u"\u2212", # −
u"\u2215", # ∕
u"\u2216", # ∖
u"\u221a", # √
u"\u2227", # ∧
u"\u2228", # ∨
u"\u2229", # ∩
u"\u222a", # ∪
u"\u2236", # ∶
u"\u2248", # ≈
u"\u2249", # ≉
u"\u225f", # ≟
u"\u2260", # ≠
u"\u2264", # ≤
u"\u2265", # ≥
u"\u2266", # ≦
u"\u2267", # ≧
u"\u22c5", # ⋅
u"\u2a85", # ⪅
u"\u2a86", # ⪆
u"\u2a89", # ⪉
u"\u2a8a", # ⪊
"~!=",
"~<=",
"~>=",
":=_",
"::=",
"!=",
"++",
"--",
"->",
"..",
":=",
":>",
"<:",
"<=",
"<>",
"==",
">=",
"~<",
"~=",
"~>",
"~~",
"!",
"%",
"&",
"*",
"+",
"-",
".",
"/",
":",
";",
"<",
"=",
">",
"^",
"_",
"|",
"\u00ac", # ¬
"\u00b0", # °
"\u00b7", # ·
"\u00d7", # ×
"\u00f7", # ÷
"\u2062", # invisible times
"\u2192", # →
"\u2208", # ∈
"\u2209", # ∉
"\u2212", # −
"\u2215", # ∕
"\u2216", # ∖
"\u221a", # √
"\u2227", # ∧
"\u2228", # ∨
"\u2229", # ∩
"\u222a", # ∪
"\u2236", # ∶
"\u2248", # ≈
"\u2249", # ≉
"\u225f", # ≟
"\u2260", # ≠
"\u2264", # ≤
"\u2265", # ≥
"\u2266", # ≦
"\u2267", # ≧
"\u22c5", # ⋅
"\u2a85", # ⪅
"\u2a86", # ⪆
"\u2a89", # ⪉
"\u2a8a", # ⪊
]

reOps = u"|".join(map(re.escape, operators))
reOps = "|".join(map(re.escape, operators))

class CindyScriptLexer(RegexLexer):
name = "CindyScript"
Expand All @@ -179,22 +179,22 @@ class CindyScriptLexer(RegexLexer):
mimetypes = ["text/x-cindyscript"]
tokens = {
'root': [
(u'[ \t\n]+', Token.Text.Whitespace),
(u'//.*', Token.Comment.Single),
(u'/\\*', Token.Comment.Multiline, 'mlc'),
('[ \t\n]+', Token.Text.Whitespace),
('//.*', Token.Comment.Single),
('/\\*', Token.Comment.Multiline, 'mlc'),
(reNumber, Token.Number),
(reOps, Token.Operator),
(u(r'\,|\[|\]|\(|\)|\{|\}'), Token.Punctuation),
(u'(?:[₊₋][ \t]*)?[₀₁₂₃₄₅₆₇₈₉](?:[ \t]*[₀₁₂₃₄₅₆₇₈₉])*', Token.Number),
(u'(?:[⁺⁻][ \t]*)?[⁰¹²³⁴⁵⁶⁷⁸⁹](?:[ \t]*[⁰¹²³⁴⁵⁶⁷⁸⁹])*', Token.Number),
(reName + u'(?=\s*\()', Token.Name.Function),
('(?:[₊₋][ \t]*)?[₀₁₂₃₄₅₆₇₈₉](?:[ \t]*[₀₁₂₃₄₅₆₇₈₉])*', Token.Number),
('(?:[⁺⁻][ \t]*)?[⁰¹²³⁴⁵⁶⁷⁸⁹](?:[ \t]*[⁰¹²³⁴⁵⁶⁷⁸⁹])*', Token.Number),
(reName + '(?=\s*\()', Token.Name.Function),
(reName, Token.Name.Variable),
(u'"[^"]*"', Token.String.Double),
('"[^"]*"', Token.String.Double),
],
'mlc': [
(u'/\\*', Token.Comment.Multiline, '#push'),
(u'\\*/', Token.Comment.Multiline, '#pop'),
(u'(?:[^*/]|\\*(?!/)|/(?!\\*))+', Token.Comment.Multiline),
('/\\*', Token.Comment.Multiline, '#push'),
('\\*/', Token.Comment.Multiline, '#pop'),
('(?:[^*/]|\\*(?!/)|/(?!\\*))+', Token.Comment.Multiline),
],
}

Expand All @@ -209,14 +209,14 @@ class CindyJsHtmlLexer(RegexLexer):
flags = re.DOTALL
tokens = {
'root': [
(u'(.*<\\s*script[^>]*\\s'
u'type\\s*=\\s*["\']text/x-cindyscript[^a-z][^>]*>)'
u'(.*?)'
u'(<\\s*/\\s*script\\s*>)',
('(.*<\\s*script[^>]*\\s'
'type\\s*=\\s*["\']text/x-cindyscript[^a-z][^>]*>)'
'(.*?)'
'(<\\s*/\\s*script\\s*>)',
bygroups(using(HtmlLexer),
using(CindyScriptLexer),
using(HtmlLexer))
),
(u'.+', using(HtmlLexer))
('.+', using(HtmlLexer))
]
}
4 changes: 2 additions & 2 deletions tests/LexerBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
if sys.version_info[0] >= 3:
u = str
else:
u = unicode
u = str

class LexerBase(unittest.TestCase):

Expand All @@ -14,7 +14,7 @@ def lex(self, string, expected = None):
if expected is not None:
if len(expected) != len(toks):
print()
print(' | '.join(v for t, v in toks))
print((' | '.join(v for t, v in toks)))
for exp, act in zip(expected, toks):
if act is None:
pass
Expand Down
62 changes: 31 additions & 31 deletions tests/TestHtml.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,41 @@ class TestCindyJsHtmlLexer(LexerBase):
lexerClass = C.CindyJsHtmlLexer

def test_JSandCS(self):
self.lex(u'''
self.lex('''
<html><body data-foo= "bar"><script type="text/javascript">
if(false)foo();
</script><script id="xy" type="text/x-cindyscript">
if(false,foo())
</script></body></html>''',
[
u'<', u'html', u'>',
u'<', u'body', u' ', u'data-foo', u'=', u' ', u'"bar"', u'>',
u'<', (T.Name.Tag, u'script'), u' ', u'type', u'=',
(T.String, u'"text/javascript"'), u'>', u'', u'\n',
(T.Keyword, u'if'),
(T.Punctuation, u'('),
(T.Keyword, u'false'),
(T.Punctuation, u')'),
(T.Name, u'foo'),
(T.Punctuation, u'('),
(T.Punctuation, u')'),
(T.Punctuation, u';'),
(T.Text, u'\n'),
u'<', u'/', (T.Name.Tag, u'script'), u'>',
u'<', u'script', u' ', u'id', u'=', u'"xy"',
u' ', (T.Name.Attribute, u'type'), u'=',
(T.String, u'"text/x-cindyscript"'), u'>', u'\n',
(T.Name.Function, u'if'),
(T.Punctuation, u'('),
(T.Name.Variable, u'false'),
(T.Punctuation, u','),
(T.Name.Function, u'foo'),
(T.Punctuation, u'('),
(T.Punctuation, u')'),
(T.Punctuation, u')'),
(T.Text, u'\n'),
u'<', u'/', (T.Name.Tag, u'script'), u'>',
u'<', u'/', u'body', u'>',
u'<', u'/', (T.Name.Tag, u'html'), u'>',
u'\n',
'<', 'html', '>',
'<', 'body', ' ', 'data-foo', '=', ' ', '"bar"', '>',
'<', (T.Name.Tag, 'script'), ' ', 'type', '=',
(T.String, '"text/javascript"'), '>', '', '\n',
(T.Keyword, 'if'),
(T.Punctuation, '('),
(T.Keyword, 'false'),
(T.Punctuation, ')'),
(T.Name, 'foo'),
(T.Punctuation, '('),
(T.Punctuation, ')'),
(T.Punctuation, ';'),
(T.Text, '\n'),
'<', '/', (T.Name.Tag, 'script'), '>',
'<', 'script', ' ', 'id', '=', '"xy"',
' ', (T.Name.Attribute, 'type'), '=',
(T.String, '"text/x-cindyscript"'), '>', '\n',
(T.Name.Function, 'if'),
(T.Punctuation, '('),
(T.Name.Variable, 'false'),
(T.Punctuation, ','),
(T.Name.Function, 'foo'),
(T.Punctuation, '('),
(T.Punctuation, ')'),
(T.Punctuation, ')'),
(T.Text, '\n'),
'<', '/', (T.Name.Tag, 'script'), '>',
'<', '/', 'body', '>',
'<', '/', (T.Name.Tag, 'html'), '>',
'\n',
])
Loading