elpi.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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
  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'\{[^\{]', Text, 'elpi-spill'),
  79. (r"\(", Text, 'elpi-in-parens'),
  80. (r'\d[\d_]*', Number.Integer),
  81. (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float),
  82. (r"[\+\*\-/\^\.]", Operator),
  83. ],
  84. '_elpi-comment': [
  85. (r'%[^\n]*\n', Comment),
  86. (r'/\*', Comment, 'elpi-multiline-comment'),
  87. (r"\s+", Text.Whitespace),
  88. ],
  89. 'elpi-multiline-comment': [
  90. (r'\*/', Comment, '#pop'),
  91. (r'.', Comment)
  92. ],
  93. 'elpi-indexing-expr':[
  94. (r'[0-9 _]+', Number.Integer),
  95. (r'\)', Text, '#pop'),
  96. ],
  97. 'elpi-type': [
  98. (r"(ctype\s+)(\")", bygroups(Keyword.Type, String.Double), 'elpi-string'),
  99. (r'->', Keyword.Type),
  100. (constant_re, Keyword.Type),
  101. (r"\(|\)", Keyword.Type),
  102. (r"\.", Text, '#pop'),
  103. include('_elpi-comment'),
  104. ],
  105. 'elpi-chr-rule-start': [
  106. (r"\{", Text, 'elpi-chr-rule'),
  107. include('_elpi-comment'),
  108. ],
  109. 'elpi-chr-rule': [
  110. (r"\brule\b", Keyword.Declaration),
  111. (r"\\", Keyword.Declaration),
  112. (r"\}", Text, '#pop:2'),
  113. include('elpi'),
  114. ],
  115. 'elpi-pred-item': [
  116. (r"[io]:", Keyword.Mode, 'elpi-ctype'),
  117. (r"\.", Text, '#pop'),
  118. include('_elpi-comment'),
  119. ],
  120. 'elpi-ctype': [
  121. (r"(ctype\s+)(\")", bygroups(Keyword.Type, String.Double), 'elpi-string'),
  122. (r'->', Keyword.Type),
  123. (constant_re, Keyword.Type),
  124. (r"\(|\)", Keyword.Type),
  125. (r",", Text, '#pop'),
  126. (r"\.", Text, '#pop:2'),
  127. include('_elpi-comment'),
  128. ],
  129. 'elpi-btick': [
  130. (r'[^` ]+', String.Double),
  131. (r'`', String.Double, '#pop'),
  132. ],
  133. 'elpi-tick': [
  134. (r'[^\' ]+', String.Double),
  135. (r'\'', String.Double, '#pop'),
  136. ],
  137. 'elpi-string': [
  138. (r'[^\"]+', String.Double),
  139. (r'"', String.Double, '#pop'),
  140. ],
  141. 'elpi-spill': [
  142. (r'\{[^\{]', Text, '#push'),
  143. (r'\}[^\}]', Text, '#pop'),
  144. include('elpi'),
  145. ],
  146. 'elpi-in-parens': [
  147. (r"\(", Operator, '#push'),
  148. (r"\)", Operator, '#pop'),
  149. include('elpi'),
  150. ],
  151. }