1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# -*- coding: utf-8 -*-
"""
pygments.lexers.lisp
~~~~~~~~~~~~~~~~~~~~
Lexers for Lispy languages.
:copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
import re
from pygments.lexer import RegexLexer, include, bygroups
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
Number, Punctuation
from pygments.lexers.python import PythonLexer
__all__ = ['HyLexer']
class HyLexer(RegexLexer):
"""
Lexer for `Hy <http://hylang.org/>`_ source code.
.. versionadded:: 2.0
"""
name = 'Hy'
aliases = ['hylang']
filenames = ['*.hy']
mimetypes = ['text/x-hy', 'application/x-hy']
special_forms = [
'cond', 'for', '->', '->>', 'car',
'cdr', 'first', 'rest', 'let', 'when', 'unless',
'import', 'do', 'progn', 'get', 'slice', 'assoc', 'with-decorator',
',', 'list_comp', 'kwapply', '~', 'is', 'in', 'is-not', 'not-in',
'quasiquote', 'unquote', 'unquote-splice', 'quote', '|', '<<=', '>>=',
'foreach', 'while',
'eval-and-compile', 'eval-when-compile'
]
declarations = [
'def', 'defn', 'defun', 'defmacro', 'defclass', 'lambda', 'fn', 'setv'
]
hy_builtins = []
hy_core = [
'cycle', 'dec', 'distinct', 'drop', 'even?', 'filter', 'inc',
'instance?', 'iterable?', 'iterate', 'iterator?', 'neg?',
'none?', 'nth', 'numeric?', 'odd?', 'pos?', 'remove', 'repeat',
'repeatedly', 'take', 'take_nth', 'take_while', 'zero?'
]
builtins = hy_builtins + hy_core
# valid names for identifiers
# well, names can only not consist fully of numbers
# but this should be good enough for now
valid_name = r'(?!#)[\w!$%*+<=>?/.#-]+'
def _multi_escape(entries):
return '(%s)' % ('|'.join(re.escape(entry) + ' ' for entry in entries))
tokens = {
'root': [
# the comments - always starting with semicolon
# and going to the end of the line
(r';.*$', Comment.Single),
# whitespaces - usually not relevant
(r'[,\s]+', Text),
# numbers
(r'-?\d+\.\d+', Number.Float),
(r'-?\d+', Number.Integer),
(r'0[0-7]+j?', Number.Oct),
(r'0[xX][a-fA-F0-9]+', Number.Hex),
# strings, symbols and characters
(r'"(\\\\|\\"|[^"])*"', String),
(r"'" + valid_name, String.Symbol),
(r"\\(.|[a-z]+)", String.Char),
(r'^(\s*)([rRuU]{,2}"""(?:.|\n)*?""")', bygroups(Text, String.Doc)),
(r"^(\s*)([rRuU]{,2}'''(?:.|\n)*?''')", bygroups(Text, String.Doc)),
# keywords
(r'::?' + valid_name, String.Symbol),
# special operators
(r'~@|[`\'#^~&@]', Operator),
include('py-keywords'),
include('py-builtins'),
# highlight the special forms
(_multi_escape(special_forms), Keyword),
# Technically, only the special forms are 'keywords'. The problem
# is that only treating them as keywords means that things like
# 'defn' and 'ns' need to be highlighted as builtins. This is ugly
# and weird for most styles. So, as a compromise we're going to
# highlight them as Keyword.Declarations.
(_multi_escape(declarations), Keyword.Declaration),
# highlight the builtins
(_multi_escape(builtins), Name.Builtin),
# the remaining functions
(r'(?<=\()' + valid_name, Name.Function),
# find the remaining variables
(valid_name, Name.Variable),
# Hy accepts vector notation
(r'(\[|\])', Punctuation),
# Hy accepts map notation
(r'(\{|\})', Punctuation),
# the famous parentheses!
(r'(\(|\))', Punctuation),
],
'py-keywords': PythonLexer.tokens['keywords'],
'py-builtins': PythonLexer.tokens['builtins'],
}
def analyse_text(text):
if '(import ' in text or '(defn ' in text:
return 0.9
|