oberon.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """
  2. pygments.lexers.oberon
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for Oberon family languages.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, include, words
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation
  12. __all__ = ['ComponentPascalLexer']
  13. class ComponentPascalLexer(RegexLexer):
  14. """
  15. For Component Pascal source code.
  16. """
  17. name = 'Component Pascal'
  18. aliases = ['componentpascal', 'cp']
  19. filenames = ['*.cp', '*.cps']
  20. mimetypes = ['text/x-component-pascal']
  21. url = 'https://blackboxframework.org'
  22. version_added = '2.1'
  23. flags = re.MULTILINE | re.DOTALL
  24. tokens = {
  25. 'root': [
  26. include('whitespace'),
  27. include('comments'),
  28. include('punctuation'),
  29. include('numliterals'),
  30. include('strings'),
  31. include('operators'),
  32. include('builtins'),
  33. include('identifiers'),
  34. ],
  35. 'whitespace': [
  36. (r'\n+', Text), # blank lines
  37. (r'\s+', Text), # whitespace
  38. ],
  39. 'comments': [
  40. (r'\(\*([^$].*?)\*\)', Comment.Multiline),
  41. # TODO: nested comments (* (* ... *) ... (* ... *) *) not supported!
  42. ],
  43. 'punctuation': [
  44. (r'[()\[\]{},.:;|]', Punctuation),
  45. ],
  46. 'numliterals': [
  47. (r'[0-9A-F]+X\b', Number.Hex), # char code
  48. (r'[0-9A-F]+[HL]\b', Number.Hex), # hexadecimal number
  49. (r'[0-9]+\.[0-9]+E[+-][0-9]+', Number.Float), # real number
  50. (r'[0-9]+\.[0-9]+', Number.Float), # real number
  51. (r'[0-9]+', Number.Integer), # decimal whole number
  52. ],
  53. 'strings': [
  54. (r"'[^\n']*'", String), # single quoted string
  55. (r'"[^\n"]*"', String), # double quoted string
  56. ],
  57. 'operators': [
  58. # Arithmetic Operators
  59. (r'[+-]', Operator),
  60. (r'[*/]', Operator),
  61. # Relational Operators
  62. (r'[=#<>]', Operator),
  63. # Dereferencing Operator
  64. (r'\^', Operator),
  65. # Logical AND Operator
  66. (r'&', Operator),
  67. # Logical NOT Operator
  68. (r'~', Operator),
  69. # Assignment Symbol
  70. (r':=', Operator),
  71. # Range Constructor
  72. (r'\.\.', Operator),
  73. (r'\$', Operator),
  74. ],
  75. 'identifiers': [
  76. (r'([a-zA-Z_$][\w$]*)', Name),
  77. ],
  78. 'builtins': [
  79. (words((
  80. 'ANYPTR', 'ANYREC', 'BOOLEAN', 'BYTE', 'CHAR', 'INTEGER', 'LONGINT',
  81. 'REAL', 'SET', 'SHORTCHAR', 'SHORTINT', 'SHORTREAL'
  82. ), suffix=r'\b'), Keyword.Type),
  83. (words((
  84. 'ABS', 'ABSTRACT', 'ARRAY', 'ASH', 'ASSERT', 'BEGIN', 'BITS', 'BY',
  85. 'CAP', 'CASE', 'CHR', 'CLOSE', 'CONST', 'DEC', 'DIV', 'DO', 'ELSE',
  86. 'ELSIF', 'EMPTY', 'END', 'ENTIER', 'EXCL', 'EXIT', 'EXTENSIBLE', 'FOR',
  87. 'HALT', 'IF', 'IMPORT', 'IN', 'INC', 'INCL', 'IS', 'LEN', 'LIMITED',
  88. 'LONG', 'LOOP', 'MAX', 'MIN', 'MOD', 'MODULE', 'NEW', 'ODD', 'OF',
  89. 'OR', 'ORD', 'OUT', 'POINTER', 'PROCEDURE', 'RECORD', 'REPEAT', 'RETURN',
  90. 'SHORT', 'SHORTCHAR', 'SHORTINT', 'SIZE', 'THEN', 'TYPE', 'TO', 'UNTIL',
  91. 'VAR', 'WHILE', 'WITH'
  92. ), suffix=r'\b'), Keyword.Reserved),
  93. (r'(TRUE|FALSE|NIL|INF)\b', Keyword.Constant),
  94. ]
  95. }
  96. def analyse_text(text):
  97. """The only other lexer using .cp is the C++ one, so we check if for
  98. a few common Pascal keywords here. Those are unfortunately quite
  99. common across various business languages as well."""
  100. result = 0
  101. if 'BEGIN' in text:
  102. result += 0.01
  103. if 'END' in text:
  104. result += 0.01
  105. if 'PROCEDURE' in text:
  106. result += 0.01
  107. if 'END' in text:
  108. result += 0.01
  109. return result