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

16 statements  

1""" 

2 pygments.lexers.elm 

3 ~~~~~~~~~~~~~~~~~~~ 

4 

5 Lexer for the Elm programming language. 

6 

7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS. 

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11from pygments.lexer import RegexLexer, words, include, bygroups 

12from pygments.token import Comment, Keyword, Name, Number, Punctuation, \ 

13 String, Whitespace 

14 

15__all__ = ['ElmLexer'] 

16 

17 

18class ElmLexer(RegexLexer): 

19 """ 

20 For Elm source code. 

21 """ 

22 

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' 

29 

30 validName = r'[a-z_][a-zA-Z0-9_\']*' 

31 

32 specialName = r'^main ' 

33 

34 builtinOps = ( 

35 '~', '||', '|>', '|', '`', '^', '\\', '\'', '>>', '>=', '>', '==', 

36 '=', '<~', '<|', '<=', '<<', '<-', '<', '::', ':', '/=', '//', '/', 

37 '..', '.', '->', '-', '++', '+', '*', '&&', '%', 

38 ) 

39 

40 reservedWords = words(( 

41 'alias', 'as', 'case', 'else', 'if', 'import', 'in', 

42 'let', 'module', 'of', 'port', 'then', 'type', 'where', 

43 ), suffix=r'\b') 

44 

45 tokens = { 

46 'root': [ 

47 

48 # Comments 

49 (r'\{-', Comment.Multiline, 'comment'), 

50 (r'--.*', Comment.Single), 

51 

52 # Whitespace 

53 (r'\s+', Whitespace), 

54 

55 # Strings 

56 (r'"', String, 'doublequote'), 

57 

58 # Modules 

59 (r'^(\s*)(module)(\s*)', bygroups(Whitespace, Keyword.Namespace, 

60 Whitespace), 'imports'), 

61 

62 # Imports 

63 (r'^(\s*)(import)(\s*)', bygroups(Whitespace, Keyword.Namespace, 

64 Whitespace), 'imports'), 

65 

66 # Shaders 

67 (r'\[glsl\|.*', Name.Entity, 'shader'), 

68 

69 # Keywords 

70 (reservedWords, Keyword.Reserved), 

71 

72 # Types 

73 (r'[A-Z][a-zA-Z0-9_]*', Keyword.Type), 

74 

75 # Main 

76 (specialName, Keyword.Reserved), 

77 

78 # Prefix Operators 

79 (words((builtinOps), prefix=r'\(', suffix=r'\)'), Name.Function), 

80 

81 # Infix Operators 

82 (words(builtinOps), Name.Function), 

83 

84 # Numbers 

85 include('numbers'), 

86 

87 # Variable Names 

88 (validName, Name.Variable), 

89 

90 # Parens 

91 (r'[,()\[\]{}]', Punctuation), 

92 

93 ], 

94 

95 'comment': [ 

96 (r'-(?!\})', Comment.Multiline), 

97 (r'\{-', Comment.Multiline, 'comment'), 

98 (r'[^-}]', Comment.Multiline), 

99 (r'-\}', Comment.Multiline, '#pop'), 

100 ], 

101 

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 ], 

108 

109 'imports': [ 

110 (r'\w+(\.\w+)*', Name.Class, '#pop'), 

111 ], 

112 

113 'numbers': [ 

114 (r'_?\d+\.(?=\d+)', Number.Float), 

115 (r'_?\d+', Number.Integer), 

116 ], 

117 

118 'shader': [ 

119 (r'\|(?!\])', Name.Entity), 

120 (r'\|\]', Name.Entity, '#pop'), 

121 (r'(.*)(\n)', bygroups(Name.Entity, Whitespace)), 

122 ], 

123 }