spice.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """
  2. pygments.lexers.spice
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for the Spice programming language.
  5. :copyright: Copyright 2006-2023 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 Text, Comment, Operator, Keyword, Name, String, \
  10. Number, Punctuation, Whitespace
  11. __all__ = ['SpiceLexer']
  12. class SpiceLexer(RegexLexer):
  13. """
  14. For Spice source.
  15. .. versionadded:: 2.11
  16. """
  17. name = 'Spice'
  18. url = 'https://www.spicelang.com'
  19. filenames = ['*.spice']
  20. aliases = ['spice', 'spicelang']
  21. mimetypes = ['text/x-spice']
  22. tokens = {
  23. 'root': [
  24. (r'\n', Whitespace),
  25. (r'\s+', Whitespace),
  26. (r'\\\n', Text),
  27. # comments
  28. (r'//(.*?)\n', Comment.Single),
  29. (r'/(\\\n)?[*]{2}(.|\n)*?[*](\\\n)?/', String.Doc),
  30. (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
  31. # keywords
  32. (r'(import|as)\b', Keyword.Namespace),
  33. (r'(f|p|type|struct|interface|enum|alias|operator)\b', Keyword.Declaration),
  34. (words(('if', 'else', 'for', 'foreach', 'do', 'while', 'break',
  35. 'continue', 'return', 'assert', 'unsafe', 'ext'), suffix=r'\b'), Keyword),
  36. (words(('const', 'signed', 'unsigned', 'inline', 'public', 'heap'),
  37. suffix=r'\b'), Keyword.Pseudo),
  38. (words(('new', 'switch', 'case', 'yield', 'stash', 'pick', 'sync',
  39. 'class'), suffix=r'\b'), Keyword.Reserved),
  40. (r'(true|false|nil)\b', Keyword.Constant),
  41. (words(('double', 'int', 'short', 'long', 'byte', 'char', 'string',
  42. 'bool', 'dyn'), suffix=r'\b'), Keyword.Type),
  43. (words(('printf', 'sizeof', 'alignof', 'len'), suffix=r'\b(\()'),
  44. bygroups(Name.Builtin, Punctuation)),
  45. # numeric literals
  46. (r'[-]?[0-9]*[.][0-9]+([eE][+-]?[0-9]+)?', Number.Double),
  47. (r'0[bB][01]+[slu]?', Number.Bin),
  48. (r'0[oO][0-7]+[slu]?', Number.Oct),
  49. (r'0[xXhH][0-9a-fA-F]+[slu]?', Number.Hex),
  50. (r'(0[dD])?[0-9]+[slu]?', Number.Integer),
  51. # string literal
  52. (r'"(\\\\|\\[^\\]|[^"\\])*"', String),
  53. # char literal
  54. (r'\'(\\\\|\\[^\\]|[^\'\\])\'', String.Char),
  55. # tokens
  56. (r'<<=|>>=|<<|>>|<=|>=|\+=|-=|\*=|/=|\%=|\|=|&=|\^=|&&|\|\||&|\||'
  57. r'\+\+|--|\%|\^|\~|==|!=|::|[.]{3}|#!|#|[+\-*/&]', Operator),
  58. (r'[|<>=!()\[\]{}.,;:\?]', Punctuation),
  59. # identifiers
  60. (r'[^\W\d]\w*', Name.Other),
  61. ]
  62. }