spice.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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', 'thread', 'unsafe', 'ext',
  36. 'dll'), suffix=r'\b'), Keyword),
  37. (words(('const', 'signed', 'unsigned', 'inline', 'public', 'heap'),
  38. suffix=r'\b'), Keyword.Pseudo),
  39. (words(('new', 'switch', 'case', 'yield', 'stash', 'pick', 'sync',
  40. 'class'), suffix=r'\b'), 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', 'len', 'tid', 'join'), suffix=r'\b(\()'),
  45. bygroups(Name.Builtin, Punctuation)),
  46. # numeric literals
  47. (r'[0-9]*[.][0-9]+', Number.Double),
  48. (r'0[bB][01]+[sl]?', Number.Bin),
  49. (r'0[oO][0-7]+[sl]?', Number.Oct),
  50. (r'0[xXhH][0-9a-fA-F]+[sl]?', Number.Hex),
  51. (r'(0[dD])?[0-9]+[sl]?', 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. }