1"""
2 pygments.lexers.smithy
3 ~~~~~~~~~~~~~~~~~~~~~~
4
5 Lexers for the Smithy IDL.
6
7 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
10
11from pygments.lexer import RegexLexer, bygroups, words
12from pygments.token import Text, Comment, Keyword, Name, String, \
13 Number, Whitespace, Punctuation
14
15__all__ = ['SmithyLexer']
16
17
18class SmithyLexer(RegexLexer):
19 """
20 For Smithy IDL
21 """
22 name = 'Smithy'
23 url = 'https://awslabs.github.io/smithy/'
24 filenames = ['*.smithy']
25 aliases = ['smithy']
26 version_added = '2.10'
27
28 unquoted = r'[A-Za-z0-9_\.#$-]+'
29 identifier = r"[A-Za-z0-9_\.#$-]+"
30
31 simple_shapes = (
32 'use', 'byte', 'short', 'integer', 'long', 'float', 'document',
33 'double', 'bigInteger', 'bigDecimal', 'boolean', 'blob', 'string',
34 'timestamp', 'enum', 'intEnum', # enum/intEnum are IDL 2.0 shapes
35 )
36
37 aggregate_shapes = (
38 'apply', 'list', 'map', 'set', 'structure', 'union', 'resource',
39 'operation', 'service', 'trait'
40 )
41
42 tokens = {
43 'root': [
44 (r'///.*$', Comment.Multiline),
45 (r'//.*$', Comment),
46 (r'@[0-9a-zA-Z\.#-]*', Name.Decorator),
47 (r'(:=)', Name.Decorator), # inline structure / operation shape operator
48 (r'(=)', Name.Decorator),
49 (r'^(\$[A-Za-z_][A-Za-z0-9_]*)(:)(.+)',
50 bygroups(Keyword.Declaration, Name.Decorator, Name.Class)),
51 (r'^(namespace)(\s+' + identifier + r')\b',
52 bygroups(Keyword.Declaration, Name.Class)),
53 (words(simple_shapes,
54 prefix=r'^', suffix=r'(\s+' + identifier + r')\b'),
55 bygroups(Keyword.Declaration, Name.Class)),
56 (words(aggregate_shapes,
57 prefix=r'^', suffix=r'(\s+' + identifier + r')'),
58 bygroups(Keyword.Declaration, Name.Class)),
59 (r'^(metadata)(\s+)((?:\S+)|(?:\"[^"]+\"))(\s*)(=)',
60 bygroups(Keyword.Declaration, Whitespace, Name.Class,
61 Whitespace, Name.Decorator)),
62 (r"(true|false|null)", Keyword.Constant),
63 (r"(-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)", Number),
64 (r'\b(with|for)\b', Keyword.Declaration), # mixin / resource binding keywords
65 # Target elision: the leading "$" marks an elided member, e.g. "$id"
66 (r'(\$)(' + identifier + r')', bygroups(Name.Decorator, Name.Variable.Class)),
67 (identifier + ":", Name.Label),
68 (identifier, Name.Variable.Class),
69 (r'\[', Text, "#push"),
70 (r'\]', Text, "#pop"),
71 (r'\(', Text, "#push"),
72 (r'\)', Text, "#pop"),
73 (r'\{', Text, "#push"),
74 (r'\}', Text, "#pop"),
75 # Text block: """...""", spanning escapes, lone 1-2 quotes, and any
76 # other character up to the closing triple quote.
77 (r'"""(?:\\.|"{1,2}(?!")|[^"\\])*"""', String.Doc),
78 (r'"(\\\\|\n|\\"|[^"])*"', String.Double),
79 (r"'(\\\\|\n|\\'|[^'])*'", String.Single),
80 (r'[:,]+', Punctuation),
81 (r'\s+', Whitespace),
82 ]
83 }