Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Smithy Lexer (#1878) #1879

Merged
merged 3 commits into from Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -227,6 +227,7 @@ Other contributors, listed alphabetically, are:
* Matthias Vallentin -- Bro lexer
* Benoît Vinot -- AMPL lexer
* Linh Vu Hong -- RSL lexer
* Immanuel Washington -- Smithy lexer
* Nathan Weizenbaum -- Haml and Sass lexers
* Nathan Whetsell -- Csound lexers
* Dietmar Winkler -- Modelica lexer
Expand Down
1 change: 1 addition & 0 deletions doc/languages.rst
Expand Up @@ -280,6 +280,7 @@ Other markup
* Scdoc
* Sieve
* Singularity
* `Smithy <https://awslabs.github.io/smithy/>`_
* SPARQL
* SQL, also MySQL, SQLite
* Squid configuration
Expand Down
1 change: 1 addition & 0 deletions pygments/lexers/_mapping.py
Expand Up @@ -431,6 +431,7 @@
'SmalltalkLexer': ('pygments.lexers.smalltalk', 'Smalltalk', ('smalltalk', 'squeak', 'st'), ('*.st',), ('text/x-smalltalk',)),
'SmartGameFormatLexer': ('pygments.lexers.sgf', 'SmartGameFormat', ('sgf',), ('*.sgf',), ()),
'SmartyLexer': ('pygments.lexers.templates', 'Smarty', ('smarty',), ('*.tpl',), ('application/x-smarty',)),
'SmithyLexer': ('pygments.lexers.smithy', 'Smithy', ('smithy',), ('*.smithy',), ()),
'SnobolLexer': ('pygments.lexers.snobol', 'Snobol', ('snobol',), ('*.snobol',), ('text/x-snobol',)),
'SnowballLexer': ('pygments.lexers.dsls', 'Snowball', ('snowball',), ('*.sbl',), ()),
'SolidityLexer': ('pygments.lexers.solidity', 'Solidity', ('solidity',), ('*.sol',), ()),
Expand Down
79 changes: 79 additions & 0 deletions pygments/lexers/smithy.py
@@ -0,0 +1,79 @@
"""
pygments.lexers.smithy
~~~~~~~~~~~~~~~~~~~~~~~~~~

Lexers for the Smithy IDL.

:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""

import re

from pygments.lexer import RegexLexer, bygroups, words
from pygments.token import Text, Comment, Keyword, Name, String, \
Number, Whitespace, Punctuation

__all__ = ['SmithyLexer']


class SmithyLexer(RegexLexer):
"""
For Smithy IDL
Anteru marked this conversation as resolved.
Show resolved Hide resolved

.. versionadded:: 2.10
"""
name = 'Smithy'
filenames = ['*.smithy']
aliases = ['smithy']

flags = re.MULTILINE | re.UNICODE
unquoted = r'[A-Za-z0-9_\.#$-]+'
identifier = r"[A-Za-z0-9_\.#$-]+"

simple_shapes = (
'use', 'byte', 'short', 'integer', 'long', 'float', 'document',
'double', 'bigInteger', 'bigDecimal', 'boolean', 'blob', 'string',
'timestamp',
)

aggregate_shapes = (
'apply', 'list', 'map', 'set', 'structure', 'union', 'resource',
'operation', 'service', 'trait'
)

tokens = {
'root': [
(r'///.*$', Comment.Multiline),
(r'//.*$', Comment),
(r'@[0-9a-zA-Z\.#-]*', Name.Decorator),
(r'(=)', Name.Decorator),
(r'^(\$version)(:)(.+)',
bygroups(Keyword.Declaration, Name.Decorator, Name.Class)),
(r'^(namespace)(\s+' + identifier + r')\b',
bygroups(Keyword.Declaration, Name.Class)),
(words(simple_shapes,
prefix=r'^', suffix=r'(\s+' + identifier + r')\b'),
bygroups(Keyword.Declaration, Name.Class)),
(words(aggregate_shapes,
prefix=r'^', suffix=r'(\s+' + identifier + r')'),
bygroups(Keyword.Declaration, Name.Class)),
(r'^(metadata)(\s+.+)(\s*)(=)',
bygroups(Keyword.Declaration, Name.Class, Whitespace, Name.Decorator)),
(r"(true|false|null)", Keyword.Constant),
(r"(-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)", Number),
(identifier + ":", Name.Label),
(identifier, Name.Variable.Class),
(r'\[', Text, "#push"),
(r'\]', Text, "#pop"),
(r'\(', Text, "#push"),
(r'\)', Text, "#pop"),
(r'\{', Text, "#push"),
(r'\}', Text, "#pop"),
(r'"{3}(\\\\|\n|\\")*"{3}', String.Doc),
(r'"(\\\\|\n|\\"|[^"])*"', String.Double),
(r"'(\\\\|\n|\\'|[^'])*'", String.Single),
(r'[:,]+', Punctuation),
(r'\s+', Whitespace),
]
}