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