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

22 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:07 +0000

1""" 

2 pygments.lexers.oberon 

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

4 

5 Lexers for Oberon family languages. 

6 

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

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11import re 

12 

13from pygments.lexer import RegexLexer, include, words 

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

15 Number, Punctuation 

16 

17__all__ = ['ComponentPascalLexer'] 

18 

19 

20class ComponentPascalLexer(RegexLexer): 

21 """ 

22 For Component Pascal source code. 

23 

24 .. versionadded:: 2.1 

25 """ 

26 name = 'Component Pascal' 

27 aliases = ['componentpascal', 'cp'] 

28 filenames = ['*.cp', '*.cps'] 

29 mimetypes = ['text/x-component-pascal'] 

30 

31 flags = re.MULTILINE | re.DOTALL 

32 

33 tokens = { 

34 'root': [ 

35 include('whitespace'), 

36 include('comments'), 

37 include('punctuation'), 

38 include('numliterals'), 

39 include('strings'), 

40 include('operators'), 

41 include('builtins'), 

42 include('identifiers'), 

43 ], 

44 'whitespace': [ 

45 (r'\n+', Text), # blank lines 

46 (r'\s+', Text), # whitespace 

47 ], 

48 'comments': [ 

49 (r'\(\*([^$].*?)\*\)', Comment.Multiline), 

50 # TODO: nested comments (* (* ... *) ... (* ... *) *) not supported! 

51 ], 

52 'punctuation': [ 

53 (r'[()\[\]{},.:;|]', Punctuation), 

54 ], 

55 'numliterals': [ 

56 (r'[0-9A-F]+X\b', Number.Hex), # char code 

57 (r'[0-9A-F]+[HL]\b', Number.Hex), # hexadecimal number 

58 (r'[0-9]+\.[0-9]+E[+-][0-9]+', Number.Float), # real number 

59 (r'[0-9]+\.[0-9]+', Number.Float), # real number 

60 (r'[0-9]+', Number.Integer), # decimal whole number 

61 ], 

62 'strings': [ 

63 (r"'[^\n']*'", String), # single quoted string 

64 (r'"[^\n"]*"', String), # double quoted string 

65 ], 

66 'operators': [ 

67 # Arithmetic Operators 

68 (r'[+-]', Operator), 

69 (r'[*/]', Operator), 

70 # Relational Operators 

71 (r'[=#<>]', Operator), 

72 # Dereferencing Operator 

73 (r'\^', Operator), 

74 # Logical AND Operator 

75 (r'&', Operator), 

76 # Logical NOT Operator 

77 (r'~', Operator), 

78 # Assignment Symbol 

79 (r':=', Operator), 

80 # Range Constructor 

81 (r'\.\.', Operator), 

82 (r'\$', Operator), 

83 ], 

84 'identifiers': [ 

85 (r'([a-zA-Z_$][\w$]*)', Name), 

86 ], 

87 'builtins': [ 

88 (words(( 

89 'ANYPTR', 'ANYREC', 'BOOLEAN', 'BYTE', 'CHAR', 'INTEGER', 'LONGINT', 

90 'REAL', 'SET', 'SHORTCHAR', 'SHORTINT', 'SHORTREAL' 

91 ), suffix=r'\b'), Keyword.Type), 

92 (words(( 

93 'ABS', 'ABSTRACT', 'ARRAY', 'ASH', 'ASSERT', 'BEGIN', 'BITS', 'BY', 

94 'CAP', 'CASE', 'CHR', 'CLOSE', 'CONST', 'DEC', 'DIV', 'DO', 'ELSE', 

95 'ELSIF', 'EMPTY', 'END', 'ENTIER', 'EXCL', 'EXIT', 'EXTENSIBLE', 'FOR', 

96 'HALT', 'IF', 'IMPORT', 'IN', 'INC', 'INCL', 'IS', 'LEN', 'LIMITED', 

97 'LONG', 'LOOP', 'MAX', 'MIN', 'MOD', 'MODULE', 'NEW', 'ODD', 'OF', 

98 'OR', 'ORD', 'OUT', 'POINTER', 'PROCEDURE', 'RECORD', 'REPEAT', 'RETURN', 

99 'SHORT', 'SHORTCHAR', 'SHORTINT', 'SIZE', 'THEN', 'TYPE', 'TO', 'UNTIL', 

100 'VAR', 'WHILE', 'WITH' 

101 ), suffix=r'\b'), Keyword.Reserved), 

102 (r'(TRUE|FALSE|NIL|INF)\b', Keyword.Constant), 

103 ] 

104 } 

105 

106 def analyse_text(text): 

107 """The only other lexer using .cp is the C++ one, so we check if for 

108 a few common Pascal keywords here. Those are unfortunately quite 

109 common across various business languages as well.""" 

110 result = 0 

111 if 'BEGIN' in text: 

112 result += 0.01 

113 if 'END' in text: 

114 result += 0.01 

115 if 'PROCEDURE' in text: 

116 result += 0.01 

117 if 'END' in text: 

118 result += 0.01 

119 

120 return result