vip.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. """
  2. pygments.lexers.vip
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexers for Visual Prolog & Grammar files.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, inherit, words, include
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation, Whitespace
  12. __all__ = ['VisualPrologLexer', 'VisualPrologGrammarLexer']
  13. class VisualPrologBaseLexer(RegexLexer):
  14. minorendkw = ('try', 'foreach', 'if')
  15. minorkwexp = ('and', 'catch', 'do', 'else', 'elseif', 'erroneous', 'externally', 'failure', 'finally', 'foreach', 'if', 'or', 'orelse', 'otherwise', 'then',
  16. 'try', 'div', 'mod', 'rem', 'quot')
  17. dockw = ('short', 'detail', 'end', 'withdomain')
  18. tokens = {
  19. 'root': [
  20. (r'\s+', Whitespace),
  21. (words(minorendkw, prefix=r'\bend\s+', suffix=r'\b'), Keyword.Minor),
  22. (r'end', Keyword),
  23. (words(minorkwexp, suffix=r'\b'), Keyword.Minor),
  24. (r'0[xo][\da-fA-F_]+', Number),
  25. (r'((\d[\d_]*)?\.)?\d[\d_]*([eE][\-+]?\d+)?', Number),
  26. (r'_\w*', Name.Variable.Anonymous),
  27. (r'[A-Z]\w*', Name.Variable),
  28. (r'@\w+', Name.Variable),
  29. (r'[a-z]\w*', Name),
  30. (r'/\*', Comment, 'comment'),
  31. (r'\%', Comment, 'commentline'),
  32. (r'"', String.Symbol, 'string'),
  33. (r'\'', String.Symbol, 'stringsingle'),
  34. (r'@"', String.Symbol, 'atstring'),
  35. (r'[\-+*^/!?<>=~:]+', Operator),
  36. (r'[$,.[\]|(){}\\]+', Punctuation),
  37. (r'.', Text),
  38. ],
  39. 'commentdoc': [
  40. (words(dockw, prefix=r'@', suffix=r'\b'), Comment.Preproc),
  41. (r'@', Comment),
  42. ],
  43. 'commentline': [
  44. include('commentdoc'),
  45. (r'[^@\n]+', Comment),
  46. (r'$', Comment, '#pop'),
  47. ],
  48. 'comment': [
  49. include('commentdoc'),
  50. (r'[^@*/]+', Comment),
  51. (r'/\*', Comment, '#push'),
  52. (r'\*/', Comment, '#pop'),
  53. (r'[*/]', Comment),
  54. ],
  55. 'stringescape': [
  56. (r'\\u[0-9a-fA-F]{4}', String.Escape),
  57. (r'\\[\'"ntr\\]', String.Escape),
  58. ],
  59. 'stringsingle': [
  60. include('stringescape'),
  61. (r'\'', String.Symbol, '#pop'),
  62. (r'[^\'\\\n]+', String),
  63. (r'\n', String.Escape.Error, '#pop'),
  64. ],
  65. 'string': [
  66. include('stringescape'),
  67. (r'"', String.Symbol, '#pop'),
  68. (r'[^"\\\n]+', String),
  69. (r'\n', String.Escape.Error, '#pop'),
  70. ],
  71. 'atstring': [
  72. (r'""', String.Escape),
  73. (r'"', String.Symbol, '#pop'),
  74. (r'[^"]+', String),
  75. ]
  76. }
  77. class VisualPrologLexer(VisualPrologBaseLexer):
  78. """Lexer for VisualProlog
  79. """
  80. name = 'Visual Prolog'
  81. url = 'https://www.visual-prolog.com/'
  82. aliases = ['visualprolog']
  83. filenames = ['*.pro', '*.cl', '*.i', '*.pack', '*.ph']
  84. version_added = '2.17'
  85. majorkw = ('goal', 'namespace', 'interface', 'class', 'implement', 'where', 'open', 'inherits', 'supports', 'resolve',
  86. 'delegate', 'monitor', 'constants', 'domains', 'predicates', 'constructors', 'properties', 'clauses', 'facts')
  87. minorkw = ('align', 'anyflow', 'as', 'bitsize', 'determ', 'digits', 'erroneous', 'externally', 'failure', 'from',
  88. 'guard', 'multi', 'nondeterm', 'or', 'orelse', 'otherwise', 'procedure', 'resolve', 'single', 'suspending')
  89. directivekw = ('bininclude', 'else', 'elseif', 'endif', 'error', 'export', 'externally', 'from', 'grammargenerate',
  90. 'grammarinclude', 'if', 'include', 'message', 'options', 'orrequires', 'requires', 'stringinclude', 'then')
  91. tokens = {
  92. 'root': [
  93. (words(minorkw, suffix=r'\b'), Keyword.Minor),
  94. (words(majorkw, suffix=r'\b'), Keyword),
  95. (words(directivekw, prefix='#', suffix=r'\b'), Keyword.Directive),
  96. inherit
  97. ]
  98. }
  99. def analyse_text(text):
  100. """Competes with IDL and Prolog on *.pro; div. lisps on*.cl and SwigLexer on *.i"""
  101. # These are *really* good indicators (and not conflicting with the other languages)
  102. # end-scope first on line e.g. 'end implement'
  103. # section keyword alone on line e.g. 'clauses'
  104. if re.search(r'^\s*(end\s+(interface|class|implement)|(clauses|predicates|domains|facts|constants|properties)\s*$)', text):
  105. return 0.98
  106. else:
  107. return 0
  108. class VisualPrologGrammarLexer(VisualPrologBaseLexer):
  109. """Lexer for VisualProlog grammar
  110. """
  111. name = 'Visual Prolog Grammar'
  112. url = 'https://www.visual-prolog.com/'
  113. aliases = ['visualprologgrammar']
  114. filenames = ['*.vipgrm']
  115. version_added = '2.17'
  116. majorkw = ('open', 'namespace', 'grammar', 'nonterminals',
  117. 'startsymbols', 'terminals', 'rules', 'precedence')
  118. directivekw = ('bininclude', 'stringinclude')
  119. tokens = {
  120. 'root': [
  121. (words(majorkw, suffix=r'\b'), Keyword),
  122. (words(directivekw, prefix='#', suffix=r'\b'), Keyword.Directive),
  123. inherit
  124. ]
  125. }
  126. def analyse_text(text):
  127. """No competditors (currently)"""
  128. # These are *really* good indicators
  129. # end-scope first on line e.g. 'end grammar'
  130. # section keyword alone on line e.g. 'rules'
  131. if re.search(r'^\s*(end\s+grammar|(nonterminals|startsymbols|terminals|rules|precedence)\s*$)', text):
  132. return 0.98
  133. else:
  134. return 0