Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/solidity.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 07:45 +0000

1""" 

2 pygments.lexers.solidity 

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

4 

5 Lexers for Solidity. 

6 

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

8 :license: BSD, see LICENSE for details. 

9""" 

10 

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

12from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ 

13 Number, Punctuation, Whitespace 

14 

15__all__ = ['SolidityLexer'] 

16 

17 

18class SolidityLexer(RegexLexer): 

19 """ 

20 For Solidity source code. 

21 

22 .. versionadded:: 2.5 

23 """ 

24 

25 name = 'Solidity' 

26 aliases = ['solidity'] 

27 filenames = ['*.sol'] 

28 mimetypes = [] 

29 

30 datatype = ( 

31 r'\b(address|bool|(?:(?:bytes|hash|int|string|uint)(?:8|16|24|32|40|48|56|64' 

32 r'|72|80|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208' 

33 r'|216|224|232|240|248|256)?))\b' 

34 ) 

35 

36 tokens = { 

37 'root': [ 

38 include('whitespace'), 

39 include('comments'), 

40 (r'\bpragma\s+solidity\b', Keyword, 'pragma'), 

41 (r'\b(contract)(\s+)([a-zA-Z_]\w*)', 

42 bygroups(Keyword, Whitespace, Name.Entity)), 

43 (datatype + r'(\s+)((?:external|public|internal|private)\s+)?' + 

44 r'([a-zA-Z_]\w*)', 

45 bygroups(Keyword.Type, Whitespace, Keyword, Name.Variable)), 

46 (r'\b(enum|event|function|struct)(\s+)([a-zA-Z_]\w*)', 

47 bygroups(Keyword.Type, Whitespace, Name.Variable)), 

48 (r'\b(msg|block|tx)\.([A-Za-z_][a-zA-Z0-9_]*)\b', Keyword), 

49 (words(( 

50 'block', 'break', 'constant', 'constructor', 'continue', 

51 'contract', 'do', 'else', 'external', 'false', 'for', 

52 'function', 'if', 'import', 'inherited', 'internal', 'is', 

53 'library', 'mapping', 'memory', 'modifier', 'msg', 'new', 

54 'payable', 'private', 'public', 'require', 'return', 

55 'returns', 'struct', 'suicide', 'throw', 'this', 'true', 

56 'tx', 'var', 'while'), prefix=r'\b', suffix=r'\b'), 

57 Keyword.Type), 

58 (words(('keccak256',), prefix=r'\b', suffix=r'\b'), Name.Builtin), 

59 (datatype, Keyword.Type), 

60 include('constants'), 

61 (r'[a-zA-Z_]\w*', Text), 

62 (r'[~!%^&*+=|?:<>/-]', Operator), 

63 (r'[.;{}(),\[\]]', Punctuation) 

64 ], 

65 'comments': [ 

66 (r'//(\n|[\w\W]*?[^\\]\n)', Comment.Single), 

67 (r'/(\\\n)?[*][\w\W]*?[*](\\\n)?/', Comment.Multiline), 

68 (r'/(\\\n)?[*][\w\W]*', Comment.Multiline) 

69 ], 

70 'constants': [ 

71 (r'("(\\"|.)*?")', String.Double), 

72 (r"('(\\'|.)*?')", String.Single), 

73 (r'\b0[xX][0-9a-fA-F]+\b', Number.Hex), 

74 (r'\b\d+\b', Number.Decimal), 

75 ], 

76 'pragma': [ 

77 include('whitespace'), 

78 include('comments'), 

79 (r'(\^|>=|<)(\s*)(\d+\.\d+\.\d+)', 

80 bygroups(Operator, Whitespace, Keyword)), 

81 (r';', Punctuation, '#pop') 

82 ], 

83 'whitespace': [ 

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

85 (r'\n', Whitespace) 

86 ] 

87 }