elpi.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. """
  2. pygments.lexers.elpi
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the `Elpi <http://github.com/LPCIC/elpi>`_ programming language.
  5. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, bygroups, include
  9. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  10. Number, Punctuation
  11. __all__ = ['ElpiLexer']
  12. class ElpiLexer(RegexLexer):
  13. """
  14. Lexer for the Elpi programming language.
  15. .. versionadded:: 2.11
  16. """
  17. name = 'Elpi'
  18. url = 'http://github.com/LPCIC/elpi'
  19. aliases = ['elpi']
  20. filenames = ['*.elpi']
  21. mimetypes = ['text/x-elpi']
  22. lcase_re = r"[a-z]"
  23. ucase_re = r"[A-Z]"
  24. digit_re = r"[0-9]"
  25. schar2_re = r"([+*^?/<>`'@#~=&!])"
  26. schar_re = r"({}|-|\$|_)".format(schar2_re)
  27. idchar_re = r"({}|{}|{}|{})".format(lcase_re,ucase_re,digit_re,schar_re)
  28. idcharstarns_re = r"({}*(\.({}|{}){}*)*)".format(idchar_re, lcase_re, ucase_re, idchar_re)
  29. symbchar_re = r"({}|{}|{}|{}|:)".format(lcase_re, ucase_re, digit_re, schar_re)
  30. constant_re = r"({}{}*|{}{}|{}{}*|_{}+)".format(ucase_re, idchar_re, lcase_re, idcharstarns_re, schar2_re, symbchar_re, idchar_re)
  31. 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~)"
  32. escape_re = r"\(({}|{})\)".format(constant_re,symbol_re)
  33. const_sym_re = r"({}|{}|{})".format(constant_re,symbol_re,escape_re)
  34. tokens = {
  35. 'root': [
  36. include('elpi')
  37. ],
  38. 'elpi': [
  39. include('_elpi-comment'),
  40. (r"(:before|:after|:if|:name)(\s*)(\")",
  41. bygroups(Keyword.Mode, Text.Whitespace, String.Double),
  42. 'elpi-string'),
  43. (r"(:index)(\s*\()", bygroups(Keyword.Mode, Text.Whitespace),
  44. 'elpi-indexing-expr'),
  45. (r"\b(external pred|pred)(\s+)({})".format(const_sym_re),
  46. bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
  47. 'elpi-pred-item'),
  48. (r"\b(external type|type)(\s+)(({}(,\s*)?)+)".format(const_sym_re),
  49. bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
  50. 'elpi-type'),
  51. (r"\b(kind)(\s+)(({}|,)+)".format(const_sym_re),
  52. bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
  53. 'elpi-type'),
  54. (r"\b(typeabbrev)(\s+)({})".format(const_sym_re),
  55. bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
  56. 'elpi-type'),
  57. (r"\b(accumulate)(\s+)(\")",
  58. bygroups(Keyword.Declaration, Text.Whitespace, String.Double),
  59. 'elpi-string'),
  60. (r"\b(accumulate|namespace|local)(\s+)({})".format(constant_re),
  61. bygroups(Keyword.Declaration, Text.Whitespace, Text)),
  62. (r"\b(shorten)(\s+)({}\.)".format(constant_re),
  63. bygroups(Keyword.Declaration, Text.Whitespace, Text)),
  64. (r"\b(pi|sigma)(\s+)([a-zA-Z][A-Za-z0-9_ ]*)(\\)",
  65. bygroups(Keyword.Declaration, Text.Whitespace, Name.Variable, Text)),
  66. (r"\b(constraint)(\s+)(({}(\s+)?)+)".format(const_sym_re),
  67. bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
  68. 'elpi-chr-rule-start'),
  69. (r"(?=[A-Z_]){}".format(constant_re), Name.Variable),
  70. (r"(?=[a-z_]){}\\".format(constant_re), Name.Variable),
  71. (r"_", Name.Variable),
  72. (r"({}|!|=>|;)".format(symbol_re), Keyword.Declaration),
  73. (constant_re, Text),
  74. (r"\[|\]|\||=>", Keyword.Declaration),
  75. (r'"', String.Double, 'elpi-string'),
  76. (r'`', String.Double, 'elpi-btick'),
  77. (r'\'', String.Double, 'elpi-tick'),
  78. (r'\{\{', Punctuation, 'elpi-quote'),
  79. (r'\{[^\{]', Text, 'elpi-spill'),
  80. (r"\(", Text, 'elpi-in-parens'),
  81. (r'\d[\d_]*', Number.Integer),
  82. (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float),
  83. (r"[\+\*\-/\^\.]", Operator),
  84. ],
  85. '_elpi-comment': [
  86. (r'%[^\n]*\n', Comment),
  87. (r'/\*', Comment, 'elpi-multiline-comment'),
  88. (r"\s+", Text.Whitespace),
  89. ],
  90. 'elpi-multiline-comment': [
  91. (r'\*/', Comment, '#pop'),
  92. (r'.', Comment)
  93. ],
  94. 'elpi-indexing-expr':[
  95. (r'[0-9 _]+', Number.Integer),
  96. (r'\)', Text, '#pop'),
  97. ],
  98. 'elpi-type': [
  99. (r"(ctype\s+)(\")", bygroups(Keyword.Type, String.Double), 'elpi-string'),
  100. (r'->', Keyword.Type),
  101. (constant_re, Keyword.Type),
  102. (r"\(|\)", Keyword.Type),
  103. (r"\.", Text, '#pop'),
  104. include('_elpi-comment'),
  105. ],
  106. 'elpi-chr-rule-start': [
  107. (r"\{", Text, 'elpi-chr-rule'),
  108. include('_elpi-comment'),
  109. ],
  110. 'elpi-chr-rule': [
  111. (r"\brule\b", Keyword.Declaration),
  112. (r"\\", Keyword.Declaration),
  113. (r"\}", Text, '#pop:2'),
  114. include('elpi'),
  115. ],
  116. 'elpi-pred-item': [
  117. (r"[io]:", Keyword.Mode, 'elpi-ctype'),
  118. (r"\.", Text, '#pop'),
  119. include('_elpi-comment'),
  120. ],
  121. 'elpi-ctype': [
  122. (r"(ctype\s+)(\")", bygroups(Keyword.Type, String.Double), 'elpi-string'),
  123. (r'->', Keyword.Type),
  124. (constant_re, Keyword.Type),
  125. (r"\(|\)", Keyword.Type),
  126. (r",", Text, '#pop'),
  127. (r"\.", Text, '#pop:2'),
  128. include('_elpi-comment'),
  129. ],
  130. 'elpi-btick': [
  131. (r'[^` ]+', String.Double),
  132. (r'`', String.Double, '#pop'),
  133. ],
  134. 'elpi-tick': [
  135. (r'[^\' ]+', String.Double),
  136. (r'\'', String.Double, '#pop'),
  137. ],
  138. 'elpi-string': [
  139. (r'[^\"]+', String.Double),
  140. (r'"', String.Double, '#pop'),
  141. ],
  142. 'elpi-quote': [
  143. (r'\{\{', Punctuation, '#push'),
  144. (r'\}\}', Punctuation, '#pop'),
  145. (r"(lp:)((?=[A-Z_]){})".format(constant_re), bygroups(Keyword, Name.Variable)),
  146. (r"[^l\}]+", Text),
  147. (r"l|\}", Text),
  148. ],
  149. 'elpi-spill': [
  150. (r'\{[^\{]', Text, '#push'),
  151. (r'\}[^\}]', Text, '#pop'),
  152. include('elpi'),
  153. ],
  154. 'elpi-in-parens': [
  155. (r"\(", Operator, '#push'),
  156. (r"\)", Operator, '#pop'),
  157. include('elpi'),
  158. ],
  159. }