You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

116 lines
3.9 KiB

  1. """
  2. pygments.lexers.whiley
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for the Whiley language.
  5. :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, bygroups, words
  9. from pygments.token import Comment, Keyword, Name, Number, Operator, \
  10. Punctuation, String, Text
  11. __all__ = ['WhileyLexer']
  12. class WhileyLexer(RegexLexer):
  13. """
  14. Lexer for the Whiley programming language.
  15. """
  16. name = 'Whiley'
  17. url = 'http://whiley.org/'
  18. filenames = ['*.whiley']
  19. aliases = ['whiley']
  20. mimetypes = ['text/x-whiley']
  21. version_added = '2.2'
  22. # See the language specification:
  23. # http://whiley.org/download/WhileyLanguageSpec.pdf
  24. tokens = {
  25. 'root': [
  26. # Whitespace
  27. (r'\s+', Text),
  28. # Comments
  29. (r'//.*', Comment.Single),
  30. # don't parse empty comment as doc comment
  31. (r'/\*\*/', Comment.Multiline),
  32. (r'(?s)/\*\*.*?\*/', String.Doc),
  33. (r'(?s)/\*.*?\*/', Comment.Multiline),
  34. # Keywords
  35. (words((
  36. 'if', 'else', 'while', 'for', 'do', 'return',
  37. 'switch', 'case', 'default', 'break', 'continue',
  38. 'requires', 'ensures', 'where', 'assert', 'assume',
  39. 'all', 'no', 'some', 'in', 'is', 'new',
  40. 'throw', 'try', 'catch', 'debug', 'skip', 'fail',
  41. 'finite', 'total'), suffix=r'\b'), Keyword.Reserved),
  42. (words((
  43. 'function', 'method', 'public', 'private', 'protected',
  44. 'export', 'native'), suffix=r'\b'), Keyword.Declaration),
  45. # "constant" & "type" are not keywords unless used in declarations
  46. (r'(constant|type)(\s+)([a-zA-Z_]\w*)(\s+)(is)\b',
  47. bygroups(Keyword.Declaration, Text, Name, Text, Keyword.Reserved)),
  48. (r'(true|false|null)\b', Keyword.Constant),
  49. (r'(bool|byte|int|real|any|void)\b', Keyword.Type),
  50. # "from" is not a keyword unless used with import
  51. (r'(import)(\s+)(\*)([^\S\n]+)(from)\b',
  52. bygroups(Keyword.Namespace, Text, Punctuation, Text, Keyword.Namespace)),
  53. (r'(import)(\s+)([a-zA-Z_]\w*)([^\S\n]+)(from)\b',
  54. bygroups(Keyword.Namespace, Text, Name, Text, Keyword.Namespace)),
  55. (r'(package|import)\b', Keyword.Namespace),
  56. # standard library: https://github.com/Whiley/WhileyLibs/
  57. (words((
  58. # types defined in whiley.lang.Int
  59. 'i8', 'i16', 'i32', 'i64',
  60. 'u8', 'u16', 'u32', 'u64',
  61. 'uint', 'nat',
  62. # whiley.lang.Any
  63. 'toString'), suffix=r'\b'), Name.Builtin),
  64. # byte literal
  65. (r'[01]+b', Number.Bin),
  66. # decimal literal
  67. (r'[0-9]+\.[0-9]+', Number.Float),
  68. # match "1." but not ranges like "3..5"
  69. (r'[0-9]+\.(?!\.)', Number.Float),
  70. # integer literal
  71. (r'0x[0-9a-fA-F]+', Number.Hex),
  72. (r'[0-9]+', Number.Integer),
  73. # character literal
  74. (r"""'[^\\]'""", String.Char),
  75. (r"""(')(\\['"\\btnfr])(')""",
  76. bygroups(String.Char, String.Escape, String.Char)),
  77. # string literal
  78. (r'"', String, 'string'),
  79. # operators and punctuation
  80. (r'[{}()\[\],.;]', Punctuation),
  81. (r'[+\-*/%&|<>^!~@=:?'
  82. # unicode operators
  83. r'\u2200\u2203\u2205\u2282\u2286\u2283\u2287'
  84. r'\u222A\u2229\u2264\u2265\u2208\u2227\u2228'
  85. r']', Operator),
  86. # identifier
  87. (r'[a-zA-Z_]\w*', Name),
  88. ],
  89. 'string': [
  90. (r'"', String, '#pop'),
  91. (r'\\[btnfr]', String.Escape),
  92. (r'\\u[0-9a-fA-F]{4}', String.Escape),
  93. (r'\\.', String),
  94. (r'[^\\"]+', String),
  95. ],
  96. }