qvt.py 5.9 KB

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