qvt.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.qvt
  4. ~~~~~~~~~~~~~~~~~~~
  5. Lexer for QVT Operational language.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.lexer import RegexLexer, bygroups, include, combined, default, \
  10. words
  11. from pygments.token import Text, Comment, Operator, Keyword, Punctuation, \
  12. Name, String, Number
  13. __all__ = ['QVToLexer']
  14. class QVToLexer(RegexLexer):
  15. u"""
  16. For the `QVT Operational Mapping language <http://www.omg.org/spec/QVT/1.1/>`_.
  17. Reference for implementing this: «Meta Object Facility (MOF) 2.0
  18. Query/View/Transformation Specification», Version 1.1 - January 2011
  19. (http://www.omg.org/spec/QVT/1.1/), see §8.4, «Concrete Syntax» in
  20. particular.
  21. Notable tokens assignments:
  22. - Name.Class is assigned to the identifier following any of the following
  23. keywords: metamodel, class, exception, primitive, enum, transformation
  24. or library
  25. - Name.Function is assigned to the names of mappings and queries
  26. - Name.Builtin.Pseudo is assigned to the pre-defined variables 'this',
  27. 'self' and 'result'.
  28. """
  29. # With obvious borrowings & inspiration from the Java, Python and C lexers
  30. name = 'QVTO'
  31. aliases = ['qvto', 'qvt']
  32. filenames = ['*.qvto']
  33. tokens = {
  34. 'root': [
  35. (r'\n', Text),
  36. (r'[^\S\n]+', Text),
  37. (r'(--|//)(\s*)(directive:)?(.*)$',
  38. bygroups(Comment, Comment, Comment.Preproc, Comment)),
  39. # Uncomment the following if you want to distinguish between
  40. # '/*' and '/**', à la javadoc
  41. # (r'/[*]{2}(.|\n)*?[*]/', Comment.Multiline),
  42. (r'/[*](.|\n)*?[*]/', Comment.Multiline),
  43. (r'\\\n', Text),
  44. (r'(and|not|or|xor|##?)\b', Operator.Word),
  45. (r'(:{1,2}=|[-+]=)\b', Operator.Word),
  46. (r'(@|<<|>>)\b', Keyword), # stereotypes
  47. (r'!=|<>|==|=|!->|->|>=|<=|[.]{3}|[+/*%=<>&|.~]', Operator),
  48. (r'[]{}:(),;[]', Punctuation),
  49. (r'(true|false|unlimited|null)\b', Keyword.Constant),
  50. (r'(this|self|result)\b', Name.Builtin.Pseudo),
  51. (r'(var)\b', Keyword.Declaration),
  52. (r'(from|import)\b', Keyword.Namespace, 'fromimport'),
  53. (r'(metamodel|class|exception|primitive|enum|transformation|'
  54. r'library)(\s+)(\w+)',
  55. bygroups(Keyword.Word, Text, Name.Class)),
  56. (r'(exception)(\s+)(\w+)',
  57. bygroups(Keyword.Word, Text, Name.Exception)),
  58. (r'(main)\b', Name.Function),
  59. (r'(mapping|helper|query)(\s+)',
  60. bygroups(Keyword.Declaration, Text), 'operation'),
  61. (r'(assert)(\s+)\b', bygroups(Keyword, Text), 'assert'),
  62. (r'(Bag|Collection|Dict|OrderedSet|Sequence|Set|Tuple|List)\b',
  63. Keyword.Type),
  64. include('keywords'),
  65. ('"', String, combined('stringescape', 'dqs')),
  66. ("'", String, combined('stringescape', 'sqs')),
  67. include('name'),
  68. include('numbers'),
  69. # (r'([a-zA-Z_]\w*)(::)([a-zA-Z_]\w*)',
  70. # bygroups(Text, Text, Text)),
  71. ],
  72. 'fromimport': [
  73. (r'(?:[ \t]|\\\n)+', Text),
  74. (r'[a-zA-Z_][\w.]*', Name.Namespace),
  75. default('#pop'),
  76. ],
  77. 'operation': [
  78. (r'::', Text),
  79. (r'(.*::)([a-zA-Z_]\w*)([ \t]*)(\()',
  80. bygroups(Text, Name.Function, Text, Punctuation), '#pop')
  81. ],
  82. 'assert': [
  83. (r'(warning|error|fatal)\b', Keyword, '#pop'),
  84. default('#pop'), # all else: go back
  85. ],
  86. 'keywords': [
  87. (words((
  88. 'abstract', 'access', 'any', 'assert', 'blackbox', 'break',
  89. 'case', 'collect', 'collectNested', 'collectOne', 'collectselect',
  90. 'collectselectOne', 'composes', 'compute', 'configuration',
  91. 'constructor', 'continue', 'datatype', 'default', 'derived',
  92. 'disjuncts', 'do', 'elif', 'else', 'end', 'endif', 'except',
  93. 'exists', 'extends', 'forAll', 'forEach', 'forOne', 'from', 'if',
  94. 'implies', 'in', 'inherits', 'init', 'inout', 'intermediate',
  95. 'invresolve', 'invresolveIn', 'invresolveone', 'invresolveoneIn',
  96. 'isUnique', 'iterate', 'late', 'let', 'literal', 'log', 'map',
  97. 'merges', 'modeltype', 'new', 'object', 'one', 'ordered', 'out',
  98. 'package', 'population', 'property', 'raise', 'readonly',
  99. 'references', 'refines', 'reject', 'resolve', 'resolveIn',
  100. 'resolveone', 'resolveoneIn', 'return', 'select', 'selectOne',
  101. 'sortedBy', 'static', 'switch', 'tag', 'then', 'try', 'typedef',
  102. 'unlimited', 'uses', 'when', 'where', 'while', 'with', 'xcollect',
  103. 'xmap', 'xselect'), suffix=r'\b'), Keyword),
  104. ],
  105. # There is no need to distinguish between String.Single and
  106. # String.Double: 'strings' is factorised for 'dqs' and 'sqs'
  107. 'strings': [
  108. (r'[^\\\'"\n]+', String),
  109. # quotes, percents and backslashes must be parsed one at a time
  110. (r'[\'"\\]', String),
  111. ],
  112. 'stringescape': [
  113. (r'\\([\\btnfr"\']|u[0-3][0-7]{2}|u[0-7]{1,2})', String.Escape)
  114. ],
  115. 'dqs': [ # double-quoted string
  116. (r'"', String, '#pop'),
  117. (r'\\\\|\\"', String.Escape),
  118. include('strings')
  119. ],
  120. 'sqs': [ # single-quoted string
  121. (r"'", String, '#pop'),
  122. (r"\\\\|\\'", String.Escape),
  123. include('strings')
  124. ],
  125. 'name': [
  126. (r'[a-zA-Z_]\w*', Name),
  127. ],
  128. # numbers: excerpt taken from the python lexer
  129. 'numbers': [
  130. (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float),
  131. (r'\d+[eE][+-]?[0-9]+', Number.Float),
  132. (r'\d+', Number.Integer)
  133. ],
  134. }