| 1 |
|
| 2 |
import sys |
| 3 |
from os.path import join, dirname |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
root = join(dirname(__file__), "..") |
| 13 |
sys.path.insert(0, root) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
from alectryon.cli import main |
| 18 |
from alectryon.serapi import SerAPI |
| 19 |
|
| 20 |
SerAPI.DEFAULT_PP_ARGS['pp_margin'] = 55 |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
from pygments.lexer import RegexLexer, default, words, bygroups, include, inherit |
| 25 |
from pygments.regexopt import regex_opt, regex_opt_inner |
| 26 |
from pygments.token import \ |
| 27 |
Text, Comment, Operator, Keyword, Name, String, Number, Punctuation |
| 28 |
|
| 29 |
class ElpiLexer(RegexLexer): |
| 30 |
""" |
| 31 |
Lexer for the Elpi programming language. |
| 32 |
""" |
| 33 |
|
| 34 |
name = 'Elpi' |
| 35 |
url = 'http://github.com/LPCIC/elpi' |
| 36 |
aliases = ['elpi'] |
| 37 |
filenames = ['*.elpi'] |
| 38 |
mimetypes = ['text/x-elpi'] |
| 39 |
version_added = '2.11' |
| 40 |
|
| 41 |
lcase_re = r"[a-z]" |
| 42 |
ucase_re = r"[A-Z]" |
| 43 |
digit_re = r"[0-9]" |
| 44 |
schar2_re = r"([+*^?/<>`'@#~=&!])" |
| 45 |
schar_re = rf"({schar2_re}|-|\$|_)" |
| 46 |
idchar_re = rf"({lcase_re}|{ucase_re}|{digit_re}|{schar_re})" |
| 47 |
idcharstarns_re = rf"({idchar_re}*(\.({lcase_re}|{ucase_re}){idchar_re}*)*)" |
| 48 |
symbchar_re = rf"({lcase_re}|{ucase_re}|{digit_re}|{schar_re}|:)" |
| 49 |
constant_re = rf"({ucase_re}{idchar_re}*|{lcase_re}{idcharstarns_re}|{schar2_re}{symbchar_re}*|_{idchar_re}+)" |
| 50 |
symbol_re = r"(,|<=>|-->|:-|:>|;|\?-|->|&|=>|\bas\b|\buvar\b|<|=<|=|==|>=|>|\bi<|\bi=<|\bi>=|\bi>|\bis\b|\br<|\br=<|\br>=|\br>|\bs<|\bs=<|\bs>=|\bs>|@|::|\[\]|`->|`:|`:=|\^|-|\+|\bi-|\bi\+|r-|r\+|/|\*|\bdiv\b|\bi\*|\bmod\b|\br\*|~|\bi~|\br~)" |
| 51 |
const_sym_re = rf"({constant_re}|{symbol_re}|\({symbol_re}\))" |
| 52 |
|
| 53 |
tokens = { |
| 54 |
'root': [ |
| 55 |
include('elpi') |
| 56 |
], |
| 57 |
|
| 58 |
'elpi': [ |
| 59 |
include('_elpi-comment'), |
| 60 |
|
| 61 |
(r"(:before|:after|:if|:name)(\s*)(\")", |
| 62 |
bygroups(Keyword.ElpiMode, Text.Whitespace, String.Double), |
| 63 |
'elpi-string'), |
| 64 |
(r"(:functional)", |
| 65 |
bygroups(Keyword.ElpiMode)), |
| 66 |
(r"(:index)(\s*)(\()", bygroups(Keyword.ElpiMode, Text.Whitespace, Punctuation), |
| 67 |
'elpi-indexing-expr'), |
| 68 |
(rf"\b(external pred|pred)(\s+)({const_sym_re})", |
| 69 |
bygroups(Keyword.ElpiKeyword, Text.Whitespace, Name.ElpiFunction), |
| 70 |
'elpi-pred-item'), |
| 71 |
(rf"\b(func)(\s+)({const_sym_re})", |
| 72 |
bygroups(Keyword.ElpiKeyword, Text.Whitespace, Name.ElpiFunction), |
| 73 |
'elpi-func-item'), |
| 74 |
(rf"\b(external type|type)(\s+)(({const_sym_re}(,\s*)?)+)", |
| 75 |
bygroups(Keyword.ElpiKeyword, Text.Whitespace, Name.ElpiFunction), |
| 76 |
'elpi-type'), |
| 77 |
(rf"\b(kind)(\s+)(({const_sym_re}|,)+)", |
| 78 |
bygroups(Keyword.ElpiKeyword, Text.Whitespace, Name.ElpiFunction), |
| 79 |
'elpi-type'), |
| 80 |
(rf"\b(typeabbrev)(\s+)({const_sym_re})", |
| 81 |
bygroups(Keyword.ElpiKeyword, Text.Whitespace, Name.ElpiFunction), |
| 82 |
'elpi-type'), |
| 83 |
(r"\b(typeabbrev)(\s+)(\([^)]+\))", |
| 84 |
bygroups(Keyword.ElpiKeyword, Text.Whitespace, Name.ElpiFunction), |
| 85 |
'elpi-type'), |
| 86 |
(r"\b(accumulate)(\s+)(\")", |
| 87 |
bygroups(Keyword.ElpiKeyword, Text.Whitespace, String.Double), |
| 88 |
'elpi-string'), |
| 89 |
(rf"\b(accumulate|namespace|local)(\s+)({constant_re})", |
| 90 |
bygroups(Keyword.ElpiKeyword, Text.Whitespace, Text)), |
| 91 |
(rf"\b(shorten)(\s+)({constant_re}\.)", |
| 92 |
bygroups(Keyword.ElpiKeyword, Text.Whitespace, Text)), |
| 93 |
(r"\b(pi|sigma)(\s+)([a-zA-Z][A-Za-z0-9_ ]*)(\\)", |
| 94 |
bygroups(Keyword.ElpiKeyword, Text.Whitespace, Name.ElpiVariable, Text)), |
| 95 |
(rf"\b(constraint)(\s+)(({const_sym_re}(\s+)?)+)", |
| 96 |
bygroups(Keyword.ElpiKeyword, Text.Whitespace, Name.ElpiFunction), |
| 97 |
'elpi-chr-rule-start'), |
| 98 |
|
| 99 |
(rf"(?=[A-Z_]){constant_re}", Name.ElpiVariable), |
| 100 |
(rf"(?=[a-z_])({constant_re}|_)(:[A-Za-z0-9_ ]*)?(\\)", Name.ElpiVariable), |
| 101 |
(r"_", Name.ElpiVariable), |
| 102 |
(rf"({symbol_re}|!|=>|;)", Keyword.ElpiKeyword), |
| 103 |
(constant_re, Text), |
| 104 |
(r"\[|\]|\||=>", Keyword.ElpiKeyword), |
| 105 |
(r":", Keyword.ElpiKeyword), |
| 106 |
(r'"', String.Double, 'elpi-string'), |
| 107 |
(r'`', String.Double, 'elpi-btick'), |
| 108 |
(r'\'', String.Double, 'elpi-tick'), |
| 109 |
|
| 110 |
(r'\{[^\{]', Text, 'elpi-spill'), |
| 111 |
(r"\(", Punctuation, 'elpi-in-parens'), |
| 112 |
(r'\d[\d_]*', Number.Integer), |
| 113 |
(r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float), |
| 114 |
(r"[\+\*\-/\^\.]", Operator), |
| 115 |
], |
| 116 |
'_elpi-comment': [ |
| 117 |
(r'%[^\n]*\n', Comment), |
| 118 |
(r'/(?:\\\n)?[*](?:[^*]|[*](?!(?:\\\n)?/))*[*](?:\\\n)?/', Comment), |
| 119 |
(r"\s+", Text.Whitespace), |
| 120 |
], |
| 121 |
'elpi-indexing-expr':[ |
| 122 |
(r'[0-9 _]+', Number.Integer), |
| 123 |
(r'\)', Punctuation, '#pop'), |
| 124 |
], |
| 125 |
'elpi-type': [ |
| 126 |
(r"(ctype\s+)(\")", bygroups(Keyword.Type, String.Double), 'elpi-string'), |
| 127 |
(r'->', Keyword.Type), |
| 128 |
(r'prop', Keyword.ElpiMode), |
| 129 |
(constant_re, Keyword.Type), |
| 130 |
(r"\(|\)", Keyword.Type), |
| 131 |
(r"\.", Text, '#pop'), |
| 132 |
include('_elpi-comment'), |
| 133 |
], |
| 134 |
'elpi-chr-rule-start': [ |
| 135 |
(r"\{", Punctuation, 'elpi-chr-rule'), |
| 136 |
include('_elpi-comment'), |
| 137 |
], |
| 138 |
'elpi-chr-rule': [ |
| 139 |
(r"\brule\b", Keyword.ElpiKeyword), |
| 140 |
(r"\\", Keyword.ElpiKeyword), |
| 141 |
(r"\}", Punctuation, '#pop:2'), |
| 142 |
include('elpi'), |
| 143 |
], |
| 144 |
'elpi-pred-item': [ |
| 145 |
(r"[io]:", Keyword.ElpiMode), |
| 146 |
(r"\.", Text, '#pop'), |
| 147 |
(r",", Keyword.ElpiMode), |
| 148 |
include('_elpi-inner-pred-fun'), |
| 149 |
(r"\)", Keyword.ElpiMode, '#pop'), |
| 150 |
(r"\(", Keyword.Type, '_elpi-type-item'), |
| 151 |
include('_elpi-comment'), |
| 152 |
include('_elpi-type-item'), |
| 153 |
], |
| 154 |
'elpi-func-item': [ |
| 155 |
(constant_re, Keyword.Type), |
| 156 |
(r"\.", Text, '#pop'), |
| 157 |
(r",", Keyword.ElpiMode), |
| 158 |
(r'->', Keyword.ElpiMode), |
| 159 |
include('_elpi-inner-pred-fun'), |
| 160 |
(r"\)", Keyword.ElpiMode, '#pop'), |
| 161 |
(r"\(", Keyword.Type, '_elpi-type-item'), |
| 162 |
include('_elpi-comment'), |
| 163 |
include('_elpi-type-item'), |
| 164 |
], |
| 165 |
'_elpi-inner-pred-fun': [ |
| 166 |
(r"(\()(\s*)(pred)", bygroups(Keyword.ElpiMode,Text.Whitespace,Keyword.ElpiKeyword), 'elpi-pred-item'), |
| 167 |
(r"(\()(\s*)(func)", bygroups(Keyword.ElpiMode,Text.Whitespace,Keyword.ElpiKeyword), 'elpi-func-item'), |
| 168 |
], |
| 169 |
'_elpi-type-item': [ |
| 170 |
(r'->', Keyword.Type), |
| 171 |
(constant_re, Keyword.Type), |
| 172 |
include('_elpi-inner-pred-fun'), |
| 173 |
(r"\(", Keyword.Type, '#push'), |
| 174 |
(r"\)", Keyword.Type, '#pop'), |
| 175 |
include('_elpi-comment'), |
| 176 |
], |
| 177 |
|
| 178 |
'' |
| 179 |
|
| 180 |
'elpi-btick': [ |
| 181 |
(r'[^` ]+', String.Double), |
| 182 |
(r'`', String.Double, '#pop'), |
| 183 |
], |
| 184 |
'elpi-tick': [ |
| 185 |
(r'[^\' ]+', String.Double), |
| 186 |
(r'\'', String.Double, '#pop'), |
| 187 |
], |
| 188 |
'elpi-string': [ |
| 189 |
(r'[^\"]+', String.Double), |
| 190 |
(r'"', String.Double, '#pop'), |
| 191 |
], |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
'elpi-spill': [ |
| 205 |
(r'\{[^\{]', Text, '#push'), |
| 206 |
(r'\}[^\}]', Text, '#pop'), |
| 207 |
include('elpi'), |
| 208 |
], |
| 209 |
'elpi-in-parens': [ |
| 210 |
(r"\(", Punctuation, '#push'), |
| 211 |
include('elpi'), |
| 212 |
(rf"(\))(:[A-Za-z0-9_ ]*)?", bygroups(Punctuation,Name.ElpiVariable), '#pop'), |
| 213 |
], |
| 214 |
} |
| 215 |
|
| 216 |
from pygments.lexers._mapping import LEXERS |
| 217 |
LEXERS['ElpiLexer'] = ('alectryon_elpi','Elpi',('elpi',),('*.elpi',),('text/x-elpi',)) |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
from alectryon.pygments_lexer import CoqLexer |
| 222 |
|
| 223 |
class CoqElpiLexer(CoqLexer, ElpiLexer): |
| 224 |
|
| 225 |
tokens = { |
| 226 |
'root': [ |
| 227 |
|
| 228 |
include('_basic'), |
| 229 |
include('_vernac'), |
| 230 |
include('_keywords'), |
| 231 |
include('_other'), |
| 232 |
], |
| 233 |
'_quotations': [ |
| 234 |
(r"lp:\{\{",String.Interpol, 'elpi'), |
| 235 |
(r"(lp:)([A-Za-z_0-9']+)",bygroups(String.Interpol, Name.ElpiVariable)), |
| 236 |
(r"(lp:)(\()([A-Z][A-Za-z_0-9']*)([a-z0-9 ]+)(\))",bygroups(String.Interpol,String.Interpol,Name.ElpiVariable,Text,String.Interpol)), |
| 237 |
], |
| 238 |
'antiquotation': [ |
| 239 |
(r"\}\}",String.Interpol,'#pop'), |
| 240 |
include('root') |
| 241 |
], |
| 242 |
'elpi': [ |
| 243 |
(r"\}\}",String.Interpol,'#pop'), |
| 244 |
(r"\b(global|sort|app|fun|let|prod|match|fix)\b", Keyword.ElpiKeyword), |
| 245 |
(r"\{\{(:[a-z]+)?",String.Interpol,'antiquotation'), |
| 246 |
inherit |
| 247 |
], |
| 248 |
'_other': [ |
| 249 |
include('_quotations'), |
| 250 |
inherit |
| 251 |
], |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
import alectryon.pygments_lexer |
| 256 |
alectryon.pygments_lexer.CoqLexer = CoqElpiLexer |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
import docutils |
| 261 |
from docutils.parsers.rst import directives, roles |
| 262 |
from docutils import nodes |
| 263 |
|
| 264 |
def set_line(node, lineno, sm): |
| 265 |
node.source, node.line = sm.get_source_and_line(lineno) |
| 266 |
|
| 267 |
import re |
| 268 |
import time |
| 269 |
import pickle |
| 270 |
import atexit |
| 271 |
|
| 272 |
ghref_cache = {} |
| 273 |
|
| 274 |
def dump_ghref_cache(): |
| 275 |
when = int(time.time() / 1000) |
| 276 |
file = '/tmp/ghref_cache_{}'.format(str(when)) |
| 277 |
pickle.dump(ghref_cache,open(file,'wb')) |
| 278 |
|
| 279 |
atexit.register(dump_ghref_cache) |
| 280 |
|
| 281 |
try: |
| 282 |
when = int(time.time() / 1000) |
| 283 |
file = '/tmp/ghref_cache_{}'.format(str(when)) |
| 284 |
ghref_cache = pickle.load(open(file,'rb')) |
| 285 |
|
| 286 |
except: |
| 287 |
|
| 288 |
ghref_cache = {} |
| 289 |
|
| 290 |
ghref_scrape_re = re.compile("\"sha\"[: ]*\"([a-zA-Z0-9]+)\"",re.IGNORECASE) |
| 291 |
|
| 292 |
def ghref_role(role, rawtext, text, lineno, inliner, options={}, content=[]): |
| 293 |
src = options.get('src',None) |
| 294 |
if src is None: |
| 295 |
msg = inliner.reporter.error("{}: no src option".format(role), line=lineno) |
| 296 |
return [inliner.problematic(rawtext, rawtext, msg)], [msg] |
| 297 |
components = str.split(src,sep=" ") |
| 298 |
if len(components) != 4: |
| 299 |
msg = inliner.reporter.error("{}: src should be 4 space separated strings".format(role), line=lineno) |
| 300 |
return [inliner.problematic(rawtext, rawtext, msg)], [msg] |
| 301 |
org, repo, branch, path = components |
| 302 |
uri = "https://github.com/{}/{}/blob/{}/{}".format(org,repo,branch,path) |
| 303 |
roles.set_classes(options) |
| 304 |
options.setdefault("classes", []).append("ghref") |
| 305 |
if uri in ghref_cache: |
| 306 |
code, rawuri, uri = ghref_cache[uri] |
| 307 |
else: |
| 308 |
from urllib import request |
| 309 |
apiuri = "https://api.github.com/repos/{}/{}/commits/{}/branches-where-head".format(org,repo,branch) |
| 310 |
try: |
| 311 |
with request.urlopen(apiuri) as f: |
| 312 |
json = f.read().decode('utf-8') |
| 313 |
except: |
| 314 |
msg = inliner.reporter.error("{}: could not download: {}".format(role,apiuri), line=lineno) |
| 315 |
return [inliner.problematic(rawtext, rawtext, msg)], [msg] |
| 316 |
try: |
| 317 |
|
| 318 |
sha = ghref_scrape_re.search(json).group(1) |
| 319 |
except: |
| 320 |
msg = inliner.reporter.error("{}: could not scrape for permalink: {}".format(role,uri), line=lineno) |
| 321 |
return [inliner.problematic(rawtext, rawtext, msg)], [msg] |
| 322 |
puri = "https://github.com/{}/{}/blob/{}/{}".format(org,repo,sha,path) |
| 323 |
rawuri = "https://raw.githubusercontent.com/{}/{}/{}/{}".format(org,repo,sha,path) |
| 324 |
try: |
| 325 |
with request.urlopen(rawuri) as f: |
| 326 |
code = f.read().decode('utf-8') |
| 327 |
except: |
| 328 |
msg = inliner.reporter.error("{}: could not download: {}".format(role,rawuri), line=lineno) |
| 329 |
return [inliner.problematic(rawtext, rawtext, msg)], [msg] |
| 330 |
ghref_cache[uri]=(code,rawuri,puri) |
| 331 |
uri=puri |
| 332 |
mangler = options.get('replace',None) |
| 333 |
mangler_with = options.get('replace_with','') |
| 334 |
if mangler is None: |
| 335 |
name = text |
| 336 |
else: |
| 337 |
name = re.sub(mangler,mangler_with,text) |
| 338 |
pattern = options.get('pattern','') |
| 339 |
from string import Template |
| 340 |
pattern = Template(pattern).safe_substitute(name = re.escape(name)) |
| 341 |
pattern = re.compile(pattern) |
| 342 |
for num, line in enumerate(code.splitlines(), 1): |
| 343 |
if pattern.search(line): |
| 344 |
uri = uri + '#L' + str(num) |
| 345 |
break |
| 346 |
else: |
| 347 |
msg = inliner.reporter.error("{}: {} not found in {} using pattern {}".format(role,text,rawuri,pattern), line=lineno) |
| 348 |
return [inliner.problematic(rawtext, rawtext, msg)], [msg] |
| 349 |
node = nodes.reference(rawtext, text, refuri=uri, **options) |
| 350 |
set_line(node, lineno, inliner.reporter) |
| 351 |
return [node], [] |
| 352 |
ghref_role.name = "ghref" |
| 353 |
ghref_role.options = { |
| 354 |
|
| 355 |
|
| 356 |
"src": directives.unchanged, |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
"pattern": directives.unchanged, |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
"replace": directives.unchanged, |
| 366 |
"replace_with": directives.unchanged |
| 367 |
} |
| 368 |
|
| 369 |
roles.register_canonical_role("ghref", ghref_role) |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
__all__ = [ "ElpiLexer", "CoqElpiLexer"] |
| 374 |
|
| 375 |
if __name__ == "__main__": |
| 376 |
main() |