futhark.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """
  2. pygments.lexers.futhark
  3. ~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the Futhark 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
  9. from pygments.token import Comment, Operator, Keyword, Name, String, \
  10. Number, Punctuation, Whitespace
  11. from pygments import unistring as uni
  12. __all__ = ['FutharkLexer']
  13. class FutharkLexer(RegexLexer):
  14. """
  15. A Futhark lexer
  16. """
  17. name = 'Futhark'
  18. url = 'https://futhark-lang.org/'
  19. aliases = ['futhark']
  20. filenames = ['*.fut']
  21. mimetypes = ['text/x-futhark']
  22. version_added = '2.8'
  23. num_types = ('i8', 'i16', 'i32', 'i64', 'u8', 'u16', 'u32', 'u64', 'f32', 'f64')
  24. other_types = ('bool', )
  25. reserved = ('if', 'then', 'else', 'def', 'let', 'loop', 'in', 'with',
  26. 'type', 'type~', 'type^',
  27. 'val', 'entry', 'for', 'while', 'do', 'case', 'match',
  28. 'include', 'import', 'module', 'open', 'local', 'assert', '_')
  29. ascii = ('NUL', 'SOH', '[SE]TX', 'EOT', 'ENQ', 'ACK',
  30. 'BEL', 'BS', 'HT', 'LF', 'VT', 'FF', 'CR', 'S[OI]', 'DLE',
  31. 'DC[1-4]', 'NAK', 'SYN', 'ETB', 'CAN',
  32. 'EM', 'SUB', 'ESC', '[FGRU]S', 'SP', 'DEL')
  33. num_postfix = r'({})?'.format('|'.join(num_types))
  34. identifier_re = '[a-zA-Z_][a-zA-Z_0-9\']*'
  35. # opstart_re = '+\-\*/%=\!><\|&\^'
  36. tokens = {
  37. 'root': [
  38. (r'--(.*?)$', Comment.Single),
  39. (r'\s+', Whitespace),
  40. (r'\(\)', Punctuation),
  41. (r'\b({})(?!\')\b'.format('|'.join(reserved)), Keyword.Reserved),
  42. (r'\b({})(?!\')\b'.format('|'.join(num_types + other_types)), Keyword.Type),
  43. # Identifiers
  44. (r'#\[([a-zA-Z_\(\) ]*)\]', Comment.Preproc),
  45. (rf'[#!]?({identifier_re}\.)*{identifier_re}', Name),
  46. (r'\\', Operator),
  47. (r'[-+/%=!><|&*^][-+/%=!><|&*^.]*', Operator),
  48. (r'[][(),:;`{}?.\'~^]', Punctuation),
  49. # Numbers
  50. (r'0[xX]_*[\da-fA-F](_*[\da-fA-F])*_*[pP][+-]?\d(_*\d)*' + num_postfix,
  51. Number.Float),
  52. (r'0[xX]_*[\da-fA-F](_*[\da-fA-F])*\.[\da-fA-F](_*[\da-fA-F])*'
  53. r'(_*[pP][+-]?\d(_*\d)*)?' + num_postfix, Number.Float),
  54. (r'\d(_*\d)*_*[eE][+-]?\d(_*\d)*' + num_postfix, Number.Float),
  55. (r'\d(_*\d)*\.\d(_*\d)*(_*[eE][+-]?\d(_*\d)*)?' + num_postfix, Number.Float),
  56. (r'0[bB]_*[01](_*[01])*' + num_postfix, Number.Bin),
  57. (r'0[xX]_*[\da-fA-F](_*[\da-fA-F])*' + num_postfix, Number.Hex),
  58. (r'\d(_*\d)*' + num_postfix, Number.Integer),
  59. # Character/String Literals
  60. (r"'", String.Char, 'character'),
  61. (r'"', String, 'string'),
  62. # Special
  63. (r'\[[a-zA-Z_\d]*\]', Keyword.Type),
  64. (r'\(\)', Name.Builtin),
  65. ],
  66. 'character': [
  67. # Allows multi-chars, incorrectly.
  68. (r"[^\\']'", String.Char, '#pop'),
  69. (r"\\", String.Escape, 'escape'),
  70. ("'", String.Char, '#pop'),
  71. ],
  72. 'string': [
  73. (r'[^\\"]+', String),
  74. (r"\\", String.Escape, 'escape'),
  75. ('"', String, '#pop'),
  76. ],
  77. 'escape': [
  78. (r'[abfnrtv"\'&\\]', String.Escape, '#pop'),
  79. (r'\^[][' + uni.Lu + r'@^_]', String.Escape, '#pop'),
  80. ('|'.join(ascii), String.Escape, '#pop'),
  81. (r'o[0-7]+', String.Escape, '#pop'),
  82. (r'x[\da-fA-F]+', String.Escape, '#pop'),
  83. (r'\d+', String.Escape, '#pop'),
  84. (r'(\s+)(\\)', bygroups(Whitespace, String.Escape), '#pop'),
  85. ],
  86. }