whiley.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.whiley
  4. ~~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for the Whiley language.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.lexer import RegexLexer, bygroups, words
  10. from pygments.token import Comment, Keyword, Name, Number, Operator, \
  11. Punctuation, String, Text
  12. __all__ = ['WhileyLexer']
  13. class WhileyLexer(RegexLexer):
  14. """
  15. Lexer for the Whiley programming language.
  16. .. versionadded:: 2.2
  17. """
  18. name = 'Whiley'
  19. filenames = ['*.whiley']
  20. aliases = ['whiley']
  21. mimetypes = ['text/x-whiley']
  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. (u'[+\\-*/%&|<>^!~@=:?'
  82. # unicode operators
  83. u'\u2200\u2203\u2205\u2282\u2286\u2283\u2287'
  84. u'\u222A\u2229\u2264\u2265\u2208\u2227\u2228'
  85. u']', 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. }