spice.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """
  2. pygments.lexers.spice
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for the Spice programming language.
  5. :copyright: Copyright 2006-2024 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. """
  16. name = 'Spice'
  17. url = 'https://www.spicelang.com'
  18. filenames = ['*.spice']
  19. aliases = ['spice', 'spicelang']
  20. mimetypes = ['text/x-spice']
  21. version_added = '2.11'
  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', 'switch', 'case', 'default', 'for', 'foreach', 'do',
  35. 'while', 'break', 'continue', 'fallthrough', 'return', 'assert',
  36. 'unsafe', 'ext'), suffix=r'\b'), Keyword),
  37. (words(('const', 'signed', 'unsigned', 'inline', 'public', 'heap', 'compose'),
  38. suffix=r'\b'), Keyword.Pseudo),
  39. (words(('new', 'yield', 'stash', 'pick', 'sync', 'class'), suffix=r'\b'),
  40. Keyword.Reserved),
  41. (r'(true|false|nil)\b', Keyword.Constant),
  42. (words(('double', 'int', 'short', 'long', 'byte', 'char', 'string',
  43. 'bool', 'dyn'), suffix=r'\b'), Keyword.Type),
  44. (words(('printf', 'sizeof', 'alignof', 'len', 'panic'), suffix=r'\b(\()'),
  45. bygroups(Name.Builtin, Punctuation)),
  46. # numeric literals
  47. (r'[-]?[0-9]*[.][0-9]+([eE][+-]?[0-9]+)?', Number.Double),
  48. (r'0[bB][01]+[slu]?', Number.Bin),
  49. (r'0[oO][0-7]+[slu]?', Number.Oct),
  50. (r'0[xXhH][0-9a-fA-F]+[slu]?', Number.Hex),
  51. (r'(0[dD])?[0-9]+[slu]?', Number.Integer),
  52. # string literal
  53. (r'"(\\\\|\\[^\\]|[^"\\])*"', String),
  54. # char literal
  55. (r'\'(\\\\|\\[^\\]|[^\'\\])\'', String.Char),
  56. # tokens
  57. (r'<<=|>>=|<<|>>|<=|>=|\+=|-=|\*=|/=|\%=|\|=|&=|\^=|&&|\|\||&|\||'
  58. r'\+\+|--|\%|\^|\~|==|!=|->|::|[.]{3}|#!|#|[+\-*/&]', Operator),
  59. (r'[|<>=!()\[\]{}.,;:\?]', Punctuation),
  60. # identifiers
  61. (r'[^\W\d]\w*', Name.Other),
  62. ]
  63. }