whiley.py 3.9 KB

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