Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pygments/lexers/elm.py: 100%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2 pygments.lexers.elm
3 ~~~~~~~~~~~~~~~~~~~
5 Lexer for the Elm programming language.
7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
11from pygments.lexer import RegexLexer, words, include, bygroups
12from pygments.token import Comment, Keyword, Name, Number, Punctuation, \
13 String, Whitespace
15__all__ = ['ElmLexer']
18class ElmLexer(RegexLexer):
19 """
20 For Elm source code.
21 """
23 name = 'Elm'
24 url = 'https://elm-lang.org/'
25 aliases = ['elm']
26 filenames = ['*.elm']
27 mimetypes = ['text/x-elm']
28 version_added = '2.1'
30 validName = r'[a-z_][a-zA-Z0-9_\']*'
32 specialName = r'^main '
34 builtinOps = (
35 '~', '||', '|>', '|', '`', '^', '\\', '\'', '>>', '>=', '>', '==',
36 '=', '<~', '<|', '<=', '<<', '<-', '<', '::', ':', '/=', '//', '/',
37 '..', '.', '->', '-', '++', '+', '*', '&&', '%',
38 )
40 reservedWords = words((
41 'alias', 'as', 'case', 'else', 'if', 'import', 'in',
42 'let', 'module', 'of', 'port', 'then', 'type', 'where',
43 ), suffix=r'\b')
45 tokens = {
46 'root': [
48 # Comments
49 (r'\{-', Comment.Multiline, 'comment'),
50 (r'--.*', Comment.Single),
52 # Whitespace
53 (r'\s+', Whitespace),
55 # Strings
56 (r'"', String, 'doublequote'),
58 # Modules
59 (r'^(\s*)(module)(\s*)', bygroups(Whitespace, Keyword.Namespace,
60 Whitespace), 'imports'),
62 # Imports
63 (r'^(\s*)(import)(\s*)', bygroups(Whitespace, Keyword.Namespace,
64 Whitespace), 'imports'),
66 # Shaders
67 (r'\[glsl\|.*', Name.Entity, 'shader'),
69 # Keywords
70 (reservedWords, Keyword.Reserved),
72 # Types
73 (r'[A-Z][a-zA-Z0-9_]*', Keyword.Type),
75 # Main
76 (specialName, Keyword.Reserved),
78 # Prefix Operators
79 (words((builtinOps), prefix=r'\(', suffix=r'\)'), Name.Function),
81 # Infix Operators
82 (words(builtinOps), Name.Function),
84 # Numbers
85 include('numbers'),
87 # Variable Names
88 (validName, Name.Variable),
90 # Parens
91 (r'[,()\[\]{}]', Punctuation),
93 ],
95 'comment': [
96 (r'-(?!\})', Comment.Multiline),
97 (r'\{-', Comment.Multiline, 'comment'),
98 (r'[^-}]', Comment.Multiline),
99 (r'-\}', Comment.Multiline, '#pop'),
100 ],
102 'doublequote': [
103 (r'\\u[0-9a-fA-F]{4}', String.Escape),
104 (r'\\[nrfvb\\"]', String.Escape),
105 (r'[^"]', String),
106 (r'"', String, '#pop'),
107 ],
109 'imports': [
110 (r'\w+(\.\w+)*', Name.Class, '#pop'),
111 ],
113 'numbers': [
114 (r'_?\d+\.(?=\d+)', Number.Float),
115 (r'_?\d+', Number.Integer),
116 ],
118 'shader': [
119 (r'\|(?!\])', Name.Entity),
120 (r'\|\]', Name.Entity, '#pop'),
121 (r'(.*)(\n)', bygroups(Name.Entity, Whitespace)),
122 ],
123 }